CSS
Mastering CSS Align-items and Align-self for Seamless Layouts
align-items
and align-self
are CSS properties used in the context of flexbox and grid layouts to control the alignment of items within a container. These properties are part of the alignment properties in CSS and are particularly useful when working with flex containers.
¶align-items:
-
The
align-items
property is used to align items along the cross axis of the container. In a flex container, the cross axis is perpendicular to the main axis. -
It accepts the following values:
i. stretch
(default): Items are stretched to fill the container.
ii. flex-start:
Items are aligned at the start of the cross axis.
iii. flex-end:
Items are aligned at the end of the cross axis.
iv. center:
Items are centered along the cross axis.
v. baseline:
Items are aligned such that their baselines align.
.container {
display: flex;
align-items: center;
}
In this example, items inside the flex container will be centered along the cross axis.
¶align-self:
-
The
align-self
property is used to override thealign-items
property for individual flex items. It allows you to specify the alignment for a specific item. -
It accepts the same values as
align-items
.
.item {
align-self: flex-end;
}
In this example, the specific item with the class “item” will be aligned to the end of the cross axis, regardless of the align-items
property set on the container.
Here’s a simple HTML example to illustrate both properties:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
display: flex;
height: 200px;
align-items: center; /* Align items vertically in the center */
}
.item {
width: 50px;
height: 50px;
background-color: lightblue;
margin: 5px;
}
.item:nth-child(2) {
align-self: flex-end; /* Override alignment for the second item */
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</body>
</html>
¶Output
In this example, the items are placed in a flex container, and the `align-items: center;` style centers them along the cross axis. The second item, however, has its alignment overridden by `align-self: flex-end;`, so it aligns to the end of the cross axis.Optimizing Layouts with CSS Justify-content Flex-start to Space-evenly
What is Grid in CSS and How to use Grid CSS better way
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development