| title | Display inline | ||||||
|---|---|---|---|---|---|---|---|
| description | Learn the behavior of inline-level elements in CSS, including how they flow horizontally, ignore explicit width/height settings, and only respect horizontal margins/padding. | ||||||
| keywords |
|
||||||
| tags |
|
||||||
| sidebar_label | inline |
The display: inline value defines an element that flows naturally with text content, sitting horizontally alongside its neighbors. Elements that default to display: inline are called inline-level elements. They are primarily used for marking up small segments of text within a line.
Inline elements are designed to be non-disruptive and flexible, resulting in several important limitations compared to block elements:
Inline elements sit side-by-side on the same line. When the content reaches the end of the container, the element (and the text after it) will wrap to the next line, maintaining the flow of the text.
An inline element's width and height are determined only by its content. Any explicit CSS declarations for width or height (e.g., width: 200px;) are completely ignored.
Inline elements only respect horizontal dimensions for spacing:
- Horizontal Padding/Border:
padding-left,padding-right,border-left, andborder-rightare respected and add space horizontally. - Vertical Padding/Border:
padding-top,padding-bottom,border-top, andborder-bottomare respected visually but do not affect the vertical position or spacing of neighboring lines of text. They may visually overlap content above or below. - Vertical Margin:
margin-topandmargin-bottomare completely ignored. Only horizontal margins (margin-left,margin-right) are respected.
The following HTML elements default to display: inline:
<span>(The generic inline container)<a>(Anchor/Link)<strong>/<b>(Bold text)<em>/<i>(Italic text)<label><q>(Quote)
In the demo below, observe the key characteristics:
- The elements flow on the same line until they wrap.
- The explicit
width: 150pxis completely ignored. - The vertical margins (
margin-top: 20px) are ignored. - The vertical padding (
padding: 20px) is applied but causes the borders to visually overlap the line of text above.