为什么应用 css 宽度会导致该框下拉一行?

发布于 2024-12-11 03:22:54 字数 1633 浏览 0 评论 0原文

我意识到这个问题相当具体......我试图清楚地了解“宽度”对 css 盒模型的影响。

我在两个单选按钮的左侧有一个标签。标签向左浮动,单选按钮显示在标签的右侧。单选按钮本身的右侧有标签,并且这些标签可能很长。

无线电位于 UL 块中。

当我有 width:auto; 时在 UL 块上,然后将浏览器缩小到相当窄,单选按钮及其各自的标签就会下降到下一行。

如果我删除 width:auto,单选按钮将保持浮动,并且单选按钮旁边的标签字符串将继续缩小,这就是我想要的。

我的问题是:为什么 width:auto 会导致无线电及其标签下降一条线?是因为 width:auto 尝试创建一个容纳标签字符串全长而不流动文本的框吗?还是有其他事情在起作用?

以下是演示该行为的示例代码:

<style>
    .inlineLabels ul{
        float:left;
        width: 66%;
        padding: 0;
    }

    p.label{
        float:left;
        text-align:right;
        position:relative;
        width:32%;
        margin: .3em 2% 0 0;
        display:block;
    }

    ul.blockLabels{
        /*width:auto; why does uncommenting this cause the radios to drop down to a new line when shrinking browser size? */ 
        list-style:none;
    }
</style>
<fieldset class="inlineLabels">
    <div>
        <p class="label">Choose:</p>
        <ul class="blockLabels">
            <li>
                <label for="a-1">
                    <input name="a" id="a-1" value="0" type="radio">&nbsp;<span class="">A short string</span>
                </label>
            </li>
            <li>
                <label for="a-2">
                    <input name="a" id="a-2" value="1" type="radio">&nbsp;<span class="">A very very very  very very very very very very  very very very string</span>
                </label>
            </li>
        </ul>
    </div>
</fieldset>

I realize this question is rather specific... I'm trying to get my head clearly around the affects of "width" on the css box model.

I have a label to the left of two radio buttons. The label floats left, and the radio buttons appear to the right of the label. The radio buttons themselves have labels to the right of them, and these labels can be long.

The radios are in a UL block.

When I have width:auto; on the UL block, and then shrink the browser to be rather narrow, the radio buttons and their respective labels drop down to the next line.

If I remove the width:auto, the radio buttons remain floated and the label strings beside the radios continue to shrink, which is what I want.

My question is: Why would width:auto cause the radios and their labels to drop down a line? is it because width:auto attempts to create a box which accomodates the full length of the label strings without flowing the text? Or is something else at work?

Here's sample code which demonstrates the behavior:

<style>
    .inlineLabels ul{
        float:left;
        width: 66%;
        padding: 0;
    }

    p.label{
        float:left;
        text-align:right;
        position:relative;
        width:32%;
        margin: .3em 2% 0 0;
        display:block;
    }

    ul.blockLabels{
        /*width:auto; why does uncommenting this cause the radios to drop down to a new line when shrinking browser size? */ 
        list-style:none;
    }
</style>
<fieldset class="inlineLabels">
    <div>
        <p class="label">Choose:</p>
        <ul class="blockLabels">
            <li>
                <label for="a-1">
                    <input name="a" id="a-1" value="0" type="radio"> <span class="">A short string</span>
                </label>
            </li>
            <li>
                <label for="a-2">
                    <input name="a" id="a-2" value="1" type="radio"> <span class="">A very very very  very very very very very very  very very very string</span>
                </label>
            </li>
        </ul>
    </div>
</fieldset>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-12-18 03:22:54

TLDR:宽度为 auto 的浮动元素的“首选宽度”是没有换行符的内容的宽度。这就是为什么引擎将首先包裹浮动体,并且仅作为最后一步,它才会开始在浮动体中包裹文本。

您可以阅读规范中的答案: http://www.w3 .org/TR/CSS2/visudet.html#float-width

如果“width”计算为“auto”,则使用的值为“shrink-to-fit”宽度。

收缩至适合宽度的计算类似于使用自动表格布局算法计算表格单元格的宽度。粗略地:通过格式化内容来计算首选宽度,而不在发生显式换行的情况下换行,并且还计算首选最小宽度,例如,通过尝试所有可能的换行符。 CSS 2.1 没有定义确切的算法。第三,找到可用宽度:在本例中,这是包含块的宽度减去使用的“margin-left”、“border-left-width”、“padding-left”、“padding-right”的值, 'border-right-width'、'margin-right' 以及任何相关滚动条的宽度。

那么收缩到适合的宽度为:min(max(首选最小宽度,可用宽度),首选宽度)。

TLDR: the "preferred width" of floated elements with width auto is the width of the content without line breaks. That's why the engine will first wrap the float and only as a last measure it will start wrapping text within the float.

You can read the answer in the spec: http://www.w3.org/TR/CSS2/visudet.html#float-width

If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.

Calculation of the shrink-to-fit width is similar to calculating the width of a table cell using the automatic table layout algorithm. Roughly: calculate the preferred width by formatting the content without breaking lines other than where explicit line breaks occur, and also calculate the preferred minimum width, e.g., by trying all possible line breaks. CSS 2.1 does not define the exact algorithm. Thirdly, find the available width: in this case, this is the width of the containing block minus the used values of 'margin-left', 'border-left-width', 'padding-left', 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.

Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).

你在我安 2024-12-18 03:22:54

我想无论是否设置宽度,您可能都需要设置 LI 的样式。

ul.blockLabels li{float:left;}

I guess independent of setting a width or not, you probably need to style the LI.

ul.blockLabels li{float:left;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文