Reputation: 3920
I want to simply center 2 divs horizontally regardless of the screen width and without using a wrapper. I have the following simple code:
#div1 {
display: inline-block;
width: 100px;
border: 1px solid #000000;
}
#div2 {
display: inline-block;
width: 200px;
border: 1px solid #000000;
}
I created the following fiddle for illustration: http://jsfiddle.net/axe89/
Upvotes: 0
Views: 372
Reputation: 326
You can add
text-align: center;
to the body tag or to whatever you are planning to wrap the divs with.
Upvotes: 0
Reputation: 517
@setek has the solution above, just wanted to add this quick rule of thumb:
To horizontally center display:inline
and display:inline-block
items, use text-align:center;
.
To horizontally center display:block
items, use margin: 0 auto;
.
Upvotes: 1
Reputation: 3243
as alluded to by setek, you can define a container for your divs, with a width of 100% so that it scales with the screen/device width. Also set its text align to center to achieve your desired effect.
#container{text-align:center;width:100%;}
here is your updated fiddle
and for slightly modified markup and css - http://jsfiddle.net/axe89/5/
Upvotes: 0