Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

CSS Layout – Horizontal & Vertical Align

Center Align Elements

To center a block element horizontally, apply margin: auto; Additionally, setting its width prevents it from extending to the container’s edges. This ensures it occupies the specified width, with the remaining space evenly distributed between the margins.

This div element is centered.

 

Example

.center {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}

Note: Center alignment won’t take effect unless the width property is specified (or set to 100%).

Center Align Text

To center the text within an element, simply apply the text-align: center; property.

This text is centered.

 

Example

.center {
  text-align: center;
  border: 3px solid green;
}

Center an Image

To center an image, transform it into a block element and set both left and right margins to auto.

Image

 

Example

img {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 40%;
}

Left and Right Align – Using position

One technique for aligning elements involves using 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

.right {
  position: absolute;
  right: 0px;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}

Note: Elements positioned absolutely are taken out of the document’s normal flow, potentially resulting in overlap with other elements.

Left and Right Align – Using float

Yet another approach to aligning elements involves utilizing the float property.

Example

.right {
  float: right;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}

The clearfix Hack

NOTE: If an element’s height exceeds that of its containing element and it is floated, it may extend beyond the container’s boundaries. To address this issue, the “clearfix hack” can be employed to resolve overflow concerns (refer to the example below).

Image

Subsequently, we can incorporate the clearfix hack into the parent element to resolve this issue:

 

Example

.clearfix::after {
  content: “”;
  clear: both;
  display: table;
}

Center Vertically – Using padding

Numerous techniques exist for vertically centering an element in CSS. One straightforward approach involves utilizing top and bottom padding:

I am vertically centered.

 

Example

.center {
  padding: 70px 0;
  border: 3px solid green;
}

For achieving both vertical and horizontal centering, apply padding along with text-align: center:

I am vertically and horizontally centered.

IMG_3526

Example

.center {
  padding: 70px 0;
  border: 3px solid green;
  text-align: center;
}

Center Vertically – Using line-height

Yet another technique involves utilizing the line-height property with a value matching the height property:

IMG_3526

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

In scenarios where padding and line-height aren’t feasible, an alternative approach involves employing positioning along with the transform property:

IMG_3526

Example

.center {
  height: 200px;
  position: relative;
  border: 3px solid green;
}

.center p {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Center Vertically – Using Flexbox

Flexbox can also be utilized for centering elements. However, it’s worth noting that flexbox lacks support in IE10 and earlier versions:

IMG_3526

Example

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  border: 3px solid green;
}
Tip: You can learn more about Flexbox in our CSS Flexbox Chapter.