VOOZH about

URL: https://www.geeksforgeeks.org/css/css-align/

⇱ CSS Align - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSS Align

Last Updated : 23 Jul, 2024

CSS alignment techniques are essential for positioning items and distributing space between content. These techniques include horizontal and vertical alignment using various properties such as margin: auto, position: absolute, text-align, and padding. This article covers multiple methods to achieve alignment in CSS.

Methods of Aligning in CSS:

1. Center Align Using Margin:

The margin: auto property centers a block element horizontally.

Note: Using margin: auto will not work in IE8 unless a !DOCTYPE is declared.

Example 1: This example describes the CSS align using the margin: auto property.

Output:

👁 Image

2. Align Using Position:

The position: absolute property aligns elements relative to their containing block.

Note: The position properties  top, bottom, left, and right will not work unless the position property is set first.

Example 2: This example describes the CSS align using the position: absolute; property.

Output:

👁 Image

3. Text Align:

The text-align: center property centers text within its container

Example 3: This example describes the CSS align using the text-align: center; property.

Output:

👁 Image

4. Vertical Align Using Padding:

The padding property can be used to center elements vertically.

Example 4: This example describes the CSS align using the padding property.

Output:

👁 Image

5. Combined Vertical and Horizontal Align:

Combining padding and text-align: center to center elements both vertically and horizontally.

Example 5: This example describes the CSS align using the padding & text-align properties.

Output:

👁 Image

CSS alignment techniques provide a range of options for positioning elements and managing the space between them. Understanding and using properties like margin, position, text-align, and padding effectively can significantly enhance the layout and design of web pages.

Supported Browser:

  • Google Chrome 95.0
  • Microsoft Edge 95.0
  • Firefox 93.0
  • Opera 80.0
  • Safari 15.0
Comment
Article Tags: