标题宽度(H1、H2 等)

发布于 2024-07-29 03:35:04 字数 581 浏览 2 评论 0原文

我希望我的标题(h1 到 h6)具有背景颜色(与页面背景不同),我还希望该背景的宽度与标题中文本的宽度相同,加上填充(不是标头所在容器的宽度,这就是现在正在发生的情况)。

我没有太多意义向您展示任何代码,因为这相当简单(或者无论如何都应该如此!)。 容器的宽度是固定的。 现在我只有一些为 h1、h2、h3 等标签设置的边距、填充、背景颜色和字体样式。

编辑:我想代码会有帮助! https://web.archive.org/web/20090724165158/http ://adriantrimble.com/headers (这应用了 Gerald 的解决方案,尽管显然这在 IE6/7 中仍然不起作用,并且在较新的浏览器中仍然存在问题)。 使用 display:inline 导致的问题比它解决的问题还要多,使用 float: left 和clear: left 如上所述会因为 2 列布局而出现问题。 感谢迄今为止大家的帮助。

I want my headers (h1 through h6) to have a background colour (different to that of the page background), I also want the width of this background to be the same as the width of the text in the header, plus padding (Not the width of the container that the header is in, which is what is happening now).

There isn't much point in me showing you any code as this is fairly simple (or it should be anyway!). The container is a fixed width. Right now I have just some margins, padding, background colour and font styling set for the h1, h2, h3 etc tags.

EDIT: I guess code would help! https://web.archive.org/web/20090724165158/http://adriantrimble.com/headers (this has Gerald's solution applied, although obviously this would still not work in IE6/7 and still has problems in newer browsers). Using display:inline causes more problems than it solves, using float: left and clear: left as mentioned has problems because of the 2 column layout. Thanks for everyones help so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧时光的容颜 2024-08-05 03:35:04

h1-h6 是块级元素,即使它们不在容器(或只是主体)中,它们也会拉伸到窗口的宽度。 一种解决方案是将它们的显示更改为内联块:

<h1 style="background-color:pink; display:inline-block; padding:1em;">Testheader</h1>

请注意,如果下一个元素不是像 p 这样的块级元素,则必须在标题后进行手动中断。

h1-h6 are block level elements and even if they are not in a container (or just body) they'll stretch over the width of the window. One solution is to change their display to be inline-block instead:

<h1 style="background-color:pink; display:inline-block; padding:1em;">Testheader</h1>

Note that you'd have to make a manual break after the header if the next element isn't a block-level element like p.

月竹挽风 2024-08-05 03:35:04

我会

#rightcol h1, #rightcol h2, #rightcol h3, #rightcol h4, #rightcol h6, #rightcol h6 {
   float:left;
   clear:left;
}
#rightcol p {clear:left;} 

在评论后使用编辑

如果包含的div浮动,则清除不会清除左列。 因此将 rightcol 向左浮动并删除边距

#rightcol {
   float:left;
   padding:70px 20px 20px 0px;
   width:585px;
}

I would use

#rightcol h1, #rightcol h2, #rightcol h3, #rightcol h4, #rightcol h6, #rightcol h6 {
   float:left;
   clear:left;
}
#rightcol p {clear:left;} 

edit after comment

If the containing div is floated, the clear won't clear the left col. So float rightcol left and remove the margin

#rightcol {
   float:left;
   padding:70px 20px 20px 0px;
   width:585px;
}
为人所爱 2024-08-05 03:35:04

标头标签 (h1-h6) 是块级元素,因此填充其所在容器的宽度。将其更改为

display:inline 

将使背景恰好与文本宽度相同。 但这会引入其他问题(因为内联元素在许多其他方面的行为与块元素不同)。

另一种方法是设置标题本身的宽度。 如果你想变得非常棘手,你可以使用 javascript 来做到这一点,以便它专门为每个标头计算它。

您必须尝试一下并找出适合您情况的方法。

A header tag (h1-h6) is a block-level element and thus fills the width of the container it's in. Changing it to

display:inline 

will bring the background in to just the width of the text. This will introduce other issues though (since inline elements don't behave like block elements in lots of other ways).

The alternative is to set a width for the header itself. If you wanted to be really tricky you could do that with javascript so that it calculates it for each header specifically.

You'll have to play around a bit and figure out what works in your situation.

那片花海 2024-08-05 03:35:04

正如其他人提到的 display:inline-block; 将是一个选项,但不适用于所有浏览器。 我现在能想到的是给标题一个 float:left; 和后面的元素 clear:left;

另一种方式 - 不那么干净 - 是添加跨越标题文本,并为其指定背景颜色:

<h1><span>your text</span></h1>

h1 span { background-color:#369; }

as others mentioned display:inline-block; would be an option, but does not work in all broswers. what i could think of right now is giving the headers a float:left; and the element that follows a clear:left;

another way - not as clean - is to add spans around your heading's text, and give that spans a background color:

<h1><span>your text</span></h1>

h1 span { background-color:#369; }
许一世地老天荒 2024-08-05 03:35:04

如果您希望您的 hx h1h2h3 等与中的文本相同它,你可以尝试

h1 {
    display:table;
}

它不会像 display: inline-blockfloat: left 那样破坏边距折叠。

请参阅片段:

#page {
  background-color: #ededed;
  height: 300px;
}
h1 {
  display: table;
  background-color: #10c459;
  padding: 10px;
  font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
  color: #fff;
  font-size: 24px;
  font-weight: 700;
}
<div id="page">
  <h1>Hello world</h1>
</div>

If you want your hx h1, h2, h3, etc. to have the same with as the text in it, you could try

h1 {
    display:table;
}

It doesn't break margin collapse like display: inline-block or float: left.

See snippet :

#page {
  background-color: #ededed;
  height: 300px;
}
h1 {
  display: table;
  background-color: #10c459;
  padding: 10px;
  font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
  color: #fff;
  font-size: 24px;
  font-weight: 700;
}
<div id="page">
  <h1>Hello world</h1>
</div>

丶视觉 2024-08-05 03:35:04

我的解决方案是在 h1 内放置一个跨度并设置跨度的样式。

My solution was to put a span inside the h1 and style the span instead.

方圜几里 2024-08-05 03:35:04

另一种向标题元素添加背景颜色的方法是使用 CSS 首行选择器,标题元素的宽度仅与其文本一样宽(不使用额外的元素或显示属性);

例子:

h1:first-line { 
 background-color: yellow;
}

Another way to add a background colour to a header element which only is as wide as its text (without using extra elements or display properties) is to use the CSS first-line selector;

Example:

h1:first-line { 
 background-color: yellow;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文