【问题标题】:Clearfix-Bug with negative margin带有负边距的 Clearfix-Bug
【发布时间】:2015-04-18 05:31:05
【问题描述】:

我注意到每个人都在使用的“默认”clearfix 存在问题。出于某种原因,.gallery 上的负边距打破了它。我找不到任何合适的解决方案。我发现的那些会干扰我页面的其他部分。

所以我只需要对 clearfix 本身进行修复。

另外一个 CSS 解决方案可以添加到 .gallery 并且 不是 overflow: hidden 将是 - 假设:好的。

谢谢!

.gallery {
  position: relative;
  margin: 0 -20px -20px 0; /* remove margins caused by inner items */
}

/* ##### Please fix this clearfix ##### */
.gallery:after {
  display: table;
  clear: both;
  content: '';
}

.gallery .galleryItem {
  margin: 0 20px 20px 0;
  width: 100px;
  height: 100px;
  background: #666;
  float: left;
}



/* ----- do not edit below - this part contains prerequisites ----- */

.teaser {
  position: relative;
  margin: 0;
  padding: 20px;
  background: #444;
}
.teaser .title {
  margin: 10px 0 0 0;
  font-weight: normal;
  font-size: 14px;
  background: #555;
}
.teaser + .teaser {
  margin-top: 20px;
}
.teaser:nth-child(1) .galleryItem {
  background: #cc6600;
}
.teaser:nth-child(2) .galleryItem {
  background: #cc0000;
}
.teaser:nth-child(3) .galleryItem {
  background: #00cc00;
}
.teaser:nth-child(4) .galleryItem {
  background: #00cc00;
}
body {
  background: #333;
  color: #eee;
  font-family: Helvetica, sans-serif;
}
#sidebar {
  padding: 10px;
  width: 380px;
}
.tooltip {
  display: table;
  position: absolute;
  left: 100%;
  padding: 10px 15px;
  background: #eee !important;
  color: #333;
  min-width: 100px;
  max-width: 300px;
  border-radius: 3px;
  box-shadow: 1px 1px 10px #000;
}
.tooltip:before {
  position: absolute;
  top: 50%;
  right: 100%;
  margin-top: -11px;
  border-width: 11px 11px 11px 0;
  border-style: solid;
  border-color: transparent #eee transparent;
  content: '';
}
<div id="sidebar">
  
  <div class="teaser">
    <div class="gallery">
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
    </div>
    <h3 class="title">This has only a wrong "padding-top"</h3>
  </div>

  <div class="teaser">
    <div class="gallery">
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
    </div>
    <h3 class="title">Here additionally the clearfix does not work like expected</h3>
  </div>
  
  <div class="teaser">
    <div class="thisIsAWorkaround">
      <div class="gallery">
        <div class="galleryItem"></div>
        <div class="galleryItem"></div>
      </div>
    </div>
    <h3 class="title">This is like it should, but has an additional DIV as workaround =/</h3>
  </div>

  <div class="teaser">
    <div class="tooltip title">This one is like it should (has same paddings to all sides)</div>
    <div class="gallery">
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
    </div>
  </div>
  
</div>

Codepen 相同(但使用 LESS =)

【问题讨论】:

  • 为什么不溢出:隐藏;?我已经使用了很多年了,从来没有遇到过问题。
  • 我在该图库中获得了一些其他元素,例如弹出/弹出/导航箭头,这些元素将被切断。

标签: html css clearfix


【解决方案1】:

在这里-您不需要 clearfix-只需添加例如display:inline-block.gallery 以强制使用新的 block formatting context

块格式化上下文是视觉 CSS 渲染的一部分 网页。是块框布局发生的区域 以及在其中浮动相互交互。

.gallery {
  position: relative;
  margin: 0 -20px -20px 0;
  /* remove margins caused by inner items */
}
/* ##### Please fix this clearfix ##### */

.gallery {
 display:inline-block
}
.gallery .galleryItem {
  margin: 0 20px 20px 0;
  width: 100px;
  height: 100px;
  background: #666;
  float: left;
}
/* ----- do not edit below - this part contains prerequisites ----- */

.teaser {
  position: relative;
  margin: 0;
  padding: 20px;
  background: #444;
}
.teaser .title {
  margin: 10px 0 0 0;
  font-weight: normal;
  font-size: 14px;
  background: #555;
}
.teaser + .teaser {
  margin-top: 20px;
}
.teaser:nth-child(1) .galleryItem {
  background: #cc6600;
}
.teaser:nth-child(2) .galleryItem {
  background: #cc0000;
}
.teaser:nth-child(3) .galleryItem {
  background: #00cc00;
}
.teaser:nth-child(4) .galleryItem {
  background: #00cc00;
}
body {
  background: #333;
  color: #eee;
  font-family: Helvetica, sans-serif;
}
#sidebar {
  padding: 10px;
  width: 380px;
}
.tooltip {
  display: table;
  position: absolute;
  left: 100%;
  padding: 10px 15px;
  background: #eee !important;
  color: #333;
  min-width: 100px;
  max-width: 300px;
  border-radius: 3px;
  box-shadow: 1px 1px 10px #000;
}
.tooltip:before {
  position: absolute;
  top: 50%;
  right: 100%;
  margin-top: -11px;
  border-width: 11px 11px 11px 0;
  border-style: solid;
  border-color: transparent #eee transparent;
  content: '';
}
<div id="sidebar">

  <div class="teaser">
    <div class="gallery">
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
    </div>
    <h3 class="title">This has only a wrong "padding-top"</h3>
  </div>

  <div class="teaser">
    <div class="gallery">
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
    </div>
    <h3 class="title">Here additionally the clearfix does not work like expected</h3>
  </div>

  <div class="teaser">
    <div class="thisIsAWorkaround">
      <div class="gallery">
        <div class="galleryItem"></div>
        <div class="galleryItem"></div>
      </div>
    </div>
    <h3 class="title">This is like it should, but has an additional DIV as workaround =/</h3>
  </div>

  <div class="teaser">
    <div class="tooltip title">This one is like it should (has same paddings to all sides)</div>
    <div class="gallery">
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
      <div class="galleryItem"></div>
    </div>
  </div>

</div>

【讨论】:

  • 正如我在问题中所说,溢出:隐藏不是解决方案。我在那个画廊中有一些弹出/弹出窗口将被切断
  • @Möhre - 请参阅上面的修订版,如链接文章中所述,设置 BFC 的方法不止一种。
  • @SW40 - 我测试了上述显示模式。 display:table 似乎是迄今为止唯一没有任何副作用的。 (display:inline-block 创建额外的微小边距)。此外,我必须添加width:100%,因为表是“非贪婪的”并且box-sizing:border-box 不会破坏现有的填充。仍然不像 block,但如果不支持 flex-box,这可能是最好的解决方案 - 谢谢!
  • @SW40 你介意改变你对表格的回答吗(因为问题比内联块少)?我认为使用那个会“更好”。
猜你喜欢
  • 2014-08-01
  • 2012-12-15
  • 2011-08-10
  • 2013-02-22
  • 2014-01-14
  • 2015-03-01
  • 1970-01-01
  • 2015-10-24
  • 2012-06-21
相关资源
最近更新 更多