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 Minnick, the co-founder of WatzThis?, has overseen the development of hundreds of web and mobile projects for customers from small businesses to some of the world’s largest companies. A prolific writer, Chris has authored and co-authored books and articles on a wide range of Internet-related topics including HTML, CSS, mobile apps, e-commerce, e-business, Web design, XML, and application servers. His published books include Adventures in Coding, JavaScript For Kids For Dummies, Writing Computer Code, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW E-Commerce Designer Certification Bible, and 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, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.