在 < h1 > 标记后删除换行符?

我在删除 <h1>标签后面的换行符时遇到了一个问题,因为每次它打印时,它都会在后面直接添加一个换行符,所以像 <h1>Hello World!</h1> <h2>Hello Again World!</h2>这样的东西打印出来是这样的:

Hello World!


Hello Again World!

我不确定我需要在 CSS 中更改哪些标记,但我希望它与填充或边距有关

我也想保持垂直填充,如果可能的话。

164618 次浏览

<h1> tags have {display: block} set. They are block-level elements. To turn this off:

{display: inline}

Sounds like you want to format them as inline. By default, h1 and h2 are block-level elements which span the entire width of the line. You can change them to inline with css like this:

h1, h2 {
display: inline;
}

Here's an article that explains the difference between block and inline in more detail: http://www.webdesignfromscratch.com/html-css/css-block-and-inline/

To maintain vertical padding, use inline-block, like this:

h1, h2 {
display: inline-block;
}

I just solved this problem by setting h1 margin value to minus in html style section. It works perfecly for my needs.

<style>
h1 {
display: block;
font-size: 0.9em;
margin-top: -1.91em;
margin-bottom: -1.91em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
</style>
<h1 style="text-align:center"> Headline </h1>

<style>
h1 {
padding: 0px;
margin: 0px;
}
</style>