我在使用HTML,CSS和JavaScript创建两行导航栏时遇到问题。
我的HTML:
<div id="top-bar">
<div class="container">
<ul>
<li><a href="#"><i class="fab fa-facebook"></i> <i class="fab fa-twitter"></i> <i class="fab fa-instagram"></i></a> </li>
<li style="padding:0;"><a href="#" style="position: absolute; z-index:1; top:0;"><img src="https://via.placeholder.com/250x115" alt="" width="100%"></a>
<li><a href="#">01283 575 671</a></li>
<li><a href="#">Join Now </a></li>
</ul>
</div>
</div>
<div class="nav-bg">
<div class="container">
<ul id="nav">
<li><a href="#" class="nav_first">Facilties</a></li>
<li><a href="#">Timetable</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#" class="nav_blank"></a></li>
<li><a href="#">The Team</a></li>
<li><a href="#">Events</a></li>
<li><a href="#" class="nav_last">Contact Us</a></li>
</ul>
</div>
</div>
我的CSS:
* { box-sizing:border-box }
body {
margin: 0px;
font-family: 'Montserrat', sans-serif;
}
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
@media (min-width: 768px) {
.container {
width: 750px;
}
}
@media (min-width: 992px) {
.container {
width: 970px;
}
}
@media (min-width: 1200px) {
.container {
width: 1170px;
}
}
#top-bar {
float: left;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 0px;
overflow: hidden;
width:100%;
min-width: 650px;
min-height: 50px;
background: #FFDE00;
}
#top-bar div ul {
margin:0;
padding:0;
list-style:none;
}
#top-bar div ul li {
float:left;
padding: 15px 15px;
width: 20%;
background: #FFDE00;
font-weight:500;
text-transform:uppercase;
}
#top-bar div ul li:first-child{
width:40%;
}
#top-bar div ul li:last-child{
background:#081F2D;
}
#top-bar div ul li a {
display: block;
color:#081F2D;
text-decoration:none;
text-align: center;
font-size:17px;
}
#top-bar > div > ul > li:nth-child(4) > a{
color:#FFDE00;
}
#nav {
text-align: justify;
min-width: 500px;
font-size: 0;
padding-left:0;
}
.nav-bg{
background-color: #EEEEEE;
}
#nav:after {
content: '';
display: inline-block;
width: 100%;
}
#nav li {
display: inline-block;
background-color: #ccc;
}
#nav a {
padding: 15px 40px;
display: block;
font-size: 1rem;
text-align: center;
color:#081F2D;
text-transform:uppercase;
font-weight:600;
text-decoration: none;
background-color: #EEEEEE;
}
#nav .nav_blank { width: 250px; padding: 15px 0; }
#nav .nav_first { padding-left: 0; }
#nav .nav_last { padding-right: 0; }
答案 0 :(得分:1)
通过实现CSS网格,按如下所示重做您的结构
CSS部分:
#parent_column
{
grid-template-columns:1fr 0.61fr 1fr;
display: grid;
width: 100vw;
height: 20vh;
}
#first_column
{
grid-column: 1;
display:grid;
grid-template-rows: 1fr 1fr;
}
#second_column
{
grid-column: 2;
}
#third_column
{
grid-column: 3;
display:grid;
grid-template-rows: 1fr 1fr;
}
HTML为
<div id="parent_column">
<div id="first_column">
<div style="background-color: rebeccapurple;">Top Bar Content on Left</div>
<div style="background-color: gold;">Bottom Bar Content on Left</div>
</div>
<div id="second_column">
<a href="#" style="position: absolute; z-index:1; top:0;"><img src="https://via.placeholder.com/250x115" alt=""
width="100%"></a>
</div>
<div id="third_column">
<div style="background-color: red;">Top Bar Content on Left</div>
<div style="background-color: pink">Bottom Bar Content on Left</div>
</div>
</div>
结果: 现在87.85%的浏览器都支持CSS-Grid,对于IE浏览器,您需要添加前缀ms-
有关浏览器support on CSS Grid
的更多参考答案 1 :(得分:0)
除了使用grid
模板或将导航吐到左右两个区域之外,我看不到其他任何选择。
如果是网格,这可以为您提供帮助:A Complete Guide to Grid
答案 2 :(得分:0)
我建议使用div
和span
而不是ul
和li
。另外,将所有内容分为3部分-左,中和右。这将使您的任务更容易完成,如下所示。
HTML:
<body>
<div id="top-bar">
<div class="container">
<div class="left-right-container">
<div class="left-container">
<span><a href="#"><i class="fab fa-facebook"></i> <i class="fab fa-twitter"></i> <i class="fab fa-instagram"></i></a> </span>
</div>
<div class="right-container">
<span><a href="#">01283 575 671</a></span>
<span><a href="#">Join Now </a></span>
</div>
</div>
<div class="center-container">
<span style="padding:0;"><a href="#" style="position: absolute; z-index:1; top:0;"><img src="https://via.placeholder.com/250x115" alt="" width="100%"></a></span>
</div>
</div>
</div>
<div class="nav-bg">
<div class="container">
<div class="left-right-nav">
<div class="left-nav">
<div>
<span><a href="#" class="nav_first">Facilties</a></span>
<span><a href="#">Timetable</a></span>
<span><a href="#">Blog</a></span>
</div>
</div>
<div class="right-nav">
<div>
<span><a href="#">The Team</a></span>
<span><a href="#">Events</a></span>
<span><a href="#" class="nav_last">Contact Us</a></span>
</div>
</div>
</div>
<div class="center-nav">
</div>
</div>
</div>
CSS:
* { box-sizing:border-box }
body {
margin: 0px;
font-family: 'Montserrat', sans-serif;
}
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
.left-container, .center-container, .right-container{
position:absolute;
display:inline-block;
margin:0;
padding:0;
width: calc(50% - 125px);
}
.left-container{
left:0;
height: 50px;
overflow:hidden;
}
.left-container span {
margin: auto;
left: 0;
right: 0;
position: absolute;
}
.center-container{
width:250px;
height:115px;
z-index:2;
left:0;
right:0;
margin:auto;
}
.right-container{
right:0;
height: 50px;
overflow:hidden;
}
.right-container span:last-child{
background:#081F2D;
width:40%;
float:right;
}
.right-container span:last-child a{
color:#FFDE00;
}
.left-right-container{
background: #FFDE00;
height: inherit;
}
.left-container span, .right-container span {
padding: 15px 15px;
font-weight:500;
text-transform:uppercase;
width:59%;
display: inline-block;
}
@media (min-width: 768px) {
.container {
width: 750px;
}
}
.left-container span a, .right-container span a {
display: block;
color:#081F2D;
text-decoration:none;
text-align: center;
font-size:17px;
}
@media (min-width: 992px) {
.container {
width: 970px;
}
}
@media (min-width: 1200px) {
.container {
width: 1170px;
}
}
#top-bar {
float: left;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 0px;
overflow: hidden;
width:100%;
min-width: 650px;
min-height: 50px;
background: #FFDE00;
}
/******************/
.nav-bg{
width:100%;
position:absolute;
top:50px;
height: 50px;
z-index:1;
}
.left-right-nav{
background-color: #EEEEEE;
height: inherit;
}
.left-nav, .center-nav, .right-nav {
position:absolute;
display:inline-block;
width: calc(50% - 125px);
}
.left-nav{
left:0;
}
.center-nav{
width:250px;
height:50px;
z-index:1;
left:0;
right:0;
margin:auto;
}
.right-nav{
right:0;
}
.left-nav div, .right-nav div{
margin: auto;
left: 0;
right: 0;
position:absolute;
background-color: #EEEEEE;
text-align:right;
}
.left-nav div span{
}
.right-nav div span{
float:left;
}
.left-nav div span, .right-nav div span{
display: inline-block;
}
.left-nav span a, .right-nav span a {
padding: 15px 40px;
display: block;
font-size: 1rem;
text-align: center;
color:#081F2D;
text-transform:uppercase;
font-weight:600;
text-decoration: none;
background-color: #EEEEEE;
}