[英]CSS: remove spacing before and after display:block nested in <pre>
I want to display a consecutive series of <pre>
elements without any blank space between them (like consecutive lines of a same paragraph).我想显示一系列连续的
<pre>
元素,它们之间没有任何空格(如同一段落的连续行)。
I can't achieve that while having display: block
在
display: block
Here is a sample code这是一个示例代码
body { max-width: 940px; margin: auto; } pre { display:block; white-space: pre-wrap; margin-bottom:0; margin-top:0; padding: 0 }
<p>One-line paragraph.</p> <pre>One-line pre element, without space before or after.</pre> <pre>Another pre line.</pre> <p>Some paragraph.</p> <:-- EDIT: the actual problematic code, nested pre --> <pre> <pre>One-line pre element. without space before or after.</pre> <pre>Another pre line.</pre> </pre>
I wish the output would simply have no large interline between each line我希望 output 每条线之间没有大的中间线
NOTE: using display:inline
works, but I need to use block because I want the pre
lines to be larger than body
.注意:使用
display:inline
有效,但我需要使用 block 因为我希望pre
行大于body
。
EDIT : I realise this code snippets displays correctly in Stackoverflow, which is not the case of my larger document:编辑:我意识到这个代码片段在 Stackoverflow 中正确显示,这不是我的大文档的情况:
Explanation found (see updated code): pre lines were already nested in pre element.找到解释(参见更新的代码):pre 行已经嵌套在 pre 元素中。 In this case, the CSS solution is to use
display: inline-block
在这种情况下,CSS 解决方案是使用
display: inline-block
You can try to achieve this with the float: left
CSS property:您可以尝试使用
float: left
CSS 属性:
pre:not(.parent) { display: block; white-space: pre-wrap; margin-bottom: 0; margin-top: 0; padding: 0; }.parent pre { display: block; white-space: pre-wrap; margin-bottom: 0; margin-top: 0; padding: 0; float: left; /* new line */ }
<p>One-line paragraph.</p> <pre>One-line pre element, without space before or after.</pre> <pre>Another pre line.</pre> <p>Some paragraph.</p> <:-- EDIT: the actual problematic code, nested pre --> <pre class="parent"> <pre>One-line pre element. without space before or after.</pre> <pre>Another pre line.</pre> </pre>
Just remove the margin of the paragraph tag.只需删除段落标签的边距。
body { max-width: 940px; margin: auto; } pre { display: block; white-space: pre-wrap; margin: 0; /* padding: 0; */ /* does nothing */ } p { margin: 0; }
<p>one-line paragraph</p> <pre>one-line pre element, without space before or after</pre> <pre>another pre line</pre> <p>some paragraph</p>
According to W3 , this is the default styling for paragraph elements: margin: 1.12em 0
根据W3 ,这是段落元素的默认样式:
margin: 1.12em 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.