I've been working on centering an element within its parent container, and I'm having trouble ensuring it remains centered responsively.
Here's what I have so far:
HTML
<div class="example">
<span class="element1">Element 1</span>
<span class="element2">Element 2</span>
</div>
CSS
.example {
position: relative;
}
.element1 {
width: 300px;
height: 200px;
background-color: #789845;
}
.element2 {
position: absolute;
top: 40%;
left: (I'm unsure of what to use here); I want this element to always stay centered regardless of the browser window's width.
width: 100px;
background-color: #EEEE78;
}
I'm struggling with determining the appropriate left
value, as setting a static value results in the element appearing slightly off-center on various browser widths. Can anyone offer guidance? Thank you in advance!