简体   繁体   English

ul不拉伸宽度以填充div的空间

[英]ul not stretching width to fill div's space

I am trying to get my ul (containing li elements) to stretch as necessary in order to fill the available width of the div that it is in, but for some reason it is not working. 我试图让我的ul(包含li元素)根据需要进行拉伸,以填充它所在的div的可用宽度,但是由于某种原因,它无法正常工作。 I tried using display: table; 我尝试使用display: table; and table-layout: fixed; table-layout: fixed; but they didn't work for me either. 但他们也不适合我。

 .wrapper { width: 90%; height: auto; margin: 10px auto 10px auto; border: 2px solid #000000; background-color: #0099cc; } #menu { background: #ffffff; margin-bottom: 50px; } #menu ul { list-style: none; margin: 0; padding: 0; line-height: 1; display: block; zoom: 1; display: table; } #menu ul:after { content: ' '; display: block; font-size: 0; height: 0; clear: both; visibility: hidden; } #menu ul li { float: left; display: block; padding: 0; display: table-cell; } #menu ul li a { color: #000; text-decoration: none; display: block; padding: 15px 25px; font-family: 'Work Sans', sans-serif; font-size: 1.5em; font-weight: 500; position: relative; -webkit-transition: color .25s; -moz-transition: color .25s; -ms-transition: color .25s; -o-transition: color .25s; transition: color .25s; } #menu ul li a:hover { color: #000; } #menu ul li a:hover:before { width: 100%; } #menu ul li a:before { content: ''; display: block; position: absolute; left: 0; bottom: 0; height: 3px; width: 0; background: #000; -webkit-transition: width .25s; -moz-transition: width .25s; -ms-transition: width .25s; -o-transition: width .25s; transition: width .25s; } #menu ul li.last > a:after, #menu ul li:last-child > a:after { display: none; } #menu ul li.active a { color: #000; } #menu ul li.active a:before { width: 100%; } 
 <div class="wrapper"> <div> <h1>Page title</h1> </div> <div id="menu"> <ul> <li><a><span>January</span></a> </li> <li><a><span>February</span></a> </li> <li><a><span>March</span></a> </li> <li><a><span>April</span></a> </li> <li><a><span>May</span></a> </li> <li><a><span>June</span></a> </li> <li><a><span>July</span></a> </li> <li><a><span>August</span></a> </li> <li><a><span>September</span></a> </li> <li><a><span>October</span></a> </li> <li><a><span>November</span></a> </li> <li><a><span>December</span></a> </li> <li><a><span>Spring</span></a> </li> <li><a><span>Summer</span></a> </li> <li><a><span>Autumn</span></a> </li> <li><a><span>Winter</span></a> </li> </ul> </div> </div> 

For dynamic space filling layout, you will have to look at using CSS flexbox. 对于动态空间填充布局,您将不得不使用CSS flexbox。 Most modern browsers in use today actually support it (85% unprefixed, 97% global), so you can treat it as a graceful degradation for users on browsers that do not, or progressive enhancement for users on browsers that do ;) 当今使用的大多数现代浏览器实际上都支持该功能 (无前缀的为85%,全球为97%),因此您可以将其视为对不使用浏览器的用户的正常降级,或对使用浏览器的用户的渐进式增强;)

All you need to change is the following: 您只需更改以下内容:

  1. Use display: flex on the <ul> element, and allowing wrapping within it by setting flex-wrap: wrap <ul>元素上使用display: flex ,并通过设置flex-wrap: wrap允许在其中进行flex-wrap: wrap
  2. Ditch float: left on the inner <li> element. 沟渠float: left在内部<li>元素上。 Use flex-grow: 1 to allow them to fill up remaining space on each row. 使用flex-grow: 1允许它们填充每一行的剩余空间。
  3. For a better visual effect, use text-align: center on the nested <a> element so that months appear visually centered in all links. 为了获得更好的视觉效果,请使用text-align: center在嵌套的<a>元素上居中,以使月份在所有链接中都以视觉上居中。

 .wrapper { width: 90%; height: auto; margin: 10px auto 10px auto; border: 2px solid #000000; background-color: #0099cc; } #menu { background: #ffffff; margin-bottom: 50px; } #menu ul { list-style: none; margin: 0; padding: 0; line-height: 1; /* Added the following for flexbox */ display: flex; flex-wrap: wrap; } #menu ul:after { content: ' '; display: block; font-size: 0; height: 0; clear: both; visibility: hidden; } #menu ul li { display: block; padding: 0; /* Added the following for flexbox */ flex-grow: 1; } #menu ul li a { color: #000; text-decoration: none; display: block; padding: 15px 25px; font-family: 'Work Sans', sans-serif; font-size: 1.5em; font-weight: 500; position: relative; -webkit-transition: color .25s; -moz-transition: color .25s; -ms-transition: color .25s; -o-transition: color .25s; transition: color .25s; /* Added the following for flexbox */ /* So that text appear visually centered */ text-align: center; } #menu ul li a:hover { color: #000; } #menu ul li a:hover:before { width: 100%; } #menu ul li a:before { content: ''; display: block; position: absolute; left: 0; bottom: 0; height: 3px; width: 0; background: #000; -webkit-transition: width .25s; -moz-transition: width .25s; -ms-transition: width .25s; -o-transition: width .25s; transition: width .25s; } #menu ul li.last > a:after, #menu ul li:last-child > a:after { display: none; } #menu ul li.active a { color: #000; } #menu ul li.active a:before { width: 100%; } 
 <div class="wrapper"> <div> <h1>Page title</h1> </div> <div id="menu"> <ul> <li><a><span>January</span></a> </li> <li><a><span>February</span></a> </li> <li><a><span>March</span></a> </li> <li><a><span>April</span></a> </li> <li><a><span>May</span></a> </li> <li><a><span>June</span></a> </li> <li><a><span>July</span></a> </li> <li><a><span>August</span></a> </li> <li><a><span>September</span></a> </li> <li><a><span>October</span></a> </li> <li><a><span>November</span></a> </li> <li><a><span>December</span></a> </li> <li><a><span>Spring</span></a> </li> <li><a><span>Summer</span></a> </li> <li><a><span>Autumn</span></a> </li> <li><a><span>Winter</span></a> </li> </ul> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM