Horizontal Alignment with CSS

How can I horizontally center an element?

With flexbox it is very easy to style the div horizontally and vertically centered.

#inner {  
border: 0.05em solid black;
}

#outer {
border: 0.05em solid red;
width:100%;
display: flex;
justify-content: center;
}
<div id="outer">
<div id="inner">Foo foo</div>
</div>

How can I horizontally align my divs?

To achieve what you are trying to do:

Consider using display: inline-block instead of float.

Vertical and horizontal align (middle and center) with CSS

There are many methods :

  1. Center horizontal and vertical align of an element with fixed measure

CSS

 <div style="width:200px;height:100px;position:absolute;left:50%;top:50%;
margin-left:-100px;margin-top:-50px;">
<!–content–>
</div>

2 . Center horizontally and vertically a single line of text

CSS

<div style="width:400px;height:200px;text-align:center;line-height:200px;">
<!–content–>
</div>

3 . Center horizontal and vertical align of an element with no specific measure

CSS

<div style="display:table;height:300px;text-align:center;">
<div style="display:table-cell;vertical-align:middle;">
<!–content–>
</div>
</div>


Align HTML elements horizontally in a div

Just add text-align: center to your wrapper to set the horizontal alignment of all non-floating/non-positioned children:

div{
text-align:center;
}

<span> are inline elements by default. Thus you either have to use display:block on them and style them appropriately, or just tweak the parent style a little bit. Since there are no other children than the <span> and the <button>s your best of with this simple solution.

Note that text-align:<value> gets inherited from the parent, so if you want to include something else in your wrapper you should check whether text-align:center is okay for you. Otherwise use text-align:<value> in the specific element, where value is left, right, center or justify.

JSFiddle Demo

Vertical and Horizontal alignment of text with CSS

You can change just your CSS to this (no HTML changes):

div{
background: red;
bottom: 0;
height: 100px;
left: 0;
margin: auto;
position: absolute;
top: 0;
right: 0;
width: 100px;
text-align: center;
line-height: 100px;
}

The text-align is self-explanatory. The line-height forces the text to the center by matching the height of a single line to that of the div. You will have to adjust it to your needs each time.

JSFiddle

How do you easily horizontally center a div using CSS?

In the case of a non-fixed width div (i.e. you don't know how much space the div will occupy).

#wrapper {  background-color: green; /* for visualization purposes */  text-align: center;}#yourdiv {  background-color: red; /* for visualization purposes */  display: inline-block;}
<div id="wrapper">        <div id="yourdiv">Your text</div></div>

I would like to horizontally align buttons and center them with css

The best way to do that is with a text alig in the container, like this:

.container {  text-align: center;}
 <div class='container'>      <button>MyBtn1</button>      <button>MyBtn1</button>      <button>MyBtn1</button> </div>

Bootstrap Center Vertical and Horizontal Alignment

Bootstrap 5 (Updated 2021)

Bootstrap 5 is still flexbox based so vertical centering works the same way as it did in Bootstrap 4. For example, align-items-center (flex-direction: row) and justify-content-center (flex-direction: column) can used on the flexbox parent (row or d-flex).

Centering examples in Bootstrap 5

Vertical center (don't forget the parent must have a defined height!):

  • my-auto for centering inside flex (.d-flex) elements
  • my-auto can be used to center columns (.col-) inside row
  • align-items-center to center columns (col-*) inside row

Horizontal center:

  • text-center to center display:inline elements & column content
  • mx-auto for centering inside flex elements
  • mx-auto can be used to center columns (.col-) inside row
  • justify-content-center to center columns (col-*) inside row

Bootstrap 4.3+ (Update 2019)

There's no need for extra CSS. What's already included in Bootstrap will work. Make sure the container(s) of the form are full height. Bootstrap 4 now has a h-100 class for 100% height...

Vertical center:

<div class="container h-100">
<div class="row h-100 justify-content-center align-items-center">
<form class="col-12">
<div class="form-group">
<label for="formGroupExampleInput">Example label</label>
<input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
</div>
<div class="form-group">
<label for="formGroupExampleInput2">Another label</label>
<input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">
</div>
</form>
</div>
</div>

https://codeply.com/go/raCutAGHre

the height of the container with the item(s) to center should be 100%
(or whatever the desired height is relative to the centered item)

Note: When using height:100% (percentage height) on any element, the element takes in the height of it's container. In modern browsers vh units height:100vh; can be used instead of % to get the desired height.

Therefore, you can set html, body {height: 100%}, or use the new min-vh-100 class on container instead of h-100.


Horizontal center:

  • text-center to center display:inline elements & column content
  • mx-auto for centering inside flex elements
  • offset-* or mx-auto can be used to center columns (.col-)
  • justify-content-center to center columns (col-*) inside row

Vertical Align Center in Bootstrap

Bootstrap 4 full-screen centered form

Bootstrap 4 center input group

Bootstrap 4 horizontal + vertical center full screen

How to center an element horizontally and vertically

  • Approach 1 - transform translateX/translateY:

Example Here / Full Screen Example

In supported browsers (most of them), you can use top: 50%/left: 50% in combination with translateX(-50%) translateY(-50%) to dynamically vertically/horizontally center the element.