Module - 2 CSS Part-2
Module - 2 CSS Part-2
CSS Layout
The position property specifies the type of positioning method used for an
element (static, relative, fixed, absolute or sticky).
The position Property
The position property specifies the type of positioning method used for an
element.
There are five different position values:
• static
• relative
• fixed
• absolute
• sticky
Elements are then positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set first.
They also work differently depending on the position value.
Positioning
• position: defines the positioning of the element in the page
content flow
• The value is one of:
• static (default)
• relative – relative position according to where the element
would appear with static position
• absolute – position according to the innermost positioned
parent element
• fixed – same as absolute, but ignores page scrolling
3
position: static
Static positioned elements are not affected by the top, bottom, left,
and right properties.
<p>With absolute positioning, an element can be placed anywhere on a page. The heading below is placed
100px from the left of the page and 150px from the top of the page.</p>
</body>
</html>
<html><head><style>
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;}
</style>
</head>
<body>
<h2>position: absolute;</h2>
<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of
positioned relative to the viewport, like fixed):</p>
<div class="relative">This div element has position: relative;
<div class="absolute">This div element has position: absolute;</div>
</div>
</body>
</html>
<html>
<head>
<style>
#parent1 {
position: static;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child1 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent2 {
position: relative;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child2 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent3 {
position: absolute;
border: 1px solid blue;
width: 300px;
height: 100px;
top: 750px;
right: 15px;
}
#child3 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent4 {
position: fixed;
border: 1px solid blue;
background-color: rgba(255,200,200,0.5);
width: 300px;
height: 100px;
bottom: 0;
left: 0;
right: 0;
}
#child4 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
</style>
</head>
<body>
<h2>position: static;</h2>
<p>The Parent1 element has position: static, and will remain in the natural flow of the page.
It will NOT act as anchor point for the absolutely positioned Child1 element:</p>
<div id="parent1">Parent1: position: static.
<div id="child1">Child1: position: absolute, right: 15px, top: 70px</div>
</div>
<h2>position: relative;</h2>
<p>The Parent2 element has position: relative, and will remain in the natural flow of the page.
It will also act as anchor point for the absolutely positioned Child2 element:</p>
<div id="parent2">Parent2: position: relative.
<div id="child2">Child2: position: absolute, right: 15px, top: 70px</div>
</div>
<h2>position: absolute;</h2>
<p>The Parent3 element has position: absolute, and will NOT remain in the natural flow of the page.
It will position itself according to the closest positioned ancestor.
It will also act as anchor point for the absolutely positioned Child3 element:</p>
<div id="parent3">Parent3: position: absolute, top: 750px, right: 15px.
<div id="child3">Child3: position: absolute, right: 15px, top: 70px</div>
</div>
<h2>position: fixed;</h2>
<p>The Parent4 element has position: fixed, and will NOT remain in the natural
flow of the page.
It will position itself according to the viewport.
It will also act as anchor point for the absolutely positioned Child4 element:</p>
<div id="parent4">Parent4: position: fixed, bottom: 0, left: 0, right: 0.
<div id="child4">Child4: position: absolute, right: 15px, top: 70px</div>
</div>
</body>
</html>
position: sticky
You must also specify at least one of top, right, bottom or left for sticky
positioning to work.
<html><head><style>
div.sticky {
position: sticky;
top: 0;
padding: 5px;
background-color: #cae8ca;
border: 2px solid #4CAF50;}
</style></head>
<body>
<p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>
<p>Note: IE/Edge 15 and earlier versions do not support sticky position.</p>
<div class="sticky">I am sticky!</div>
<div style="padding-bottom:2000px">
<p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
<p>Scroll back up to remove the stickyness.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et
eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et.
Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et
eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et.
Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div></body></html>
Overlapping Elements
This is a heading
Because the image has a z-index of -1, it will be placed behind
the text.
<html>
<head>
<style>
img {
position: absolute;
left: 10px;
top: 10px;
z-index: -1;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<img src=“fish.jpg" width="100" height="140">
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>
</body>
</html>
Positioning Text In an Image
Image Text