How to make child elements flow outside of parent? - html

Currently I have an html file with DOM structure like this:
<div id="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
And I want to have a layout like this: the "child" divs float outside the parent, lining up like table cells in the same row, each child has bigger width and smaller height than the parent, and they child divs will cover up the bottom of the parent div.(I cannot post picture yet, so I can only describe it), this has to be done without changing the DOM structure.
I tried to make the child elements inline blocks and float left, but they just seem to keep piling up, is there a way to achieve it without changing the DOM?

You could use absolute positioning instead:
#parent{
height:300px; width:300px; position:relative;
}
.child{
height:100px; width:300px; position:absolute;
}
.child:first-child{
left:-50px;
}
Of course, this is only part of the CSS you need.

I'd recommend using flex instead:
display: flex
It's more responsive than using
display: block
position: absolute
HTML:
<div id="parent">
<div class="child">hi</div>
<div class="child">hello</div>
<div class="child">hey</div>
</div>
CSS:
#parent {
display: flex;
flex-direction: row;
width: 400px;
}
.child {
flex-grow: 1;
height: 30px;
text-align: center;
color: white;
}
.child:nth-of-type(1) {
background:red;
}
.child:nth-of-type(2) {
background: green;
}
.child:nth-of-type(3) {
background: purple;
}
Here's a sample from jsfiddle:
https://jsfiddle.net/pbu05aaL/

Related

HTML CSS make divs side by side, out of screen width

My goal is to put div with width=100vw, after that div there should be second div with width for example 300px (so that second div should be out of screen). I tried many things with float, display inline and so on, now I don't have any more ideas.
<div id="div1"></div>
<div id="div2"></div>
Here is fiddle with example code
https://jsfiddle.net/kg5ea4sc/5/
You can use white-space: nowrap on parent element and display: inline-block on two inner elements. Also maybe you want to add vertical-align: top so it will look like this Fiddle
.element {
white-space: nowrap;
}
#div1{
background: green;
display: inline-block;
width:100vw;
height: 80px;
}
#div2{
background: red;
display: inline-block;
width:300px;
height: 100px;
}
<div class="element">
<div id="div1"></div>
<div id="div2"></div>
</div>
https://jsfiddle.net/guanzo/kg5ea4sc/18/
The second div is outside of the screen. You'll have to manipulate either it's position or the overflow:hidden property on the container if you want to see it though.
HTML
<div id="container">
<div id="div1"></div>
<div id="div2"></div>
</div>
CSS
#div1{
background: green;
width:100vw;
height: 80px;
}
#div2{
background: red;
width:300px;
height: 100px;
}
div{
display:inline-block;
}
#container{
width:100vw;
white-space:nowrap;
overflow:hidden;
}
Here is my fork of your fiddle: https://jsfiddle.net/nyzvbvo7/1/
You can scoll to the right to see the second div
What I changed:
I added
body {
width: calc(100vw + 300px);
margin: 0;
}
#div1, #div2 {
display: inline-block;
vertical-align: top;
}
So I made the body wide enough to hold both containers and set the container's display to inline-block. vertical-align: top; can be left out, the the containers will be algned at their baseline (which can vary depending on the content)

Parent container's height is not stretching for children?

Let's say I have this HTML:
<div id="container">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
...and this CSS:
#container {
border:1px #ff0000 solid;
position:relative;
}
#container .child {
width:200px;
height:200px;
background:#f5f5f5;
float:left;
}
Without using "float", does anyone know how to make #container's height stretch to collective height of all DIV's with class.child?
http://jsfiddle.net/TLxxR/
In case anyone is wondering why, I'm trying to center #container (like this: http://jsfiddle.net/TLxxR/1/) and using "float:left" for example removes the centering.
#container {
border:1px #ff0000 solid;
position:relative;
overflow: hidden;
}
Adding overflow hidden will clear your floats
edit: adding overflow: auto also clears the float if you find that other elements are being cut off
You need to use a clearfix:
#container:after {
display: table;
content: '';
clear: both;
}
The complexity of them will depend on the browsers your application needs to support, but the above should get you started.
Here's a great article that explains it a little better and in more details.
And here's a jsFiddle Demo.
You can add a <div style="clear: both"></div> after last .child.
Just set the parent to display: inline-block and it will expand with the child.
#container {
border:1px blue solid;
position:relative;
display: inline-block;
}

How to stack the divs using relative positioning?

I positioned divs relative and stacked them one below the other with fixed height. Next i am moving a div 20px up like top:-20px. the problem is for all the following divs i have to do top:-20, otherwise there is a gap of 20px. Is there a work around for this.
I have added a fiddle. http://jsfiddle.net/xS3Kt/
html
<div class="class1">hello</div>
<div class="class2">hello</div>
<div class="class3">hello</div>
<div class="class4">hello</div>
css
div{
hieght:50px;
position:relative;
width:100%;
}
.class1{background:#bbb;}
.class2{top:-5px;background:#999;}
.class3{background:#777;}
.class4{background:#555;}
here you can see there is a gap between 3rd div and fourth div. to correct it i have to position all the following divs. is there a work around
I think this jsfiddle answers the question. You need to add a wrapper that groups the divs you want to shift upward.
Html:
<div class="wrapper">
<div class="class1">hello</div>
<div class="class2">hello</div>
<div class="class3">hello</div>
<div class="class4">hello</div>
</div>
<div>hello</div>
Css:
div {
border: .1em solid rgb(0,0,0);
height:50px;
position:relative;
width:100%;
}
.wrapper {
height : auto;
margin-bottom: -5px;
top:-5px;
}
.class1 {
background:#bbb;
}
.class2 {
background:#999;
}
.class3 {
background:#777;
}
.class4 {
background:#555;
}

floats and wrong parent width

I have a parent div with few child divs inside. Both have float left. Problem is when a child divs breaks to next line, parent div get a wrong width.
Here is my HTML
<div class='parent'>
<div class='child'></div>
<div class='child'></div>
<div class='child'></div>
</div>
And CSS:
.parent{
float:left;
border:1px solid black;
}
.child{
margin:1px;
float:left;
width:300px;
height:50px;
border:1px solid black;
background:#65AEF1;
}
Here is fiddle:
http://jsfiddle.net/FDGqR/1/
As you can see, parent div got extra width. How can I force parent div to take exactly the width that childs really take?
This can be achieved using media queries..
Just add the following code to your css:
#media (max-width: 840px) {
.parent {
width: 548px;
}
}
#media (max-width: 550px) {
.parent {
width: 275px;
}
}
You may need to set the above fixed widths for better design.
Even I am new to this.. so let me know if something goes wrong :)
Working Fiddle
You could also set percentages % for width
.child{
margin:1px;
width:33%;
height:50px;
border:1px solid black;
background:#65AEF1;
}
Check this jsFiddle, if this is what you wanted.
You could do something like that:
<div class='parent'>
<div class='child'></div>
<div class='child'></div>
<div style="clear:both;"></div>
<div class='child'></div>
</div>
With the use of clear the parent div will take only the width of the two child divs
Couldn't you also just set the .parent width to 610px (double the child width plus a little room for the margin) and then you would have two columns of .child divs?
.parent{width:610px;}

Expanding a parent div horizontally to fit its floated children

I have a parent div with a variable number of equally sized child divs floated left. I want the parent div to expand to the width of the children no matter what, even if it means overflowing its own container.
Is there a way to do this naturally with HTML/CSS?
Example (The stretchable div would wind up being 180px wide):
HTML:
<div id="stretchable-div">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
...
</div
CSS:
.child {
width: 60px;
height: 60px;
float:left;
}
In this example the stretchable-div element will bust out of its parent, and stretch to its children.
Live Demo
css
#parent{
width:200px;
height:180px;
background:red;
}
#stretchable-div{
background:blue;
position: absolute;
}
.child {
width: 60px;
height: 60px;
float:left;
}
Markup
<div id="parent">Im a parent
<div id="stretchable-div">
<div class="child">a</div>
<div class="child">b</div>
<div class="child">c</div>
<div class="child">c</div>
<div class="child">c</div>
<div class="child">c</div>
<div class="child">c</div>
</div>
</div>
Just like #Pizzicato's example, but using overflow:hidden to clear the parent div: http://jsfiddle.net/dSjv4/.
There's a great article about positioning and clearing div's on A List Apart here (near the end of the article).
you can add display: inline-block; for the parent element. To work in ie7 also you need to use display:inline;zoom:100%; instead.
So a possible css for what you need is this:
#stretchable-div {
background: none repeat scroll 0 0 red;
display: inline-block;
overflow: auto; /* clear the floats */
*display:inline; /* ie7 hack even better use conditional comment */
zoom:100%;
}
example: http://jsfiddle.net/8JJSf/