Learn HTML
Lesson 1 - Overview Of The Web And HTML
Lesson 2 - HTML Tags And Elements
Lesson 3 - Images And Multimedia
Lesson 4 - Writing Your First HTML Page
Lesson 5 - Overview Of CSS
Lesson 6 - CSS Selectors And Properties
Lesson 7 - Applying CSS To HTML
Lesson 8 - Box Model
Lesson 9 - Positioning And Layout
Flexbox is a powerful layout model in CSS that simplifies the creation of complex web layouts. It allows you to efficiently arrange, align, and distribute space among elements within a container, regardless of their varying sizes or content.
Imagine you have a container (flex-container
) and inside it are several elements (flex-item
), like boxes on a shelf. Flexbox enables you to control how these boxes are positioned and aligned within the shelf.
To begin using Flexbox, you designate a container as a flex container by setting its display
property to flex
or inline-flex
. This simple declaration activates Flexbox capabilities for that container:
<style> .flex-container { display: flex; /* or */ display: inline-flex; } </style> <div class="flex-container"> <div class="flex-item">Item 1</div> <div class="flex-item">Item 2</div> <div class="flex-item">Item 3</div> </div>
In this example:
flex-container
becomes a flex container, and any direct child elements (flex-item
) automatically become flex items.flex-direction
property to create a vertical column layout if needed.<style> .flex-container { display: flex; flex-direction: column; /* Arrange items vertically */ } </style> <div class="flex-container"> <div class="flex-item">Item 1</div> <div class="flex-item">Item 2</div> <div class="flex-item">Item 3</div> </div>
Flexbox introduces two main axes:
flex-direction
. It determines how flex items are laid out horizontally (row
) or vertically (column
).To align items within a flex container:
justify-content
to align items along the main axis (horizontally for rows, vertically for columns).align-items
to align items along the cross axis (vertically for rows, horizontally for columns).<style> .flex-container { height: 200px; display: flex; justify-content: center; /* Center items along the main axis */ align-items: center; /* Center items along the cross axis */ } </style> <div class="flex-container"> <div class="flex-item">Item 1</div> <div class="flex-item">Item 2</div> <div class="flex-item">Item 3</div> </div>
Flexbox also supports wrapping of items onto new lines with flex-wrap
, allowing for responsive layouts that adjust automatically based on available space.
Flexbox empowers developers to create modern and responsive web layouts efficiently, offering a robust set of tools to manage the arrangement and appearance of elements within a container.