How to Horizontally Center an Element

How to horizontally center an element

You can apply this CSS to the inner <div>:

#inner {
width: 50%;
margin: 0 auto;
}

Of course, you don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering.

If you are targeting Internet Explorer 8 (and later), it might be better to have this instead:

#inner {
display: table;
margin: 0 auto;
}

It will make the inner element center horizontally and it works without setting a specific width.

Working example here:

#inner {
display: table;
margin: 0 auto;
border: 1px solid black;
}

#outer {
border: 1px solid red;
width:100%
}
<div id="outer">
<div id="inner">Foo foo</div>
</div>

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>

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.

.container {    position: absolute;    top: 50%;    left: 50%;    -moz-transform: translateX(-50%) translateY(-50%);    -webkit-transform: translateX(-50%) translateY(-50%);    transform: translateX(-50%) translateY(-50%);}
<div class="container">    <span>I'm vertically/horizontally centered!</span></div>

How to horizontally align ul to center of div?

Following is a list of solutions to centering things in CSS horizontally. The snippet includes all of them.

html {  font: 1.25em/1.5 Georgia, Times, serif;}
pre { color: #fff; background-color: #333; padding: 10px;}
blockquote { max-width: 400px; background-color: #e0f0d1;}
blockquote > p { font-style: italic;}
blockquote > p:first-of-type::before { content: open-quote;}
blockquote > p:last-of-type::after { content: close-quote;}
blockquote > footer::before { content: "\2014";}
.container,blockquote { position: relative; padding: 20px;}
.container { background-color: tomato;}
.container::after,blockquote::after { position: absolute; right: 0; bottom: 0; padding: 2px 10px; border: 1px dotted #000; background-color: #fff;}
.container::after { content: ".container-" attr(data-num); z-index: 1;}
blockquote::after { content: ".quote-" attr(data-num); z-index: 2;}
.container-4 { margin-bottom: 200px;}
/** * Solution 1 */.quote-1 { max-width: 400px; margin-right: auto; margin-left: auto;}
/** * Solution 2 */.container-2 { text-align: center;}
.quote-2 { display: inline-block; text-align: left;}
/** * Solution 3 */.quote-3 { display: table; margin-right: auto; margin-left: auto;}
/** * Solution 4 */.container-4 { position: relative;}
.quote-4 { position: absolute; left: 50%; transform: translateX(-50%);}
/** * Solution 5 */.container-5 { display: flex; justify-content: center;}
<main>  <h1>CSS: Horizontal Centering</h1>
<h2>Uncentered Example</h2> <p>This is the scenario: We have a container with an element inside of it that we want to center. I just added a little padding and background colors so both elements are distinquishable.</p>
<div class="container container-0" data-num="0"> <blockquote class="quote-0" data-num="0"> <p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p> <footer>Tasha Yar about Data</footer> </blockquote> </div>
<h2>Solution 1: Using <code>max-width</code> & <code>margin</code> (IE7)</h2>
<p>This method is widely used. The upside here is that only the element which one wants to center needs rules.</p>
<pre><code>.quote-1 { max-width: 400px; margin-right: auto; margin-left: auto;}</code></pre>
<div class="container container-1" data-num="1"> <blockquote class="quote quote-1" data-num="1"> <p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p> <footer>Tasha Yar about Data</footer> </blockquote> </div>
<h2>Solution 2: Using <code>display: inline-block</code> and <code>text-align</code> (IE8)</h2>
<p>This method utilizes that <code>inline-block</code> elements are treated as text and as such they are affected by the <code>text-align</code> property. This does not rely on a fixed width which is an upside. This is helpful for when you don’t know the number of elements in a container for example.</p>
<pre><code>.container-2 { text-align: center;}
.quote-2 { display: inline-block; text-align: left;}</code></pre>
<div class="container container-2" data-num="2"> <blockquote class="quote quote-2" data-num="2"> <p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p> <footer>Tasha Yar about Data</footer> </blockquote> </div>
<h2>Solution 3: Using <code>display: table</code> and <code>margin</code> (IE8)</h2>
<p>Very similar to the second solution but only requires to apply rules on the element that is to be centered.</p>
<pre><code>.quote-3 { display: table; margin-right: auto; margin-left: auto;}</code></pre>
<div class="container container-3" data-num="3"> <blockquote class="quote quote-3" data-num="3"> <p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p> <footer>Tasha Yar about Data</footer> </blockquote> </div>
<h2>Solution 4: Using <code>translate()</code> and <code>position</code> (IE9)</h2>
<p>Don’t use as a general approach for horizontal centering elements. The downside here is that the centered element will be removed from the document flow. Notice the container shrinking to zero height with only the padding keeping it visible. This is what <i>removing an element from the document flow</i> means.</p>
<p>There are however applications for this technique. For example, it works for <b>vertically</b> centering by using <code>top</code> or <code>bottom</code> together with <code>translateY()</code>.</p>
<pre><code>.container-4 { position: relative;}
.quote-4 { position: absolute; left: 50%; transform: translateX(-50%);}</code></pre>
<div class="container container-4" data-num="4"> <blockquote class="quote quote-4" data-num="4"> <p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p> <footer>Tasha Yar about Data</footer> </blockquote> </div>
<h2>Solution 5: Using Flexible Box Layout Module (IE10+ with vendor prefix)</h2>
<p></p>
<pre><code>.container-5 { display: flex; justify-content: center;}</code></pre>
<div class="container container-5" data-num="5"> <blockquote class="quote quote-5" data-num="5"> <p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p> <footer>Tasha Yar about Data</footer> </blockquote> </div></main>

How to horizontally center an element

You can apply this CSS to the inner <div>:

#inner {
width: 50%;
margin: 0 auto;
}

Of course, you don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering.

If you are targeting Internet Explorer 8 (and later), it might be better to have this instead:

#inner {
display: table;
margin: 0 auto;
}

It will make the inner element center horizontally and it works without setting a specific width.

Working example here:

#inner {
display: table;
margin: 0 auto;
border: 1px solid black;
}

#outer {
border: 1px solid red;
width:100%
}
<div id="outer">
<div id="inner">Foo foo</div>
</div>

How to horizontally center a link element with CSS?

You should try like this-

  .center {  text-align: center;  /*align-self: center;*/  margin: 0 auto;}.expander_link {  padding-top: 50px;  padding-bottom: 50px;  margin: 0 auto;  font-size: 100%;  font-weight: bold;  color: hotpink;  text-align: center;  align-self: center;  text-decoration: none;  display: block; }
<h4 class="center">hello</h4><p>  blah blah blah blah blah blah blah blah blah blah</p><a id="link_1" href="3801" data="1" class="expander_link">center me horizontally</a>

How to centering a div horizontally without width & margin

Make the content you want to center (.container) inline-block. This way you can center it with text-align: center on the parent div.

.container {  display: inline-block;  background-color: red;}
.content_wrapper { background-color: blue; text-align: center;}
<div class="main_section">  <div class="content_wrapper">    <div class="container">My content</div>  </div></div>

How can I center text (horizontally and vertically) inside a div block?

If it is one line of text and/or image, then it is easy to do. Just use:

text-align: center;
vertical-align: middle;
line-height: 90px; /* The same as your div height */

That's it. If it can be multiple lines, then it is somewhat more complicated. But there are solutions on http://pmob.co.uk/. Look for "vertical align".

Since they tend to be hacks or adding complicated divs... I usually use a table with a single cell to do it... to make it as simple as possible.


Update for 2020:

Unless you need make it work on earlier browsers such as Internet Explorer 10, you can use flexbox. It is widely supported by all current major browsers. Basically, the container needs to be specified as a flex container, together with centering along its main and cross axis:

#container {
display: flex;
justify-content: center;
align-items: center;
}

To specify a fixed width for the child, which is called a "flex item":

#content {
flex: 0 0 120px;
}

Example: http://jsfiddle.net/2woqsef1/1/

To shrink-wrap the content, it is even simpler: just remove the flex: ... line from the flex item, and it is automatically shrink-wrapped.

Example: http://jsfiddle.net/2woqsef1/2/

The examples above have been tested on major browsers including MS Edge and Internet Explorer 11.

One technical note if you need to customize it: inside of the flex item, since this flex item is not a flex container itself, the old non-flexbox way of CSS works as expected. However, if you add an additional flex item to the current flex container, the two flex items will be horizontally placed. To make them vertically placed, add the flex-direction: column; to the flex container. This is how it works between a flex container and its immediate child elements.

There is an alternative method of doing the centering: by not specifying center for the distribution on the main and cross axis for the flex container, but instead specify margin: auto on the flex item to take up all extra space in all four directions, and the evenly distributed margins will make the flex item centered in all directions. This works except when there are multiple flex items. Also, this technique works on MS Edge but not on Internet Explorer 11.


Update for 2016 / 2017:

It can be more commonly done with transform, and it works well even in older browsers such as Internet Explorer 10 and Internet Explorer 11. It can support multiple lines of text:

position: relative;
top: 50%;
transform: translateY(-50%);

Example: https://jsfiddle.net/wb8u02kL/1/

To shrink-wrap the width:

The solution above used a fixed width for the content area. To use a shrink-wrapped width, use

position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Example: https://jsfiddle.net/wb8u02kL/2/

If the support for Internet Explorer 10 is needed, then flexbox won't work and the method above and the line-height method would work. Otherwise, flexbox would do the job.

How do I horizontally center a span element inside a div

One option is to give the <a> a display of inline-block and then apply text-align: center; on the containing block (remove the float as well):

div { 
background: red;
overflow: hidden;
text-align: center;
}

span a {
background: #222;
color: #fff;
display: inline-block;
/* float:left; remove */
margin: 10px 10px 0 0;
padding: 5px 10px
}

http://jsfiddle.net/Adrift/cePe3/



Related Topics



Leave a reply



Submit