简体   繁体   English

CSS:删除显示前后的间距:块嵌套在<pre></pre>

[英]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: blockdisplay: 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

CSS:after and:before 为创建间距<div id="text_translate"><p>因此,我正在尝试通过向我的 CSS 添加其他属性来自定义一些 HTML 标签,这将创建间距:before和:after下一个<html>内容。</p><p> 我不能用<table>成功,我想要一个表在它之前和之后产生空间。 请在下面查看我的图片,了解基于我的 CSS 的结果。</p><pre> table:before { content: '\a'; white-space: pre; } table, th, td:after { padding-top: 1em; } h1, h2, h3, h4, h5, h6:after { content: '\a'; white-space: pre; } p:after { content: '\a'; white-space: pre; } p:before { content: '\a'; white-space: pre; }</pre><p> 我如何修复或调整我的 CSS 表格以实际产生空间(用于表格的顶部和底部)</p><hr><p> <a href="https://i.stack.imgur.com/QzJyJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QzJyJ.png" alt="在此处输入图像描述"></a></p><p> 正如你在这张图片中看到的,我的 CSS 通过在后面添加一个换行符将表格 header 分成两半,并且之前没有空格。</p><p> 我怎样才能用我的 CSS 解决这个问题?</p></div>不能正常工作<table> </table> - CSS :after and :before to create spacing for <table> not working properly

暂无
暂无

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

相关问题 浮动间距后的显示块 - Display Block after Float Spacing 显示数学前后的Mathjax怪异间距 - Mathjax weird spacing before and after display math 如何删除CSS中block元素上方的间距? - How can I remove the spacing above a block element in CSS? 使用:after和:before在CSS中嵌套资源 - Nested resources in css with :after and :before css:内容间距之前 - css :before content spacing 无法删除单元格第一列之前和最后一个单元格之后的间距? - Unable to remove spacing before first column of cells and after last cell? 如何在元素的slideUp()之前阻止滚动和主体高度,并在新元素的slideDown()之后移除CSS? -jQuery的 - How block scroll and height of body before slideUp() of element and remove css after slideDown() of new element ? - Jquery CSS:after and:before 为创建间距<div id="text_translate"><p>因此,我正在尝试通过向我的 CSS 添加其他属性来自定义一些 HTML 标签,这将创建间距:before和:after下一个<html>内容。</p><p> 我不能用<table>成功,我想要一个表在它之前和之后产生空间。 请在下面查看我的图片,了解基于我的 CSS 的结果。</p><pre> table:before { content: '\a'; white-space: pre; } table, th, td:after { padding-top: 1em; } h1, h2, h3, h4, h5, h6:after { content: '\a'; white-space: pre; } p:after { content: '\a'; white-space: pre; } p:before { content: '\a'; white-space: pre; }</pre><p> 我如何修复或调整我的 CSS 表格以实际产生空间(用于表格的顶部和底部)</p><hr><p> <a href="https://i.stack.imgur.com/QzJyJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QzJyJ.png" alt="在此处输入图像描述"></a></p><p> 正如你在这张图片中看到的,我的 CSS 通过在后面添加一个换行符将表格 header 分成两半,并且之前没有空格。</p><p> 我怎样才能用我的 CSS 解决这个问题?</p></div>不能正常工作<table> </table> - CSS :after and :before to create spacing for <table> not working properly CSS样式中带有:before&:after伪类的按钮链接,显示为完整块 - Button link with :before & :after pseudo classes styled in CSS to display as a complete block CSS属性删除间距 - CSS Properties to remove spacing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM