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>
CSS How to center a div horizontally
Take a look here at this tutorial to learn how to do it. You need to have an element with a fixed width. This can be done with css or jquery.
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 div?
You must add margin:0 auto
to the element you need to center.
<div style="width:100%; margin: 0 auto;"> <div style="width:50%; margin:0 auto; background:#000"> <p style="color:#fff">Text must be left.</p> </div></div>
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 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>
Center a DIV horizontally and vertically
After trying a lot of things I find a way that works. I share it here if it is useful to anyone. You can see it here working: http://jsbin.com/iquviq/30/edit
.content {
width: 200px;
height: 600px;
background-color: blue;
position: absolute; /*Can also be `fixed`*/
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
/*Solves a problem in which the content is being cut when the div is smaller than its' wrapper:*/
max-width: 100%;
max-height: 100%;
overflow: auto;
}
how to horizontally center a responsive div?
What are you talking about? Horizontal centering is done with margin: 0 auto;
it doesn't matter what size the container has.
Related Topics
How to Position Div Below Another Div
How to Make Multiple Divs Display in One Line But Still Retain Width
How to Center Text in Select Box
How Would You Make Two <Div>S Overlap
Download Attribute in <A> Tag Doesn't Work When File Url Called from Sub Domain
Css Change Button Style After Click
Css Files Are Not Loading in Laravel View
Img Src Svg Changing the Styles With CSS
Swap Div Position With CSS Only
How to Disable the Border on Bootstrap 4 Cards
Css Auto Hide Elements After 5 Seconds
Make a Div Fill an Entire Table Cell
Css Hover Drop Down Menu Disappears When Trying to Select Menu Option
Css @Media Print Issues With Background-Color;
Angular 5 - Failed to Load Resource: the Server Responded With a Status of 404 (Not Found)