英文:
An element with CSS float moving out from its parent element
问题
在我的应用中,有一个部分列出了文章。显示标题和作者。问题是,当作者不能在同一行显示标题时,但标题不足以分为两行时,作者("由某人")会位于边框的中间,如下所示:
我对float
属性(以及Less)的了解不多,我唯一知道的是它可以将元素移动到所有DOM兄弟元素的左侧或右侧。如何确保这个"由某人"的span
不会像这样移出布局(而是使父元素增加其大小)?
我的代码(我使用Less来样式化它):
a.post {
text-decoration: none;
padding: 10px;
display: block;
cursor: pointer;
&:not(:last-child) {
border-bottom: #aaa solid 1px;
}
&:hover {
background: #f5f5f5;
}
&:active {
background: #eee;
}
.author {
float: right;
}
}
<span>
<a class="post">
<b>你曾经看过的最好的文章</b>
<span class="author">由某人</span>
</a>
<a class="post">
<b>你曾经看过的最好的文章 2</b>
<span class="author">由某人</span>
</a>
<a class="post">
<b>你曾经看过的最好的文章,但标题很长很长</b>
<span class="author">由某人</span>
</a>
<a class="post">
<b>你曾经看过的最好的文章 3</b>
<span class="author">由某人</span>
</a>
</span>
注意,我使用了a
元素,因为在我的真实应用程序中,它链接到某个地方。
英文:
In my app, there is a part where it lists articles. Title and author are displayed. The problem is that when the author can't fit to one line with the title but the title is not long enough to split into two lines, the author ("by Someone") is positioned in the middle of the border like this:
I haven't got much knowledge with float
property (and same with Less), the only thing I know is that it can move things to left or right of all DOM siblings. How to make sure that this "by Someone" span
won't move out from layout like this (instead it will make the parent increase its size)?
My code (I used Less to style that):
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
a.post {
text-decoration: none;
padding: 10px;
display: block;
cursor: pointer;
&:not(:last-child) {
border-bottom: #aaa solid 1px;
}
&:hover {
background: #f5f5f5;
}
&:active {
background: #eee;
}
.author {
float: right;
}
}
<!-- language: lang-html -->
<span>
<a class="post">
<b>The best article you've ever seen</b>
<span class="author">by Someone</span>
</a>
<a class="post">
<b>The best article you've ever seen 2</b>
<span class="author">by Someone</span>
</a>
<a class="post">
<b>The best article you've ever seen but it has a very very long title</b>
<span class="author">by Someone</span>
</a>
<a class="post">
<b>The best article you've ever seen 3</b>
<span class="author">by Someone</span>
</a>
</span>
<!-- end snippet -->
Note that I used an a
element because in my real app it links somewhere.
答案1
得分: 1
你的HTML存在问题 - 我建议遵循语义规则,并在CSS中探索flexbox或grid系统。
.post {
padding: 10px;
cursor: pointer;
border-bottom: #aaa solid 1px;
}
.post:last-child {
border-bottom: none;
}
.post:hover {
background: #f5f5f5;
}
.post:active {
background: #eee;
}
.post a {
text-decoration: none;
color: inherit;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.post h1 {
width: 80%;
}
.post .author {
width: 20%;
text-align: right;
}
<section>
<div class="post">
<a href="#">
<h1>你曾经见过的最好的文章</h1>
<p class="author">作者:某人</p>
</a>
</div>
<div class="post">
<a href="#">
<h1>你曾经见过的最好的文章2</h1>
<p class="author">作者:某人</p>
</a>
</div>
<div class="post">
<a href="#">
<h1>你曾经见过的最好的文章,但标题非常非常长</h1>
<p class="author">作者:某人</p>
</a>
</div>
<div class="post">
<a href="#">
<h1>你曾经见过的最好的文章3</h1>
<p class="author">作者:某人</p>
</a>
</div>
</section>
英文:
Your HTML is questionable - I would suggest following semantic rule as well as exploring flexbox or grid system in CSS.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.post {
padding: 10px;
cursor: pointer;
border-bottom: #aaa solid 1px;
}
.post:last-child {
border-bottom: none;
}
.post:hover {
background: #f5f5f5;
}
.post:active {
background: #eee;
}
.post a {
text-decoration: none;
color: inherit;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.post h1 {
width: 80%;
}
.post .author {
width: 20%;
text-align: right;
}
<!-- language: lang-html -->
<section>
<div class="post">
<a href="#">
<h1>The best article you've ever seen</h1>
<p class="author">by Someone</p>
</a>
</div>
<div class="post">
<a href="#">
<h1>The best article you've ever seen 2</h1>
<p class="author">by Someone</p>
</a>
</div>
<div class="post">
<a href="#">
<h1>The best article you've ever seen but it has a very very long title</h1>
<p class="author">by Someone</p>
</a>
</div>
<div class="post">
<a href="#">
<h1>The best article you've ever seen 3</h1>
<p class="author">by Someone</p>
</a>
</div>
</section>
<!-- end snippet -->
答案2
得分: 0
你可以通过将a.post
样式添加width:100%; float:left;
来解决这个问题,就像这样:
a.post {
text-decoration: none;
padding: 10px;
display: block;
cursor: pointer;
width: 100%;
float: left;
&:not(:last-child) {
border-bottom: #aaa solid 1px;
}
&:hover {
background: #f5f5f5;
}
&:active {
background: #eee;
}
.author {
float: right;
}
}
如果width: 100%
导致容器的宽度溢出,你可以添加box-sizing: border-box;
,这样它的内边距会被计算在100%的宽度内,而不会被添加到它上面。
希望这有所帮助!问候。
英文:
I think you can solve this by adding width:100%; float:left;
to the a.post
style like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
a.post {
text-decoration: none;
padding: 10px;
display: block;
cursor: pointer;
width:100%;
float:left;
&:not(:last-child) {
border-bottom: #aaa solid 1px;
}
&:hover {
background: #f5f5f5;
}
&:active {
background: #eee;
}
.author {
float: right;
}
}
<!-- language: lang-html -->
<span>
<a class="post">
<b>The best article you've ever seen</b>
<span class="author">by Someone</span>
</a>
<a class="post">
<b>The best article you've ever seen 2</b>
<span class="author">by Someone</span>
</a>
<a class="post">
<b>The best article you've ever seen but it has a very very long title.</b>
<span class="author">by Someone</span>
</a>
<a class="post">
<b>The best article you've ever seen 3</b>
<span class="author">by Someone</span>
</a>
</span>
<!-- end snippet -->
If width: 100%
overflows the width of your container, you can add box-sizing: border-box;
so its padding is calculated in the 100% width, and not added to it.
I hope this helps! Greetings.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论