which elements are affected by the line-height property?
It applies to all HTML elements, but has different behaviour depending on the element.
From the CSS spec:
All elements have a 'line-height'
property that, in principle, gives the
total height of a line of text.
It affects all of them according to the W3 line-height spec. It's behavior is different depending on the type of element it is applied to:
block-level, table-cell, table-caption or inline-block
[on one of the above] whose content is composed of
inline-level elements, 'line-height'
specifies the minimal height of line
boxes within the element.
inline-level
...specifies the height
that is used in the calculation of the
line box height (except for inline
replaced elements, where the height of
the box is given by the 'height'
property).
Line box height is then defined as:
the distance between the uppermost box top and the lowermost box bottom.
Applies to all elements, with different behaviour of course:
http://www.w3.org/TR/CSS2/visudet.html#line-height
On a block-level, table-cell,
table-caption or inline-block element
whose content is composed of
inline-level elements, 'line-height'
specifies the minimal height of line
boxes within the element. The minimum
height consists of a minimum height
above the block's baseline and a
minimum depth below it, exactly as if
each line box starts with a zero-width
inline box with the block's font and
line height properties (what TEX calls
a "strut").
On an inline-level element,
'line-height' specifies the height
that is used in the calculation of the
line box height (except for inline
replaced elements, where the height of
the box is given by the 'height'
property).
Related
I am wondering why img tag accept margin top property?
isn't this an inline tag? and inline tags does't accept top and bottom margins?
It's because img is an inline replaced element and it does accept margin-top. It behaves differently than inline non-replaced element (like span for example).
Related part of the specification detailing this: https://www.w3.org/TR/CSS21/visudet.html#inline-replaced-height
Note that there is no restriction or special behavior related to margin unlike with non-replaced inline element where you can read:
The vertical padding, border and margin of an inline, non-replaced box start at the top and bottom of the content area, and has nothing to do with the 'line-height'. But only the 'line-height' is used when calculating the height of the line box.
Same logic for width/height. They work with img but not with span.
Another related question dealing with transform where the same logic apply: CSS transform doesn't work on inline elements
This question already has answers here:
Why does an inline element accept width and height when floated?
(2 answers)
Closed 5 years ago.
I was unable set the width for an inline element <span> while creating a table-row with multiple span elements. I was able to modify the width once I put a float: left as well.
Why was I not able to set the width and height of inline elements? How does adding a float change things?
Answer A:
You can't explicitly set the width of an inline element because it is relative to the surrounding elements, and thus is restricted by their widths.
Answer B:
Floating an element with float automatically makes it into a block-level element by applying display: block to it. A block-level element occupies the entire space of its parent element (container), thereby creating a 'block'.
Hope this helps! :)
Inline elements by definition do not take explicit width or height but are inline and conform to the surrounding elements. They only occupy space bounded by the tags. For example:
This is some <span>text</span>
<span>text</span> only occupies the space that houses text. It's in the same line with the other text nodes in the HTML.
The float property implies block layout and the display property of elements is changed to block (in most cases) thus making them block-level elements where you can set explicit width and height because they occupy all of their container. Per the MDN documentation:
As float implies the use of the block layout, it modifies the computed value of the display values, in some cases
In this case, the table (viewable at the MDN documentation) shows that any elements with display: table-row would blockified and have display: block when floated.
Let's say that I want to make a <p> element that has a fixed height and a width that only grows if the height is not sufficient to display all of the text. By default, <p> is a block level element. This means it has a greedy width and lazy height. I want the opposite, lazy dynamic width and fixed/greedy height. An inline-block element tries to display the text in one line if possible, which is not something I want. I want a pure CSS solution just because. Is it possible?
This is not possible.
9.4.2 Inline formatting contexts
In an inline formatting context, boxes are laid out horizontally
[...]. The rectangular area that contains the boxes that form a line
is called a line box.
The width of a line box is determined by a containing block and the
presence of floats. [...]
In general, the left edge of a line box touches the left edge of its
containing block and the right edge touches the right edge of its
containing block. However, floating boxes may come between the
containing block edge and the line box edge. Thus, although line boxes
in the same inline formatting context generally have the same width
(that of the containing block), they may vary in width if available
horizontal space is reduced due to floats. [...]
Line boxes are created as needed to hold inline-level content within
an inline formatting context. [...]
Therefore, the width of the line boxes will only be affected by the width of the containing block and the presence of floats. And then, there will be as many line boxes as necessary.
Look at the following example:
CSS: reducing line spacing of text?
I've tried and the CSS property line height is almost ignored if put into a span element. Why?
Why do I need to display it as a block to make line-height work?
On block level elements, the line-height CSS property specifies the minimal height of line boxes within the element.
On non-replaced inline elements, line-height specifies the height that is used in the calculation of the line box height.
On replaced inline elements, like buttons or other input element, line-height has no effect.
Src: https://developer.mozilla.org/en-US/docs/Web/CSS/line-height
I see the w3c writing
In the case that the ancestor is an inline element, the containing
block is the bounding box around the padding boxes of the first and
the last inline boxes generated for that element. In CSS 2.1, if the
inline element is split across multiple lines, the containing block is
undefined.
I don't quite understand what's this mean, what's the diff from when the containing block is block element, Can someone give me an example?
the containing block is the bounding box around the padding boxes of
the first and the last inline boxes
in other words, width of the inline element will be equal to it's content, while width of the block level element will extend on available area (padding of ancestor reduces this area, see qoute from spec).
Otherwise, the containing block is formed by the padding edge of the
ancestor.