CSS

CSS (Cascading Style Sheets) is the language that brings web pages to life with colors, layouts, and animations. It works alongside HTML to create visually stunning and responsive designs. Let's explore the power of CSS.

CSS Inline-Block

1 April 2025 | Category:

In CSS, elements have default display properties that define how they behave in a document flow. The most commonly used values are block, inline, and inline-block. Understanding these is crucial for layout design.


1️⃣ Block-Level Elements (display: block;)

Block-level elements take up the full width of their parent container and always start on a new line.

Characteristics of Block Elements:
✔ Takes up the full width available.
✔ Starts on a new line.
✔ Can have height, width, margin, and padding.
✔ Other elements are placed below them.

📍 Example of Block Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        div {
            display: block;
            width: 200px;
            height: 100px;
            background: lightblue;
            margin: 10px;
        }
    </style>
</head>
<body>
    <div>Block Element 1</div>
    <div>Block Element 2</div>
</body>
</html>

Examples of Default Block Elements:

  • <div>, <p>, <h1><h6>, <ul>, <ol>, <section>, <article>

2️⃣ Inline Elements (display: inline;)

Inline elements do not start on a new line and only take up as much width as needed.

Characteristics of Inline Elements:
Does NOT start on a new line.
✔ Only takes as much width as necessary.
Height & width settings DO NOT apply.
Margin & padding only apply horizontally (left/right) but not vertically.

📍 Example of Inline Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        span {
            display: inline;
            background: yellow;
            padding: 10px; /* Padding applies only horizontally */
        }
    </style>
</head>
<body>
    <p>This is an <span>inline element</span> inside a paragraph.</p>
</body>
</html>

Examples of Default Inline Elements:

  • <span>, <a>, <strong>, <em>, <b>, <i>, <u>, <label>

🚨 ⚠ Issues with Inline Elements

  • You cannot set height & width directly.
  • Margin & padding do not affect vertical spacing.

3️⃣ Inline-Block Elements (display: inline-block;)

Inline-block elements behave like inline elements but allow setting width and height, making them more flexible.

Characteristics of Inline-Block Elements:
✔ Behaves like an inline element (does not start on a new line).
Supports width & height properties.
Allows margin & padding in all directions.

📍 Example of Inline-Block Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .box {
            display: inline-block;
            width: 150px;
            height: 50px;
            background: lightcoral;
            margin: 5px;
            text-align: center;
            line-height: 50px;
        }
    </style>
</head>
<body>
    <div class="box">Box 1</div>
    <div class="box">Box 2</div>
</body>
</html>

Examples of Elements That Can Be Made Inline-Block:

  • Buttons
  • Menu items
  • Form elements

4️⃣ Comparison Table

Propertyblockinlineinline-block
Starts on a new line?✅ Yes❌ No❌ No
Takes full width?✅ Yes❌ No❌ No
Can set width/height?✅ Yes❌ No✅ Yes
Can apply vertical margin/padding?✅ Yes❌ No✅ Yes

5️⃣ When to Use What?

  • Use block for large sections like <div>, <p>, <h1>, <section>.
  • Use inline for small elements inside text, like <span>, <a>, <strong>.
  • Use inline-block for flexible elements that should stay inline but need size control (e.g., buttons, navigation links).

6️⃣ Practical Example: Navigation Bar Using inline-block

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .menu {
            text-align: center;
        }
        .menu a {
            display: inline-block;
            padding: 10px 20px;
            background: navy;
            color: white;
            text-decoration: none;
            margin: 5px;
            border-radius: 5px;
        }
        .menu a:hover {
            background: darkred;
        }
    </style>
</head>
<body>
    <div class="menu">
        <a href="#">Home</a>
        <a href="#">About</a>
        <a href="#">Services</a>
        <a href="#">Contact</a>
    </div>
</body>
</html>

📌 Conclusion

block elements take full width and stack on new lines.
inline elements stay in the same line but cannot have width/height.
inline-block is a mix of both—stays inline but allows width/height customization.