The most challenging aspect of the CSS align center is centering elements when building a layout. The procedures themselves are typically simple to comprehend. However, because there are so many different ways to center things, it can be a bit challenging.

Your approach may differ depending on the HTML element you're trying to center. In this article, we will discuss how to center various items vertically, and horizontally.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

What Is Align Center?

The CSS align center sets an element's alignment to the center. This method prevents the component from expanding out to the boundaries of its container, allowing it to take up the set width while dividing any remaining space evenly between the two margins.

Even if other components are surrounding it, centering an element ensures that all text stays in one location on the page. It can also make it easier for users to navigate between pages or screens by giving their eyes a constant starting point.

Two div elements with CSS styles are used in this example. "Hello, World!" is the text for the first div, which has the class "center." The second div has no style, but the text "Align Center" is set. We also have two paragraphs, one with the sentence "This is paragraph one." and the other with "This is paragraph two." It doesn't have any exceptional qualities," and one with the text "This is paragraph two." It also doesn't have any distinctive qualities."

How to Center Lines of Text Horizontally and Vertically 

You may use the text-align property to center horizontal text lines.

  • Set up a container for your textbook as the first step. This process is usually accomplished with a DIV or SPAN tag, although it can also use an HTML table. You may also use ordinary paragraph tags if the material is brief.
  • Set the line height on your container to the same as the height of your font plus a few pixels to accommodate any padding or margin that other stylesheets could add. This process ensures that all of your text fits within the container's limits and does not overflow.

Finally, add the following CSS align center code: text-align: center;


 .center {

  text-align: center;

  border: 3px solid green;


How to Center an Image 

It's simple to center an image with CSS align center, but there are a few things to consider before doing so.

  1. Make sure the image's width and height are both fixed. You'll need to use CSS align center attributes like padding or margins to center it if it doesn't have a set width and height.
  2. Second, make an image container with a relative placement value. This process will enable you to align your image with the rest of your page's components.
  3. To center your picture within its container, use the flex-box attribute. The flex-box point distributes space evenly amongst items based on their sizes.

You can also utilize the margin attribute to modify the element close to the container's edges. For instance, if you want your picture to be centered within a container with a 10px margin all the way around and 10px padding top and bottom, you'd use the following code: 

<style type="text/css"> 

div#container { background-color: red; margin: 10px auto 0; padding: 10px; } 



img {

  display: block;

  margin-left: auto;

  margin-right: auto;

  width: 40%;


Become a Certified UI UX Expert in Just 5 Months!

UMass Amherst UI UX BootcampExplore Program
Become a Certified UI UX Expert in Just 5 Months!

How to Center Div Horizontally and Vertically

You may use CSS align center to center divs horizontally and vertically. The most popular method is to utilize the text-align property to center text horizontally. Another option is to use the vertical-align and line-height attributes. 

The last method uses the justify-content and align-items attributes and is only applicable to flex elements. You may center text horizontally, vertically, or both with this approach. We'll look at each process in more detail later.

The text-align property with the value center is the most popular technique to center text in a div. Setting the line-height of your div to half of its height will align all of the text in it horizontally in the middle of the container (or width).


div { font-size: 16px; line-height: 24px; }

Text Alignment + Line Height

You can use CSS to center a div horizontally and vertically with absolute positioning and negative margins.

You should strictly follow the procedures given in the section above to center a div horizontally and vertically on a page. You should utilize the CSS align center position, top, and transform properties, except that you should add the left property to center the div horizontally.

To make things easy, we suppose you wish to center a div on your page with the ID "center-div." The first step is to change its location to absolute to remove it from the usual document flow. Then reduce the left and top attributes to 50%. 

This process instructs the browser to align our div's left and top edges with our page's horizontal and vertical center (i.e., 50 percent to the right and down our page). But if you stop there, you'll run into issues: having the margins of our page lined up in the center will make it seem off-center! 

To fix this issue, we'll require negative margins on both sides of our div equal to half our width (in this case, 100px)


.Absolute-Center {

  display: flex; // make us of Flexbox

  align-items: center; // does vertically center the desired content

  justify-content: center; // horizontally centers single line items

  text-align: center; // optional, but helps horizontally center text that breaks into multiple lines



<div class="center">

<p>The edges of this div are vertically and horizontally centered, making it look off center.</p>


You can use CSS to center a div horizontally and vertically with transform and translate.

There are two ways to center a div within a div whose height and width are unknown horizontally and vertically. Instead of using the margin property, you may utilize the transform property. It would help if you still used the CSS align center position and top and left attributes.

Follow the methods above to make the outer and inner divs in HTML. Set the height, width, background color, and position to "relative" in your CSS for the outer div. This process allows you to transition between different forms of hover positioning without worrying about the size of the image when it's completed loading on your screen.


.parent {

background: #CCCCCC;

height: 200px;

width: 200px;

position: relative;


.child {

background: #FFFF00;

position: absolute;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);



<div class="parent">

<div class="child">This div is centered vertically and horizontally.</div>


You can use CSS to center a div horizontally and vertically using CSS flexbox.

Flexbox is an alternative for this because it's responsive and doesn't require margin calculations. There are, however, a few more measures to remember.

The parent container must first be defined as a flex container by setting the display parameter to "flex." Then set the properties align-items and justify-content to "center." This process instructs the browser to vertically and horizontally center the flex item (the div within the div).

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

We'll use an example of adding a yellow background color instead of a border and no text within to demonstrate this. Instead, it will appear as a yellow square. We'll also give the parent container a background color to see how the child div element is centered horizontally and vertically.



body { height: 100%;


.parent {

height: 200px;

background: #CCCCCC;

display: flex;

align-items: center;

justify-content: center;


.child {

background-color: #FFFF00;

width: 100px;

height: 100px;



<div class="parent">

<div class="child"></div>


Browser Compatibility

Browser compatibility is one of the most significant considerations while using CSS align center. Testing your site across all browsers is the best method to guarantee that it works properly. Open a new window in each browser, or use a browser emulator like Chrome DevTools.

If you've never used an emulator before, you have to open Chrome DevTools, click "Emulation" from the left side menu (at the bottom), and choose whatever browser you want to simulate. After that, go to the top right corner of the screen and click the "Start" button.

Suppose you're using Chrome DevTools on macOS or Windows 10. In that case, you may also use an extension called User-Agent Switcher for Chrome to verify compatibility with previous browsers (you can also download it from the app store). You may use this add-on to imitate various devices and operating systems (like Android or Windows Phone).

Ensure compatibility with many devices and operating systems:

  • In CSS align center, instead of percentage measures, use pixel measurements.
  • When determining font sizes, use ems instead of pixels.
Get noticed by top hiring companies through our JobAssist program. Get complete job assistance post the Full Stack Web Developer - MEAN Stack Developer Course and unleash the endless possibilities. Get in touch with our admission counselor TODAY!


As we've seen, there is no one-size-fits-all solution for CSS content centering. In this instance, the circumstances will most likely determine your coding preference. Some alignment methods are more successful than others, but you may have additional reasons for selecting one way over another (e.g., maintainability or future flexibility). Regardless of the way you like, you now have all of the tools you need to do the task effectively and in the manner that most suits you.

Enroll in Full Stack Web Developer - MEAN Stack certification course that provides extensive training in front-end, middleware, and back-end software development technologies, including testing and quality assurance. This program will advance your career as a MEAN stack developer. You’ll learn top skills such as MongoDB, Express.js, Angular, and Node.js (“MEAN”), plus GIT, HTML, CSS, and JavaScript to build and deploy interactive applications and services throughout this Full Stack MEAN Developer program.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Automation Test Engineer

Cohort Starts: 29 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 4 Jun, 2024

6 Months$ 1,449
Full Stack Developer - MERN Stack

Cohort Starts: 18 Jun, 2024

6 Months$ 1,449