在不设置父宽度的情况下防止浮动换行

发布于 2024-09-02 06:53:13 字数 566 浏览 2 评论 0原文

我想让几个项目无限水平地堆叠(一行),而不设置其父容器的宽度。我完全知道将容器 div 设置为 width:1000px;将使它们堆叠,但由于各种原因我不想使用这个解决方案。还有其他选择吗?

<html>
<head>
<style type="text/css">
div {white-space:nowrap; clear:none;}
div div {width:300px; border:1px solid black; float:left; display:inline;}
</style>
</head>
<body>
<div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
</div>
</body>
</html>

I would like to have several items stack horizontally infinitely (in one row), without setting the width of their parent container. I'm fully aware that setting the container div to width:1000px; will make them stack, but for various reasons I do not want to use this solution. Are there any alternatives?

<html>
<head>
<style type="text/css">
div {white-space:nowrap; clear:none;}
div div {width:300px; border:1px solid black; float:left; display:inline;}
</style>
</head>
<body>
<div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
    <div>x</div>
</div>
</body>
</html>

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

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

发布评论

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

评论(2

夏至、离别 2024-09-09 06:53:14

这不一定是一个可靠的解决方案,但如果您可以在每个内部 div 中获取恰好 300px 的内容,则可以删除浮动和宽度属性。也许包含一个 1x300 透明图像,并确保没有太多文本?也许

div.dummycontent {float:left; width:200px; background-color:Blue; }
<div class="dummycontent"> </div>

或者如果不设置宽度的原因主要是直到运行时才知道尺寸需要是多少,也许用 JavaScript 计算,然后设置它?

It's not necessarily a robust solution, but if you can get exactly 300px of content in each of the inner divs, you could remove the float and width properties. Maybe include a 1x300 transparent image, and make sure you don't have too much text? Maybe

div.dummycontent {float:left; width:200px; background-color:Blue; }
<div class="dummycontent"> </div>

Or if the reasons for not setting a width are primarily along the lines of not knowing until runtime what the size will need to be, maybe calculate with JavaScript, and set it then?

写给空气的情书 2024-09-09 06:53:13
  display: inline;
  white-space: nowrap;
  float: none;

在父元素上你需要

  overflow:hidden;
  display: inline;
  white-space: nowrap;
  float: none;

and on a parent element you'll need

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