- CSS align-items Property
- Definition and Usage
- Browser Support
- CSS Syntax
- Property Values
- More Examples
- Example
- Example
- Example
- Example
- Example with grid
- Example with absolute positioning
- CSS Layout — Horizontal & Vertical Align
- Example
- Center Align Text
- Example
- Center an Image
- Example
- Left and Right Align — Using position
- Example
- Left and Right Align — Using float
- Example
- The clearfix Hack
- Without Clearfix
- With Clearfix
- Example
- Center Vertically — Using padding
- Example
- Example
- Center Vertically — Using line-height
- Example
- Center Vertically — Using position & transform
- Example
- Center Vertically — Using Flexbox
- Example
- How to Align Content in CSS?
- Align Block-level Elements
- How to center Align Text in CSS
- Align Inline Elements
- Align an Image
- Horizontal Alignment
- How to center align an element
- How to align elements using position property
- How to align elements using the float property.
- Vertical Alignment
- How to align elements vertically using padding
- How to align elements using line-height
- How to align elements vertically using flexbox
- Conclusion
- About the author
- Naima Aftab
CSS align-items Property
Center the alignments for all the items of the flexible element:
More «Try it Yourself» examples below.
Definition and Usage
The align-items property specifies the default alignment for items inside a flexbox or grid container.
- In a flexbox container, the flexbox items are aligned on the cross axis, which is vertical by default (opposite of flex-direction).
- In a grid container, the grid items are aligned in the block direction. For pages in English, block direction is downward and inline direction is left to right.
For this property to have any alignment effect, the items need available space around themselves in the appropriate direction.
Tip: Use the align-self property of each item to override the align-items property.
Default value: | normal |
---|---|
Inherited: | no |
Animatable: | no. Read about animatable |
Version: | CSS3 |
JavaScript syntax: | object.style.alignItems=»center» Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
CSS Syntax
Property Values
Value | Description | Play it |
---|---|---|
normal | Default. Behaves like ‘stretch’ for flexbox and grid items, or ‘start’ for grid items with a defined block size. | Demo ❯ |
stretch | Items are stretched to fit the container | Demo ❯ |
center | Items are positioned at the center of the container | Demo ❯ |
flex-start | Items are positioned at the beginning of the container | Demo ❯ |
flex-end | Items are positioned at the end of the container | Demo ❯ |
start | Items are positioned at the beginning of their individual grid cells, in the block direction | |
end | Items are positioned at the end of the their individual grid cells, in the block direction | |
baseline | Items are positioned at the baseline of the container | Demo ❯ |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
Items are positioned at the beginning of the container:
Example
Items are positioned at the end of the container:
Example
Items are positioned at the baseline of the container:
Example
Items are stretched to fit the container:
Example with grid
Items are aligned at the start of each grid cell in the block direction:
Example with absolute positioning
Items are aligned at the end of each grid cell in the block direction for absolute positioned grid items:
#container <
display: grid;
position: relative;
align-items: end;
>
#container > div position: absolute;
>
CSS Layout — Horizontal & Vertical Align
Setting the width of the element will prevent it from stretching out to the edges of its container.
The element will then take up the specified width, and the remaining space will be split equally between the two margins:
This div element is centered.
Example
Note: Center aligning has no effect if the width property is not set (or set to 100%).
Center Align Text
To just center the text inside an element, use text-align: center;
Example
Tip: For more examples on how to align text, see the CSS Text chapter.
Center an Image
To center an image, set left and right margin to auto and make it into a block element:
Example
Left and Right Align — Using position
One method for aligning elements is to use position: absolute; :
In my younger and more vulnerable years my father gave me some advice that I’ve been turning over in my mind ever since.
Example
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Left and Right Align — Using float
Another method for aligning elements is to use the float property:
Example
The clearfix Hack
Note: If an element is taller than the element containing it, and it is floated, it will overflow outside of its container. You can use the «clearfix hack» to fix this (see example below).
Without Clearfix
With Clearfix
Then we can add the clearfix hack to the containing element to fix this problem:
Example
Center Vertically — Using padding
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding :
Example
To center both vertically and horizontally, use padding and text-align: center :
I am vertically and horizontally centered.
Example
Center Vertically — Using line-height
Another trick is to use the line-height property with a value that is equal to the height property:
I am vertically and horizontally centered.
Example
.center <
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
>
/* If the text has multiple lines, add the following: */
.center p line-height: 1.5;
display: inline-block;
vertical-align: middle;
>
Center Vertically — Using position & transform
If padding and line-height are not options, another solution is to use positioning and the transform property:
I am vertically and horizontally centered.
Example
.center <
height: 200px;
position: relative;
border: 3px solid green;
>
.center p margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
>
Tip: You will learn more about the transform property in our 2D Transforms Chapter.
Center Vertically — Using Flexbox
You can also use flexbox to center things. Just note that flexbox is not supported in IE10 and earlier versions:
Example
.center <
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid green;
>
Tip: You will learn more about Flexbox in our CSS Flexbox Chapter.
How to Align Content in CSS?
Arrangement of the content appearing on a website is highly significant when it comes to making your website exquisitely pleasing and enhancing its user experience. Content can be an HTML element (inline or block-level), some text, an image, etc. There are various approaches available in CSS to align these types of content on your web page. In this post, we are going to discuss some of these approaches to make the concept of content alignment clear to you.
Align Block-level Elements
There is a huge amount of elements that fall under the category of block-level elements such as
, to , , etc. You can align these elements in different ways. We have discussed some here for you.
How to center Align Text in CSS
You can align text using the CSS text-align property. This property renders different values such as left, right, center, and justify.
Suppose you want to align some text present inside a div container to the right. Follow the code below.
This text is right aligned. < / p >
We placed a
element inside a element and assigned a class “right” to the element.
Using the text-align property we are aligning the text to the right side of the div container.
The text has been aligned to the right using the text-align property.
Align Inline Elements
There comes such time when we need to align inline elements such as images. You can align these types of elements by changing their element type to block level using display property of CSS and then align it various css properties according to the need.
Align an Image
Using various CSS properties such as display, margin-left, or margin-right, you can align an image in whatever style you like.
Suppose you want to center align an image horizontally apart from whether it is in a container or not.
Here we have inserted an image using the tag.
To center align an image horizontally, set its display to block, and both margins(left and right) to auto. Moreover, you can provide some width or height of your choice.
The image has been center-aligned successfully.
Horizontal Alignment
Let’s explore different approaches that can be used to align block-level elements horizontally.
How to center align an element
Let’s suppose you have a and you want it to take only a specified amount of space instead of taking up the entire horizontal space and align it in the center of the page. Here is how you do it.
Here, we have created a element and nested a
element in it.
Now for the div element to take up the specified space only so that it does not stretch out to the corners of its container give it some width. To center align it horizontally, set the margin to auto.
The div has been center-aligned successfully.
How to align elements using position property
In this example, we will align a div container to the right using the absolute value of the position property.
Aligning Elements using CSS Position Property < / p >
This div is aligned to the right using the aboslute value of the CSS position property. < / p >
Here we have created two div containers to understand the working of absolute value of the position property. The class “div” is assigned to the first div tag and the class “green” is assigned to the second div tag.
Both div containers have been provided some style, however, the position property of second div has been set to absolute. Now the second div will be aligned with respect to the position of the first div.
Output
The “green” div has been absolutely positioned with respect to the the first div using the CSS position property.
How to align elements using the float property.
Suppose you want to float an div container to the right side of the page.
In the above code, we have created a div container and nested a
element in it.
Using the float property we are floating the entire div and the content inside it to the right.
The div container has been floated to the right successfully.
Vertical Alignment
Here we have explained some ways that you can use to align elements vertically.
How to align elements vertically using padding
Suppose you want vertically center some text present inside a div container.
This paragraph is centered vertically. < / p >
Here we have created a div element and nested a
element inside it.
In the above CSS code, we are giving the padding of 80px from the top and bottom and 0px from the left and right 0px to the
tag to center align it inside the div.
The text has been center-aligned successfully.
How to align elements using line-height
Using the same example as above we can center align text using the CSS line-height property as well.
Just set the line-height of the div container to 200px and then set the line-height of
element to 1.5.
The HTML elements have been aligned vertically as well as horizontally using line-height.
How to align elements vertically using flexbox
Again we are considering the same example and using flexbox this time to vertically and horizontally align elements.
Here, we are setting the display of the div container as flex and center aligning the content and items present inside the div container using justify-content and align-items properties provided by the flex-box CSS. Moreover, we are also providing some height and border to it as well.
The text is successfully center-aligned(both vertically and horizontally) using flexbox.
Conclusion
There are various approaches available in CSS to align the HTML elements such as you can vertically as well as horizontally align them to right, or left, or center using position property, float property, padding, line- height, or flexbox, etc. Furthermore you can use these properties to align text and images appearing on your website. We have discussed in detail some different approaches along suitable examples, that come in handy when dealing with alignment of the content on your web pages.
About the author
Naima Aftab
I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.