我正在编写基于HTML列表的响应式导航。
目标是导航,它总是填充标题的100%宽度,同样宽的li元素。背景颜色仅显示在单词后面。它没有填写整个li元素,就像它应该的那样。
这里是HTML:
<nav>
<ul>
<li class="home"><a href="#">Home</a></li>
<li class="about"><a href="#" >About Us</a></li>
<li class="sports"><a href="#" >Sports</a></li>
<li class="news"><a href="#" >News & Events</a></li>
<li class="vols"><a href="#" >Volunteer</a>/li>
<li class="donate"><a href="#" >Donate</a></li>
</ul>
</nav>
这里是CSS:
.mainheader nav {
background-color: #000000;
height: 10%;
}
.mainheader nav ul {
list-style: none;
margin: 0 auto;
padding: 0%;
}
.mainheader nav ul li {
float: left;
display: inline;
width: 14.28%;
padding: 0%;
font-size: 20px;
text-align: center
}
.mainheader nav a:link, .mainheader nav a:visited {
color: #FFF;
display: inline-block;
text-align: 0 auto;
padding: 17.5px 0px 17.5px 0px;
height: 40px
}
.mainheader nav .about a:link, .mainheader nav .about a:visited {
background-color: #7ab503; /* color not hover */
text-shadow: none;
}
.mainheader nav .about a:visited, .mainheader nav .about a:hover {
background-color: #bffc43; /* color hover */
}
/* all the other li elements have the same css structure as the .about one! */
感谢您的帮助!
答案 0 :(得分:0)
如何使用CSS flexbox?
.mainheader nav ul {
display: flex;
list-style:none;
margin: 0 auto;
padding: 0%;
background: #7ab503;
}
.mainheader nav ul li {
display: block;
flex: 0 1 auto; /* Default */
float:left;
display: inline;
width: 14.28%;
padding: 0% ;
font-size: 20px;
text-align: center;
background: #7ab503;
}
然后你可以摆弄背景颜色
答案 1 :(得分:0)
代码非常混乱,所以我在jsFiddle
上清理了一下HTML:
<header class="mainheader">
<nav>
<ul>
<li class="home"><a href="#">Home</a></li>
<li class="about"><a href="#" >About Us</a></li>
<li class="sports"><a href="#" >Sports</a></li>
<li class="news"><a href="#" >News & Events</a></li>
<li class="vols"><a href="#" >Volunteer</a></li>
<li class="donate"><a href="#" >Donate</a></li>
</ul>
</nav>
CSS:
.mainheader nav {
background-color: #000;
text-align:center;
}
.mainheader nav ul {
list-style: none;
margin:0 auto;
padding:0;
}
.mainheader nav ul:after {
content:"";
display:block;
clear:both;
}
.mainheader nav ul li {
display:table-cell;
width:14.28%;
}
.mainheader nav a {
color: #FFF;
display: block;
padding:10px;
line-height: 20px
font-size: 20px;
}
.mainheader nav .about a {
background-color: #7ab503; /* color not hover */
text-shadow: none;
}
.mainheader nav .about a:visited, .mainheader nav .about a:hover {
background-color: #bffc43; /* color hover */
}
目前所有导航都有居中和灵活的按钮,没有浮动的乱码,也没有与IE8不兼容的弹性框