set div width to content without inline-block and keep divs under each other center aligned
on the parent div put white-space: pre-line;
on the child divs add clear : both
#wrapper{ text-align: center; white-space: pre-line; }
#div1, #div2{
display: inline-block;
clear: both;
border: 1px solid grey;
margin: 3px auto 3px auto;
width: auto;
}
<div id="wrapper">
<div id="div1" class="clearfix">some content here that is bigger</div>
<div id="div2" class="clearfix">some content here</div>
</div>
http://jsfiddle.net/judsonmusic/8HCWp/
set div width to content without inline-block and keep divs under each other center aligned
on the parent div put white-space: pre-line;
on the child divs add clear : both
#wrapper{ text-align: center; white-space: pre-line; }
#div1, #div2{
display: inline-block;
clear: both;
border: 1px solid grey;
margin: 3px auto 3px auto;
width: auto;
}
<div id="wrapper">
<div id="div1" class="clearfix">some content here that is bigger</div>
<div id="div2" class="clearfix">some content here</div>
</div>
http://jsfiddle.net/judsonmusic/8HCWp/
set div width to content without inline-block
Working jsFiddle Demo
Consider the following markup:
<div class="text">apple</div>
<div class="text">banana</div>
<div class="text">kiwi</div>
<div class="text">orange</div>
Float and clear all, here is the CSS:
.text {
background: yellow;
float: left;
margin-bottom: 3px;
clear: both;
}
3 Divs equal width centered within Div (Clean without Floats)
If you want to consider inline-block
no need to specify margin. You simply need to set the width and center the elements.
#content-container { text-align: center; font-size:0;}#content-container > div { display: inline-block; height: 100px; width: 30%; font-size:initial; animation:anime 2s infinite linear alternate;}
#box1 { background-color: orange;}
#box2 { background-color: blue;}
#box3 { background-color: yellow;}
@keyframes anime { from { width:30%; } to { width:20%; }
}
<div id="content-container" class="container"> <div id="box1"> <h1>Box 1</h1> </div> <div id="box2"> <h1>Box 2</h1> </div> <div id="box3"> <h1>Box 3</h1> </div></div>
How to align a div to the middle (horizontally/width) of the page
<body>
<div style="width:800px; margin:0 auto;">
centered content
</div>
</body>
Center align a collection of inline-block elements without centering each element
You can do this using CSS3 Media Queries by setting the width of the div based on different resolution bounding values. I've copied the css here, in case jsfiddle doesn't keep it around forever.
.img-block{
max-width: 160px;
max-height: 160px;
}
.div-block{
display: inline-block;
}
.img-container{
margin-left: auto;
margin-right: auto;
}
@media all and (min-width:984px){
.img-container{
width: 985px;
}
}
@media all and (min-width:820px) and (max-width: 984px){
.img-container{
width: 820px;
}
}
@media all and (min-width:656px) and (max-width:820px){
.img-container{
width: 656px;
}
}
@media all and (min-width:492px) and (max-width:656px){
.img-container{
width: 492px;
}
}
@media all and (min-width:328px) and (max-width:492px){
.img-container{
width: 328px;
}
}
@media all and (min-width:164px) and (max-width:328px){
.img-container{
width: 164px;
}
}
Here's a modified version of the original code http://jsfiddle.net/hxHuV/9/
How can I center a div within another div?
You need to set the width
of the container (auto
won't work):
#container {
width: 640px; /* Can be in percentage also. */
height: auto;
margin: 0 auto;
padding: 10px;
position: relative;
}
The CSS reference by MDN explains it all.
Check out these links:
- auto - CSS reference | MDN
- margin - CSS reference | MDN
- What is the meaning of
auto
value in a CSS property - Stack Overflow
In action at jsFiddle.
Related Topics
Empty Style (.CSS/.Scss) Files
Vertically Align Text to The Bottom of The Box
Twitter Bootstrap Grid Vertical Align Bottom
How to @Import .CSS File as a .Scss File in Sass
CSS: Text-Align-Last Doesnt Work in Chrome
In CSS Use "Display:None" on The Element, But Keep Its ":After"
Why Is This Div's Calculated Height More Than Its Content
How to Override Background Image Defined in CSS with Another CSS
How to Draw Triangle with Transparent Background with Border
Width: 100% Without Scrollbars
Using CSS Attribute Selectors to Target The Src of Background-Image
Use an Image Instead of a Bootstrap's Glyphicon
How to Style a Svg Using CSS in Javafx Fxml
How to Implement Rtl Bootstrap 4 Navbar
How to Remove Parent Opacity in CSS
Applying CSS Rules Based on Input Checkbox Status
Media Query to Differentiate Desktop from iPad in Landscape Mode