如何在CSS中实现负填充?

Zac*_*ack 6 html css fonts css-position padding

我正在寻找一种有效实现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;
}
Run Code Online (Sandbox Code Playgroud)
<head>
  <link href="https://fonts.googleapis.com/css?family=Righteous|Roboto:400,700,400i" rel="stylesheet">
</head>

<body>
  <h1>Lorem Ipsun Al</h1>
</body>
Run Code Online (Sandbox Code Playgroud)

您可以看到,因为我添加了:height: 49px;,文本看起来好像正在流入页面的其余部分.我希望实现这种外观,但也适用于文本的顶部,而不仅仅是底部.

我曾经尝试过什么

我试过了:

  • 为元素设置both paddingmarginto .0h1
  • 玩各种价值观vertical-align.
  • 设置height为许多不同的值.
  • 设置所有h1的父元素" paddingmargin0.

我相信我遇到的问题是我使用的字体顶部(和大多数字体)有一些空间.这就是为什么我希望能够实现负填充,在屏幕上移动文本而不移动内容框.

Joh*_*nes 5

它完全取决于你使用的字体,但在你的特定情况下 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;
}
Run Code Online (Sandbox Code Playgroud)
<!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>
Run Code Online (Sandbox Code Playgroud)