<main class="main container">
<section class="section relative">
<h2><code>position: relative;</code></h2>
<div class="box"></div>
<p>The above box has <strong><code>position: relative;</code></strong></p>
</section>
<p>Note how the <code>div.relative</code> element appears in the normal flow of document.</p>
<p>Then the element is placed <em>relative to itself</em> depending on the values of <code>top, right, left, bottom</code>.</p>
<p>In the above example, the box is placed <br />
<strong style="display: inline-block; margin-top: 0.1rem; padding: 0.5rem 1rem; background-color:yellowgreen;">
<code>
div.relative {
position: relative;
left: 50%;
top: 5em;
}
</code>
</strong>
</p>
</main>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
height: 100%;
}
.container {
margin: auto;
max-width: 900px;
padding: 1rem;
}
.main > p {
margin: 1rem 0 0 0;
text-align: center;
}
.section {
padding: 1rem;
margin: 2rem auto;
max-width: 400px;
background-color: lightskyblue;
/* text-align: center; */
color: white;
max-width: 100%;
}
.section h2 {
font-size: 1.3rem;
background-color: darkgreen;
padding: 1rem;
}
.section p {
font-size: 1.1rem;
background-color: darkgreen;
padding: 1rem;
}
.section .box {
background-color: navy;
width: 200px;
height: 200px;
margin: 2rem 0;
}
section.relative .box {
position: relative;
left: 50%;
top: 5em;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.