导航栏使用填充填充父div 100%

时间:2018-03-31 11:21:54

标签: css

以下是产生以下导航的代码:

<style>
body {
    background-color: #b40404;
}
#wrapper {
    margin: auto;
    text-align: center;
    background-color: white;
    max-width: 750px;
    padding-left: 1.5%;
    padding-right: 1.5%;
    margin-top: 30px;
    border-radius: 10px;
}
ul {
    list-style-type: none;
    padding: 0px;
    overflow: hidden;
    background-color: #dc4b4b;
    border-top-right-radius: 10px;
    border-top-left-radius: 10px;
}

li {
    float: left;
}
li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}
</style>
<div id="wrapper">
<ul>
  <li><a href="nav1.html">Nav 1</a></li>
  <li><a href="nav2.html">Nav 2</a></li>
  <li><a href="nav3.html">Nav 3</a></li>
</ul>
</div>

enter image description here

如何让更浅的红色导航完全用额外的3%填充填充父div?填充需要是允许内容响应的百分比。我试图实现以下目标: enter image description here

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以将导航位置设为绝对位置:

body {
  background-color: #b40404;
}

#wrapper {
  margin: auto;
  text-align: center;
  background-color: white;
  max-width: 750px;
  padding-top:45px;
  padding-left: 1.5%;
  padding-right: 1.5%;
  margin-top: 30px;
  border-radius: 10px;
  position:relative;
}

ul {
  position:absolute;
  top:0;
  right:0;
  left:0;
  list-style-type: none;
  padding: 0px;
  margin:0;
  overflow: hidden;
  background-color: #dc4b4b;
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
.content {
  background:pink;
  padding:50px;
}
<div id="wrapper">
  <ul>
    <li><a href="nav1.html">Nav 1</a></li>
    <li><a href="nav2.html">Nav 2</a></li>
    <li><a href="nav3.html">Nav 3</a></li>
  </ul>
  <div class="content">
    some content here
  </div>
</div>

答案 1 :(得分:-5)

#wrapper {
    margin: auto;
    text-align: center;
    background-color: white;
    max-width: 750px;
    margin-left: 1.5%;
    margin-right: 1.5%;
    margin-top: 30px;
    border-radius: 10px;
}