html - margin : 0; is not setting margin to 0

标签 html css

我是网页设计的新手,我正在尝试为网页创建标题。但是,我似乎在它上面有一点空白。我已经尝试在我的文档中添加规范化样式表,并将 html、正文和标题元素的边距和填充设置为 0,但似乎没有任何效果。我怎样才能删除这个空白?

HTML

<html>
  <head>
    <link rel="stylesheet" src="//normalize-css.googlecode.com/svn/trunk/normalize.css" />
  </head>
  <body>
    <header>
      <a href="#">
        <h1>Jason K</h1>
        <h2>Bum</h2>
      </a>
    </header>
    <div id="wrapper">
      <section>
      <ul id="gallery">
        <li>
          <a href="#">
            <img src="http://www.asi.it/files/images/1024X768_02_0.jpg" alt="New York">
            <p>sample sample sample</p>
          </a>
        </li>
        <li>
          <a href="#">
            <img src="http://oldweb.lbto.org/images/n891_1024x768.jpg" alt="Los Angeles">
            <p>sample sample sample</p>
          </a>
        </li>
      </ul>
      </section>
    </div>
  </body>
</html>

CSS

html, body, header {
  margin: 0;
  padding: 0;
}

body {
  background-color:#d7d7e8;
}

/*************************************************
NAV
*************************************************/
header {
  width:100%;
  float:left;
  margin:0 0 30px 0;
  background-color:cyan;
}

/**************************************************
PORTFOLIO PAGE
***************************************************/
#wrapper {
  max-width:1550px;
  margin:0px auto;
}

#gallery li {
  width:30%;
  float:left;
  list-style:none;
  margin:0% 10%;
}

img {
  max-width:100%;
}

a img {
  margin-bottom:-20px;
}

a p {
  background-color:#ffffff;
  text-align:center;
  padding:5px;
  border-radius:0 0 6px 6px;
}

li a, header a {
  text-decoration:none;
  color:#58585b;
}

最佳答案

这是一个由两部分组成的问题,有两个备选解决方案。

解决方案一:

首先,<h1> <header> 中的元素元素。 <强> h1 元素有一个固有的 margin-topmargin-bottom 16 像素。

二、float: left;导致你的标题向下移动。删除 float: left;来自 header {}并将其添加到您的 CSS 中,使您的标题与页面顶部齐平:

header h1 {
    margin-top: 0px;
}

例子:

header h1 {
    margin-top: 0px;
}

html, body, header {
  margin: 0;
  padding: 0;
}


body {
  background-color:#d7d7e8;
}

/*************************************************
NAV
*************************************************/
header {
  width:100%;
  margin:0 0 30px 0;
  background-color:cyan;
}

/**************************************************
PORTFOLIO PAGE
***************************************************/
#wrapper {
  max-width:1550px;
  margin:0px auto;
}

#gallery li {
  width:30%;
  float:left;
  list-style:none;
  margin:0% 10%;
}

img {
  max-width:100%;
}

a img {
  margin-bottom:-20px;
}

a p {
  background-color:#ffffff;
  text-align:center;
  padding:5px;
  border-radius:0 0 6px 6px;
}

li a, header a {
  text-decoration:none;
  color:#58585b;
}
<header>
  <a href="#">
    <h1>Jason K</h1>
    <h2>Bum</h2>
  </a>
</header>
<div id="wrapper">
  <section>
    <ul id="gallery">
      <li>
        <a href="#">
          <img src="http://www.asi.it/files/images/1024X768_02_0.jpg" alt="New York">
          <p>sample sample sample</p>
        </a>
      </li>
      <li>
        <a href="#">
          <img src="http://oldweb.lbto.org/images/n891_1024x768.jpg" alt="Los Angeles">
          <p>sample sample sample</p>
        </a>
      </li>
    </ul>
  </section>
</div>


方案二:

或者,您可以留下 float: left;并删除 margin-top来自 ul 元素,它在顶部和底部也有 16px 的固有边距。添加这个:

header h1, #gallery {
    margin-top: 0;
}

您将获得与解决方案 1 相同的效果。


您应该避免使用通用选择器,因为这会导致意想不到的后果,更不用说通用选择器非常慢的事实了。互联网上有一小部分关于使用 * {} 的危险的文献。适用于任何东西,尤其是作为 CSS 重置工具。

关于html - margin : 0; is not setting margin to 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665047/

相关文章:

javascript - css3 图像淡入淡出

Javascript - 从选择中附加选择列表

javascript - 选择后如何从输入颜色中获取新的颜色值?

javascript - 通过摇动按钮创建一个小摆动

css - 定位嵌套表

jquery - 我想摆脱这个 magnifico 弹出 jquery 效果

css - CSS 网格中具有不同宽度的动态列数

javascript - 检测列表框是否为空(没有选项)

html - flexbox - 1/3 和 2/3 比例的中心元素

jquery - 如何显示封面并将其放在特定位置?