如何让 CSS 浮动保持在一行?

发布于 2024-07-08 14:10:46 字数 773 浏览 13 评论 0原文

我想在同一行上使用 float: left 作为左侧的项目。

我独自实现这一点没有任何问题。 问题是,我希望这两个项目保持在同一行即使您将浏览器大小调整得非常小。 你知道...就像桌子一样。

目标是无论如何都让右侧的项目不被包裹。

如何使用 CSS 告诉浏览器我宁愿拉伸包含的 div 而不是包裹它,以便 float: right; div 位于下面float: left; div?

我想要的是:

                                   \
 +---------------+  +------------------------/
 | float: left;  |  | float: right;          \
 |               |  |                        /
 |               |  |content stretching      \   Screen Edge
 |               |  |the div off the screen  /  <---
 +---------------+  +------------------------\
                                             /

I want to have two items on the same line using float: left for the item on the left.

I have no problems achieving this alone. The problem is, I want the two items to stay on the same line even when you resize the browser very small. You know... like how it was with tables.

The goal is to keep the item on the right from wrapping no matter what.

How to I tell the browser using CSS that I would rather stretch the containing div than wrap it so the the float: right; div is below the float: left; div?

what I want:

                                   \
 +---------------+  +------------------------/
 | float: left;  |  | float: right;          \
 |               |  |                        /
 |               |  |content stretching      \   Screen Edge
 |               |  |the div off the screen  /  <---
 +---------------+  +------------------------\
                                             /

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

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

发布评论

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

评论(10

ζ澈沫 2024-07-15 14:10:46

另一种选择是,不是浮动,而是将空白属性 nowrap 设置为父 div:

.parent {
     white-space: nowrap;
}

并重置空白并使用内联块显示,以便 div 保持在同一行,但您仍然可以为其指定宽度。

.child {
    display:inline-block;
    width:300px;
    white-space: normal;
}

这是一个 JSFiddle: https://jsfiddle.net/9g8ud31o/

Another option is, instead of floating, to set the white-space property nowrap to a parent div:

.parent {
     white-space: nowrap;
}

and reset the white-space and use an inline-block display so the divs stay on the same line but you can still give it a width.

.child {
    display:inline-block;
    width:300px;
    white-space: normal;
}

Here is a JSFiddle: https://jsfiddle.net/9g8ud31o/

好倦 2024-07-15 14:10:46

将浮动

包裹在使用此跨浏览器最小宽度 hack 的容器

中:
.minwidth { min-width:100px; width: auto !important; width: 100px; }

您也可能需要设置“溢出”,但可能不需要。

这是有效的,因为:

  • !important 声明与 min-width 相结合,导致所有内容在 IE7+ 中保持在同一行
  • IE6 不实现 min-width code>,但它有一个错误,即 width: 100px 覆盖 !important 声明,导致容器宽度为 100px。

Wrap your floating <div>s in a container <div> that uses this cross-browser min-width hack:

.minwidth { min-width:100px; width: auto !important; width: 100px; }

You may also need to set "overflow" but probably not.

This works because:

  • The !important declaration, combined with min-width cause everything to stay on the same line in IE7+
  • IE6 does not implement min-width, but it has a bug such that width: 100px overrides the !important declaration, causing the container width to be 100px.
分开我的手 2024-07-15 14:10:46

将浮动广告包裹在一个 div 中,其最小宽度大于浮动广告的宽度+边距之和。

无需修改或 HTML 表格。

Wrap your floaters in a div with a min-width greater than the combined width+margin of the floaters.

No hacks or HTML tables needed.

小情绪 2024-07-15 14:10:46

解决方案一:

显示:表格单元格(未得到广泛支持)

解决方案2:

(我讨厌黑客。)

Solution 1:

display:table-cell (not widely supported)

Solution 2:

tables

(I hate hacks.)

纵情客 2024-07-15 14:10:46

另一种选择:不要浮动右栏; 只需给它一个左边距,将其移到浮动之外即可。 您需要一两次破解才能修复 IE6,但这就是基本想法。

Another option: Do not float your right column; just give it a left margin to move it beyond the float. You'll need a hack or two to fix IE6, but that's the basic idea.

三生池水覆流年 2024-07-15 14:10:46

您确定浮动块级元素是解决此问题的最佳方案吗?

根据我的经验,CSS 经常遇到困难,事实证明,我看不到做我想做的事情的方法的原因是我对我的标记陷入了狭隘视野(思考“我怎样才能使 < em>这些元素做这个?”)而不是回去看看我到底需要实现什么,也许稍微修改我的html以促进这一点。

Are you sure that floated block-level elements are the best solution to this problem?

Often with CSS difficulties in my experience it turns out that the reason I can't see a way of doing the thing I want is that I have got caught in a tunnel-vision with regard to my markup ( thinking "how can I make these elements do this?" ) rather than going back and looking at what exactly it is I need to achieve and maybe reworking my html slightly to facilitate that.

倾其所爱 2024-07-15 14:10:46

我建议使用表格来解决这个问题。 我遇到了类似的问题,只要表格只是用于显示一些数据而不是用于主页布局就可以了。

i'd recommend using tables for this problem. i'm having a similar issue and as long as the table is just used to display some data and not for the main page layout it is fine.

孤君无依 2024-07-15 14:10:46

将此行添加到浮动元素选择器中

.floated {
    float: left;
    ...
    box-sizing: border-box;
}

它将阻止填充和边框添加到宽度,因此元素始终保持在行中,即使您有例如。 宽度为 33.33333% 的三个元素

Add this line to your floated element selector

.floated {
    float: left;
    ...
    box-sizing: border-box;
}

It will prevent padding and borders to be added to width, so element always stay in row, even if you have eg. three elements with width of 33.33333%

◇流星雨 2024-07-15 14:10:46

当用户水平缩小窗口大小并导致浮动垂直堆叠时,请删除浮动并在第二个 div(这是一个浮动)上使用 margin-top: -123px (您的值)和 margin-left: 444px (您的值)来将 div 放置在它们与浮动一起出现的位置。
这样做后,当窗口变窄时,右侧 div 会保持在原位,并在页面太窄而无法包含它时消失。 ...(对我来说)这比当用户缩小浏览器窗口时让右侧 div“跳”到左侧 div 下方更好。

When user reduces window size horizontally and this causes floats to stack vertically, remove the floats and on the second div (that was a float) use margin-top: -123px (your value) and margin-left: 444px (your value) to position the divs as they appeared with floats.
When done this way, when the window narrows, the right-side div stays in place and disappears when page is too narrow to include it. ... which (to me) is better than having the right-side div "jump" down below the left-side div when the browser window is narrowed by the user.

停滞 2024-07-15 14:10:46

我解决这个问题的方法是使用一些 jQuery。 我这样做的原因是因为 A 和 B 是百分比宽度。

HTML:

<div class="floatNoWrap">
    <div id="A" style="float: left;">
        Content A
    </div>
    <div id="B" style="float: left;">
        Content B
    </div>
    <div style="clear: both;"></div>
</div>

CSS:

.floatNoWrap
{
    width: 100%;
    height: 100%;
}

jQuery:

$("[class~='floatNoWrap']").each(function () {
    $(this).css("width", $(this).outerWidth());
});

The way I got around this was to use some jQuery. The reason I did it this way was because A and B were percent widths.

HTML:

<div class="floatNoWrap">
    <div id="A" style="float: left;">
        Content A
    </div>
    <div id="B" style="float: left;">
        Content B
    </div>
    <div style="clear: both;"></div>
</div>

CSS:

.floatNoWrap
{
    width: 100%;
    height: 100%;
}

jQuery:

$("[class~='floatNoWrap']").each(function () {
    $(this).css("width", $(this).outerWidth());
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文