The Wayback Machine - https://web.archive.org/web/20240914132928/https://www.geeksforgeeks.org/how-to-center-an-element-using-positionfixed-in-css/
Open In App

How to center an element using position:fixed in CSS ?

Last Updated : 06 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

When designing web pages, achieving proper centering of elements is crucial for aesthetics and user experience. In this guide, we’ll see how to center an element using position fixed property in CSS. Whether you’re creating a landing page, a form, or any other web component, understanding these methods will enhance your design skills.

Let’s first create a simple HTML structure with a div container along with h1 and h3 tags inside it. Then we will give some styling.

HTML




<!DOCTYPE html>
<html lang="en">
   <head>
      <title>
         How to center a position:fixed element ?
      </title>
      <style>
         body {
         font-family: sans-serif;
         padding: 0;
         margin: 0;
         background-color: black;
         color: white;
         }
         .container{
         border: 2px solid white;
         width: 50%;
         }
         h1 {
         color: springgreen;
         }
      </style>
   </head>
   <body>
      <div class="container">
         <h1>GeeksforGeeks</h1>
         <h3>How to center a position:fixed element?</h3>
      </div>
   </body>
</html>


Now that we have seen the structure of our code, let’s discuss the different examples of centering the element using the position fixed property in CSS.

Examples to Center an Element using position:fixed in CSS

1. Centering Using top, left, and transform Properties:

Example: To center the element, we set the top property and left property to 50% and 50%. That will move the top left corner of the element to the center. Now, because of that the element would not be exactly in the center, rather it’s in the top left corner. Hence, using the translate property, we would shift the element 50% towards the left and 50% towards the top (based on its size).

  • Set the top to “50%” and left to “50%”.
  • Now, set the transform property to “translate(-50%, -50%)”.

We have a div element with a position as “fixed” and we have centered it on the screen using the above code.

HTML




<!DOCTYPE html>
<html lang="en">
   <head>
      <title>
         How to center a position:fixed element ?
      </title>
      <style>
         body {
         font-family: sans-serif;
         padding: 0;
         margin: 0;
         background-color: black;
         color: white;
         }
         .container {
         border: 2px solid white;
         width: 50%;
         position: fixed;
         top: 50%;
         left: 50%;
         transform: translate(-50%, -50%);
         }
         h1 {
         color: springgreen;
         }
      </style>
   </head>
   <body>
      <div class="container">
         <h1>GeeksforGeeks</h1>
         <h3>How to center a position:fixed element?</h3>
      </div>
   </body>
</html>


Output:

Centering Using top, left, and transform Properties

2. Centering a Fixed-Position Element:

Example: We have created a `<p>` tag with a light green background. By applying `position: fixed;`, the element remains fixed relative to the viewport. Utilizing `top: 50%;` and `left: 50%;` positions the element at the center, while `transform: translate(-50%, -50%);` refines the centering by adjusting the position based on the element’s dimensions.

HTML




<!DOCTYPE html>
<html lang="en">
   <head>
      <title>
         How to center a position:fixed element?
      </title>
      <style>
         body {
         font-family: sans-serif;
         padding: 0;
         margin: 0;
         }
         p {
         background-color: rgba(0, 255, 127, .3);
         width: 25%;
         padding: 5px;
         position: fixed;
         top: 50%;
         left: 50%;
         transform: translate(-50%, -50%);
         }
         h1 {
         color: springgreen;
         }
      </style>
   </head>
   <body>
      <h1>GeeksforGeeks</h1>
      <h3>How to center a position:fixed element?</h3>
      <p class="child">I am child p tag</p>
   </body>
</html>


Output:

Centering a Fixed-Position Element



Similar Reads

What is the difference between position:sticky and position:fixed in CSS ?
In CSS, position: sticky and position: fixed both offer elements the ability to stay in a fixed position during scrolling. These two CSS properties, while seemingly similar, have distinct behaviors that can significantly impact the layout and user experience of a webpage. This article will tell you the differences, providing you more understanding
6 min read
How to Fixed an Element to Center in Tailwind CSS ?
Tailwind CSS is a utility-first CSS library that provides developers to build responsive and customizable user interfaces/Front-end. Tailwind CSS is the best way to write your code in an inline style. You don't need to write a single line of your CSS code you need to write only tailwind utility classes. In this article, we will see how to center th
4 min read
How to Position a Fixed Element in the Top Right Corner using CSS ?
In CSS, Positioning the fixed element in the top fight corner consists of maintaining the fixed position of the element by using Absolute Positioning or by using Flexbox Layout. These are the following methods: Table of Content Using Absolute PositioningUsing FlexboxUsing Absolute PositioningIn this approach, we are using absolute positioning to fi
2 min read
Differences between HTML &lt;center&gt; Tag and CSS "text-align: center;" Property
If you are designing a simple web page, then not much difference is noticeable, but it is very necessary to understand the basic differences between these two. Since we are only concerned with the text, these elements do not have separate meanings.HTML &lt;center&gt; tag: The &lt;center&gt; tag in HTML is used to set to align of text into the cente
3 min read
Difference between relative , absolute and fixed position in CSS
CSS positioning is a fundamental concept in web design and development. It allows us to control the layout of elements on a webpage. This article will tell you the differences between relative, absolute, and fixed positioning in CSS. 1. Relative PositioningRelative Position: Relative positioning is a CSS technique that allows an element to be adjus
2 min read
How to set fixed position but relative to container in CSS ?
In this article, we are going to learn about the fixed position but relative to the container in CSS, basically, we have two common methods, position: sticky property and position: fixed property. To set the position of an element to fixed but relative to its container, we can use the combination of the "position: fixed" and "position: relative" pr
4 min read
Bootstrap 5 Position Fixed bottom
Bootstrap 5 Position Fixed bottom is used to set the position of the element to the bottom of the viewport. We can use some CSS property to display the effect of position fixed bottom. Position Fixed Bottom Class: fixed-bottom: This class is used to set the position of the element to the bottom of the viewport. Syntax: &lt;div class="fixed-bottom"
2 min read
Bootstrap 5 Position Fixed top
Bootstrap 5 Position Fixed top is used to set the position of the element to the top of the viewport. We can use some CSS property to display the effect of position fixed top. Position Fixed Top Class: fixed-top: This class is used to set the position of the element to the top of the viewport. Syntax: &lt;div class="fixed-top"&gt; Content &lt;/div
2 min read
How to center the absolutely positioned element in div using CSS ?
When designing web pages, achieving proper centering of elements is crucial for aesthetics and user experience. In this guide, we’ll explore techniques to center an absolutely positioned element within a &lt;div&gt; container. Whether you’re creating a landing page, a form, or any other web component, understanding these methods will enhance your d
2 min read
4 Different Ways to Center an Element using CSS
When designing web pages, achieving proper centering of elements is important. We’ll explore different techniques to center an element using CSS. Whether you’re creating a landing page, a form, or any other web component, understanding these methods will enhance your design skills. So let us take a look at 4 different ways to center an element usin
3 min read
How to center an element horizontally using the CSS Box Model ?
Centering an element horizontally using the box model in CSS involves manipulating the margins or using positioning properties to achieve the desired centering effect. Different Ways to Center an Element Horizontally1. Using auto Margins:The auto margins will distribute the available space equally on both sides, horizontally centering the element.
2 min read
How to Center an Image Horizontally Within a Div Element using CSS?
Centering an image horizontally generally means occupying the center of its parent element along the x-axis. Below are the approaches to Center an Image Horizontally Within a Div Element: Table of Content Using Flexbox PropertyUsing margin auto PropertyUsing Position and Transform PropertiesUsing Flexbox PropertyExample: This example uses Flexbox t
2 min read
Bootstrap 5 Position Center elements
Bootstrap 5 Position Center elements facilitate aligning the elements to the absolute center by implementing the transform utility class .translate-middle, which implements the transformations translateX(-50%) &amp; translateY(-50%) to the element, along with combining the edge positioning utilities. Bootstrap 5 provides the 3 different transform u
2 min read
How to Set Offset an HTML Anchor Element to Adjust Fixed Header in CSS ?
When designing a website with a fixed header, we might face an issue where clicking on a link obscures the target element due to the header. This affects the user experience negatively as the content gets hidden. In this article, we will see how to offset HTML anchors for a fixed header, ensuring proper visibility for users. HTML anchors, also call
5 min read
How to Set the Fixed Width of a Span Element in CSS ?
Setting a fixed width of a span element is useful when we need to control the size of a specific inline element. The span element is mostly used in styling specific parts of text and other inline elements. There are various approaches to setting a fixed width of a span element in CSS including, width property, max-width property, and min-width prop
4 min read
How to Horizontally and Vertically Center an Element in Tailwind CSS ?
To horizontally and vertically center an element in Tailwind CSS, you can use the combination of flexbox and alignment utility classes. Apply flex and alignment classes to the parent container, and use mx-auto and my-auto for horizontal and vertical centering respectively. Syntax&lt;div class="flex items-center justify-center h-screen"&gt; &lt;div
1 min read
How to Center a Button Element Vertically and Horizontally with CSS?
Centering a button element both vertically and horizontally on a webpage is a common requirement in web design. It ensures that the button is always displayed at the centre of the webpage, regardless of the screen size. In this article, we will cover different approaches to centre a button element vertically and horizontally with CSS. Below are som
3 min read
How to create fixed/sticky footer on the bottom using Tailwind CSS ?
In this article, we are going to create a fixed/sticky footer on the bottom using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. With Tailwind CSS we can create a design by simply adding classes. Installation: Method 1: Install Tailwind via npm Step 1: npm
2 min read
Create a Fixed Sidebar using HTML and CSS
A fixed sidebar (side navigation bar) is a common layout pattern where the sidebar remains fixed on the screen while the rest of the content scrolls. This article will create a fixed sidebar using HTML and CSS. HTML creates the structure layout, and CSS applies styles to the elements. First, we create a container div element that contains two div's
4 min read
How to align form elements to center using Tailwind CSS ?
You can easily align form elements in the center using flex property in Tailwind CSS. Tailwind uses justify-center and items-center property which is an alternative to the flex-property in CSS. Syntax: &lt;div class="flex flex-col justify-center items-center"&gt; .... &lt;/div&gt; flex property: flex-col: This property stacks the flex items column-
3 min read
How to center a &lt;div&gt; using Flexbox property of CSS ?
In this article, we will learn to center an HTML div element using flexbox property of CSS. Please refer this article to know more about flexbox. Approach: To center the &lt;div&gt; element horizontally using flexbox. We use the property of display set to flex i.e. display: flex;Align items to center using align-items: center;The last step is to se
1 min read
How to Center an Image using Tailwind CSS ?
Tailwind CSS is a CSS framework that helps in building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Tailwind CSS creates small utilities with a defined set of options enabling easy integration of existing classes into the HTML code. In this article, we'll see how to c
3 min read
How to align a logo image to center of navigation bar using HTML and CSS ?
The navigation bar is an essential component of a website that helps users to navigate between different pages. It is usually located at the top of the page and consists of a list of horizontal links, logos, and other elements. The alignment of logos can be adjusted using CSS. In this tutorial, we will focus on how to align the logos to the center
2 min read
How to center a &lt;div&gt; using CSS grid Property ?
In this article, we will learn to center an HTML &lt;div&gt; element using the CSS grid property. We can center any element using the below-mentioned approaches. Here we will learn to center a div in 5 different ways. Table of Content Centering div - CSS Grid and place-selfCentering div - CSS Grid and place-itemsCentering div - CSS Grid and place-c
5 min read
How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS ?
We will know how to center-align the image in a div tag using CSS &amp; will also understand its implementation through the example. Given an image, we need to set the image that align to the center (vertically and horizontally) inside a bigger div. But first let's create a basic structure, in which inside the body tag with id=outer there is an img
2 min read
How to horizontally center a div using CSS ?
To make an HTML div center horizontally using CSS is quite easy. We can horizontally center any element using the below-mentioned approaches. Before centering a div horizontally we need to create a fixed-size div. We have given a border to the div so the centering of that div can be visible. Set the main container width to 100% so the div can be mo
2 min read
How to Center a Div using CSS ?
Centering a &lt;div&gt; element both vertically and horizontally can be achieved through various techniques. We will explore different methods for perfect alignment, including flexbox properties, positioning, translate, and the calc() function. Below are the approaches to Center a Div using CSS: Table of Content Using Flexbox PropertiesUsing Positi
3 min read
How to align items at the center of the container using CSS ?
Centering items in a container using CSS involves aligning elements horizontally and vertically within a parent element. This can be done using different CSS properties and techniques, ensuring that the content appears visually centered and balanced within its container. Here are some common approaches Table of Content Centering with text-align, di
2 min read
How to align block elements to center using CSS ?
Block elements are known for taking up the full line space, forcing other elements to start on a new line. This means they have a width of 100% of the webpage or container holding the block. In this article, we will explore how block elements usually behave and how to center them using CSS. Table of Content Understanding Block ElementsHow to Center
3 min read
How to Center an Image using text-align Property in CSS ?
Aligning an image horizontally within a container can sometimes be challenging. In this article, we will explore how to center an image using the text-align property in CSS. Understanding the text-align PropertyThe text-align property in CSS is used to specify the horizontal alignment of text within an element. It can also be used to center inline
2 min read