html - CSS 视差效果。如何更改上边距

标签 html css parallax

我正在尝试处理这个 CSS 视差效果示例。如何更改/删除第一张图片和页面顶部边缘之间的空间?我“玩”了很长时间的代码,但还没有弄明白。

/* ============================================================
  PRIMARY STRUCTURE
============================================================ */
.container {
  max-width: 960px;
  margin: 0 auto;
}
/* ============================================================
  SECTIONS
============================================================ */
section.module:last-child {
  margin-bottom: 0;
}
section.module h2 {
  margin-bottom: 40px;
  font-family: "Roboto Slab", serif;
  font-size: 30px;
}
section.module p {
  margin-bottom: 40px;
  font-size: 16px;
  font-weight: 300;
}
section.module p:last-child {
  margin-bottom: 0;
}
section.module.content {
  padding: 40px 0;
}
section.module.parallax {
  height: 600px;
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}
section.module.parallax h1 {
  color: rgba(255, 255, 255, 0.8);
  font-size: 48px;
  line-height: 600px;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
  text-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
section.module.parallax-1 {
  background-image: url("http://www.fonstola.ru/pic/201112/2560x1600/fonstola.ru-63832.jpg");
}
section.module.parallax-2 {
  background-image: url("http://hdoboi.net/uploads/hd/95263_krasivyiy_osenniy_peyzaj.jpg");
}
section.module.parallax-3 {
  background-image: url("http://drobs.ru/opyat/8/peyzazh_priroda_holmy_derevya_zelen_tuman_rassvet_solnce_5576x3087.jpg");
}

@media all and (min-width: 600px) {
  section.module h2 {
    font-size: 42px;
  }
  section.module p {
    font-size: 20px;
  }
  section.module.parallax h1 {
    font-size: 96px;
  }
}
@media all and (min-width: 960px) {
  section.module.parallax h1 {
    font-size: 160px;
  }
}
<section class="module parallax parallax-1">
  <div class="container">
    <h1>Serene</h1>
  </div>
</section>

<section class="module content">
  <div class="container">
    <h2>Lorem Ipsum Dolor</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  </div>
</section>

<section class="module parallax parallax-2">
  <div class="container">
    <h1>Rise</h1>
  </div>
</section>

<section class="module content">
  <div class="container">
    <h2>Lorem Ipsum Dolor</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  </div>
</section>

<section class="module parallax parallax-3">
  <div class="container">
    <h1>Calm</h1>
  </div>
</section>

<section class="module content">
  <div class="container">
    <h2>Lorem Ipsum Dolor</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  </div>
</section>

最佳答案

上述问题是由边距折叠引起的。引用:https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing

您可以通过删除第一个 h1 元素的默认上边距和默认主体边距来解决它。引用以下代码:

/* ============================================================
  PRIMARY STRUCTURE
============================================================ */
.container {
  max-width: 960px;
  margin: 0 auto;
}
/* ============================================================
  SECTIONS
============================================================ */
section.module:last-child {
  margin-bottom: 0;
}
section.module h2 {
  margin-bottom: 40px;
  font-family: "Roboto Slab", serif;
  font-size: 30px;
}
section.module p {
  margin-bottom: 40px;
  font-size: 16px;
  font-weight: 300;
}
section.module p:last-child {
  margin-bottom: 0;
}
section.module.content {
  padding: 40px 0;
}
section.module.parallax {
  height: 600px;
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}
section.module.parallax h1 {
  color: rgba(255, 255, 255, 0.8);
  font-size: 48px;
  line-height: 600px;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
  text-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
section.module.parallax-1 {
  background-image: url("http://www.fonstola.ru/pic/201112/2560x1600/fonstola.ru-63832.jpg");
}
section.module.parallax-2 {
  background-image: url("http://hdoboi.net/uploads/hd/95263_krasivyiy_osenniy_peyzaj.jpg");
}
section.module.parallax-3 {
  background-image: url("http://drobs.ru/opyat/8/peyzazh_priroda_holmy_derevya_zelen_tuman_rassvet_solnce_5576x3087.jpg");
}

/* Added to remove the margin at the top of the page and the default body margin */
.no-margin-top {
   margin-top: 0;
}

body {
    margin: 0;
}

@media all and (min-width: 600px) {
  section.module h2 {
    font-size: 42px;
  }
  section.module p {
    font-size: 20px;
  }
  section.module.parallax h1 {
    font-size: 96px;
  }
}
@media all and (min-width: 960px) {
  section.module.parallax h1 {
    font-size: 160px;
  }
}
<section class="module parallax parallax-1">
  <div class="container">
    <h1 class="no-margin-top">Serene</h1>
  </div>
</section>

<section class="module content">
  <div class="container">
    <h2>Lorem Ipsum Dolor</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  </div>
</section>

<section class="module parallax parallax-2">
  <div class="container">
    <h1>Rise</h1>
  </div>
</section>

<section class="module content">
  <div class="container">
    <h2>Lorem Ipsum Dolor</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  </div>
</section>

<section class="module parallax parallax-3">
  <div class="container">
    <h1>Calm</h1>
  </div>
</section>

<section class="module content">
  <div class="container">
    <h2>Lorem Ipsum Dolor</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  </div>
</section>

关于html - CSS 视差效果。如何更改上边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38174141/

相关文章:

javascript - 如何用 php 数组填充 javascript?

html - 注册表未将数据存储在数据库中

javascript - 如何对 SVG 中链接的图像进行去饱和处理?

ios - 每次 View 滚动时,将 KIImagePager 与外部图像一起加载

javascript - 仅当我更新或编辑时,才会出现简单 CRUD 应用程序中的“保存”按钮

javascript - 在导航栏悬停时更改 div 的背景图像

jquery - 修复 Canvas 外导航菜单的标题/菜单栏?

javascript - div 内内容的高度

jquery - 使用视差滚动的内容下方的空白空间

javascript - 如何设置元素的高度以匹配缩放 div 的偏移速度?