如果我有 display: inline,为什么我的背景颜色不显示?

发布于 2024-12-05 04:06:54 字数 330 浏览 0 评论 0原文

<html>
    <body>
       <div style="display: inline; background-color: #555;">
            <h3>test</h3>
       </div>
    </body>
</html>

这是我的代码。我想知道为什么我的背景颜色没有显示。如果我将 css 显示从内联更改为块,那么它就会显示出来。如果内联显示,为什么不显示? 我正在尝试了解问题的原因,而不是寻找解决方案。

<html>
    <body>
       <div style="display: inline; background-color: #555;">
            <h3>test</h3>
       </div>
    </body>
</html>

Here is my code. I am wondering why my background color isn't showing. If I change css display from inline to block, then it show up. Why is it not showing up if display is inline? I am trying to understand the reason of the problem other than looking for a solution.

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

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

发布评论

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

评论(6

差↓一点笑了 2024-12-12 04:06:54

如果 div 是内联的,则不会占用空间。如果您想要一个显示为子元素高度的内联元素,请使用 display: inline-block;

至于好的讨论,我相信 QuirksMode 的观点比我自己的观点更好。要点是内联元素不会将其他元素推开。

The div doesn't take up space if it's inline. if you want an inline element that shows as the children's height, then use display: inline-block;.

As for a good discussion, I'd trust QuirksMode's take better than my own. The gist is that an inline element doesn't push other elements out of the way.

笑看君怀她人 2024-12-12 04:06:54

问题是您在内联元素内部有一个H3,一个阻塞元素

您可以通过以下方式查看发生了什么:

h3
{
    background-color: inherit;   
}

或使 H3 内联:

h3
{
 display: inline;   
}

The issue is you have an H3, a blocking element, inside of the inline element.

You can see what's happening with:

h3
{
    background-color: inherit;   
}

or make H3 inline:

h3
{
 display: inline;   
}
七颜 2024-12-12 04:06:54

div 默认是一个块元素。将块元素的显示模型更改为内联并不是一个好的做法。标题也是块元素。将块元素嵌套到内联元素中不是有效的 html。如果您想要类似突出显示的效果(仅为文本而不是整个元素框提供背景颜色),您需要使用像 span 这样的内联元素。

<html>
    <body>
       <div>
            <h3><span style="background-color: #555;">test</span></h3>
       </div>
    </body>
</html>

div is a block element by default. Changing display model of a block element to inline is not a good practice. headings are block elements too. Nesting a block element into a inline element is not valid html. If you want a highlighting like effect (giving background color just to text not whole element box) you need to use an inline element like an span.

<html>
    <body>
       <div>
            <h3><span style="background-color: #555;">test</span></h3>
       </div>
    </body>
</html>
嘴硬脾气大 2024-12-12 04:06:54

简单的解决方案,在某些情况下最好是向包含标题的内联 div 添加一些填充

<div style="display: inline; background-color: #555; padding:5px;">
<h3>test</h3>
</div>

simple solution, best in some cases is to add some padding to the inline div containing your heading

<div style="display: inline; background-color: #555; padding:5px;">
<h3>test</h3>
</div>
半仙 2024-12-12 04:06:54

CSS2.1 的最新修订版有这样的说法关于此事:

当行内框包含流入块级框时,行内框
(及其在同一行框中的内联祖先)被打破
块级框(以及任何连续的块级同级框)
或仅由可折叠空白和/或外流分隔
元素),将内联框分成两个框(即使其中一个
边是空的),块级框的每一侧都有一个。线路
中断之前和中断之后的框都用匿名括起来
块框,并且块级框成为这些框的兄弟
匿名盒子。当这样的行内框受到relative的影响时
定位,任何产生的翻译也会影响块级
内联框中包含的框。

也就是说,从布局的角度来看,内联div和h3组合形成了一个内联框、一个块框和另一个内联框。只有两个内联框采用格式(即背景颜色),而块框不形成 div 定义的内联框的任何部分,因此采用其默认背景颜色设置(透明,通过背景显示)其包含块框的颜色)。

The latest revision of CSS2.1 has this to say on the matter:

When an inline box contains an in-flow block-level box, the inline box
(and its inline ancestors within the same line box) are broken around
the block-level box (and any block-level siblings that are consecutive
or separated only by collapsible whitespace and/or out-of-flow
elements), splitting the inline box into two boxes (even if either
side is empty), one on each side of the block-level box(es). The line
boxes before the break and after the break are enclosed in anonymous
block boxes, and the block-level box becomes a sibling of those
anonymous boxes. When such an inline box is affected by relative
positioning, any resulting translation also affects the block-level
box contained in the inline box.

In other words, from a layout point of view, the inlined div and h3 combination forms an inline box, a block box and another inline box. Only the two inline boxes take the formatting (i.e. the background-color) and the block box does not form any part of the inline box defined by the div and so takes its default background-color setting (which is transparent, showing through the background color of its containing block box).

魂ガ小子 2024-12-12 04:06:54

如果您尝试创建荧光笔效果,请改用以下内容:

<h3><span style="background-color: #555;">test</span></h3>

If you are trying to create a highlighter effect use the below instead:

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