I have 3 different divs in html file.
div {
width: 200px;
height: 200px;
position: absolute;
}
.second {
background: red;
left: 200px;
top: 200px;
}
.third {
background: green;
top: 400px;
left: 400px;
}
.first {
background: yellow;
}
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
I want to move one div.Anyone I want.But when I move any divs this must effect others.Example:When I added margin to .second div this divs change position in page flow.I want this effects to .first and .third div.How can I do it?
Expected output:
The purpose of CSS position:absolute is to remove the element from the document flow so that changing the margins or size won't effect other elements around it.
If you want your <div>s to be affected by margins of other divs, instead of using position:absolute try using float:left (or flexbox for a more powerful solution).
Instead of position: absolute let it be relative
Have a look at the CodePen Example if this solves your problem
div {
width: 200px;
height: 200px;
/* position: absolute; */
}
.second {
background: red;
margin-left: 200px;
}
.third {
background: green;
margin-left: 400px;
}
.first {
background: yellow;
}
HTML:
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
If I understand this correctly, you want to add a margin on all three divs?
We can do that by putting them inside a container.
<div class="container">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
</div>
And adding the margin to the container class.
.container {
margin: 10px
}
Working JSFiddle: https://jsfiddle.net/pcyq20vx/
Related
I have several divs, each a child of the previous. They're all positioned relative, so that I can offset the children relative to the parents. I want to have the right sides line up, but I can't figure out how. Doing width: 100% obviously doesn't work because it ignores that the divs are not at left: 0.
As you can see, the divs escape the viewport:
div {
position: relative;
width: 100%;
height: 250px;
left: 50px;
top: 50px;
}
#div1 {
background-color: #4286f4;
}
#div2 {
background-color: #3267bc;
}
#div3 {
background-color: #244a87;
}
<div id="div1">
<div id="div2">
<div id="div3">
</div>
</div>
</div>
Or if you prefer: JSFiddle
How can I position the right side of each div at some specific distance from the right side of the screen with only CSS?
This is the expected output (the black rectangle around the outside represents the edges of the screen):
You might consider using margin-left instead of left.
With the CSS box model default, "width and height properties include the content, but does not include the padding, border, or margin," (box-sizing).
Also, block-level elements take up "the full width available", so width:100% isn't necessary (Block-level elements).
body {
margin: 20px 60px 20px 20px;
}
div {
position: relative;
}
div div {
height: 80px;
margin-left: 40px;
top: 40px;
}
#div1 {
background-color: #4286f4;
}
#div2 {
background-color: #3267bc;
}
#div3 {
background-color: #244a87;
}
<div id="div1">
<div id="div2">
<div id="div3"></div>
</div>
</div>
Quick and easy question. I'd like to have a floating box that stays in the bottom right of a div (in HTML). How would I do this with css?
Thanks! (attached is what I want it to look like)
Hope this will be what you are looking for.
.navBar {
height: 100px;
background-color: blue;
}
.div1 {
position: relative;
height: 200px;
}
.div1 .box {
position: absolute;
bottom: 40px;;
right: 40px;;
width: 200px;
height: 40px;
background-color: red;
}
.div2 {
height: 100px;
background: green;
}
<div class="main-container">
<div class="navBar"></div>
<div class="div1"><div class="box"></div></div>
<div class="div2"></div>
</div>
what you're looking for is:
position:absolute;
bottom:0;
right:0; which will position things relative to the positioned parent.Note that the parent element (div) needs to have its position set as well. Most people do position:relative;
The values bottom:0 and right:0 means to move it 0px away from the bottom of the parent and 0 px away from the right side of the parent.
See the following w3schools for further information:
https://www.w3schools.com/css/css_positioning.asp
https://www.w3schools.com/css/tryit.asp?filename=trycss_position_absolute
I have a DIV like below
<div class="parent">
<div class="fixedHt"></div>
<div class="fluidHt"></div>
</div>
I have written CSS like below
.fixedHt{
height:30px;
}
.fluidHt{
margin-top:30px;
}
I want to achieve the same with columns I can achieve with floats, how can I achieve this in rows?
jsFiddle Demo
HTML:
<div class="parent">
<div class="fixedHt small"></div>
<div class="fluidHt streched"></div>
</div>
CSS:
.streched {
position: absolute;
top: 30px;
bottom: 0;
left: 0;
right: 0;
}
For browsers who support CSS3, use calc function (no need for absolute positioning):
see this Fiddle
HTML:
<div class="parent">
<div class="fixedHt"></div>
<div class="fluidHt"></div>
</div>
CSS
.parent {
height: 300px;
width: 300px;
border: 3px solid #000;
}
.fixedHt
{
height: 30px;
background-color: red;
}
.fluidHt
{
height: calc(100% - 30px);
background-color: blue;
}
Tested on: IE10, IE10 using IE9 mode, FF, Chrome
Edit:
CSS3 is not supported in IE8, so instead you can use height: 270px; in the .fluidHt rule (that is only if the fixed height is not a problem for you) like this Fiddle [Works with all Broswers],
or you can apply a Script that fix the second div's height dynamically. like this Fiddle [Works with all Broswers]
I write here another answer, that don't use calc() and don't relay on the fixed height of the first div, so even if it's height is changing, the second will always span the rest of the container.
also: it has better browser support (IE8+ & all major browsers), and its Pure CSS.
Check this Working Fiddle
HTML: (same)
<div class="parent">
<div class="fixedHt"></div>
<div class="fluidHt"></div>
</div>
CSS:
.parent
{
height: 300px;
width: 300px;
border: 3px solid #000;
}
.parent:before
{
content: '';
height: 100%;
float: left;
}
.fixedHt
{
height: 30px; /*can be any height*/
background-color: red;
}
.fluidHt
{
background-color: blue;
}
.fluidHt:after
{
content: '';
clear: both;
display: block;
}
Here's the fiddle: http://jsfiddle.net/gBQqQ/
Here's the html:
<div id='testtexture'>
<div id='testinside'>
<div style='vertical-align: top;' class='test'></div>
</div>
</div>
And the css:
.test {
width: 50px;
position: relative;
margin-left: auto;
margin-right: auto;
min-height: 130px;
height:auto;
padding-bottom:50px;
background:blue;
}
#testtexture {
width: 100%;
position: relative;
top: 10px;
}
#testinside {
z-index: 3;
background:red;
position:relative;
}
I do not see why there is an issue. I expect either there is something obvious that I am missing, or there is an underlying issue which means I cannot make the red div go above the blue div- maybe because it is a child of the blue div?
Generally not the best idea to have a child div you want to appear behind it's parent. Usually you would take the child div outside the parent to do this. Nonetheless it is possible. Add z-index:-1 to the child div and remove position:relative from the parent.
HTML
<div id='testtexture'>
<div id='testinside'>
<div class="test"></div>
</div>
</div>
CSS
.test {
position: relative;
z-index: -1;
width: 50px;
margin: 0 auto;
height: auto;
min-height: 130px;
padding-bottom: 50px;
background: blue; }
#testinside { background: red; }
See fiddle: http://jsfiddle.net/gBQqQ/1/
If you use firebug, you can see div.test is still there in the correct position behind it's parent. As a side note, the styling vertical-align you had on a div won't do anything.
I want the "blue" container to always be 70px high, while the previous "green" div always max out the height available when the div is resized with javascript.
I've played around with it for a while without finding a proper solution. Help will be appreciated.
As promised, here's my answer.
absolute inside relative positioning is the easiest way to do this.
Live Demo
HTML:
<div id="parent">
<div id="left">height: 100%</div>
<div id="right">Content</div>
<div id="rightFooter">height: 70px</div>
</div>
CSS:
#parent {
position: relative;
height: 200px
}
#left, #right, #rightFooter {
position: absolute
}
#left {
width: 200px;
top: 0;
bottom: 0;
left: 0
}
#right {
top: 0;
right: 0;
bottom: 70px;
left: 200px;
overflow-y: auto
}
#rightFooter {
height: 70px;
right: 0;
bottom: 0;
left: 200px
}
Would something like this work?
Live Demo
Added an animation of the height so you can see the content extending.
Markup
<div id="parent">
<div class="left">
Lefty
</div>
<div class="right">
<div id="rightContent">
right Content
</div>
<div id="rightFooter">
Right Footer
</div>
</div>
<div class="clear"></div>
</div>
CSS
#parent{
height:300px;
}
.left{
float: left;
width: 33%;
background: red;
height:100%;
}
.right{
float : left;
width: 66%;
height:100%;
}
#rightContent{
height: 100%;
background: blue;
}
#rightFooter{
background: yellow;
height: 70px;
float: right;
width: 100%;
margin-top: -70px;
}
.clear{
clear:both;
}
Bah, before the comments come this is a partial solution, the text for the content area will bleed into the footer... looking at a solution for this, or someone else might be able to modify my markup/css to account for that.
Made an example for you here :)
you need to have a left floated div for the left content and a wrapper for the two other right divs, also floated left.
Take a look :)