gpt4 book ai didi

css - 将列的宽度限制为特定网格项的宽度

转载 作者:行者123 更新时间:2023-11-28 09:32:14 25 4
gpt4 key购买 nike

我有一个简单的用例:商品标题和描述。标题应显示在说明上方,我希望标题确定整个列的宽度,也适用于“说明”行。

使用 CSS Grid 可以实现吗?如果可以,如何实现?

这是期望的输出:

demo of desired output

这就是代码,基本上:

.grid-container {
display: grid;
grid-template-columns: 1fr;
}
<div class="grid-container">
<div class="A">
DIV A contains the TITLE
</div>
<div class="B">
DIV B has much more text but I want its text to wrap to the width of div.A (no cut of words, just wrapping)
</div>
</div>

最佳答案

不要将列宽设置为 1fr,而是使用 min-content

使用 min-content,该列将利用所有换行符(文本换行)机会

它基本上环绕所有文本,直到列达到最长单词的宽度。

.grid-container {
display: grid;
grid-template-columns: min-content; /* formerly 1fr */
}
<div class="grid-container">
<div class="A">
DIV A contains the TITLE
</div>
<div class="B">
DIV B has much more text but I want its text to wrap to the width of div.A (no cut of words, just wrapping)
</div>
</div>

然后,抑制标题框中的换行符:

.grid-container {
display: grid;
grid-template-columns: min-content;
}

.A {
white-space: nowrap;
background-color: lightgreen; /* just for illustration */
}
<div class="grid-container">
<div class="A">
DIV A contains the TITLE
</div>
<div class="B">
DIV B has much more text but I want its text to wrap to the width of div.A (no cut of words, just wrapping)
</div>
</div>

引用资料:

关于css - 将列的宽度限制为特定网格项的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47452362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com