In the following codepen - https://codepen.io/tanmaylodha/pen/MWKXJWW
CSS:Line-26; the left:50%
is not working correctly.
But if I set display:inline-block
on the containing block .section-first a
of absolutely positioned element .badge
then it works fine.
<section class="section section-first">
<a href="#">
<h1 class="badge">Recommended</h1>
<h1 class="plus-plan">Our PLUS Plan</h1>
<h2>The most popular choice of our customers.</h2>
<p>
Benefit from increased storage and faster support to ensure that
your mission-critical data and applications are always available!
</p>
</a>
</section>
.section {
color: #6c6164;
background-color: #f7fafd;
padding: 1.563rem;
margin-bottom: 1.563rem;
border: 5px solid #fca156;
margin-right: 12.5rem;
box-shadow: inset 5px 5px 10px 2px #4fbf99;
}
.section-first {
margin-top: 8rem;
}
.section-first a {
position: relative;
}
.badge {
font-family: "Red Hat Display";
background-color: #60a7bd;
padding: 0.625rem;
border-radius: 5px;
margin: 0%;
position: absolute;
left: 50%;
}
.section h1.badge {
color: white;
}
.section-first .plus-plan {
margin-top: 50px;
}
.section-highlighted {
margin-left: 200px;
margin-right: 0px;
box-shadow: inset 5px 5px 10px 2px #4fbf99, 5px 5px 10px 2px #60a7bd;
text-align: right;
}
.section:hover {
border-color: #ff943c;
}
.section a {
text-decoration: none;
}
Now check this codepen - https://codepen.io/tanmaylodha/pen/jOWKyZP
But here the results are different. .child
being absolutely positioned element is getting correctly positioned after 50% width of its containing block .parent
<a href="" class="parent">
I am a Parent
<div class="child">
I am a child
</div>
</a>
.parent {
position: relative;
background-color: chocolate;
}
.child {
position: absolute;
background-color: darkgreen;
left: 50%;
}
In both the above Codepen, the containing block(being positioned relative) is always an inline element, then why the results are different?