So I want to float some div's center, orignally I was using float: left which gave me this look http://i.imgur.com/wfHacch.png after reading other answers I see that I can not do that, one of the suggestions made was to use margin: 0 auto; when I used this it made everything simply stack in a line down the center, as per this image http://i.imgur.com/xyixrdj.png
I am trying to force the floating div's to be stacked uniformly centered. Which would also adjust to the stack down the line in lower screensizes,
How best am I to do this?
So here is my CSS at the moment
.container {
overflow: hidden;
width: 100%;
height: 100%;
}
.item {
float: left;
max-width: 500px;
width: 100%;
padding: 10px;
height: 100%;
}
button {
width: 33%;
height: 100%;
padding: 20px;
background: orange;
border: 1;
border-radius: 10px;
border: 1px;
border-radius: 02px;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
transition: all 0.3s cubic-bezier(.25,.8,.25,1);
}
And my HTML
<div class="container orders">
<div class="item">
<button class="ready">
/*content*/
</button>
</div>
</div
You should use inline-block
div {
display: inline-block
}
By default divs are block elements.
You can use flexbox: https://jsfiddle.net/BradChelly/m9d3jepz/
.container {
width: 80%;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.item {
width: 30%;
height: 100px;
margin-bottom: 20px;
border:1px solid red;
}
#media only screen and (max-width: 500px) {
.item {
width: 45%;
}
}
#media only screen and (max-width: 300px) {
.item {
width: 100%;
}
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
I don't know what your code is but If I were you I would try wrap those div's in another div with display: inline-block and clearfix. And then give parent text-align: center. End code should look something like that:
.parent{
text-align: center;
}
.parent .container{
display: inline-block;
width: 340px;
zoom: 1;
}
.parent .container:after{
content: "";
display: table;
clear: both;
}
.parent .container:before{
content: "";
display: table;
}
.parent .container .element{
display: block;
float: left;
width: 100px;
bordeR: 1px solid #000000;
height: 50px;
margin: 5px
}
<div class="parent">
<div class="container">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
</div>
if you want center a div so you have to remove float left you can add text-align center i parent div and add display inline-block in child div like this
.parent{
width:100%;
float:left;
text-align:center;
}
.child{
width:100px;
background:#f00;
display:inline-block;
}
<div class="parent">
<div class="child">Some text here</div>
<div class="child">Some text here</div>
<div class="child">Some text here</div>
<div class="child">Some text here</div>
</div>
Related
I want to make equal gap for the children of flexbox. I used margin-right zero for each child, then applied the margin right back on the last child. But the margin right somehow doesn't applied. You can see after you scrolled to the end of the child.
see demo below
.flex {
display: flex;
background: pink;
width: 80px;
overflow: auto;
}
.item {
min-width: 20px;
height: 20px;
background: black;
margin: 8px;
margin-right: 0;
}
.item:last-child {
margin-right: 4px;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
add another parent div and give overflow for parent div. For .flex class apply display:inline-flex; then it will works
.flex-parent{
overflow: auto;
width: 80px;
}
.flex {
display: flex;
background: pink;
min-width:100%;
float:left;
}
.flex-parent{
overflow: auto;
width: 80px;
}
.flex {
display: inline-flex;
background: pink;
/*min-width:100%;
float:left;*/
}
.item {
min-width: 20px;
height: 20px;
background: black;
margin: 8px;
margin-right: 0;
}
.item:last-child {
margin-right: 4px;
}
<div class="flex-parent">
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
.flex {
display: flex;
background: pink;
width: 200px;
overflow: auto;
justify-content: space-between;
}
.item {
min-width: 20px;
height: 20px;
background: black;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
I have a case of three inline divs that should be displayed either inline or in column if they don't fit in their container. However, if container is not wide enough for them to fit inline while being not narrow enough for them to collapse into a single column, they are displayed like 2+1 (two inline on top of one).
How to force them to display either ALL inline, or ALL in one column, provided:
I want to avoid using media query.
Inner divs have fixed width in px.
CSS:
.container {
text-align: center;
background-color: #FFFFFF;
margin-bottom: 20px;
}
.narrow {
width: 200px;
}
.medium {
width: 400px;
}
.wide {
width: 600px;
}
.element {
width: 100px;
margin: 20px;
padding: 20px;
display: inline-block;
background-color: #FF0000;
border-radius: 5px;
}
HTML:
<div class="container narrow">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
<div class="container medium">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
<div class="container wide">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
JS Fiddle: https://jsfiddle.net/5wLeggy4/1/
The only way to do it without using media queries is using flexbox:
https://jsfiddle.net/5wLeggy4/3/
.container {
text-align: center;
background-color: #FFFFFF;
margin-bottom: 20px;
display: flex;
}
.narrow {
width: 200px;
flex-direction: column;
}
.medium {
width: 400px;
flex-direction: column;
}
.wide {
width: 600px;
}
.element {
width: 100px;
margin: 20px;
padding: 20px;
background-color: #FF0000;
border-radius: 5px;
flex-direction: row;
}
<div class="container narrow">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
<div class="container medium">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
<div class="container wide">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
Try using "display : block;" instead of "display:inline-block;".
https://jsfiddle.net/AnjuRaj/5wLeggy4/2/
So your element class should look like:
.element {
width: 100px;
margin: 20px;
padding: 20px;
display: block;
background-color: #FF0000;
border-radius: 5px;
}
About the best i can come up with: https://jsfiddle.net/5wLeggy4/5/
<div class="container wide">
<div class="one">
<div class="element"></div>
</div>
<div class="two">
<div class="element"></div>
<div class="element"></div>
</div>
</div>
<style>
.container {
text-align: center;
background-color: #FFFFFF;
margin-bottom: 20px;
}
.one {
display:inline-block;
}
.two {
width:67%;
max-width:364px;
display:inline-block;
}
.element {
width: 100px;
margin:20px;
padding: 20px;
display: inline-block;
background-color: #FF0000;
border-radius: 5px;
}
</style>
The width property on .two causes the second two elements to drop down together. The max width on the .two div means they also cannot expand.
Annoyingly there is a single pixel width of screen where they can be together due to the overlap of the two rules. if .two is 364px exactly, the two elements within will display inline.
I want to vertically align the items to the bottom of the container. The difficulty is coming from the fact that .container is floated left, I didn't find a solution so far.
.container {
width: 40px;
height: 250px;
background: #aaa;
float: left; /* cannot be removed */
}
.item {
width: 40px;
height: 40px;
background: red;
border-radius: 50%;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
If you always have 4 items and everything has a fixed height, you can simply do the math and set some top margin on the first item:
.item:first-child {
margin-top: 90px; /* 250-40x40 */
}
You can also use flexbox:
.container {
width: 40px;
height: 250px;
background: #aaa;
float: left;
/* new */
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.item {
width: 40px;
height: 40px;
background: red;
border-radius: 50%;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
I have a parent div of unknown width (the width depend on some screen width calculations). The number of the child divs is 4 and are floated so that they are horizontally aligned. The 1st, 2nd and 4th are good candidates for fixed width value. However, the 3rd element can stretch to fit the remaining space in the parent div.
I don't know why the approach of display:table; for parent and display:table-cell for children didn't work for me. The three element's width is fixed except for the concerned div where I also tried width:auto to no avail.
<div class="parent">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
<div class="fourth"></div>
</div>
A minimal CSS:
.parent
{
width: 100%;
display:table;
}
.first
{
width: 80px;
height: 80px;
float: left;
display: table-cell;
}
.second
{
width: 80px;
height: 80px;
float: left;
display: table-cell;
}
.third
{
height: 80px;
float: left;
display: table-cell;
}
.fourth
{
width: 80px;
height: 80px;
float: left;
display: table-cell;
}
Your usual help is much appreciated.
You can do this with Flexbox, so if you add flex: 1 to one child div it will take rest of free width
.parent {
display: flex;
border: 1px solid black;
}
.child {
border: 1px solid black;
margin: 5px;
padding: 5px;
width: 50px;
}
.long {
flex: 1;
}
<div class="parent">
<div class="child">Div 1</div>
<div class="child">Div 2</div>
<div class="child long">Div 3</div>
<div class="child">Div 4</div>
</div>
Or you can use CSS Table with table-layout: fixed here is Browser support
.parent {
display: table;
width: 100%;
border: 1px solid black;
table-layout: fixed;
}
.child {
border: 1px solid black;
display: table-cell;
margin: 5px;
padding: 5px;
width: 50px;
}
.long {
width: 100%;
}
<div class="parent">
<div class="child">Div 1</div>
<div class="child">Div 2</div>
<div class="child long">Div 3</div>
<div class="child">Div 4</div>
</div>
For IE9+
You can use inline-block and calc() for this.
Snippet
body {
margin:0
}
.parent {
border:solid black;
font-size:0; /*fix inline-block gap*/
}
.child {
border: 1px solid red;
margin: 5px;
width:100px;
display:inline-block;
font-size:16px /*restore font -size*/
}
.calc{
width: calc(100% - 348px)
}
<div class="parent">
<div class="child">one</div>
<div class="child">two</div>
<div class="child calc">three</div>
<div class="child">four</div>
</div>
For IE8+
you can use display:table/table-cell
Snippet
body {
margin: 0
}
.parent {
border: solid black;
display: table;
table-layout: fixed;
width: 100%;
/*optional*/
border-collapse:separate;
border-spacing:5px;
box-sizing:border-box;
}
.child {
border: 1px solid red;
width: 100px;
display:table-cell;
}
.big {
width:100%
}
<div class="parent">
<div class="child">one</div>
<div class="child">two</div>
<div class="child big">three</div>
<div class="child">four</div>
</div>
I have some problems with my markup. So, I have HTML:
<div id="wrapper">
<div id="content">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
And CSS:
#wrapper {
margin: auto;
margin-top: 10px;
width: 80%;
}
.item {
float: left;
margin: 0;
width: 320px;
height: 200px;
}
I suppose that .item divs must be centered in parent #wrapper and #content divs due margin:auto property, but it's doesn't work. How I can solve this problem?
You have to control it by using media-queries for different resolutions.
Your html:
<div id="wrapper">
<div id="content">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
css:
#wrapper {
margin-top: 10px;
width: 100%;
}
#content{
width:80%;
margin:0px auto;
}
.item {
float: left;
margin: 0;
background:blue;
border:1px solid #000;
width: 200px;
height: 200px;
}
See the demo
For different resolution you have to add media queries and define wrapper width and content width according to them.
#media only screen and (min-width: 768px) and (max-width: 1024px) { … }
#media only screen and (max-width: 768px) { … }
Hope it will help.
Real cause of your problem is- float left of .item div. If you remove float:left property than they take auto margin.
You want to have your .item div centered, not pushed to the left side.
Now that gives you trouble probably, because things don't work out as you'd want them to, because every element you have there, is floated to the left.
You can try for this :
HTML:
<div id="wrapper">
<div id="content">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
CSS:
#wrapper {
margin-top: 10px;
width: 80%;
border:1px solid black;
}
.item {
margin: auto;
width: 320px;
height: 200px;
border:1px solid black;
}
Try This
you can simply use display:inline-block instead of float:left :
#wrapper {
margin: auto;
margin-top: 10px;
width: 80%;
text-align:center;
}
.item {
display:inline-block;
margin: 0;
/* width: 320px; /* not required, you can let width:auto */
height: 200px;
text-align:left;
}
#content{overflow:hidden;}
or
#wrapper{overflow:hidden;}
You have div's with "float:left" but parent hasn't 'haslayout' property