【问题标题】:CSS and Page collapseCSS 和页面折叠
【发布时间】:2017-06-15 05:36:15
【问题描述】:

问题 #1 → 这是 Code Pen 链接 → Click Here。 当我使用 id="pagewrap"

将页脚放在主 div 之外时
      <footer>
        <h4>Footer</h4>
        <p>Footer text</p>
      </footer>
   </div><!-- end-of-pagewrap -->

如果我这样做→

 </div><!-- end-of-pagewrap -->
        <footer>
        <h4>Footer</h4>
        <p>Footer text</p>
      </footer>

这样坏掉了→https://www.screencast.com/t/VXRFGVa30


问题 #2→ 我有更多的问题要讨论。

<div id="pagewrap">

#pagewrap {
  padding: 15px;
  max-width: 1400px;
  margin: 20px auto;
}

但是,如果我将其更改为类系统,则填充或边框会消失。出了什么问题? CSS 是一样的吗?

<div class="pagewrap">

.pagewrap {
  padding: 15px;
  max-width: 1400px;
  margin: 20px auto;
}

【问题讨论】:

  • 你想在这里实现什么?你想要pagewrap div之外的页脚吗?请解释清楚。
  • 我只是在尝试不同的学习方式。是的,你可以说我现在想要页脚。
  • 我认为当您将 id 更改为 class pagewrap 时,您并没有更改 css 文件中的所有 pagewrap id。 css文件中有3个pagewrap id。
  • 但是它们是独立的ID,它们是相互关联的吗?所有 id 或所有类的任何限制。

标签: css html responsive-design


【解决方案1】:

这是因为pagewrap 的子元素上的float 属性,在pagewrap 上使用.clearfix

对于问题2,您只需将所有pagewrap 选择器从ID 更新为Class

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

@import url(http://fonts.googleapis.com/css?family=Open+Sans);
 body {
  font-family: 'Open Sans', sans-serif;
  color: #666;
}
/* STRUCTURE */

#pagewrap {
  padding: 15px;
  max-width: 1400px;
  margin: 20px auto;
}
header {
  height: 100px;
  padding: 0 15px;
}
#content {
  width: 66%;
  float: left;
  padding: 5px 15px;
}
#sidebar {
  width: 28%;
  padding: 5px 15px;
  float: right;
}
footer {
  clear: both;
  padding: 0 15px;
}
/************************************************************************************
MEDIA QUERIES
*************************************************************************************/

/* for 980px or less */

@media screen and (max-width: 980px) {
  #pagewrap {
    width: 94%;
  }
  #content {
    width: 91.5%;
    padding: 1% 4%;
    float: left;
  }
  #sidebar {
    clear: both;
    padding: 1% 4%;
    width: auto;
    float: right;
  }
  header,
  footer {
    padding: 1% 4%;
  }
}
/* for 700px or less */

@media screen and (max-width: 600px) {
  #content {
    width: auto;
    float: none;
  }
  #sidebar {
    width: auto;
    float: none;
  }
}
/* for 480px or less */

@media screen and (max-width: 480px) {
  header {
    height: auto;
  }
  h1 {
    font-size: 2em;
  }
  #sidebar {
    display: none;
  }
}
#content {
  background: #f8f8f8;
}
#sidebar {
  background: #f0efef;
}
header,
#content,
#middle,
#sidebar {
  margin-bottom: 5px;
}
#pagewrap,
header,
#content,
#middle,
#sidebar,
footer {
  border: solid 1px #ccc;
}
.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}
<div id="pagewrap" class="clearfix">
  <header>
    <h1>2 Column Responsive Layout</h1>
  </header>
  <section id="content">
    <h2>1st Content Area</h2>
    <p>This page demonstrates a 2 column responsive layout, complete with responsive images and jquery slideshow.</p>
  </section>
  <aside id="sidebar">
    <h2>2nd Content Area</h2>
    <p>Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</p>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
    <p>Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</p>
  </aside>
  <!--       <footer>
        <h4>Footer</h4>
        <p>Footer text</p>
      </footer> -->
</div>
<!-- end-of-pagewrap -->
<footer>
  <h4>Footer</h4>
  <p>Footer text</p>
</footer>

【讨论】:

  • 你已经定义了 .clearfix:after;独立的 .clearfix 呢?
【解决方案2】:

当 Abhishek 将其作为 Q#2 为您清除时,您可以在您忘记编辑的 CSS 文件的不同部分中为 #pagewrap 定义样式。这就是为什么当您更改为类系统时,某些样式会消失的原因。

【讨论】:

    【解决方案3】:
    1. 这肯定是由于浮动属性而发生的。使用 clearfix 是正确的方法。但如果您不想使用 clearfix,请移除浮动并使用显示属性。
    2. 对于第二个选项,为什么它不起作用 - 这是因为您必须将 css 中的每个 #pagewrap 替换为 .pagewrap。

    【讨论】:

      【解决方案4】:

      当父容器内的元素浮动时,父容器会忽略它们的高度。要解决这个问题,最基本的方法是在浮动元素之后在父容器内使用一个带有 clear:both 属性的空 div。更简洁的方法是使用clearfix

      您在 footer 上使用了 clear:both 属性,但它只会在 inside pagewrap(父容器)时清除浮动作为outside pagewrapfooter 不再是它的子元素。

      对于第二个问题,只需将所有 #pagewrap 更新为 .pagewrap

      【讨论】:

        猜你喜欢
        • 2018-06-10
        • 2020-01-20
        • 2014-11-25
        • 1970-01-01
        • 2014-05-12
        • 2010-11-21
        • 2010-12-20
        • 1970-01-01
        • 2010-09-11
        相关资源
        最近更新 更多