Skip to main content

CSS display: inline-block

Features of inline and block elements come together in the display: inline-block declaration.

Such an element shares a line with neighbors that are inline or inline-block, yet it accepts width, height, margin-top, and margin-bottom the way a block element does.

Example

Here display: block is compared with display: inline and display: inline-block.

Prompt
Show the difference between display: inline, display: inline-block, and display: block using three span elements. The inline span has padding and a red border. The inline-block span has width 100px, height 35px, padding 5px, and a red border. The block span has the same dimensions and border as inline-block.
Output: complete HTML page

Create a Horizontal Navigation Menu

List items normally stack vertically, and display: inline-block is often used to lay them out horizontally instead.

Example

Applying display: inline-block to the list items turns them into a horizontal navigation menu.

Prompt
Create a horizontal navigation menu using an unordered list with display: inline-block on the list items. The menu has a light gray background, no list-style, no padding/margin on the ul, and each li has 18px font-size and 15px padding.
Output: complete HTML page