Relative Positioning with CSS
When elements are positioned relatively, they are positioned relative to where they would normally appear in the flow. Unlike absolutely positioned elements, relatively positioned elements do affect the positioning of subsequent sibling elements. Learn how to use relative positioning in the following steps.
- Set the
position
property torelative
. - Set one or more "offset" properties.
The "offset" properties are
top
,right
,bottom
, andleft
. Their values can be specified in number of units (e.g., 10px) or percentage of the containing block (e.g., 20%). - The following example demonstrates relative positioning:
This code renders the following:<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <style type="text/css"> h1 { position:relative; top:60px; left:50px; border:2px solid #006; padding:1px; background-color:#600; color:#eee; } #explanation { color:#006; font-weight:bold; font-size:1.2em; } #wrapper { width:600px; background-color:#def; border:1px solid #006; } </style> <title>CSS Relative Positioning</title> </head> <body> <div id="wrapper"> <h1>CSS Relative Positioning</h1> <h2>From the Left and the Top</h2> <div id="explanation"> <p>The h1 element on this page has been positioned relative to where it otherwise would be.</p> <p>All other content on the page (including these sentences) will show up in the same position it would have if the h1 had not been positioned at all.</p> </div> </div> </body> </html>
h1 {
position:relative;
}
h1 {
position:relative;
top: 70px;
left: 50px;
}