div间隙在页面顶部?

时间:2018-11-26 21:54:27

标签: javascript html css

我的style.css中有一个Cearfix,在其他项目上一切正常。突然,我的浏览器中出现了一个间隙,导致div容器移动了aprox。向下20像素,我不知道我在这里做错了什么?

我认为我的代码在jsfiddle中运行良好。因此,我在其中放了一张图片,您可以在其中看到不需要的20px“顶部-边距”。有人可以告诉我是什么原因造成的吗?

Thx

编辑:我发现了问题。这是h1标签。如果您用p标记同时替换html和css中的h1,则在将p边距设置为0后,它可以正常工作。但是为什么呢?有人有答案吗?

    <!DOCTYPE html>
<html lang="de">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/normalize.css">
    <link href="https://fonts.googleapis.com/css?family=Lobster|Raleway:300,400,500,700" rel="stylesheet">
    <!--CSS-Font : font-family: 'Lobster', cursive;
    font-family: 'Raleway', sans-serif; -->
     <title>Appearence</title>
</head>
<div class="container">
  <body>
    <div class="main-header background-color clearfix">
      <header id="main-header-top" class=" ">
        <div class="logo-title ">
          <a href="index.html"><h1>Operation<span>Mars</span>.com</h1></a>
        </div>
      </header>
    </div>
  </body>
  <!--body-end-->
  <footer>
  </footer>
</div>
</html>


* {
  margin: 0;
  padding: 0;
  font-family: 'Dosis', sans-serif;
}

body {
  margin: 0;
  padding: 0;
}

html,
body{
  position:relative;
}

.main-header{
  width: 100%;
  height: 100px;
}
.background-color{
  background-color: black;
}

.logo-title {
  width: 400px;
  height: auto;
  left: 50%;
  position: relative;
  margin-left: -200px;
}
.logo-title a{
  text-align: center;
  text-decoration: none;
}
.logo-title a h1{
  font-size: 60px;
  text-decoration: none;
  color: white;
  font-family: 'Lobster', cursive;
}

.logo-title a h1 span{
  color:#ff7256;
  font-family: 'Lobster', cursive;
  font-size: 70px;
}
/*clearfix*/
.clearfix:before,
.clearfix:after { /*Inhalt hinzufügen*/
  content: "";
  display: table;
}

.clearfix:after {
  clear:both;
}

gap appearing

1 个答案:

答案 0 :(得分:0)

尝试一下。

* {
  margin: 0;
  padding: 0;
  font-family: 'Dosis', sans-serif;
}

body {
  margin: 0;
  padding: 0;
}

html,
body{
  position:relative;
}

.main-header{
  width: 100%;
  height: 100px;
}
.background-color{
  background-color: black;
}

.logo-title {
  width: 100%;
  height: auto;
  
}
.logo-title a{
  text-align: center;
  text-decoration: none;
  left: 50%;
  position: absolute;
  transform:translateX(-50%);
}
.logo-title a h1{
  font-size: 60px;
  text-decoration: none;
  color: white;
  font-family: 'Lobster', cursive;
}

.logo-title a h1 span{
  color:#ff7256;
  font-family: 'Lobster', cursive;
  font-size: 70px;
}
/*clearfix*/
.clearfix:before,
.clearfix:after { /*Inhalt hinzufügen*/
  content: "";
  display: table;
}

.clearfix:after {
  clear:both;
}
 <!DOCTYPE html>
<html lang="de">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/normalize.css">
    <link href="https://fonts.googleapis.com/css?family=Lobster|Raleway:300,400,500,700" rel="stylesheet">
    <!--CSS-Font : font-family: 'Lobster', cursive;
    font-family: 'Raleway', sans-serif; -->
     <title>Appearence</title>
</head>
<div class="container">
  <body>
    <div class="main-header background-color clearfix">
      <header id="main-header-top" class=" ">
        <div class="logo-title ">
          <a href="index.html"><h1>Operation<span>Mars</span>.com</h1></a>
        </div>
      </header>
    </div>
  </body>
  <!--body-end-->
  <footer>
  </footer>
</div>
</html>