是否可以防止 HTML 中的子元素换行?

发布于 2024-09-18 23:07:47 字数 400 浏览 5 评论 0原文

.container
{
 position: absolute;
 bottom: 0px;
 height: 25px;
 left: 200px;
 padding-right: 5px;
 background-color: black;
 overflow: hidden;
}


.child
{
 position: relative;
 float: left;
 height: 100%;
 width: 95px;
 background-color: #99CCFF;
 margin-left: 5px;
}

当浏览器窗口的大小小于允许所有子元素在不换行的情况下适应时,我希望有一个滚动条,而不是子元素换行的默认机制。

我无法控制子元素的数量,因此无法设置容器的宽度。如何防止子元素的换行?

.container
{
 position: absolute;
 bottom: 0px;
 height: 25px;
 left: 200px;
 padding-right: 5px;
 background-color: black;
 overflow: hidden;
}


.child
{
 position: relative;
 float: left;
 height: 100%;
 width: 95px;
 background-color: #99CCFF;
 margin-left: 5px;
}

I when the size of the browser window is smaller than will allow for all the children to fit without wrapping I would like there to be a scrollbar, not the default mechanism of the child elements wrapping.

I'm not in control of the number of child elements so I can not set a width on the container. How can I prevent the wrapping of the child elements?

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

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

发布评论

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

评论(3

不醒的梦 2024-09-25 23:07:48

如果您不想包装,则不应使用浮动 - 它们是专门为包装而创建的。

使用带有 overflow:autowhite-space:nowrap 的父容器,以及带有 display:inlineinline-block 的子容器代码>.

If you don't want wrapping, you should not use floats - they were created specifically for wrapping.

Use a parent container with overflow:auto and white-space:nowrap and children with display:inline or inline-block.

梦里南柯 2024-09-25 23:07:48

您可以添加所有子项的父持有者 ()。然后使用该div的overflow:auto。如果这不起作用,那么还可以使用 self.innerHeight 和 self.innerWidth (通过 javascript)作为 div 的高度和宽度。

You can add a parent holder () of all childes. And then use the overflow:auto of that div. If that doesn't work then also use self.innerHeight and self.innerWidth (via javascript) for the div height and width.

猫弦 2024-09-25 23:07:48

这对于简单的 HTML 和 CSS 来说实际上是不可能的。在不知道子元素数量的情况下,唯一的方法是使用 JavaScript 根据子元素的数量及其总宽度动态设置最小宽度。

This is not really possible with simple HTML and CSS. Without knowing the number of child elements, the only way would be to dynamically set a min-width using JavaScript, based on the number of child elements and their total width.

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