Align content vs Align items vs Align self Properties in CSS

  • css
  • 74 Views
  • 3 Min Read
  • 3 May 2024

Understanding the CSS properties align-content, align-items, and align-self is essential for effective layout alignment in flexbox designs. So let's get started.

 

 

The align-content property

 

The align-content property assists in aligning items vertically by providing options such as flex-start, flex-end, center, stretch, space-between, space-around, and space-even. When used with flex-wrap, it adjusts how flex items wrap.

 

Unlike align-items, which align individual flex items, align-content aligns entire lines of flex items. It allows you to position all items within a flex container, such as centering them or aligning them to the beginning or end.

 

For optimal results, this property is most effective when there are two rows of items. It may encounter limitations with three or more rows, especially if wrapping behavior is not uniform across rows. Otherwise, it ensures precise alignment of flex lines within the container.

 
<head>
    <style>
        .container {
            display: flex;
            padding: 10px;
            width: 20%;
            border: 3px solid black;
            height: 40vh;
            flex-wrap: wrap;
            align-content: space-around;
        }

        .one,
        .two,
        .three,
        .four,
        .five {
            border: 2px solid steelblue;
            padding: 0 20px;
        }
    </style>
</head>

<body>
    <h1>The align-content</h1>
    <div class="container">
        <div class="one">one</div>
        <div class="two">two</div>
        <div class="three">three</div>
        <div class="four">four</div>
        <div class="five">five</div>
    </div>
</body>

 

The align-content Property

 

flex-start: Lines are positioned at the beginning of the flex container.

 

flex-end: Lines are positioned at the end of the flex container.

 

center: Lines are centered within the flex container.

 

stretch: The default value. Lines expand to fill the entire container.

 

space-between: Lines are evenly spaced with the first line at the start and the last line at the end, leaving equal space between lines.

 

space-around: Lines are evenly spaced with equal space around each line, including space at the start and end.

 

space-evenly: Lines are evenly spaced with equal space between them, including space at the start and end, as well as the center.

 

 

The align-items property

 

With the align-items property, we can achieve vertical alignment. Flex-start, flex-end, center, stretch, and baseline are the values of aligned content. You can use this to move all of the items inside a box, such as the center, first, or last.

 
<head>
    <style>
        .container {
            display: flex;
            padding: 10px;
            width: 20%;
            border: 3px solid black;
            height: 40vh;
            flex-wrap: wrap;
            align-items: flex-end;
        }

        .one,
        .two,
        .three,
        .four {
            border: 2px solid teal;
            padding: 0 20px;
        }
    </style>
</head>

<body>
    <h1>The align-items</h1>
    <div class="container">
        <div class="one" style="height: 20px;">one</div>
        <div class="two" style="height: 40px;">two</div>
        <div class="three" style="height:90px;">three</div>
        <div class="four" style="height: 150px;">four</div>
    </div>
</body>

 

The align-items Property

 

flex-start: Items are aligned at the start of the container.

 

flex-end: Items are aligned at the end of the container.

 

center: Items are centered within the container.

 

stretch: Items are stretched to fill the container.

 

baseline: Items are aligned at the baseline of the container.

 
<head>
    <style>
        .container {
            display: flex;
            padding: 10px;
            width: 30%;
            border: 3px solid black;
            height: 40vh;
            flex-wrap: wrap;
            align-items: baseline;
        }

        .one,
        .two,
        .three,
        .four {
            border: 2px solid teal;
            padding: 0 20px;
        }
    </style>
</head>

<body>
    <h1>The align-items: baseline value</h1>
    <div class="container">
        <div class="one" style="height: 30px; font-size: 30px;">one</div>
        <div class="two" style="height: 40px; font-size: 20px;">two</div>
        <div class="three" style="height:90px; font-size: 10px;">three</div>
        <div class="four" style="height: 150px; font-size: 50px;">four</div>
    </div>
</body>

 

The align-items Property

 

 

The align-self property

 

The align-self property is used for vertical alignment in the same way as align-items. The values of aligned self are auto, flex-start, flex-end, center, stretch, and baseline.

 

The function of this property is that if you want to give alignment to an individual item, then you can use it.

 
<head>
    <style>
        .container {
            display: flex;
            padding: 10px;
            width: 30%;
            border: 3px solid black;
            height: 40vh;
            flex-wrap: wrap;
            align-self: stretch;
        }

        .one,
        .two,
        .three,
        .four {
            border: 2px solid teal;
            padding: 0 20px;
        }
    </style>
</head>

<body>
    <h1>The align-self</h1>
    <div class="container">
        <div class="one">one</div>
        <div class="two" style="align-self: flex-start;">two</div>
        <div class="three" style=" align-self: auto;">three</div>
        <div class="four" style=" align-self: flex-end;">four</div>
    </div>
</body>

 

The align-self Property

 

auto: The default value. The item inherits its parent container's alignment.

 

flex-start: The item is positioned at the start of the flex container.

 

flex-end: The item is positioned at the end of the flex container.

 

center: The item is centered vertically within the flex container.

 

stretch: The item stretches to fill the entire height of the flex container.

 

baseline: The item is aligned with the baseline of the flex container's content.

 

 

Conclusion

 

When working with flexbox layouts in CSS, understanding the differences between align-content, align-items, and align-self is crucial.

 

Align Content used for vertical alignment of flex lines within the flex container. Adjusts the spacing between flex lines.

 

Align Items used for vertical alignment of flex items within the flex container. Aligns individual flex items along the cross axis.

 

Align Self is similar to align-items but applies alignment to individual flex items, overriding the align-items property for that specific item.

 

Each property serves a distinct purpose in controlling vertical alignment, offering flexibility in designing responsive and visually appealing layouts.

 

I hope you completely understand all these properties. If you have any questions, you can ask them in the question box given below.

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!

About Author

Username

Meet Soni ( meetsoni )

Software Engineer

Joined On 13 Feb 2024

More from Meet Soni

Top 10 React Carousel Component Libraries for 2024

react-js

9 Jul 2024

Discover the top 10 React carousel libraries for 2024. Choose the best carousel based on i....

What is the spread operator in JavaScript with example?

javascript

9 Jul 2024

Learn how we can easily manipulate arrays, strings, and objects with the help of the sprea....

Top 4 Ways Centering a Div or Text Inside a Div using CSS

css

4 May 2024

Learn 4 techniques to make it easier to center a div container both horizontally and verti....

20 Amazing CSS Properties You Need to know - Part 2

css

4 May 2024

Learn about such properties of CSS with which you can easily make your website more attrac....

20 Amazing CSS Properties You Need to know - Part 1

css

9 May 2024

Learn 10 CSS properties that many developers do not know about. By understanding these all....

30 Keyboard Shortcuts You Might Not Know for Windows

programming

4 May 2024

Learn 30 Windows shortcut keys that will make it easier to use the system. In this post, w....

Popular Posts from Code Mafias

10 Fun Websites for Stress Relief and Relaxation During Coding Breaks

programming

11 Nov 2024

Top 10 fun websites for coders to relax during breaks. Recharge with interactive games, ar....

Mastering HTML: Top 12 Unique HTML Tags with Examples

html

4 May 2024

Through this article, learn those essential HTML tags that many developers do not know abo....

Top 60 Eye-Catching Button Designs Users Can’t Wait to Click - With Source Code

ui-ux

11 Oct 2024

Discover 60 eye-catching button designs with source code, crafted with HTML and CSS to enh....

How to Upload Code to GitHub: Quick Steps and Detailed Instructions for Beginners

github

16 Sep 2024

In order to upload (push) your project to GitHub, it involves multiple steps that need to ....

How to install MongoDB on windows in 2024

mongodb

2 May 2024

MongoDB is a Database Management System based on NoSQL (Not Only SQL) and utilizes JSON-li....

How to Implement Undo Functionality for Deleting Items in Your React App

react-js

28 Sep 2024

Learn how to implement undo functionality for deleting items in React. Follow a step-by-st....