This question already has answers here:
Center parent div with float children
(4 answers)
Closed 5 years ago.
I am trying to create a responsive grid. I have a 'parent' div which contains floating children. Depending on the page width a variable number of children is shown per 'row'. The children should be centered.
To get the 'parent' div to fit the children I set display:table on the parent div.
Please see the following fiddle:
http://jsfiddle.net/dwjbosman/cXuQ6/5/
css:
.page {
background: #a00;
width:700px;
margin: 20px;
}
.Parent {
background: #ccc;
border: 1px solid black;
display: table;
margin-left: auto;
margin-right:auto;
}
.Child {
float: left;
width: 150px;
height: 50px;
background:red;
margin: 5px;
}
.br {
clear: both;
}
html:
<div class='page'>O1
<div class="Parent">
<div class="Child">a</div>
<div class="Child">b</div>
<div class="Child">c</div>
<div class="Child">d</div>
</div>
Example O1 works as expected. However I want it to work with more floating children.
Example O2: works if I manually insert a clear: both after one row of children. This of course breaks the responsiveness of the layout.
If I leave out the 'clear' it no longer works and the result is example O3. The parent div becomes too wide and the children are no longer centered.
Is there a way to get the example O2 behavior while retaining responsive behavior?
Using CSS3, you can clear every 4th .Child, starting at #5:
div.Child:nth-child(4n+5){
clear: both;
background-color: #ddf;
}
Browser support isn't terrible: https://developer.mozilla.org/en-US/docs/CSS/:nth-child#Browser_compatibility
I forked your fiddle: http://jsfiddle.net/ghodmode/y8g2V/
Related
This question already has answers here:
How do you keep parents of floated elements from collapsing? [duplicate]
(15 answers)
What is a clearfix?
(10 answers)
Closed 2 years ago.
I have always had this problem and I avoid floating div's because of this problem. The code seems to work but when you inspect the code in Google's developer tools it shows that the Main div is 0px tall. the problem I have with this is what if I want the Main div to have an image or a color. The solution I found to getting this to work is applying a float to every div so Main would also get a float but floating the Main div will break the centering of the main container. Does anyone know of a way to fix the two divs going outside of the main div?
.float-left {
float: left;
}
.float-right {
float: right;
}
main {
width: 100%;
height: auto;
background-color: darkgray;
}
.div1 {
height: 200px;
width: 50%;
background-color: darkgreen;
}
.div2 {
height: 200px;
width: 50%;
background-color: darkblue;
}
<link href="https://meyerweb.com/eric/tools/css/reset/reset200802.css" rel="stylesheet" type="text/css">
<main>
<div class="div1 float-left"></div>
<div class="div2 float-right"></div>
</main>
This question already has answers here:
How to force parent div to expand by child div width/padding/margin/box?
(3 answers)
Closed 4 years ago.
I think this is a pretty basic question (at least it seems to me that it's simple). I have a div (child) inside another div (parent).
I want the parent to change its size so the child is contained inside it. Currently, no matter what I do, when the child doesn't fit the browser window, it overflows and the parent remains inside the window width boundaries.
I'm using just the code bellow:
.parent {
background-color: yellow;
height: 600px;
padding-top: 30px;
}
.child {
width: 900px;
height: 300px;
background-color: red;
}
<div class="parent">
<div class="child"></div>
</div>
What can I do so the parent div grow and overflow the browser window when its child is wider than that.
Add width: max-content; to your parent div.
.parent {
background-color: yellow;
height: 600px;
padding-top: 30px;
width: max-content;
}
.child {
width: 900px;
height: 300px;
background-color: red;
}
<div class="parent">
<div class="child"></div>
</div>
Update after comments:
This solution isn't compatible with Internet Explorer and Firefox. Therefore, if possible, display: inline-block; is a better solution.
This question already has answers here:
Margin on child element moves parent element
(18 answers)
Closed 6 years ago.
If static positioned elements are not affected by the top, bottom, left, and right properties, why does the box move along with the container when I change the margin-top value of the box element?
I have kept my code at: https://jsfiddle.net/b9rtwkq7/5/
HTML:
<div class="container">
<div class="box">
</div>
</div>
CSS:
.container
{
width:500px;
height: 500px;
background-color: grey;
margin-top: 00px;
}
.box
{
width:100px;
height: 100px;
background-color: orange;
margin-top: 100px;
}
The margins are collapsed in the jsfiddle you posted: https://www.w3.org/TR/CSS2/box.html#collapsing-margins
Add overflow: auto/hidden to .container or use the following css which I've added in a class called .no-collapse myself:
.no-collapse:before {
content: "";
display: table;
}
I've seen plenty of solutions if the child div has a fixed width, but not if it is fluid.
The parent div should have a fixed height (150px) and fluid width (80%).
The child div should have a fluid height (expands with content) and fluid width (always 100%).
I want to get the child div to vertically align within the parent div. All content within the child div should also be horizontally centered.
Here's what I have right now:
http://jsfiddle.net/6986r/
<div class="s1">
<div class="centereddiv">This green div should be vertically centered.</div>
</div>
-
body, html {
height: 100%;
margin: 0;
}
.s1 {
width:100%;
height: 150px;
display: block;
background-color: red;
float: left;
}
.centereddiv {
color: black;
text-align: center;
background-color: green;
}
If you do not mind older browser, you may use the display:flex property (aside the table property already proposed by #SW4)
Notice that display:table can be used as a fall back for older browser
DEMO
Basic update to your CSS:
.parent {
display:flex;
}
.childcentereddiv {
margin:auto;
}
Likely the most flexible implementation would be to leverage display:table, however you will also need to adapt your HTML slightly and add an additional parent:
Demo Fiddle
<div class="table">
<div class="cell">
<div class="childcentereddiv">This green div should be vertically centered.</div>
</div>
</div>
CSS
body, html {
height: 100%;
margin: 0;
width:100%;
padding:0;
}
.table {
height: 150px;
background-color: red;
display:table;
width:80%;
}
.cell {
display:table-cell;
vertical-align:middle;
}
.childcentereddiv {
color: black;
text-align: center;
background-color: green;
}
I've got some questions about CSS text alignment that I am having some difficulty understanding. The best resource I've found about vertically aligning text via CSS is this: http://blog.themeforest.net/tutorials/vertical-centering-with-css/
I have a fiddle demonstrating some ways to vertically align text, and I'd appreciate if someone gave a quick answer.
http://jsfiddle.net/zSCJr/6/
I am curious why this text is not bottom aligned in container2's child, and have 5 quick questions in the JSfiddle.
HTML:
<div class="container container2">
container2
<div class="parent">
parent
<span class="child">
child<br/>
child
</span>
</div>
</div>
CSS:
.parent {
border: 1px solid green;
height: 50%;
}
.child {
border: 1px solid red;
vertical-align: bottom;
}
.container {
border: 1px solid black;
height: 150px;
margin-bottom: 40px;
}
.container1 .parent, .container2 .parent {
display: table;
}
.container1 .child, .container2 .child {
display: table-cell;
}
.container2 {
position: relative;
}
.container2 .parent {
width: 100%;
}
.container2 .child {
position: absolute;
right: 0;
top: 0;
bottom: 0;
}
I tried to reply at what I understand to your questions.
However, if you have an image result of what you want, it will be easier to us to give you the code or tell you how to achieve what you want.
Here is the JSFIDDLE where I put your questions answer.
Questions
1) removing position: absolute from container2's child makes the text align to the bottom (as expected from vertical-align: bottom). why?
2) container3's child,child,child span only gets clipped clipped by the first ancestor which has overflow:hidden AND position:something. why is position required?
3) container4's child does not stretch vertically unless position: absolute is set (position: relative will not do anything).
4) container4's child's height: 100% will use the first parent that has a position set. why not the first parent's content height?
5)container4's child has vertical-align: bottom set. But its text is not aligned to the bottom (unlike in container1 where parent has display: table and child has display: table-cell.
Answers
1)
On your css, you can reveiw that .container2 .child and .child css is applicated to your class, so removing only one vertical align on one class will still stick the table content to the bottom because .container2 .child is display as table cell
2)
I don't understand your question, what don't you don't understand?
If you have an image result of what you want, I can code it and you will learned from it.
3)
Inside a table, everything is managed differently, you need to define how to display your content. You need to aply display: block to .container4 .child
4)
Because you have the choice =)
So set the parent position of the item that you want
5)
Because you forgot to add .container4 .parent {display: table;} and .container4 .child {display: table-cell;}
Hope this help =)