简体   繁体   English

CSS 多列布局边距问题

[英]CSS Multi Column Layout Margin Issue

I have a css based 2 column layout...我有一个基于 css 的 2 列布局...

.mainContentSection {
  font-size: 1.1em;
  margin: 20px 10px 10px;
  padding: 0;
  -moz-column-count: 2; /* Firefox */
  -webkit-column-count: 2; /* Safari and Chrome */
  column-count: 2;
  -moz-column-gap:30px; /* Firefox */
  -webkit-column-gap:30px; /* Safari and Chrome */
  column-gap:30px;
}

.mainContentSection p {
  margin: 0 0 20px 0;
  padding: 0;
  border: 1px solid gray;
}

Occasionally the top of the second column catches the margin from the bottom of the previous paragraph.有时,第二列的顶部会捕获上一段底部的边距。 This pushes down the top of the next paragraph as seen in the attached pic.如附图所示,这会推动下一段的顶部。 I have tried break-inside, changing margins, inline-block.我尝试过闯入,改变边距,内联块。 All have had some success, but not optimal.所有人都取得了一些成功,但不是最佳的。 Can I access the second column to remove that margin?我可以访问第二列以删除该边距吗?

在此处输入图像描述

I think I'm a bit late to the party but as there is no accepted answer here is mine.我想我参加聚会有点晚了,但因为这里没有公认的答案是我的。

Before going to the point and despite it shouldn't be affecting to your problem is very much recommended to always apply both column-width and column-count values to get better responsive results.在开始之前,尽管它不应该影响您的问题,但强烈建议始终同时应用列宽和列数值以获得更好的响应结果。

columns: [minimum width] [column count]

Once that said, just add that your <p> margins (margin: 0 0 20px 0;) are totally OK, that's the way to avoid any troubles.一旦说完,只需添加您的<p>边距 (margin: 0 0 20px 0;) 完全没问题,这是避免任何麻烦的方法。 You just have to add break-inside: avoid;你只需要添加break-inside: avoid; to them and you see the magic happen.对他们来说,你会看到魔法发生了。

Three thinks to take into account:三思要考虑:

  1. Break-inside applies to the content elements, not to the parent container Break-inside适用于内容元素,而不适用于父容器
  2. Break-inside browser compatibility relays on different formats, not only prefixes内部浏览器兼容性中继不同的格式,而不仅仅是前缀
  3. Break-inside has issues when changing its value dynamically (eg from the browser's inspector panel) Break-inside在动态更改其值时存在问题(例如,从浏览器的检查器面板)

Maybe those three tips were the reason why your attempts failed achieving your goal.也许这三个技巧是您的尝试未能实现目标的原因。 So the code you need is something like this:所以你需要的代码是这样的:

.mainContentSection p {
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  break-inside: avoid;
  margin: 0 0 20px 0;
  padding: 0;
  border: 1px solid gray;
}

If you want to see a working example you can do so in this pen: https://codepen.io/elcssar/pen/dypGaWy如果你想看到一个工作示例,你可以在这支笔中这样做: https : //codepen.io/elcssar/pen/dypGaWy

Simply don't use top margins on the .mainContentSection element, so the margin line would read as: margin: 0 10px 20px 10px;简单地不要在.mainContentSection元素上使用顶部边距,因此margin线将读取为: margin: 0 10px 20px 10px;

.mainContentSection {
  font-size: 1.1em;
  margin: 0 10px 20px 10px;
  padding: 0;
  -moz-column-count: 2; /* Firefox */
  -webkit-column-count: 2; /* Safari and Chrome */
  column-count: 2;
  -moz-column-gap:30px; /* Firefox */
  -webkit-column-gap:30px; /* Safari and Chrome */
  column-gap:30px;
}

I had the same issue with Safari.我在 Safari 上遇到了同样的问题。 Adding display: inline-block on the columns items solved the problem for me.在列项目上添加display: inline-block为我解决了这个问题。

The root of the issue appears to be when using column-count browsers treat margin and padding differently depending on whether there are an even or odd number of elements.问题的根源似乎是在使用column-count浏览器时,根据元素的数量是偶数还是奇数,以不同的方式处理marginpadding

This means solutions like display: inline-block; width: 100%这意味着像display: inline-block; width: 100%这样的解决方案display: inline-block; width: 100% display: inline-block; width: 100% ; display: inline-block; width: 100% or playing with margin or padding create inconsistent results.或者使用marginpadding产生不一致的结果。

Solution解决方案

The only way I found to make this consistently work across browsers is to add a parent-element and a spacer .我发现要使其跨浏览器始终如一地工作的唯一方法是添加一个parent-element和一个spacer Then use break-inside: avoid to keep the two elements together.然后使用break-inside: avoid将两个元素保持在一起。

HTML HTML

<div class="mainContentSection">
  <div class="parent-element">    <!-- added this element -->
    <p>Blah blah blah blah</p>
    <div class="spacer"></div>     <!-- added this element -->
  </div>
  <div class="parent-element">   
    <p>Blah2 blah2 blah2 blah2</p>
    <div class="spacer"></div>   
  </div>

  <!-- addition items... -->

</div>

CSS CSS

.mainContentSection{
  column-count:2
}
.parent-element{
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  break-inside: avoid;
}
.spacer{
  height: 10px;
}

You can add padding to mainContentSection or add another spacer above the paragraph depending where you need space.您可以向mainContentSection添加padding或在段落上方添加另一个spacer ,具体取决于您需要空间的位置。

Problem问题

The inconsistent alignment appears to be is driven by margin and padding being handled differently for an even or odd number of elements.不一致的对齐似乎是由marginpadding对偶数或奇数元素的不同处理所驱动的。 I found this to be true for -top and -bottom but to keep things simple I only use margin-bottom in the example.我发现这对于-top-bottom是正确的,但为了简单-bottom ,我只在示例中使用了margin-bottom

Even Number of Elements (works fine)偶数元素(工作正常)

When there's an even number of elements margin-bottom works.当元素数量为偶数时, margin-bottom起作用。

在此处输入图片说明

Odd Number of Elements (does not work)元素的奇数(不起作用)

When there's an odd number of elements margin-bottom gets moved to the second column creating the misalignment.当有奇数个元素时, margin-bottom被移动到第二列,造成错位。

在此处输入图片说明

Other Solutions其他解决方案

This means that just playing with margin or padding leads to inconsistent results across browsers and devices.这意味着仅使用marginpadding会导致浏览器和设备之间的结果不一致。

Using display: inline-block; width:100%使用display: inline-block; width:100% display: inline-block; width:100% almost works but I found for some browser/device variations if there are only two elements it keeps the two elements in one column because there is no break. display: inline-block; width:100%几乎可以工作,但我发现对于某些浏览器/设备变体,如果只有两个元素,它会将两个元素保留在一列中,因为没有中断。 This can be addressed by adding a break as proposed here but as the user mentions it seem to have inconsistent results if element heights vary.这可以通过添加此处建议的中断来解决,但正如用户所提到的,如果元素高度不同,它似乎会产生不一致的结果。

Have you tried avoiding margin?您是否尝试过避免保证金? Set margin-bottom: 0 and introduce a transparent border for the equivalent.设置margin-bottom: 0并为等效项引入透明边框。 Say border-bottom: 20px solid transparent should do the trick?border-bottom: 20px solid transparent应该可以解决问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM