【问题标题】:How to avoid blank lines at the top of new column (CSS columns)?如何避免新列(CSS 列)顶部的空行?
【发布时间】:2016-09-15 14:19:21
【问题描述】:

我的问题在于 CSS 列属性。我记得有一种方法可以防止空白 (<br>) 行最终成为右列的第一行,但我似乎无法弄清楚。

创建列效果的 CSS:

@media (min-width: 1100px) {
    .inner_p, .introtext p {
        column-count: 2;
        break-after: auto;
        column-gap: 3em;
    }
}

HTML:

<p>With an article, and a couple <br><br>s inside. These <br>s are IMO what sometimes shows up as first element in the right column, causing the following appearance:</p>

xxxxxx

xxxxxxx xxxxxx
xxxxxxx xxxxxx
xxxxxxx xxxxxx

这看起来有点乱!

我想要实现的是:

xxxxxxxxxxxx
xxxxxxx xxxxxx
xxxxxxx xxxxxx
xxxxxxx

我已经接受了 Ian M 的回答,因为它确实让我走上了正轨,但是,还有一个小问题:

当第一个孩子 p 在左列的底部结束时,一切都很好。但是当它延伸到右列时,它会(正确地)在列的顶部继续,但是(这是小故障)由于它的 margin-bottom:0 (以防止在第一行的情况下出现空白的第一行p 结束于左 col 的底部),它现在没有到下面的第二个 p 的边距。

视觉上:

第一个 p : aaa 第二个p:bbb

aaaaa bbbbb
aaaaa bbbbb
啊啊啊……

但是

啊啊啊啊啊
aaaaa bbbbb
aaaaa bbbbb
a 和 b 之间没有边距!

一个难题。

【问题讨论】:

  • 请贴出相关的html/css
  • 完成,附上小插图,希望对您有所帮助!

标签: css layout responsive-design css-multicolumn-layout


【解决方案1】:

我认为实现此目的的更好方法是重构您的代码,以便 每个段落 都是它自己的&lt;p&gt;。我建议将它们全部放入具有列和中断 CSS 的父 &lt;div&gt;

然后,您所要做的就是将 第一个 段设为零margin-top,使其与第二列齐平。这就是我的意思:

body { width: 90vw; margin: 2vw; }
.parent {
    column-count: 2;
    break-after: auto;
    column-gap: 3em;
	
    /* for testing */ 
	padding: 15px;
	border: 1px solid red;
}
.parent > p {
    /* for testing */ 
	border: 1px solid blue;
}
.parent > p:first-child {
	margin-top: 0;
}
<div class='parent'>
	<p>A look at Mrs. Clinton’s speaking venues and the whopping sums she’s received since she left State gives us an indication who’s desperate for a place at the trough — and whom another Clinton administration might favor.</p>
	<p>First off, there’s Wall Street and the financial-services industry. Democratic champions of the Little Guy are always in bed with the Street — they don’t call Barack Obama “President Goldman Sachs” for nothing, but Mrs. Clinton has room for Bob and Carol and Ted and Alice and their 10 best friends. Multiple trips to Goldman Sachs. Morgan Stanley. Deutsche Bank. Kohlberg Kravis Roberts. UBS Wealth Management.</p>
	<p>As the character of Che Guevara sings in “Evita”: “And the money kept rolling in.” And all at the bargain price of $225,000 a pop . . . to say what? We don’t know, because Hillary won’t release the transcripts.</p>
</div>

我添加了边框,以便您可以看到发生了什么。希望这会有所帮助,祝你好运!

【讨论】:

  • 哈哈,我知道你在那里做了什么!很好的答案,完全可以实施。现在将尝试。谢谢你的时间!!!
  • 哦,我不得不将第一个子段落更改为 margin-bottom:0 并将所有其他 p 更改为 margin-top:0 以避免这些不必要的中断,但它现在有效,你肯定让我正确的轨道,-> 接受的答案
猜你喜欢
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多