我试图在宽度和高度方面将导航栏放在div的中间。这个解决方案似乎可以完成这项工作,除了它将列表中的每个项目堆叠在一起。我之前读过一些帖子,这似乎与我使用绝对定位的事实有关。但我无法找到解决这个问题的方法。
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 0;
padding: 0;
}
.container {
background-color: red;
padding: 0;
margin: 0;
}
p {
margin: 0;
}
p+p {
margin-top: 10px;
}
.site-navigation {
background-color: yellow;
text-align: center;
height: auto;
/* Basically saying fit all children within the container */
overflow: hidden;
position: relative;
}
.site-navigation figure {
padding: 0;
margin: 0;
float: left;
}
.site-navigation ul {
padding: 0;
margin: 0;
}
.site-navigation ul li {
list-style-type: none;
display: inline-block;
}
.site-navigation ul li a {
text-decoration: none;
color: black;
margin-right: 20px;
padding: 0;
position: absolute;
top: 50%;
}

<header class="container site-header">
<div class="innercontainer site-header">
<p> some text here </p>
</div>
</header>
<nav class="container site-navigation">
<figure>
<img src="nav.png" />
</figure>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Repairs</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</nav>
&#13;
答案 0 :(得分:1)
您在position:absolute
标记中使用的是a
,这就是它陷入困境的原因。
更新css部分
.site-navigation ul {
padding: 0;
margin: 0;
position: absolute; /* Add this */
left: 0px; /* Add this */
right: 0px; /* Add this */
margin: 0 auto; /* Add this */
}
.site-navigation ul li a {
text-decoration: none;
color: black;
margin-right: 20px;
padding: 0;
/*position: absolute;*/
/*top: 50%;*/
}
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 0;
padding: 0;
}
.container {
background-color: red;
padding: 0;
margin: 0;
}
p {
margin: 0;
}
p+p {
margin-top: 10px;
}
.site-navigation {
background-color: yellow;
text-align: center;
height: auto;
/* Basically saying fit all children within the container */
overflow: hidden;
position: relative;
}
.site-navigation figure {
padding: 0;
margin: 0;
float: left;
}
.site-navigation ul {
padding: 0;
margin: 0;
position: absolute;
left: 0px;
right: 0px;
margin: 0 auto;
}
.site-navigation ul li {
list-style-type: none;
display: inline-block;
}
.site-navigation ul li a {
text-decoration: none;
color: black;
margin-right: 20px;
padding: 0;
}
<header class="container site-header">
<div class="innercontainer site-header">
<p> some text here </p>
</div>
</header>
<nav class="container site-navigation">
<figure>
<img src="nav.png" />
</figure>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Repairs</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</nav>
答案 1 :(得分:0)
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 0;
padding: 0;
}
.container {
background-color: red;
padding: 0;
margin: 0;
}
p {
margin: 0;
}
p+p {
margin-top: 10px;
}
.site-navigation {
background-color: yellow;
text-align: center;
height: auto;
/* Basically saying fit all children within the container */
overflow: hidden;
position: relative;
}
.site-navigation figure {
padding: 0;
margin: 0;
float: left;
}
.site-navigation ul {
padding: 0;
margin: 0;
}
.site-navigation ul li {
list-style-type: none;
}
.site-navigation ul li a {
text-decoration: none;
color: black;
margin-right: 20px;
padding: 0;
}
<header class="container site-header">
<div class="innercontainer site-header">
<p> some text here </p>
</div>
</header>
<nav class="container site-navigation">
<figure>
<img src="nav.png" />
</figure>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Repairs</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</nav>