简体   繁体   English

H1 标签前后的额外空间

[英]Extra space before and after H1 tag

Even after reseting margin and padding the h1 tag still has mysterious unwanted gaps before and after its content即使在重置边距和填充 h1 标签之后,其内容前后仍有神秘的不需要的间隙

see code pen见码笔

Adding添加

 line-height:0.7em;

rule approximately resolve the problem, however规则大致解决了问题,但是

  1. it looks unclean看起来不干净
  2. there are still few unwanted pixels仍然有一些不需要的像素
  3. not sure that this will not cause issues with different font size or units.不确定这不会导致不同字体大小或单位的问题。

Is there a clean and universal solution, maybe using sass/less mixin with math operations?是否有一个干净和通用的解决方案,也许在数学运算中使用 sass/less mixin?

HTML: HTML:

<h1>Some text</h1> 

CSS CSS

* { margin:0;padding:0; }
h1 { font-size:25em; } 

the h1 tag by default has line-heigth property you can adjust it like默认情况下 h1 标签具有 line-heigth 属性,您可以像这样调整它

h1 {
font-size: 25em;
line-height: 0.7em;
} 

updated example更新示例

 * { margin:0; padding:0; } h1 { font-size:25em; line-height:0.7em; }
 <h1>Some text</h1> <p>lorem ispum</p>

Since the h1 is a block level element, it has whitespace on the sides by default.由于h1是块级元素,因此默认情况下它的两侧有空格。 You can add display: inline to get rid of it, but it's positioning will be affected.您可以添加display: inline来摆脱它,但它的定位会受到影响。

Be sure, that your css rules of margin: 0;请确保,您的 css 边距规则:0; padding: 0;填充:0; is on.正在。 Make it !important just for test.让它成为 !important 仅用于测试。 line-height: 1;行高:1; should fix the problem (or line-height: 0).应该解决问题(或行高:0)。 You can set height for h1 element您可以为 h1 元素设置高度

h1 {
      font-size:25em;
      background-color:pink;
      line-height:0.8;
    }

What you can do is just add line-height您可以做的只是添加行高

 h1.demo { padding: 0 !important; margin: 0 !important; line-height: 40px; font-size: 40px; }
 <h1 class="demo">Some Text</h1>

In my case, the solution I had found was that, I had set a global line-height value which was messing with everything else.就我而言,我找到的解决方案是,我设置了一个全局行高值,该值与其他所有内容都混淆了。 And even apart from that, I realized that just like margin and padding, there's even default line-height values to elements.甚至除此之外,我意识到就像边距和填充一样,元素甚至还有默认的行高值。

So one thing you could do is, set their "line-height: normal" or "line-height: initial" to individual elements or even just globally (latter might yield better results).所以你可以做的一件事是,将它们的“行高:正常”“行高:初始”设置为单个元素,甚至只是全局(后者可能会产生更好的结果)。

If that doesn't work, just manually enter any line-height values that seem to work for you although that's not the most optimal solution.如果这不起作用,只需手动输入似乎适合您的任何行高值,尽管这不是最佳解决方案。 It generally means somewhere you have a line-height setting that's overriding the same for other elements or is just messing with the spacing of other nearby elements, that share a border with it.这通常意味着在某处您有一个行高设置,该设置覆盖了其他元素的相同内容,或者只是与其他附近元素的间距混淆,这些元素与其共享边框。

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

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