我正在寻找一种有效地在CSS中实现负填充的方法。
我的h1
网页上有一个元素,当前与以下代码关联:
h1 {
background-color: #375E97;
color: #FFFFFF;
font-size: 50px;
font-family: 'Righteous', cursive;
text-transform: uppercase;
text-align: center;
padding: 0;
margin: 0 auto;
height: 49px;
}
<head>
<link href="https://fonts.googleapis.com/css?family=Righteous|Roboto:400,700,400i" rel="stylesheet">
</head>
<body>
<h1>Lorem Ipsun Al</h1>
</body>
您可以看到,由于我添加了:height: 49px;
,因此文本看起来好像流入了页面的其余部分。我想实现这种外观,但也要在文本的顶部,而不仅仅是底部。
我努力了:
padding
并margin
以0
该h1
元素。vertical-align
。height
为许多不同的值。h1
的父元素“padding
和margin
到0
。我认为我面临的问题是我正在使用的字体(和大多数字体)的顶部有一些空间。这就是为什么我希望能够实现负填充,在屏幕上向上移动文本而不移动内容框的原因。
它完全取决于您使用的字体,但是在您的特定情况下height: 34px
,可以line-height: 34px;
执行您想要的操作:
h1 {
background-color: #375E97;
color: #FFFFFF;
font-size: 50px;
font-family: 'Righteous', cursive;
text-transform: uppercase;
text-align: center;
padding: 0px;
margin: 0 auto;
height: 34px;
line-height: 34px;
}
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Righteous|Roboto:400,700,400i" rel="stylesheet">
</head>
<body>
<h1>Lorem Ipsun Al</h1>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句