This question already has an answer here:
- How to horizontally center a <div>? 93 answers
I'm trying to horizontally center a <div>
block element on a page and have it set to a minimum width. What is the simplest way to do this? I want the <div>
element to be inline with rest of my page. I'll try to draw an example:
page text page text page text page text
page text page text page text page text
-------
| div |
-------
page text page text page text page text
page text page text page text page text
If you know the width of your div and it is fixed, you can use the following css:
where 'half-of-your-div-width' should be (obviously) the half of the width of your div.
Usage of margin-left:auto and margin-right:auto may not work in certain situations. Here is a solution what will always work. You specify a required width and than set a left-margin to a half of the remaining width.
I use div and span tags together with css properties such as block, cross-browser inline-block and text-align center, see my simple example
Minimum width is not globally supported, but can be implemented using
Then you can set your div to be