CSS!水平可调整大小的菜单ul li,如何将其展开到显示器的全宽?

发布于 2024-12-28 06:03:44 字数 968 浏览 5 评论 0 原文

该网站是可调整大小的,所以我不能使用px,有时最后的项目会遇到麻烦,当窗口变小的长度时它们会掉落,如何避免这种情况?让我们看看我的单页:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>li menu</title>
</head>

<body>
<style>
div {
    width: 100%;
    height: 3%;
    background: red;
    opacity: 0.7;
}
ul {
    width:100%;
    list-style:none;
    padding: 0;
    margin: 0;
}

ul li {
 background-color:yellow;
 display: inline;
 text-align: center;
 width:30%;
}
</style>
<div>
    <ul>
        <li>item 1</li>
        <li>item 2</li>
        <li>item 3</li>
        <li>item 4</li>
        <li>item 5</li>
    </ul>
</div>

</body>
</html>

the site is resizable, so I can not use px, and sometimes it's a trouble with last items, they falling down when window go into small length, how to avoid this? let's see my single page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>li menu</title>
</head>

<body>
<style>
div {
    width: 100%;
    height: 3%;
    background: red;
    opacity: 0.7;
}
ul {
    width:100%;
    list-style:none;
    padding: 0;
    margin: 0;
}

ul li {
 background-color:yellow;
 display: inline;
 text-align: center;
 width:30%;
}
</style>
<div>
    <ul>
        <li>item 1</li>
        <li>item 2</li>
        <li>item 3</li>
        <li>item 4</li>
        <li>item 5</li>
    </ul>
</div>

</body>
</html>

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

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

发布评论

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

评论(2

失而复得 2025-01-04 06:03:44

您可以添加空白:nowrap;到您的 UL 元素 http://jsfiddle.net/jvrCH/
但在这种情况下,当空间不足时,菜单元素将离开屏幕。

You can add white-space: nowrap; to your UL element http://jsfiddle.net/jvrCH/
But in this case menu elements will go off screen when not enough space.

锦欢 2025-01-04 06:03:44

如果您不关心完美支持旧的导航器,您可以在包含的 div 上设置一个 min-width ,例如:

div {
    /* current css */
    min-width:960px; /* which is the current minimum width for designing website for desktops */
}

您可以在按钮上执行相同的操作(并且使用 display:blockfloat:left 来保留 margin-top/bottompadding-top/bottomheight 工作):

ul li {
    /* current css */
    display:block; /* default display for list elements, could be omitted */
    float:left;
    width:30%;
    max-width:125px;
    min-width:50px;
}

最后到纠正浮动问题(如果所有子元素都处于浮动状态,则父元素会折叠):

ul {
    /* current css */
    overflow:hidden
}

您可以依靠 overflow:hidden 技巧。 info/css-clearfix.html" rel="nofollow">clearfix 解决方案

I you don't care about perfectly supporting old navigators, you could set a min-width on your containing div like :

div {
    /* current css */
    min-width:960px; /* which is the current minimum width for designing website for desktops */
}

And you could do the same on your buttons (and use display:block and float:left to keep margin-top/bottom, padding-top/bottom and height working):

ul li {
    /* current css */
    display:block; /* default display for list elements, could be omitted */
    float:left;
    width:30%;
    max-width:125px;
    min-width:50px;
}

And finally to correct the float issue (Parent element collapses if all children are in float) :

ul {
    /* current css */
    overflow:hidden
}

Instead of using the overflow:hidden trick, you could rely on the clearfix solution

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