Align Items
Part of the Styling with CSS section of Coddy's HTML journey — lesson 47 of 76.
In CSS, the align-items property controls how flex items are aligned along the cross axis (the opposite of the main axis). It helps position items vertically in a row or horizontally in a column. This property is useful for making layouts more flexible and responsive.
Here's the basic syntax for using the align-items property:
.container {
display: flex;
align-items: value;
}value: The desired alignment for the flex items along the cross axis, which can be one of the following:
stretch: This is the default value. Flex items are stretched to fill the container along the cross axis while respectingmin-width/max-widthandmin-height/max-height.
flex-start: Flex items are placed at the start of the cross axis.
flex-end: Flex items are placed at the end of the cross axis.
center: Flex items are centered along the cross axis.
baseline: Flex items are aligned such as their baselines align.
For example:
.container-start {
display: flex;
align-items: flex-start;
}
.container-center {
display: flex;
align-items: center;
}In this example, the .container-start element will align its flex items to the start of the cross axis, the .container-center element will center its flex items along the cross axis.
Challenge
EasyYou are given an HTML document with two divisions (<div>), each containing three child divisions.
Follow the steps below:
- Style the
<strong>.seas</strong>element:- Write a CSS rule that targets the
<div>element with the class.seas. - Set
displaytoflexto enable flexible positioning of child elements. - Set the
align-itemsproperty tocenterto align the flex items in the middle of the cross axis.
- Write a CSS rule that targets the
- Style the
<strong>.oceans</strong>element:- Write a CSS rule that targets the
<div>element with the class.oceans. - Set
displaytoflexto enable flexible positioning of child elements. - Set the
align-itemsproperty toflex-endto align the flex items at the end of the cross axis.
- Write a CSS rule that targets the
Cheat sheet
The align-items property controls how flex items are aligned along the cross axis (opposite of the main axis):
.container {
display: flex;
align-items: value;
}Available values:
stretch: Default. Items stretch to fill the containerflex-start: Items placed at the start of the cross axisflex-end: Items placed at the end of the cross axiscenter: Items centered along the cross axisbaseline: Items aligned by their baselines
.container-start {
display: flex;
align-items: flex-start;
}
.container-center {
display: flex;
align-items: center;
}Try it yourself
<html>
<head>
<title>Align Items</title>
<style>
.container {
display: flex;
height: 200px; /* Added for visualization */
border: 2px solid darkblue; /* Added for visualization */
}
/* Write CSS rules here */
</style>
</head>
<body>
<div class="container">
<div class="seas">
<div>Black Sea</div>
<div>Caribbean Sea</div>
<div>Dead Sea</div>
</div>
<div class="oceans">
<div>Atlantic Ocean</div>
<div>Pacific Ocean</div>
<div>Indian Ocean</div>
</div>
</div>
</body>
</html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Styling with CSS
5 Colors and Backgrounds
Background ColorHEX ColorsRGB ColorsTransparency with RGBARecap Challenge #1