html - 调整 .container 宽度时对导航栏产生不良影响

标签 html css

我是初学者,我正在做这个练习,我必须创建一个顶部导航栏。

这是我正在进行的工作:https://jsfiddle.net/naufragio/kscty1zh/ (你能看见它吗?)。基本上它只是一个导航栏,其中一些元素向左浮动,另一些向右浮动,中间有一个 Logo 。

这就是问题所在:

如果在 .container 选择器的 CSS 规则中,我将宽度属性从 100%(当前值)更改为 80%,那么除了 float 在条形右侧的元素外,所有元素都会根据需要进行相应调整, 不会相应调整。

与浮清有关吗?我不知道如何摆脱这种情况...

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

.container {
  margin: auto;
  width: 100%
}

body {
  margin: 0;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: fixed;
  top: 0;
  width: 100%;
}

li {
  float: left;
  font-weight: bold;
}

li a {
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  background-color: #111;
}

.imgheader {
  text-align: center;
  padding-top: 0.5%;
}

.navheader {
  border: 1px solid rgba(0, 0, 0, 0.3);
  height: 50px;
}

.buttonheader1 {
  margin: 10px;
  padding: 8px 10px;
  background-color: #6288A5;
  border: 1px solid #4D7B9F;
  border-radius: 3px;
  color: #fff;
  margin-right: 80px;
  clear: both;
}

.buttonheader2 {
  margin: 10px;
  padding: 8px 10px;
  background-color: #6288A5;
  border: 1px solid #4D7B9F;
  border-radius: 3px;
  color: #fff;
  clear: both;
}

.buttonleft {
  margin: 10px;
  padding: 8px 10px;
  background-color: white;
  border: 1px solid #4D7B9F;
  border-radius: 3px;
  color: black !important;
  font-weight: bold;
}
<body>
  <div class="container">
    <div class="nav">
      <div class="navheader">

        <div class="leftheader">
          <ul>
            <li style="float:left"><button class="buttonleft" type="button">SECTIONS</button></li>
            <li style="float:left"><a href="#home">HOME</a></li>
            <li style="float:left"><a href="#news">SEARCH</a></li>
          </ul>
        </div>
        <div class="rightheader">
          <ul>
            <li style="float:right"><button class="buttonheader1" type="button">LOG IN</button></li>
            <li style="float:right"><button class="buttonheader2" type="button">SUBSCRIBE</button></li>
          </ul>
        </div>
        <div class="imgheader">
          <img src="logo.png" alt="logo">
        </div>
      </div>
      <div class="navarticles">
      </div>
    </div>
    <div class="content">
    </div>
    <div class="footer">
    </div>
  </div>
</body>

最佳答案

  1. position: fixed 替换为 position: absolute 用于您的 .container
  2. position: relative; 给你的 navheader

position: fixed始终是视口(viewport),这就是为什么它不会留在您的 .navheader 中的原因。但是,如果您给它 position: absolute,它的位置将由下一个具有 position: relative

的父容器确定

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

.container {
  margin: auto;
  width: 100%
}

body {
  margin: 0;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: absolute;
  top: 0;
  width: 100%;
}

li {
  float: left;
  font-weight: bold;
}

li a {
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  background-color: #111;
}

.imgheader {
  text-align: center;
  padding-top: 0.5%;
}

.navheader {
  position: relative;
  border: 1px solid rgba(0, 0, 0, 0.3);
  height: 50px;
}

.buttonheader1 {
  margin: 10px;
  padding: 8px 10px;
  background-color: #6288A5;
  border: 1px solid #4D7B9F;
  border-radius: 3px;
  color: #fff;
  margin-right: 80px;
  clear: both;
}

.buttonheader2 {
  margin: 10px;
  padding: 8px 10px;
  background-color: #6288A5;
  border: 1px solid #4D7B9F;
  border-radius: 3px;
  color: #fff;
  clear: both;
}

.buttonleft {
  margin: 10px;
  padding: 8px 10px;
  background-color: white;
  border: 1px solid #4D7B9F;
  border-radius: 3px;
  color: black !important;
  font-weight: bold;
}
<body>
  <div class="container">
    <div class="nav">
      <div class="navheader">

        <div class="leftheader">
          <ul>
            <li style="float:left"><button class="buttonleft" type="button">SECTIONS</button></li>
            <li style="float:left"><a href="#home">HOME</a></li>
            <li style="float:left"><a href="#news">SEARCH</a></li>
          </ul>
        </div>
        <div class="rightheader">
          <ul>
            <li style="float:right"><button class="buttonheader1" type="button">LOG IN</button></li>
            <li style="float:right"><button class="buttonheader2" type="button">SUBSCRIBE</button></li>
          </ul>
        </div>
        <div class="imgheader">
          <img src="logo.png" alt="logo">
        </div>
      </div>
      <div class="navarticles">
      </div>
    </div>
    <div class="content">
    </div>
    <div class="footer">
    </div>
  </div>
</body>

参见:https://jsfiddle.net/3ajch21r/2/

关于html - 调整 .container 宽度时对导航栏产生不良影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45800043/

相关文章:

css - 带有背景图像的视频在页面加载时闪烁

javascript - 如何解决 Google Chrome 中的 Roboto 字体加载问题?

javascript - 如何在不更改 html 字体大小的情况下使文本框变大?

html - 除了 stylesheet.css 之外,我应该为 OpenCart 模板更新修改哪些 CSS 文件?

javascript - 用 $() 附加元素的问题

javascript - 在 Javascript 中创建下拉菜单 - Canvas 上没有显示

css - 如何使此工具提示与箭头一起显示在底部

css - 如何使用 CSS3 使嵌套的 div 居中旋转?

html - 父 div 采用第一个子元素的高度而不是所有子元素高度的总和

javascript - JQuery 更改 css 在鼠标释放时切换回