How to stack the divs using relative positioning? - html

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;
}

Related

my nav bar background isn't in the color even If i specified it [duplicate]

I'm trying to place 2 divs side by side inside of another div, so that I can have 2 columns of text and the outer div drawing a border around both of them:
HTML
<div id="outer">
<div id="left">
...
<div id="right">
</div>
CSS
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
}
#left{
float:left;
}
#right{
width:500px;
float:right;
}
However, the outer div registers a height of 0px and so the border doesn't go around the other divs. How do I make the outer div recognize the heights of the things inside it?
It's not because the floating divs doesn't have a height, it's because the floating divs don't affect the size of the parent element.
You can use the overflow style to make the parent element take the floating elements in consideration:
#outer { overflow: auto; }
There are a couple of solutions to this issue:
#outer: overflow: hidden;
or add some non-displaying content to the outer div that comes after the floated divs that you then add a clear: both style rule to.
You can also add, through css, the :after pseudo-element to insert content after those divs that you then apply clear: both to - this has the advantage of not requiring extra markup.
My preference is the first one.
Try this:
<div id="outer">
<div id="left">
...
<div id="right">
<div style="clear:both"></div>
</div>
add overflow: hidden; to the main div.
<style type="text/css">
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
overflow: hidden;
border: 1px solid green;
}
#left{
float:left;
border: 1px solid red;
}
#right{
width:500px;
float:right;
border: 1px solid yellow;
}
</style>
You could clear the float by inserting an element after the floated elements that has a clear property applied to it because floated child elements cause the parent to have 0 height since they don't take the height of the floated children into consideration.
<div id="outer">
<div id="left">
...
<div id="right">
<div class="clear"></div>
</div>
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
}
#left{
float:left;
}
#right{
width:500px;
float:right;
}
.clear{ clear: both; }
You must also float the outer div.
Div's that contain floatet divs and that are not floated themselves collapse.
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
float:left;
}
#left{
float:left;
width:300px;
}
#right{
width:500px;
float:right;
}
How bout like this:
<style type="text/css">
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
border:thin solid #000000;
height:300px;
margin:5px;
padding:10px;
}
#left{
float:left;
border:thin dashed #000000;
width:385px;
height:100px;
margin:5px;
}
#right{
width:385px;
float:left;
border:thin dashed #000000;
height:100px;
margin:5px;
}
</style>
<div id="outer">
<div id="left">
</div>
...
<div id="right">
</div>
</div>
if div inside a parent is floated it is no longer part of parent div:check it by inspecting parent element.no to fix your problem there are two methods:
1)make a empty div at end inside parent class it as .blank all following css
.blank:after{
content: "";
clear:both;
display:block;
}
Or
2) give parent a class .clear-fix and add css
.clearfix:after {
content: "";
clear: both;
display: block;
}
it will give parent a height equal to contents

Vertically align dynamic divs within parent div with only HTML

Here is our html:
<div id="1" class="right">
<div class="top"><img src=".png" alt=""></div>
<div class="content">Some dynamic text</div>
<div class="bottom"><img src=".png" alt=""></div>
</div>
And here's our CSS:
.right{position:relative; top:-304px; width:170px; height:191px;}
.content{background:url(.png) repeat-y; width:170px;}
How do we vertically align the content of #1 to always be at the bottom? Design and technical limitations mean we cannot use any CSS table properties or JavaScript.
demo (scroll to bootom of fiddle to see the image)
.right {
position:relative;
top:304px;
width:170px;
height:191px;
}
.content {
background:url(.png) repeat-y;
width:170px;
}
.bottom {
position:absolute; /* this is the key */
bottom:0;/* this is the key */
}
.bottom >img {
width:100%;
}
to do : use absolute child, of a relative parent div
Added explanation : since your .right has top:-304px; and the whole div has no content and height : 191px, so the entire markup has height = -113px (-304+191), so you wont be able to see anything...change the height to see it.
see what i am talking about
EDIT
Assuming you have fixed height div, here is a solution without using position
.right {
width:170px;
height:400px;
border:1px solid #000;
}
.content {
background:url(.png) repeat-y;
width:170px;
}
.bottom {
margin-top:300px;
margin-bottom: -200px; /* the bottom margin is the negative value of the footer's height(200px) */
}
.bottom >img {
width:100%;
}
You should change the class .right to this:
.right{
position:absolute;
bottom: 0;
width:170px;
height:191px;
}
I hope this help you :)

Can't get floats to match 100% width or expand parent

I have some basic CSS which im trying to make a post layout for a forum but i cannot get it to work.
I have one div 100% width with two floats below it side by side. They seem to never equal 100% width and so don't line up with properly.
Equally the parent div of the two floats does not expand if the floats expand and i do not know how to fix it.
This is what i have so far:
CSS
.parent{
width: 100%;
top: 10px;
position: relative;
clear: both;
color: black;
}
.line{
height:20px;
padding-left:10px;
lineHeight: 20px;
margin:0px;
border: 1px solid black;
}
.container{
width:100%;
text-align: center;
border-bottom:1px solid red;
}
.fleft{
float:left;
width:10%;
text-align:left;
margin:0px;
padding-left:10px;
border-right:1px solid black;
}
.fleft2{
float:left;
width:86%;
text-align:left;
margin:0px;
padding-left:10px;
border-right:1px solid black;
}
The HTML:
<div class="parent">
<div class="line">
<span style="float:left;">Test</span>
<span style="float:right;">Test 2</span>
</div>
<div class="container">
<div class="fleft"> Hello </div>
<div class="fleft2"> Hello Message</div>
</div>
</div>
JS Fiddle also provided:
http://jsfiddle.net/yMaqR/10/
I have one div 100% width with two floats below it side by side. They seem to never equal 100% width and so don't line up with properly.
You have to take into consideration the padding & margin. So if you add up width + padding + margin of the floated elements and they overflow the width of the parent, they'll be wrapped.
So a possible solution is to remove the padding and add it maybe to child elements.
Equally the parent div of the two floats does not expand if the floats expand and i do not know how to fix it.
The solution is to use a clearfix
More about floats and understanding how they work.

Fluid layout divs with same height and table-cell style

I need to show one big div with 3 divs inside it. The layout has to be fluid, i.e. the height of the big div must adapt to the contents of the 3 divs inside it. Moreover, I want the 3 divs have the same height, and I managed to do that with display:table property for the container div and display:table-cell property for the 3 inner divs. Nevertheless, there is a big problem: as soon as I put a div with a margin-top: inside the first of the three divs, it shifts downwards the content of the other two divs.
I really cannot understand why, any help would be much appreciated.
Here is the html and the css code:
<div id="body">
<div id="left-box">
<div id="left-container">
LEFT LEFT LEFT LEFT LEFT LEFT
</div>
</div>
<div id="central-box">
<div id="central-container">
CENTRAL CENTRAL CENTRAL CENTRAL CENTRAL
</div>
</div>
<div id="right-box">
<div id="right-container">
RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT
</div>
</div>
</div>
CSS:
#body {
width:80.9%;
margin:0 auto 0 auto;
height:auto;
/*background-color:#0F3;*/
display:table;
}
#left-box {
height:100%;
width:60%;
background-color:red;
display:table-cell;
border-right:1px solid #000;
}
#left-container {
background-color:#0CF;
width:72%;
margin-top:82px;
margin-left:2%;
}
#central-box {
background-color:#00F;
display:table-cell;
border-right:1px solid #000;
width:20%
}
#central-container {
margin-top:0px;
float:left;
background-color:#FF0;
}
#right-box {
background-color:#0C0;
display:table-cell;
border-right:1px solid #000;
width:19%;
}
#right-container {
margin-top:0px;
background-color:#FF0;
}
Try using vertical-align on the divs, for example something like this:
div {vertical-align:top;}
it is a similar phenomenon as with inline-block elements we discussed here
here I put your code + vertical-align on jsfiddle

Float:Left on divs not working as it should

I am trying to make a series of DIV elements sit side by side. Howeever i am running into problems
HTML:
<div id="comic" class="comic">
<div class="comic_panel">1</div>
<div class="comic_panel">2</div>
<div class="comic_panel">3</div>
<div class="comic_panel">4</div>
<div class="comic_panel">5</div>
<div class="comic_panel">6</div>
<div class="comic_panel">7</div>
<div class="comic_panel">8</div>
<div class="comic_panel">9</div>
<div class="comic_panel">10</div>
<div class="comic_panel">11</div>
<div class="comic_panel">12</div>
<div class="comic_panel">13</div>
<div class="comic_panel">14</div>
</div>
CSS:
#comic{
height: 563px;
width: 1000px;
background: black;
margin: auto;
color:white;
position:relative;
overflow:auto;
}
.comic_panel{
width:1000px;
height:563px;
position:relative;
float:left;
background:orange;
}
However the result I get is simply the DIVS displaying under neath one another.
Your divs are too wide to fit side by side in the container. Try giving them a width of 200px:
.comic_panel{
width:200px;
height:563px;
position:relative;
float:left;
background:orange;
}
If you want for a scroll bar to appear, use white-space:nowrap; on the container and display:inline-block on the children.
Here is a demonstration: http://jsfiddle.net/h2StP/show
Change the CSS to below,
.comic_panel{
width:6%;
height:563px;
position:relative;
float:left;
background:orange;
border:1px solid red;
}
and they should fall side by side.
Basically child divs have same width as parent , so there is no room for them to sit side by side.
DEMO
The reason is that each inner divs (.comic_panel) are using all the width of the parent container (#comic). Then, the next div can only be place right below the previous one.
If you tune up the widths, you can have your result.
For example, if you let the container div have any width, you would have all the inner divs side by side: http://jsfiddle.net/
body {
width: auto;
overflow: auto;
width: 10000px;
}
#comic{
height: 563px;
background: black;
margin: auto;
color:white;
overflow: visible;
}
.comic_panel{
border: 1px solid black;
width:100px;
height:63px;
float:left;
background:orange;
}​
To make the inner divs not wrap, you need to either set the width of the body element to a proper value (to make space for all the inner divs) via a hard-coded width css property (as in the fiddle, but not the best approach) or via javascript (a better approach).
This post explains other approaches, using tables: http://css-tricks.com/how-to-create-a-horizontally-scrolling-site/.
BTW, you may not need the position: relative that you put there to achieve this effect.
Put the whole thing into a container div like this:
<div id="container">
<div id="comic" class="comic">
<div class="comic_panel">1</div>
<div class="comic_panel">2</div>
<div class="comic_panel">3</div>
<div class="comic_panel">4</div>
<div class="comic_panel">5</div>
<div class="comic_panel">6</div>
<div class="comic_panel">7</div>
<div class="comic_panel">8</div>
<div class="comic_panel">9</div>
<div class="comic_panel">10</div>
<div class="comic_panel">11</div>
<div class="comic_panel">12</div>
<div class="comic_panel">13</div>
<div class="comic_panel">14</div>
</div>
</div>
The container div should be the same size as your 'comic' div was before:
#container {
height: 563px;
width: 1000px;
overflow: auto;
}
And the width of your 'comic' div should be 14000.
#comic{
height: 563px;
width: 14000px;
background: black;
margin: auto;
color:white;
position:relative;
overflow:auto;
}