CSS的fit-content在存在换行时无法适应真实内容

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

CSS fit-content does not fit the real content when there is a line break

问题

I have a fixed container (20em) in row direction. In this container, I would like to have 3 items:

  1. An item containing a text (dish name)
  2. An item taking the remaining space with a minimum of 10% of the main container (link between dish name and price)
  3. An item containing a text (dish price)

In HTML/CSS:

<div class="container">
  <div class="dish">Creamy Mushroom Risotto with Parmesan</div>
  <div class="dots"></div>
  <div class="price">$12.25</div>
</div>
.container {
  display: flex;
  flex-direction: row;
  width: 20em;
  border: 1px solid black;
}

.dish {
  width: fit-content;
}

.dots {
  margin-top: 1em;
  border-top: 1px dotted black;
  min-width: 10%;
  flex-grow: 1;
}

It works well, except when the first item has a line break. In this case, the second item does not take all the remaining space:
CSS的fit-content在存在换行时无法适应真实内容

https://jsfiddle.net/ptmka1c3/1/

What are my options to get what I want?

英文:

I have a fixed container (20em) in row direction. In this container, I would like to have 3 items:

  1. An item containing a text (dish name)
  2. An item taking the remaining space with a minimum of 10% of the main container (link between dish name and price)
  3. An item containing a text (dish price)

In HTML/CSS:

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;dish&quot;&gt;Creamy Mushroom Risotto with Parmesan&lt;/div&gt;
  &lt;div class=&quot;dots&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;price&quot;&gt;$12.25&lt;/div&gt;
&lt;/div&gt;
.container {
  display: flex;
  flex-direction: row;
  width: 20em;
  border: 1px solid black;
}

.dish {
  width: fit-content;
}

.dots {
  margin-top: 1em;
  border-top: 1px dotted black;
  min-width: 10%;
  flex-grow: 1;
}

It works well, except when the first item has a line break. In this case, the second item does not take all the remaining space:
CSS的fit-content在存在换行时无法适应真实内容

https://jsfiddle.net/ptmka1c3/1/

What are my options to get what I want?

答案1

得分: 1

以下是您要翻译的内容:

是的,文本节点不会根据 text-align 属性最小化文本对齐。

您可以尝试设置 text-align: justify 或尝试此答案的第二个选项:

.container {
  display: flex;
  flex-direction: row;
  width: 20em;
  border: 1px solid black;
}

.dish {
  text-align: justify;
}

.dots {
  margin-top: 1em;
  border-top: 1px dotted black;
  min-width: 10%;
  flex-grow: 1;
}
<div class="container">
  <div class="dish">Creamy Mushroom Risotto with Parmesan</div>
  <div class="dots"></div>
  <div class="price">$12.25</div>
</div>

编辑:选项 2:

为文本和点使用不同的层,并将文本插入内联元素:

.container {
  display: flex;
  flex-direction: row;
  width: 20em;
  border: 1px solid black;
  position: relative;
}

.dots {
  top: 1em;
  border-top: 1px dotted black;
  position: absolute;
  z-index: 1;
  width: 100%;
}

.price,
.dish {
  position: relative;
  z-index: 2;
}

.dish .text {
  display: inline;
  background-color: white;
}

.price {
  margin-left: 1.5rem;
  background-color: white;
}
<div class="container">
  <div class="dish">
    <span class="text">Creamy Mushroom Risotto with Parmesan</span>
  </div>
  <div class="dots"></div>
  <div class="price">$12.25</div>
</div>
英文:

Yes, text nodes does not align the text to the minimun depending on the text-align property

You could try to set text-align: justify or try the 2nd option of this answer

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  display: flex;
  flex-direction: row;
  width: 20em;
  border: 1px solid black;
}

.dish {
  text-align: justify;
}

.dots {
  margin-top: 1em;
  border-top: 1px dotted black;
  min-width: 10%;
  flex-grow: 1;
} 

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;dish&quot;&gt;Creamy Mushroom Risotto with Parmesan&lt;/div&gt;
  &lt;div class=&quot;dots&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;price&quot;&gt;$12.25&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Edit: Option 2:

Usa different layer for text and dots, and insert the text into a inline element:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  display: flex;
  flex-direction: row;
  width: 20em;
  border: 1px solid black;
  position: relative;
}

.dots {
  top: 1em;
  border-top: 1px dotted black;
  position: absolute;
  z-index: 1;
  width: 100%;
}

.price,
.dish {
  position: relative;
  z-index: 2;
}

.dish .text {
  display: inline;
  background-color: white;
}

.price {
  margin-left: 1.5rem;
  background-color: white;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;dish&quot;&gt;
  &lt;span class=&quot;text&quot;&gt;Creamy Mushroom Risotto with Parmesan&lt;/span&gt;&lt;/div&gt;
  &lt;div class=&quot;dots&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;price&quot;&gt;$12.25&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月22日 03:00:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301458.html
匿名

发表评论

匿名网友

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

确定