CSS Align

In this chapter, we will learn about CSS alignment properties. Proper alignment is crucial for creating visually appealing and user-friendly web layouts. CSS provides several properties to align elements horizontally and vertically. We will cover:

  • Text Alignment
  • Horizontal Alignment
  • Vertical Alignment
  • Aligning Inline Elements
  • Flexbox Alignment
  • Grid Alignment
  • Examples of Using CSS Alignment

Text Alignment

The text-align property sets the horizontal alignment of text within an element.

Syntax

element {
  text-align: value;
}

Values

  • left: Aligns text to the left.
  • right: Aligns text to the right.
  • center: Centers text.
  • justify: Stretches the lines so that each line has equal width (like in newspapers and magazines).

Example

p {
  text-align: center;
}

HTML

<p>This text is centered.</p>

Horizontal Alignment

For block-level elements, horizontal alignment can be achieved using the margin property.

Centering an Element

To center a block-level element, set its left and right margins to auto.

Example

div {
  width: 50%;
  margin-left: auto;
  margin-right: auto;
  background-color: lightblue;
}

HTML

<div>This div is centered horizontally.</div>

Vertical Alignment

The vertical-align property sets the vertical alignment of an inline or table-cell element.

Syntax

element {
  vertical-align: value;
}

Values

  • baseline: Aligns the baseline of the element with the baseline of its parent.
  • sub: Aligns the element as a subscript.
  • super: Aligns the element as a superscript.
  • top: Aligns the top of the element with the top of the tallest element on the line.
  • middle: Aligns the middle of the element with the middle of lowercase letters in the parent.
  • bottom: Aligns the bottom of the element with the lowest element on the line.

Example

span {
  vertical-align: super;
}

HTML

<p>This is normal text and <span>this is superscript.</span></p>

Aligning Inline Elements

The vertical-align property can also be used to align inline elements within a line.

Example

img {
  vertical-align: middle;
}

HTML

<p>Here is an image <img src="image.jpg" alt="Image"> aligned to the middle of the text.</p>

Flexbox Alignment

The Flexbox layout model provides powerful alignment capabilities for both horizontal and vertical alignment.

Syntax

.container {
  display: flex;
  justify-content: value;
  align-items: value;
}

Horizontal Alignment with Flexbox

The justify-content property aligns items horizontally within a flex container.

Values

  • flex-start: Aligns items to the start (left).
  • flex-end: Aligns items to the end (right).
  • center: Centers items.
  • space-between: Distributes items evenly, with space between them.
  • space-around: Distributes items evenly, with space around them.

Example

.container {
  display: flex;
  justify-content: center;
}

HTML

<div class="container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Vertical Alignment with Flexbox

The align-items property aligns items vertically within a flex container.

Values

  • flex-start: Aligns items to the top.
  • flex-end: Aligns items to the bottom.
  • center: Centers items.
  • baseline: Aligns items along their baselines.
  • stretch: Stretches items to fill the container.

Example

.container {
  display: flex;
  align-items: center;
}

HTML

<div class="container" style="height: 200px;">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Grid Alignment

The CSS Grid layout also provides powerful alignment capabilities.

Syntax

.container {
  display: grid;
  justify-items: value;
  align-items: value;
}

Horizontal Alignment with Grid

The justify-items property aligns grid items horizontally.

Values

  • start: Aligns items to the start (left).
  • end: Aligns items to the end (right).
  • center: Centers items.
  • stretch: Stretches items to fill the container.

Example

.container {
  display: grid;
  justify-items: center;
}

HTML

<div class="container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Vertical Alignment with Grid

The align-items property aligns grid items vertically.

Values

  • start: Aligns items to the top.
  • end: Aligns items to the bottom.
  • center: Centers items.
  • stretch: Stretches items to fill the container.

Example

.container {
  display: grid;
  align-items: center;
}

HTML

<div class="container" style="height: 200px;">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Examples of Using CSS Alignment

Example 1: Centering a Block Element Horizontally

.block {
  width: 50%;
  margin-left: auto;
  margin-right: auto;
  background-color: lightblue;
}

HTML

<div class="block">This block is centered horizontally.</div>

Example 2: Aligning Items with Flexbox

.flex-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  background-color: lightgray;
}

HTML

<div class="flex-container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Example 3: Aligning Items with Grid

.grid-container {
  display: grid;
  justify-items: center;
  align-items: center;
  height: 200px;
  background-color: lightgreen;
}

HTML

<div class="grid-container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Conclusion

In this chapter, you learned about various CSS alignment properties, including text alignment, horizontal and vertical alignment, and alignment using Flexbox and Grid. These properties allow you to create visually appealing and well-structured layouts for your web pages.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top