CSS-inline-block
Inline block
The display: inline-block Value:
- In CSS, the display property is used to control how an HTML element should be rendered within the layout of a web page.
- The display: inline-block; value combines the characteristics of both inline and block-level elements, resulting in a unique rendering behavior.
Advantages:
- Elements with display: inline-block; can be easily aligned horizontally or centered within their parent container.
- They allow for the use of dimensions (width and height) as well as margins and padding.
- You can set line-height to control the vertical alignment of text within inline-block elements.
Example:
span.a {
display: inline; /* the default for span */
width: 100px;
height: 100px;
padding: 5px;
border: 1px solid blue;
background-color: yellow;
}
span.b {
display: inline-block;
width: 100px;
height: 100px;
padding: 5px;
border: 1px solid blue;
background-color: yellow;
}
span.c {
display: block;
width: 100px;
height: 100px;
padding: 5px;
border: 1px solid blue;
background-color: yellow;
}
Output:
Using inline-block to Create Navigation Links:
- One common use for display: inline-block is to display list items horizontally instead of vertically. The following example creates horizontal navigation links:
Example:
.nav {
background-color: yellow;
list-style-type: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav li {
display: inline-block;
font-size: 20px;
padding: 20px;
}
Output: