一个带有CSS浮动的元素从其父元素中移出。

huangapple go评论72阅读模式
英文:

An element with CSS float moving out from its parent element

问题

在我的应用中,有一个部分列出了文章。显示标题和作者。问题是,当作者不能在同一行显示标题时,但标题不足以分为两行时,作者("由某人")会位于边框的中间,如下所示:
一个带有CSS浮动的元素从其父元素中移出。

我对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:
一个带有CSS浮动的元素从其父元素中移出。

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;

  &amp;:not(:last-child) {
    border-bottom: #aaa solid 1px;
  }

  &amp;:hover {
    background: #f5f5f5;
  }

  &amp;:active {
    background: #eee;
  }

  .author {
    float: right;
  }
}

<!-- language: lang-html -->

&lt;span&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen 2&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen but it has a very very long title&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen 3&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
&lt;/span&gt;

<!-- 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 -->

&lt;section&gt;
  &lt;div class=&quot;post&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;h1&gt;The best article you&#39;ve ever seen&lt;/h1&gt;
      &lt;p class=&quot;author&quot;&gt;by Someone&lt;/p&gt;
    &lt;/a&gt;
  &lt;/div&gt;
  &lt;div class=&quot;post&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;h1&gt;The best article you&#39;ve ever seen 2&lt;/h1&gt;
      &lt;p class=&quot;author&quot;&gt;by Someone&lt;/p&gt;
    &lt;/a&gt;
  &lt;/div&gt;
  &lt;div class=&quot;post&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;h1&gt;The best article you&#39;ve ever seen but it has a very very long title&lt;/h1&gt;
      &lt;p class=&quot;author&quot;&gt;by Someone&lt;/p&gt;
    &lt;/a&gt;
  &lt;/div&gt;
  &lt;div class=&quot;post&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;h1&gt;The best article you&#39;ve ever seen 3&lt;/h1&gt;
      &lt;p class=&quot;author&quot;&gt;by Someone&lt;/p&gt;
    &lt;/a&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- 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;

  &amp;:not(:last-child) {
    border-bottom: #aaa solid 1px;
  }

  &amp;:hover {
    background: #f5f5f5;
  }

  &amp;:active {
    background: #eee;
  }

  .author {
    float: right;
  }
}

<!-- language: lang-html -->

&lt;span&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen 2&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen but it has a very very long title.&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class=&quot;post&quot;&gt;
    &lt;b&gt;The best article you&#39;ve ever seen 3&lt;/b&gt;
    &lt;span class=&quot;author&quot;&gt;by Someone&lt;/span&gt;
  &lt;/a&gt;
&lt;/span&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年8月5日 16:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定