Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Positioning an absolute element above other elements

So I am building this frontendmentor site and im having trouble positioning the shopping cart div on top of the rest of the elements. At first i tried using z-index but apparently that doesnt work with elements that have a position style. I used position relative on the parent and absolute on the child so it would always appear under the shopping cart icon. Is there any other way to force an element on top or should i change the way i position the div.

.right-container {
  padding: 1rem;
}

.account-container {
  display: flex;
  align-items: center;
  position: relative;
}

.cart-icon-container {
  margin-right: 2rem;
}

.cart-icon-container img {
  height: 30px;
  width: 30px;
  aspect-ratio: 1;
}

.shopping-cart-overlay {
  position: absolute;
  top: 5rem;
  right: 100px;
  width: 400px;
  border-radius: 10px;
  box-shadow: 0 1rem 10px rgba(0, 0, 0, 0.185);
}

.shopping-cart-content {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem 1rem;
}
<div class="right-container">
  <div class="account-container">
    <div class="shopping-cart-overlay">
      <div class="shopping-cart-header">
        <h4>Cart</h4>
      </div>
      <div class="shopping-cart-content">
        <p>Your cart is empty</p>
      </div>
    </div>
    <a href="/index.html" class="cart-icon-container">
      <img src="./images/icon-cart.svg" alt="cart">
    </a>
    <img class="avatar" src="./images/image-avatar.png" alt="profile-pic">
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well it does work. Read this to get a better understanding of how z-index works with position. You just couldn't notice it because the div was transparent.

Check out the snippet below. It's your exact code, but I changed the background and positioned the .shopping-cart-overlay element closer to the other elements, just to show you that it actually works.

.right-container {
  padding: 1rem;
}

.account-container {
  display: flex;
  align-items: center;
  position: relative;
}

.cart-icon-container {
  margin-right: 2rem;
}

.cart-icon-container img {
  height: 30px;
  width: 30px;
  aspect-ratio: 1;
}

.shopping-cart-overlay {
  position: absolute;
  //top: 5rem;
  //right: 100px;
  width: 400px;
  border-radius: 10px;
  box-shadow: 0 1rem 10px rgba(0, 0, 0, 0.185);
  
  /* Changes */
  top: 0;
  left: 125px;
  z-index: 99;
  background-color: red;
}

.shopping-cart-content {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem 1rem;
}
<div class="right-container">
    <div class="account-container">
      <div class="shopping-cart-overlay">
        <div class="shopping-cart-header">
          <h4>Cart</h4>
        </div>
        <div class="shopping-cart-content">
          <p>Your cart is empty</p>
        </div>
      </div>
      <a href="/index.html" class="cart-icon-container">
        <img src="./images/icon-cart.svg" alt="cart">
      </a>
      <img class="avatar" src="./images/image-avatar.png" alt="profile-pic">
    </div>
  </div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!