CSS relative position with right side relative to viewport - html

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>

Related

css - relative DIV has no height

I do not understand whats wrong with my code. I mean, section element has the height, display value of my DIV element is definitly block and i really dont know how it works and how to combine these two elements differently positioned. Please give me your solutions and advices to learn something new today.
div {
position: relative;
margin: 0 30%;
}
div section {
position: absolute;
top: 0;
right: 0;
left: 0;
height: 100px;
background-color: yellow;
}
hr {
height: 2px;
background-color: blue;
}
<div>
<section></section>
</div>
<hr>
You want your hr on the bottom of the first div, right ?
However, this is not working because the parent div have an default height: auto property.
This mean that the parent div will have the height of his children.
When you set a position: absolute on a child, you are breaking this system.
The parent will no longer take care of his child.
So, if you want to make it works, you have two solutions:
- set a custom height (height: 100px) on the parent div (not good)
- remove the absolute position on the child section (default :position: relative)
div {
position: relative;
margin: 0 30%;
}
div section {
height: 100px;
background-color: yellow;
}
hr {
height: 2px;
background-color: blue;
}
<div>
<section></section>
</div>
<hr>
Your element has height set to AUTO. If you want to change your div height you need to write this in css.
div {
position: relative;
margin: 0 30%;
height: 200px;
background-color: red;
}
I think This is because your div has no height itself that's why it is not visible and it is increasing according to its child element which is section and is in absoulute position. I am not sure what you are up to but If you want to show the section inside the div along with the div's height the you must include the css for your div .
I provided an assumption solution for you hope it helps you
div {
position: relative;
margin: 0 30%;
background-color: green;
height: 150px;
}
section {
position: absolute;
top: 50px;
right: 0;
left: 0;
height: 50px;
background-color: yellow;
}
div hr {
height: 10px;
background-color: red;
}
<html>
<head>
</head>
<body>
<div>
<p>your div</p>
<hr>
<section>your section</section>
</div>
</body>
</html>

right side of fixed div overflow its parent

I am trying to put a position:fixed div inside an another div. I want a fixed div which has a width:100%; so it will be great for mobile and desktop at the same time.
Here is my JSfiddle
SF wants some code:
<div id="container">
<div id="item">This div is good div</div>
<div id="fixed">Right side of this div overflow its parent!!! </div>
</div>
An element with position: fixed; ignores the parent size because it is relative only to the viewport:
MDN:
Fixed positioning is similar to absolute positioning, with the exception that the element's containing block is the viewport.
You can:
Try giving it position: absolute; and set the container to position: relative;.
Use position: fixed; and set the size explicitly.
You can use the calc() method to adapt the viewport size. Just subtract right and left margin from the 100%:
Edit: I added a min-height to the body to see the "fixed-effect" on scrolling
body {
margin: 0;
min-height: 1000px;
}
#container {
margin: 10px;
background: black;
color: white;
}
#item {
height: 50px;
width: 100%;
}
#item {
background: blue;
}
#fixed {
height: 50px;
width: calc(100% - 20px);
background: green;
position: fixed;
}
<div id="container">
<div id="item">Normal div</div>
<div id="fixed">Fixed div</div>
</div>

Responsive height of div issue

One example is better than a thousand words, so here you go:
https://jsfiddle.net/jesuxapo/os53cyc1/
As you can see, the height is responsive, but not completely. The problem is the <div id="k"> with fixed height of 150px. Try to play with it and I think you'll understand exactly what I mean. I want to get rid of this 'problem' somehow.
I could use the calc() of the css3, however it's not cross-browser(especially android and IE8-9).
Perhaps there's some other solution for this using html and css languages?
You may use the display:table properties (IE8 and later):https://jsfiddle.net/os53cyc1/1/
it will grow if content is more than 100vh all together
html, body {
padding: 0;
margin: 0;
height: 100%;
background: #333;
font-weight: bold;
color: #fff;
}
body {
display:table;
width:100%;
}
body>div {
display:table-row;
}
div {
border: solid 2px #FFFF00;
}
div#a {
position: relative;
background: #800000;
width: 100%;
height: 100%;
}
div#b {
position: absolute;
top: 0;
}
div#c {
position: absolute;
bottom: 0;
}
div#k {
height: 150px;
background: #008000;
}
<div id="k">
Hello, I'm K and I just broke your code
</div>
<div id="a"><br><br><br><br>
This is relative div with height of 100% and max-height of 500px
<div id="b">
This div is aligned to the top of the Red div
</div>
<div id="c">
This div aligned to bottom of the Red div
</div>
</div>

Setting an absolute positioned div to overlap one regular div but not another

I have a page that's set up with a split pane - top and bottom (divs using calc() for their height), with the top div having an absolutely positioned div that overlays the top div. HTML + CSS below from https://jsfiddle.net/b2mb79ev/1/
<div id="everything">
<div id="top">
<div id="free"></div>
</div>
<div id="bottom"></div>
</div>
</div>
#everything {
width: 400px;
height: 400px;
}
#top {
height: calc(50%);
width: 100%;
background: blue;
}
#free {
position: absolute;
top: 50px;
left: 300px;
height: 200px;
width: 50px;
background: yellow;
border: 3px solid black;
}
#bottom {
height: calc(50%);
width: 100%;
background: rgba(0,255,0,0.6);
}
However I want the absolutely positioned div not to intrude into the bottom div. I want the bottom div to always be in a 'higher' layer. In the jsfiddle I don't want the yellow bit going into the green bit.
But from what I can gather the top and bottom divs always have the same level because they are just regularly flowed elements and won't take any notice of z-index. I can use z-index to have the absolutely positioned div above both or beneath both, but not one above and one underneath?
Is there a way to do what I'd like?
You just need to set a non-static position (e.g. position:relative) on the #bottom div to give the stacking context you're after.
jsFiddle example
#bottom {
height: calc(50%);
width: 100%;
background: rgba(0, 255, 0, 0.6);
position:relative;
}

Why doesn't the absolute positioned div start from the top?

Consider the html and css in this codepen link.
#box2 in the page has been positioned absolute and has a left offset of 200px, so moves 200 px to the right. However, the top offset also gets set to 200px somehow, but if I correctly understand absolute positioning, it should be positioned relative to its parent (body in this case), and so it should have top offset 0.
Can you explain why this happens?
With your css .box you set all boxes 1 - 3 as position: relative.
Your css #box2 sets a absolute positioning on box 2. The css selector for an id (#box2) is more specific than a class selector (.box) and therefore positioning: absolute in #box2 overrules the positioning: relative in .box
You do not define a top property/value in your css for the #box2. That means it will flow with the other tags except for the left position.
Because a div is displayed as block it will display right below #box1. It will be drawn 200px from the left because of your css left: 200px;
Try to change your css for #box2 into something like this
#box_2 { background: #44accf; left: 150px; top: 100px; position: absolute; }
and play around with the left and top values, to understand what the absolute positioning is doing.
You haven't given it a top: value - so it's placing itself where it was if it was relatively positioned/default place (As BoltClock has said, this is known as the static position).
For the purposes of this section and the next, the term "static
position" (of an element) refers, roughly, to the position an element
would have had in the normal flow.
More precisely, the static position
for 'top' is the distance from the top edge of the containing block to
the top margin edge of a hypothetical box that would have been the
first box of the element if its specified 'position' value had been
'static' and its specified 'float' had been 'none' and its specified
'clear' had been 'none'.
~W3 Spec
Here is a basic example:
html,
body {
margin: 0;
padding: 0;
}
.box {
position: relative;
width: 200px;
height: 200px;
}
#box_1 {
background: #ee3e64;
}
#box_2 {
background: #44accf;
left: 200px;
position: absolute;
top: 0;
}
#box_3 {
background: #b7d84b;
}
<div id="box_1" class="box"></div>
<div id="box_2" class="box"></div>
<div id="box_3" class="box"></div>
As you can see, this would also be able to do this using the display:inline-block property (since you're removing the defaulted 'block' styling that take's up 100% of the width), which you then wouldn't need to worry about the absolute at all:
html,
body {
margin: 0;
padding: 0;
}
.box {
position: relative;
width: 200px;
height: 200px;
display: inline-block;
}
#box_1 {
background: #ee3e64;
}
#box_2 {
background: #44accf;
}
#box_3 {
background: #b7d84b;
}
<div id="box_1" class="box"></div>
<div id="box_2" class="box"></div>
<div id="box_3" class="box"></div>
If, however, you needed it to only have two squares wide, you might want to wrap it in a container div width and set a width:
html,
body {
margin: 0;
padding: 0;
}
.box {
position: relative;
width: 200px;
height: 200px;
display: inline-block;
}
#box_1 {
background: #ee3e64;
}
#box_2 {
background: #44accf;
}
#box_3 {
background: #b7d84b;
}
.container{
width:500px;
}
<div class="container">
<div id="box_1" class="box"></div>
<div id="box_2" class="box"></div>
<div id="box_3" class="box"></div>
</div>
So, you were right to think that the absolutely positioned element is aligning to the next available parent with position:relative (which, just so happens to be the body, since you haven't declared one), you have just missed the use of top to position it where you want vertically, and so is defaulting to where 'it would be otherwise' - which is the baseline (which is at the bottom by default in divs)