Position in Html - html

I created a div tag at the beginning of my document in html with specified width and height. Now after closing the div tag, whenever I create other tags like the p tag they are always inside the div tag I created instead of at the bottom of the page.
Do I have to position every element?
How many times do I have to use the relative and absolute positioning in a document?

An absolute positioned element is out of the normal flow of document so it not takes room. You should set dimensions for its relative parent manually to simulate a normal flow. You may use a fixed height in pixels or use aspect ratio Technics for percent dimensions.
A sample of relative div with no size:
#test{
position:relative;
}
#abs{
position:absolute;
}
<div id="test">
<div id="abs">
This div will expand the relative parent with 0 pixels in height
</div>
</div>
This text has overlap with previous element.
A sample of relative div with fixed size:
#test{
position:relative;
height:60px;
background:#ff0000;
}
#abs{
position:absolute;
}
<div id="test">
<div id="abs">
This div is absolute but the parent has its own size
</div>
</div>
This text has no overlap with previous element.
A sample of relative div with aspect ratio:
#test{
position:relative;
padding-top:30%;
background:#ff0000;
}
#abs{
position:absolute;
top:0;left:0;right:0;bottom:0;
}
<div id="test">
<div id="abs">
This div is absolute but the parent has its own aspect ratio 1:0.3
</div>
</div>
This text comes after the previous element.
Finally I have to say that if you are a graphic designer and want to design a web page, you are not ought to design entire the page using relative and absolute elements. You have too many options to design a template using blocks, inline-blocks, tables , flex boxes etc. which regard the normal flow of document.

Related

Positioning text at bottom of a background image

I need to position a text element at the bottom of a background-image.
<div style="background-image: url('http://www.hdwallpapersinn.com/wp-content/uploads/2013/03/1325120512b60-45927-1.jpg'); height:100px">
<p class="caption">Test</p>
</div>
.caption{
position: absolute;
bottom:0;
}
As you can see here: http://jsfiddle.net/K98CR/
This will position the element at the bottom of the page and not at the bottom of the background image. My understanding of the "bottom" property is that it can be relative to its parent element, but it is not working in this case.
Anything I am missing? Or any other way to accomplish this? Using a margin-top is not an option as I am creating a fluid layout and that would depend on screen ratio and I do not want that.
What I want is a way to position an element at the bottom of its parent element. In this case, I want to position text at the bottom of an image styled with "background-image".
Using postion: relative; to your background image div will fix it.
<div style="background-image: url('http://www.hdwallpapersinn.com/wp-content/uploads/2013/03/1325120512b60-45927-1.jpg'); height:100px; position: relative;">
<p class="caption">Test</p>
</div>
LIVE EXAMPLE
w3schools:
An absolute position element is positioned relative to the first
parent element that has a position other than static. If no such
element is found, the containing block is <html>

Two Stacked divs and controlling heights

I am trying to stack two divs A and B.
Div A - will be scrollable but its height needs to be determined by the div underneath it, Div B so if the content in Div B changes, and it's height changes the height of Div A also changes.
Div B - needs to be aligned to the bottom of page on top of a absolute positioned footer. Its content needs to be aligned to the bottom.
I've tried using position relative and float by wrapping these divs in a wrapper, but the whole thing breaks when I try to keep the Div B aligned or positioned absolutely above the footer.
I've got a feeling this needs to go back to basics, any help would be greatly appreciated
thanks
Here's a basic example. I think I have correctly understood your requirement. This example has them appear to be stacked but in the HTML they are not actually stacked, they are nested. I wasn't sure if you could allow that in your solution but fingers crossed.
Example: http://jsfiddle.net/jyR2A/1/
CSS:
#divA {overflow-y:scroll;position:absolute;height:100%;top:-100%;background:green;width:100%;}
#divB {position:absolute;bottom:0;background:blue;width:100%;color:white;}
HTML:
<div id="divB">
<!-- Div A is nested so we can use divB's height -->
<div id="divA">
</div>
<!-- Div B content -->
<div id="divBinnerContent">
Line 1 <br />
Line 2 <br />
..Keep adding more lines to test how it works <br />
</div>
</div>
How it works:
divB is the parent element defining the height of divA. So if we set divB position relative or absolute and place divA inside then we can set divA's height to 100% to give it the height of parent element divB.
Now to position divA we make sure it has position:absolute and set top:-100% which will move it up the same distance as the height of its container divB. Position absolute not only allows us to position it correctly but it also removes it from affecting the height of its parent, divB.
And the content for divB I have made a nice container for it but it is not neccessary. Simply put it anywhere inside divB (but not inside divA) and it will be OK.
You can use the content to define the height,as I have, or use an absolute height set in CSS.
Hope this is what you were after.
Le-roy
I managed to achieve this with help from this question and fiddle.
Stack div elements in css vertically (with dynamic height)
http://jsfiddle.net/nCrEc/334/
Essentially the answer was giving my Div A a height without using the height parameter but instead using absolute positioning on top and bottom. Which meant changes to Div B changed the location of the Div A's bottom (oo er) which pushed the middle div up whenever another populates the bottom area.
<div class="con">
<div class="top"></div>
<div class="middle"></div>
<div class="bottom"></div>
</div>
then using this CSS
.con {
width:200px;
top:50px;
bottom:0;
left:0;
position:absolute;
background:#ff0;
}
.top {
width:200px;
height:20px;
position:absolute;
top:0;
left:0;
background:#f60;
}
.bottom {
width:200px;
height:50px;
position:absolute;
bottom:0;
left:0;
background:#f60;
}
.middle {
overflow-y:auto;
min-height:1px;
position:absolute;
bottom:50px;
top:20px;
left:0;
background:#06f;
}

Elements of postion absolute shifts on browser resize

When I re-size browser the elements whose position is set absolute does not changes according to other elements. if i place absolute divs inside relative then black box is not shown.
<div id="outer"></div>
<div id="blackbox"></div>
<div class="form"></div>
#outer{
width:1250px;
height:auto;
margin:auto;
position:relative;
}
#blackbox{
width:100%;
height:100%;
background:#000;
opacity:0.5;
position:absolute;
z-index:10;
left:0;
top:0;
}
.form{
width:500px;
height:350px;
z-index:20;
background:#FFF;
position:absolute;
top:100;
left:400;
}
Used to this
<div id="outer">
<div class="blackbox"></div>
<div class="form"></div>
</div>
Define to parent div position relative and child div position absolute
Live Demo
More about Position
Absolute and relative positioning is relative to a "containing block"
Absolute blocks are placed relative to their "containing block". They define a new "containing block" for their children.
Relative blocks are placed relative to their in-flow position. They also define a new "containing block" for their children.
So, when you place an absolute block X inside a relative block Y you are saying "put Y in the flow of the page, shift it around a bit and then fix X relative to Y's new position".
Looking at the code - you have set outer to have a height of auto and blackbox to have a height of 100%. So the parent's height is based on the child's height, and the child's height is based on the parent's height! So they collapse to 0px. That is why you are not seeing blackbox. Try #outer {height: 1250px;} to see things...
Hope that helps, if not then read the specification - that is always the ultimate answer to all these questions (it's how I learned CSS)!
http://www.w3.org/TR/CSS2/visuren.html
You have a typo in your css. Change from #blackbox to .blackbox
<div id="outer">
<div class="blackbox"></div>
<div class="form"></div>
</div>
#blackbox -> wrong
use .blackbox { }
Also remember that absolute positioned elements must always be inside relative positioned element

Assigning CSS width to a position:fixed element

I'd like a fixed element's width to match that of the div placed immediately below it. Imagine a header and a main content div. A problem in matching their widths occurs when the header and content divs are nested inside an outer div. In this scenario the % widths of each no longer match their parents width (e.g.,<body> tag) and the fixed element's width is based on something which is confusing me.
To better explain what I mean, contrast these two js fiddles:
http://jsfiddle.net/2dudX/4/
vs.
http://jsfiddle.net/2dudX/10/
here's the code for each:
<div id="fixed"></div>
<div id="content"></div>​
#fixed{ position:fixed; z-index:2; width:90%;
height:25px; background:yellow;}
#content{ width:90%; height:300px; background:red}
vs.
<div id="main">
<div id="fixed"></div>
<div id="content"></div>
</div >
#main{ width:95%}
#fixed{ position:fixed; z-index:2; width:90%;
height:25px; background:yellow;}
#content{ width:90%; height:300px; background:red}
Note only in jsfiddle #1 do the yellow and red divs widths match up regardless of how you resize the browser. Unfortunately, jsfiddle#2 is more of a real world scenario and I'm wondering how to correct the id="fixed" div such that its width also matches up with id="content" div.
Thoughts?
You can to it this way FIDDLE (to set % relative to the #main)
fixed element's dimensions always is calculated relative to the root element, so you need to reset %-unit accordingly
in this particular case you need to set:
#fixed {
width: 85.5%;
}
It is case #main is 95%, your static element is 90% relative to the main. So you need to calculate its width towards the root element (1 * .95 * .9 = .855)
Easy one my friend. Fixed width elements are yanked from their parents and are now relative in width to the window, so in both situations the fixed div is always relative to the size of the window, but when in a parent container with a width other than 100% the fixed element will remain relative to the window width but the non-fixed position element is now relative to the parent width. So the non-fixed element became 90% of the 95% of the window while the fixed element remained a constant 90% of the window only.
Edit:
If you wish to match the widths you can use jquery like this:
$(function(){
$('#fixed').width($('#content').outerWidth());
});

Absolutely positioned element within relative parent. Need parent to grow to the width of the element inside

I have a div that I want to float other divs in. Within the floating divs, I need the content to be positioned at the bottom because some of them have higher content than others and I don't want them to align on top. I have tried something like this:
.right {
float:right;
}
.left {
float:left;
}
.actionCell {
margin:2px 5px 2px 5px;
border:1px solid Black;
height:100%;
position:relative;
}
HTML:
<div style="position:relative;">
<div id="HeaderText" class="left actionCell"><span style="position:absolute;bottom:0px">Header Text</span></div>
<div id="SelectedItems" class="left actionCell">10 Selected Items</div>
</div>
The one with the span on the inside works, however I need its parent container to grow to its width. Any ideas how to solve this problem without using tables?
Unfortunately absolutely positioned elements no longer interact with their parent, so to get the effect you want, you'd need a hack. Fortunately, web development abounds with hacks:
CSS only, using a prop element
Create a visibility: hidden element inside your <div id="HeaderText"> that has the same content as your absolutely positioned <span>. This will force the width of <div id="HeaderText"> to contain your absolute <span>.
On the downside, this duplicates content that anyone without CSS (and search engines) will see.
jQuery to get absolute child's width
Set the width <div id="HeaderText"> to that of its absolute <span> child. This gets around the duplicate content, but your users need Javascript.
You can see them both in action here.