What is display inline in CSS: Explanation and Examples
display: inline property in CSS makes an element flow along with text and other inline elements on the same line without starting a new line. Inline elements only take up as much width as their content and ignore height and width settings.How It Works
Think of display: inline like words in a sentence. When you write a sentence, words sit next to each other on the same line until the line ends. Inline elements behave the same way—they line up side by side horizontally.
Unlike block elements that start on a new line and take full width, inline elements only use the space their content needs. They don’t force a line break before or after themselves. This makes them perfect for styling small parts of text or icons inside a paragraph.
Example
This example shows three span elements styled with display: inline. They appear side by side on the same line.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Display Inline Example</title> <style> .inline-box { display: inline; background-color: lightblue; padding: 0.5rem 1rem; margin-right: 0.5rem; border: 1px solid blue; } </style> </head> <body> <span class="inline-box">Box 1</span> <span class="inline-box">Box 2</span> <span class="inline-box">Box 3</span> </body> </html>
When to Use
Use display: inline when you want elements to flow naturally with text, like styling words, links, or icons inside paragraphs. It’s great for small elements that should not break the line.
For example, inline is perfect for:
- Highlighting a word or phrase inside a paragraph
- Adding icons next to text without breaking the line
- Styling buttons or links that appear inside sentences
If you want elements to stack vertically or control width and height, consider display: block or display: inline-block instead.
Key Points
- Inline elements flow horizontally with text.
- They do not start on a new line.
- Width and height settings are ignored on inline elements.
- Common inline elements include
<span>,<a>, and<em>. - Use inline for styling small parts of content without breaking layout flow.