Languages
[Edit]
EN

CSS - center element horizontally and vertically

7 points
Created by:
OneCricketer
460

In this article, we're going to have a look at how to center element using only CSS.

Quick solution (requires to use only 2 style properties):

use display: flex with margin: auto

Example: 

 

The below presented solutions were ordered by ~quality and ~usability of solutions.

The solutions can be divided to three groups:

  • flex layout based,
  • absolute position based,
  • inline elements (e.g. inline blocks or text) centering and other tricks (e.g. table cells).

 

Look on below examples to see how it works:

1. Flex layout with automatic margin example

Edit

This approach uses fact, the automatic margin size centers elemnt inside flex cointainer.

Note: main advantage of this approach is we dont need to set element size.

2. Grid layout with automatic margin example

Edit

This approach uses style property that centers items inside element.

Note: this style is new soltion so be careful using it (we have 2020).

3. Absolute position with translation example

Edit

This approach sets left-top corner position of internal element in the middle of container and shifts half of element size with relative negative translation.

Note: main advantage of this approach is working when size of element is not set as fixed.   

4. Absolute position with automatic margin example

Edit

This aproach is based on combination of three styles:

  • stretch absolute element inside container,
  • automatic margin to center element inside container,
  • fixed width to limit maximal element stretching size. 

Note: main disadvantage of this approach is requirement to set fixed element size.

5. Flex layout with center property value example 1

Edit

This aproach uses properties that alows to center child elements.

Note: main advantage of this approach is the child elements can be centered vertically different way. 

6. Flex layout with center property value example 2

Edit

This aproach uses properties that alows to center child elements.

Note: main advantages of this approach are:

  • no additional properties are needed,
  • it can be used to center text or inline element in easy way. 

7. Inline blocks with vertical aligning example

Edit

This approach uses fact, each inline element can be centered vertically to sibling element. When sibling elements have 100% height than middle element is vertically centered.

Note: main disadvantage of this approach is horizontal centering with text aligning that cause centering of element content - solution for this problem is reseting element style.

8. Inline element with line height equal to container height example

Edit

This approach works only with inline elements e.g. text, span, a, etc.

9. Table cell with vertical align example

Edit

This approach is based on fact, each table cell content can be vertically centered.

Note: main disadvaintage of this approach is we use table cell element without table stucture.

10. 50% absolute position example

Edit

This approach sets left-top corner position of internal element in the middle of container and shifts half of element size with negative margins.

Note: main disadvantage of this approach is the margin and size of element should be set to fixed.

 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

CSS

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join