标头隐藏/影响div之后

时间:2018-12-25 06:47:46

标签: html css

我的标题CSS让我很痛苦。它会影响随后发生的事情。我只想在内容中心放置3个链接/按钮。但是当我尝试做到这一点时。无法正常工作。 p标记或h1标记无关紧要,始终卡在标头后面。我使用了明确的修复程序,但效果不佳。我不确定为什么下一个div停留在标题后面。而且,当我尝试将CS​​S赋予行div中的a标记时。它也没有生效。

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #f4f4f4;
}

a {
  color: #000;
}

/* header */

.header {
  background-color: #fff;
  box-shadow: 1px 1px 4px 0 rgba(0,0,0,.1);
  position: fixed;
  width: 100%;
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  background-color: #fff;
}

.header li a {
  display: block;
  padding: 20px 20px;
  border-right: 1px solid #f4f4f4;
  text-decoration: none;
}

.header li a:hover,
.header .menu-btn:hover {
  background-color: #f4f4f4;
}

.header .logo {
  display: block;
  float: left;
  font-size: 2em;
  padding: 10px 20px;
  text-decoration: none;
}

/* menu */

.header .menu {
  clear: both;
  max-height: 0;
  transition: max-height .2s ease-out;
}

/* menu icon */

.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  padding: 28px 20px;
  position: relative;
  user-select: none;
}

.header .menu-icon .navicon {
  background: #333;
  display: block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.header .menu-icon .navicon:before {
  top: 5px;
}

.header .menu-icon .navicon:after {
  top: -5px;
}

/* menu btn */

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked ~ .menu {
  max-height: 240px;
}

.header .menu-btn:checked ~ .menu-icon .navicon {
  background: transparent;
}

.header .menu-btn:checked ~ .menu-icon .navicon:before {
  transform: rotate(-45deg);
}

.header .menu-btn:checked ~ .menu-icon .navicon:after {
  transform: rotate(45deg);
}

.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
  top: 0;
}


html {
	font-family: Helvetica Neue;
}


.clearfix:after { 
   content: "."; 
   visibility: hidden; 
   display: block; 
   height: 0; 
   clear: both;
}

.row {
	margin: auto;
	text-align: center;
	width: 85%;
	color: black;
}

.button a:link {  	
	text-decoration: none;
	background-color: black;
	border: 1px solid red;
	padding: 15px;
}

a:visited {
  	text-decoration: none !important;
}

/* 48em = 768px */

@media (min-width: 48em) {
  .header li {
    float: left;
  }
  .header li a {
    padding: 20px 30px;
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<header class="header">
    <a href="" class="logo">GO</a>
    <input class="menu-btn" type="checkbox" id="menu-btn" />
    <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
    <ul class="menu">
        <li><a href="#">Menu 1</a></li>
        <li><a href="#">Menu 2</a></li>
        <li><a href="#">Menu 3</a></li>
    </ul>
</header>

<div class="clearfix"></div>
<div class="row">
    <a href="#" class="button">Click</a>
</div>

知道为什么会这样吗?

2 个答案:

答案 0 :(得分:2)

padding-top添加到您的body中,并用headertop粘贴到top: 0;

参见下文:

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #f4f4f4;
  padding-top: 80px; /* added padding-top */
}

a {
  color: #000;
}



.header {
  background-color: #fff;
  box-shadow: 1px 1px 4px 0 rgba(0,0,0,.1);
  position: fixed;
  width: 100%;
  top: 0; /* stick to top */
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  background-color: #fff;
}

.header li a {
  display: block;
  padding: 20px 20px;
  border-right: 1px solid #f4f4f4;
  text-decoration: none;
}

.header li a:hover,
.header .menu-btn:hover {
  background-color: #f4f4f4;
}

.header .logo {
  display: block;
  float: left;
  font-size: 2em;
  padding: 10px 20px;
  text-decoration: none;
}

/* menu */

.header .menu {
  clear: both;
  max-height: 0;
  transition: max-height .2s ease-out;
}

/* menu icon */

.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  padding: 28px 20px;
  position: relative;
  user-select: none;
}

.header .menu-icon .navicon {
  background: #333;
  display: block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.header .menu-icon .navicon:before {
  top: 5px;
}

.header .menu-icon .navicon:after {
  top: -5px;
}

/* menu btn */

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked ~ .menu {
  max-height: 240px;
}

.header .menu-btn:checked ~ .menu-icon .navicon {
  background: transparent;
}

.header .menu-btn:checked ~ .menu-icon .navicon:before {
  transform: rotate(-45deg);
}

.header .menu-btn:checked ~ .menu-icon .navicon:after {
  transform: rotate(45deg);
}

.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
  top: 0;
}


html {
	font-family: Helvetica Neue;
}


.clearfix:after { 
   content: "."; 
   visibility: hidden; 
   display: block; 
   height: 0; 
   clear: both;
}

.row {
	margin: auto;
	text-align: center;
	width: 85%;
	color: black;
}

.button:link {  	
	text-decoration: none;
	background-color: black;
	border: 1px solid red;
	padding: 15px;
  color: white; /* added for demonstration */
}

a:visited {
  	text-decoration: none !important;
}

/* 48em = 768px */

@media (min-width: 48em) {
  .header li {
    float: left;
  }
  .header li a {
    padding: 20px 30px;
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<header class="header">
		    <a href="" class="logo">Reactive Design</a>
		        <input class="menu-btn" type="checkbox" id="menu-btn" />
		        <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
		        <ul class="menu">
				    <li><a href="#">Menu 1</a></li>
				    <li><a href="#">Menu 2</a></li>
				    <li><a href="#">Menu 3</a></li>
		 		</ul>
		</header>

		<div class="clearfix"></div>
		<div class="row">
			
			<a href="#" class="button">Click</a>

		</div>

答案 1 :(得分:1)

似乎您正在尝试使页眉始终位于页面顶部(即使向下滚动),因此您在其CSS中添加了position: fixed。这种样式使所有其他内容都从页面顶部开始,而不是从标题下面开始。因此,您可以给其下面的div一些margin-top: 80px,也可以给padding-top: 100px赋予body,给top: 0赋予header

如果您不希望在向下滚动时标题保持在页面顶部。那么@Ana Liza Pandac的回答似乎是个好方法。