• google plus

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.

  1. Set the position property to relative.
  2. 
    h1 {
    	position:relative;
    }
    	
  3. Set one or more "offset" properties.

    The "offset" properties are top, right, bottom, and left. Their values can be specified in number of units (e.g., 10px) or percentage of the containing block (e.g., 20%).

  4. 
    h1 {
    	position:relative;
    	top: 70px;
    	left: 50px;
    }
    	
  5. The following example demonstrates relative positioning:
    <!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>
    This code renders the following:
    Relative Positioning
Author: Chris Minnick
Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.
About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, XML, Windows, Java, Adobe, HTML5, JavaScript, Angular, and much more. Check out our complete course catalog.