CSS Inline Block

Home / CSS / CSS Inline Block

CSS Inline Block


CSS Inline Block:

In CSS, inline-block is a value for the display property that is used to style elements in a way that they behave like inline elements but have block-level properties. Here's a breakdown of what inline-block means and how it works:

Display Property: The display property in CSS is used to define how an HTML element should be displayed on the web page. The inline-block value is one of several options for this property.

Inline Elements: By default, HTML elements like <span>, <a>, and <em> are displayed inline. This means they flow inline with the text and other elements, and they do not start on a new line. Inline elements typically only take up as much width as necessary to contain their content.

Block Elements: Block-level elements like <div>, <p>, and <h1> start on a new line and take up the full width of their container. They create a block-level box that usually contains other elements.

Inline-Block Elements: When you set an element's display property to inline-block, you make it behave like a combination of both inline and block elements. Here's how it works:

Inline Behavior: Elements with display: inline-block still flow inline with text and other elements. They do not start on a new line unless there is a line break or if they are at the beginning of a block-level container.

Block Behavior: Elements with display: inline-block take up only as much width as necessary to contain their content, like inline elements. However, they can have block-level properties applied to them, such as setting a specific width, height, margins, padding, and text alignment. This makes them similar to block-level elements in terms of styling.

Multiple Elements on One Line: You can have multiple elements with display: inline-block on the same line, and they will sit next to each other horizontally as long as there is enough space in the parent container.

Example:

<style>
    .inline-block-element {
        display: inline-block;
        width: 150px;
        height: 100px;
        margin: 10px;
        padding: 5px;
        border: 1px solid #000;
        text-align: center;
    }
</style>