Flexible Box Layout Module | CSS Grid
Flexbox, or the Flexible Box Layout Module, is a CSS layout model that allows you to design complex layouts with ease. It is particularly useful for creating responsive designs. Here’s an overview of the key concepts and properties in the flexbox layout model:
Key Concepts
-
Flex Container
- The parent element with
display: flex
ordisplay: inline-flex
. - Contains flex items.
- The parent element with
-
Flex Items
- The direct children of a flex container.
- Can be controlled using various flex properties.
Properties for the Flex Container
-
display
flex
: Defines a flex container.inline-flex
: Defines an inline flex container.
.container {
display: flex;
} -
flex-direction
- Defines the main axis (direction) along which flex items are placed.
row
(default): Left to right.row-reverse
: Right to left.column
: Top to bottom.column-reverse
: Bottom to top.
.container {
flex-direction: row;
} -
flex-wrap
- Defines whether flex items should wrap onto multiple lines.
nowrap
(default): All flex items will be on one line.wrap
: Flex items will wrap onto multiple lines.wrap-reverse
: Flex items will wrap onto multiple lines in reverse.
.container {
flex-wrap: wrap;
} -
flex-flow
- A shorthand for
flex-direction
andflex-wrap
.
.container {
flex-flow: row wrap;
} - A shorthand for
-
justify-content
- Defines the alignment along the main axis.
flex-start
(default): Items are packed toward the start of the main axis.flex-end
: Items are packed toward the end of the main axis.center
: Items are centered along the main axis.space-between
: Items are evenly distributed with the first item at the start and the last item at the end.space-around
: Items are evenly distributed with equal space around them.space-evenly
: Items are evenly distributed with equal space between them.
.container {
justify-content: center;
} -
align-items
- Defines the alignment along the cross axis.
stretch
(default): Items are stretched to fill the container.flex-start
: Items are aligned toward the start of the cross axis.flex-end
: Items are aligned toward the end of the cross axis.center
: Items are centered along the cross axis.baseline
: Items are aligned along their baselines.
.container {
align-items: center;
} -
align-content
- Defines the alignment of the flex lines (if
flex-wrap
is set towrap
). stretch
(default): Lines stretch to take up the remaining space.flex-start
: Lines are packed toward the start of the cross axis.flex-end
: Lines are packed toward the end of the cross axis.center
: Lines are centered along the cross axis.space-between
: Lines are evenly distributed with the first line at the start and the last line at the end.space-around
: Lines are evenly distributed with equal space around them.space-evenly
: Lines are evenly distributed with equal space between them.
.container {
align-content: center;
} - Defines the alignment of the flex lines (if
Properties for Flex Items
-
order
- Defines the order of the flex items.
- Default is 0, lower values are placed first.
.item {
order: 1;
} -
flex-grow
- Defines how much the flex item will grow relative to the rest of the flex items.
- Default is 0 (do not grow).
.item {
flex-grow: 2;
} -
flex-shrink
- Defines how much the flex item will shrink relative to the rest of the flex items.
- Default is 1 (shrink if necessary).
.item {
flex-shrink: 1;
} -
flex-basis
- Defines the default size of the flex item before the remaining space is distributed.
- Can be a length (e.g.,
20%
,50px
) orauto
.
.item {
flex-basis: 100px;
} -
flex
- A shorthand for
flex-grow
,flex-shrink
, andflex-basis
. - Default is
0 1 auto
.
.item {
flex: 1 0 auto;
} - A shorthand for
-
align-self
- Allows the default alignment (or the one specified by
align-items
) to be overridden for individual flex items. auto
(default): Inherits fromalign-items
.flex-start
: Aligns the item to the start of the cross axis.flex-end
: Aligns the item to the end of the cross axis.center
: Centers the item along the cross axis.baseline
: Aligns the item along its baseline.stretch
: Stretches the item to fill the container.
.item {
align-self: center;
} - Allows the default alignment (or the one specified by
Example Usage
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Example</title>
<style>
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
align-content: space-around;
height: 100vh;
border: 2px solid black;
}
.item {
flex: 1 1 100px; /* flex-grow, flex-shrink, flex-basis */
margin: 10px;
padding: 20px;
background-color: lightblue;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
</body>
</html>
In this example, the container is a flex container with multiple flex items. The flex properties are used to control the layout and distribution of space among the items, ensuring a responsive and adaptable design.
The display
property in CSS determines how an element is displayed on a webpage. It has several values that dictate the layout behavior of elements. Here's a comprehensive list of the possible values for the display
property and what each one does:
Basic Display Values
-
block
- The element is displayed as a block element.
- It starts on a new line, and takes up the full width available.
- Examples:
<div>
,<p>
,<h1>
,<section>
,<article>
.
.block {
display: block;
} -
inline
- The element is displayed as an inline element.
- It does not start on a new line and only takes up as much width as necessary.
- Examples:
<span>
,<a>
,<strong>
.
.inline {
display: inline;
} -
inline-block
- The element is displayed as an inline-level block container.
- It does not start on a new line but respects width and height properties.
- Examples:
<img>
,<button>
.
.inline-block {
display: inline-block;
}
Flex and Grid Layouts
-
flex
- The element becomes a flex container, allowing child elements (flex items) to be arranged according to flexbox layout rules.
.flex {
display: flex;
} -
inline-flex
- The element behaves like a flex container but is displayed inline.
.inline-flex {
display: inline-flex;
} -
grid
- The element becomes a grid container, allowing child elements to be arranged according to grid layout rules.
.grid {
display: grid;
} -
inline-grid
- The element behaves like a grid container but is displayed inline.
.inline-grid {
display: inline-grid;
}
Table Display Types
-
table
- The element behaves like a table.
- Examples:
<table>
.
.table {
display: table;
} -
table-row
- The element behaves like a table row.
- Examples:
<tr>
.
.table-row {
display: table-row;
} -
table-cell
- The element behaves like a table cell.
- Examples:
<td>
,<th>
.
.table-cell {
display: table-cell;
} -
table-caption
- The element behaves like a table caption.
- Examples:
<caption>
.
.table-caption {
display: table-caption;
} -
table-column
- The element behaves like a table column.
- Examples:
<col>
.
.table-column {
display: table-column;
} -
table-column-group
- The element behaves like a table column group.
- Examples:
<colgroup>
.
.table-column-group {
display: table-column-group;
} -
table-header-group
- The element behaves like a table header group.
- Examples:
<thead>
.
.table-header-group {
display: table-header-group;
} -
table-footer-group
- The element behaves like a table footer group.
- Examples:
<tfoot>
.
.table-footer-group {
display: table-footer-group;
} -
table-row-group
- The element behaves like a table row group.
- Examples:
<tbody>
.
.table-row-group {
display: table-row-group;
}
Special Display Values
-
none
- The element is not displayed at all (it has no effect on layout).
- It does not occupy any space.
.none {
display: none;
} -
contents
- The element itself does not generate any box, but its children are still displayed as normal.
.contents {
display: contents;
} -
run-in
- The element is rendered as either block or inline, depending on context. This is not well-supported across browsers.
.run-in {
display: run-in;
} -
inherit
- The element inherits the
display
value from its parent.
.inherit {
display: inherit;
} - The element inherits the
-
initial
- The element resets the
display
value to its initial value.
.initial {
display: initial;
} - The element resets the
-
unset
- The element resets the
display
value to its inherited value if it inherits from its parent or to its initial value if not.
.unset {
display: unset;
} - The element resets the
Each of these values allows for different types of layout and rendering behavior, enabling a wide range of design possibilities for web pages.
CSS Grid Layout Module
The CSS Grid Layout Module, commonly referred to as "CSS Grid," is a powerful tool for creating complex, responsive web layouts. It allows you to define both rows and columns, giving you precise control over the placement and alignment of elements. Here are the key concepts and properties of CSS Grid:
Key Concepts
-
Grid Container
- The parent element with
display: grid
ordisplay: inline-grid
. - Contains grid items.
- The parent element with
-
Grid Items
- The direct children of a grid container.
- Can be placed into specific cells within the grid.
Properties for the Grid Container
-
display
grid
: Defines a block-level grid container.inline-grid
: Defines an inline-level grid container.
.container {
display: grid;
} -
grid-template-columns
andgrid-template-rows
- Define the columns and rows of the grid.
- Accept values such as lengths, percentages, or the
fr
unit (fraction of available space).
.container {
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: 100px auto;
} -
grid-template-areas
- Defines named grid areas for layout.
- Items can be placed into these areas using the
grid-area
property.
.container {
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
} -
grid-template
- A shorthand for
grid-template-rows
,grid-template-columns
, andgrid-template-areas
.
.container {
grid-template:
"header header header" 100px
"sidebar main main" auto
"footer footer footer" 50px
/ 1fr 2fr 1fr;
} - A shorthand for
-
grid-column-gap
andgrid-row-gap
- Define the spacing (gutters) between columns and rows.
.container {
grid-column-gap: 10px;
grid-row-gap: 15px;
} -
grid-gap
- A shorthand for
grid-column-gap
andgrid-row-gap
.
.container {
grid-gap: 10px 15px;
} - A shorthand for
-
justify-items
- Aligns grid items along the inline (row) axis.
- Possible values:
start
,end
,center
,stretch
.
.container {
justify-items: center;
} -
align-items
- Aligns grid items along the block (column) axis.
- Possible values:
start
,end
,center
,stretch
.
.container {
align-items: stretch;
} -
justify-content
- Aligns the grid container along the inline (row) axis.
- Possible values:
start
,end
,center
,stretch
,space-between
,space-around
,space-evenly
.
.container {
justify-content: space-between;
} -
align-content
- Aligns the grid container along the block (column) axis.
- Possible values:
start
,end
,center
,stretch
,space-between
,space-around
,space-evenly
.
.container {
align-content: center;
}
Properties for Grid Items
-
grid-column-start
andgrid-column-end
- Define the start and end positions for a grid item within the columns.
.item {
grid-column-start: 1;
grid-column-end: 3;
} -
grid-row-start
andgrid-row-end
- Define the start and end positions for a grid item within the rows.
.item {
grid-row-start: 2;
grid-row-end: 4;
} -
grid-column
- A shorthand for
grid-column-start
andgrid-column-end
.
.item {
grid-column: 1 / 3;
} - A shorthand for
-
grid-row
- A shorthand for
grid-row-start
andgrid-row-end
.
.item {
grid-row: 2 / 4;
} - A shorthand for
-
grid-area
- Specifies a grid item’s location and size using named grid areas or line numbers.
.item {
grid-area: header;
} -
justify-self
- Aligns a grid item along the inline (row) axis.
- Possible values:
start
,end
,center
,stretch
.
.item {
justify-self: end;
} -
align-self
- Aligns a grid item along the block (column) axis.
- Possible values:
start
,end
,center
,stretch
.
.item {
align-self: center;
}
Example Usage
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Grid Example</title>
<style>
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: 100px auto 50px;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-gap: 10px;
}
.header {
grid-area: header;
background-color: lightblue;
}
.sidebar {
grid-area: sidebar;
background-color: lightgreen;
}
.main {
grid-area: main;
background-color: lightcoral;
}
.footer {
grid-area: footer;
background-color: lightgoldenrodyellow;
}
</style>
</head>
<body>
<div class="container">
<div class="header">Header</div>
<div class="sidebar">Sidebar</div>
<div class="main">Main Content</div>
<div class="footer">Footer</div>
</div>
</body>
</html>
In this example, the container is a grid container with four grid items: header, sidebar, main content, and footer. The grid is defined with three columns and three rows, and grid areas are specified for layout purposes. The grid-template-areas
property is used to define named areas for a clean and easy-to-read layout. The items are placed into these areas using the grid-area
property.