Setting the width of inline elements

2019-01-02 15:28发布

You can set the width of inline elements like <span>, <em> and <strong>, but you won’t notice any effect until you position them.

a) I thought the width of inline an inline element can’t be set?

b) Assuming width can be set - we won’t notice any effects ( thus the width we specify ) until we position inline element. Position how/where?

c) Why is the width of inline elements apparent only when we “position” them?

标签: html css xhtml
7条回答
残风、尘缘若梦
2楼-- · 2019-01-02 16:10

That basically means that if you apply position: absolute to inline element, it will become block element and gain width.

查看更多
倾城一夜雪
3楼-- · 2019-01-02 16:15

a) The width of an inline element is ignored

b,c) When you "position" an inline element (I assume that means using position:absolute), you are actually making it a block element, whose width is interpreted by the browser

查看更多
不再属于我。
4楼-- · 2019-01-02 16:24

I think it's due to the fact that when you specify position attributes for an "inline" element, the element is no longer being displayed inline and instead is being treated as a block-level element.

查看更多
皆成旧梦
5楼-- · 2019-01-02 16:24

a. Width of inline elements are ignored.

b. Actually you can apply width to element if set display: inline-block; but to see results you also should apply overflow: hidden;.

To have all benefits of inline and block types you can use follow snippet:

display: inline-block; width: 50%; // or px, em, etc. overflow: hidden; text-overflow: ellipsis;

In this case you can manage width and have text ellipsis feature.

查看更多
步步皆殇っ
6楼-- · 2019-01-02 16:25

As others have mentioned, setting the width (or some other position-related property) of an inline element will cause the browser to then display the element as a block element.

You can explicitly declare this sort of behavior through using the CSS display property. The most common settings are display: inline (default), display: block, and display: none.
A full reference for the display property is available here.

However, it should be noted that the HTML 4.01 specification discourages the use of "overriding the conventional interpretation of HTML elements":

Style sheets provide the means to specify the rendering of arbitrary elements, including whether an element is rendered as block or inline. In some cases, such as an inline style for list elements, this may be appropriate, but generally speaking, authors are discouraged from overriding the conventional interpretation of HTML elements in this way.

查看更多
无与为乐者.
7楼-- · 2019-01-02 16:25

Inline element cannot have width. Width is a property of block element. So to use property of width over an inline element or an element with display type inline set display as inline-block eg: display:inline-block;

查看更多
登录 后发表回答