如何在CSS中实现负填充? [英] How do I achieve negative padding in CSS?

查看:53
本文介绍了如何在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元素的paddingmargin都设置为0.
  • 播放vertical-align的各种值.
  • height设置为许多不同的值.
  • 将所有h1的父元素的paddingmargin设置为0.
  • Setting both padding and margin to 0 for the h1 element.
  • Playing with various values for vertical-align.
  • Setting height to many different values.
  • Setting all the h1's parent elements' padding and margin to 0.

我认为我面临的问题是我正在使用的字体(和大多数字体)的顶部有一定的空间.这就是为什么我希望能够实现负填充,在屏幕上向上移动文本而不移动内容框的原因.

推荐答案

它完全取决于您使用的字体,但是在特定情况下height: 34pxline-height: 34px;会执行您想要的操作:

It completly depends on the font you use, but in your particular case height: 34pxand 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆