标题宽度(H1、H2 等)
我希望我的标题(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
h1-h6 是块级元素,即使它们不在容器(或只是主体)中,它们也会拉伸到窗口的宽度。 一种解决方案是将它们的显示更改为内联块:
请注意,如果下一个元素不是像 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:
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.
我会
在评论后使用编辑
如果包含的div浮动,则清除不会清除左列。 因此将 rightcol 向左浮动并删除边距
I would use
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
标头标签 (h1-h6) 是块级元素,因此填充其所在容器的宽度。将其更改为
将使背景恰好与文本宽度相同。 但这会引入其他问题(因为内联元素在许多其他方面的行为与块元素不同)。
另一种方法是设置标题本身的宽度。 如果你想变得非常棘手,你可以使用 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
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.
正如其他人提到的
display:inline-block;
将是一个选项,但不适用于所有浏览器。 我现在能想到的是给标题一个float:left;
和后面的元素clear:left;
另一种方式 - 不那么干净 - 是添加跨越标题文本,并为其指定背景颜色:
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 afloat:left;
and the element that follows aclear:left;
another way - not as clean - is to add spans around your heading's text, and give that spans a background color:
如果您希望您的
hx
h1
、h2
、h3
等与中的文本相同它,你可以尝试它不会像
display: inline-block
或float: left
那样破坏边距折叠。请参阅片段:
If you want your
hx
h1
,h2
,h3
, etc. to have the same with as the text in it, you could tryIt doesn't break margin collapse like
display: inline-block
orfloat: left
.See snippet :
我的解决方案是在 h1 内放置一个跨度并设置跨度的样式。
My solution was to put a span inside the h1 and style the span instead.
另一种向标题元素添加背景颜色的方法是使用 CSS 首行选择器,标题元素的宽度仅与其文本一样宽(不使用额外的元素或显示属性);
例子:
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: