删除h1和h2之间的空间

6 html css html-heading

我偶然发现了一个似乎无法以任何方式解决的问题,也许我div是以错误的方式使用s?

.greeting h1 {
  font-family: 'Raleway', sans-serif;
  font-weight: lighter;
  font-size: 100px;
  text-align: center
}
.greeting h2 {
  font-family: 'Raleway', sans-serif;
  font-weight: lighter;
  font-size: 35px;
  line-height: 0px;
  text-align: center
}
Run Code Online (Sandbox Code Playgroud)
<div class="greeting">
  <h1>Hi.</h1>
  <h2>Select a group</h2>
</div>
Run Code Online (Sandbox Code Playgroud)

这是结果:

SS

我想我的降低之间的空间<h1>和<h2>,和我发现的方式做到这一点是要设置line-height在h1来0px.

但是当我这样做时,我的整个页面会像这样向上移动:

SS

我希望将文本保持在与更改之前相同的位置line-height.我怀疑我使用div类函数错误.这更多是理论问题.

dip*_*pas 11

标题h1要h6具有margin在默认情况下,所以你需要重置,设置:margin:0.

.greeting h1 {
  font-family: 'Raleway', sans-serif;
  font-weight: lighter;
  font-size: 100px;
  text-align: center;
  margin: 0
}
.greeting h2 {
  font-family: 'Raleway', sans-serif;
  font-weight: lighter;
  font-size: 35px;
  text-align: center;
  margin: 0
}
Run Code Online (Sandbox Code Playgroud)
<div class="greeting">
  <h1>Hi.</h1>
  <h2>Select a group</h2>
</div>
Run Code Online (Sandbox Code Playgroud)


Muh*_*mad 5

HTML 标题标签在大多数浏览器中都有一些默认的 CSS 值。以下是的值h1和h2默认被应用到它们,所以你需要重写margin-bottom的h1和margin-top的h2,如果你想降低您之间的间距h1和h2。

h1 { 
  display: block;
  font-size: 2em;
  margin-top: 0.67em;
  margin-bottom: 0.67em;
  margin-left: 0;
  margin-right: 0;
  font-weight: bold;
}

h2 {
  display: block;
  font-size: 1.5em;
  margin-top: 0.83em;
  margin-bottom: 0.83em;
  margin-left: 0;
  margin-right: 0;
  font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)

h1 { 
  display: block;
  font-size: 2em;
  margin-top: 0.67em;
  margin-bottom: 0.67em;
  margin-left: 0;
  margin-right: 0;
  font-weight: bold;
}

h2 {
  display: block;
  font-size: 1.5em;
  margin-top: 0.83em;
  margin-bottom: 0.83em;
  margin-left: 0;
  margin-right: 0;
  font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)
.greeting h1 {
  font-family: 'Raleway', sans-serif;
  font-weight: lighter;
  font-size: 100px;
  text-align: center;
  margin-bottom: 0;
}
.greeting h2 {
  font-family: 'Raleway', sans-serif;
  font-weight: lighter;
  font-size: 35px;
  line-height: 0px;
  text-align: center;
  margin-top: 0;
}
Run Code Online (Sandbox Code Playgroud)