How to Make Element Position in Center Fixed in CSS?

How to Make Element Position in Center Fixed in CSS?

  • CSS
  • 1 min read

You can use position or transform CSS properties to make an element's position centered and fixed. Below are the examples.

CSS Position Property

A document element's position can be modified using the position CSS attribute. The position of positioned elements is set by their top, right, bottom, and left properties.

Making Element Position in Center Fixed in CSS

The below CSS code uses the position and transform attributes to make the centered class in the center fixed position. Assign centered class to any element and use the following CSS:

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Vertically Centered and Fixed using CSS

To make an element centered fixed vertically use the following CSS:

#yourDiv {
  position: fixed;
  width: 500px;
  height: 200px;
  margin: 5% auto; 
  left: 0;
  right: 0;
}