如何在CSS中实现负填充? [英] How do I achieve negative padding in CSS?
问题描述
我正在寻找一种有效地实现CSS负填充的方法.
I am looking for a way to effectively achieve negative padding in CSS.
我的网页上有一个h1
元素,当前与以下代码关联:
I have an h1
element on my web page that currently has the following code associated with it:
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;
,因此文本看起来好像流入页面的其余部分.我要实现这种外观,但也要在文本的顶部,而不仅仅是底部.
You can see that since I have added: height: 49px;
, the text looks as if it is flowing into the rest of the page. I want to achieve this look, but also for the top of the text, not just the bottom.
我尝试过:
- 将
h1
元素的padding
和margin
都设置为0
. - 播放
vertical-align
的各种值. - 将
height
设置为许多不同的值. - 将所有
h1
的父元素的padding
和margin
设置为0
.
- Setting both
padding
andmargin
to0
for theh1
element. - Playing with various values for
vertical-align
. - Setting
height
to many different values. - Setting all the
h1
's parent elements'padding
andmargin
to0
.
我认为我面临的问题是我正在使用的字体(和大多数字体)的顶部有一定的空间.这就是为什么我希望能够实现负填充,在屏幕上向上移动文本而不移动内容框的原因.
推荐答案
它完全取决于您使用的字体,但是在特定情况下height: 34px
和line-height: 34px;
会执行您想要的操作:
It completly depends on the font you use, but in your particular case height: 34px
and line-height: 34px;
do what you want:
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>
这篇关于如何在CSS中实现负填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!