CSS Inline-Block
The CSS inline-block
property is a combination of the `inline` and `block` display properties. It allows an element to be formatted as an inline element but with block-level styling capabilities. This means that the element can sit next to other inline elements but also allows for width and height properties.
Understanding Inline-Block
An element with the inline-block
value will:
- Be positioned inline with other elements, meaning they will be placed next to each other if space allows.
- Honor width and height properties, unlike `inline` elements.
- Respect vertical alignment settings, making it easier to align elements vertically.
Syntax
The syntax for the CSS display
property with the inline-block
value is:
/* Inline-Block Property Syntax */
selector {
display: inline-block;
}
Examples
Example 1: Basic Inline-Block
/* Basic Inline-Block */
.inline-block-example {
display: inline-block;
width: 150px;
height: 100px;
background: #f0f0f0;
text-align: center;
line-height: 100px;
}
Box 1
Box 2
Box 3
Example 2: Vertical Alignment
/* Vertical Alignment */
.vertical-align-example {
display: inline-block;
width: 150px;
height: 100px;
background: #f0f0f0;
vertical-align: top; /* Change to middle or bottom for different alignment */
text-align: center;
line-height: 100px;
}
Top Aligned
Middle Aligned
Bottom Aligned
Example 3: Inline-Block with Margins
/* Inline-Block with Margins */
.margin-example {
display: inline-block;
width: 150px;
height: 100px;
background: #f0f0f0;
margin: 10px;
text-align: center;
line-height: 100px;
}
Box 1
Box 2
Box 3