Home > database >  Pseudo-element layers under element above it
Pseudo-element layers under element above it

Time:09-01

I'm trying to show a notification overlay on a div. My attempt thus far has failed to get the overlay on top of everything, while keeping it relative to it's related button.

Perhaps using ::after isn't the right approach? Any suggestions would be appreciated.

.user-container {
  display: flex;
  flex-flow: column;
  width: 300px;
  float: left;
}

.user-welcome-message-container {
  border-bottom: solid 2px #ffe398;
  display: flex;
  float: right;
  text-align: right;
  padding-top: 15px;
  justify-content: flex-end;
}

.user-action-btns-container {
  display: flex;
  justify-content: flex-end;
}

#user-btn-1:hover {
  cursor: pointer;
  background-color: #c6e5ff;
  border-top: solid 0px;
  border-left: solid 1px #5babff;
  border-bottom: solid 1px #5babff;
  border-right: solid 1px #5babff;
  box-sizing: border-box;
  transition: 0.2s;
}

#user-btn-1 {
  font-family: sans-serif;
  font-style: normal;
  position: relative;
  width: 110px;
  font-size: 0.8em;
  color: black;
  background-color: #fff3d3;
  padding: 7px;
  margin-right: 15px;
  text-align: center;
  box-sizing: border-box;
  border-top: solid 0px;
  border-left: solid 1px #e8cb2a;
  border-bottom: solid 1px #e8cb2a;
  border-right: solid 1px #e8cb2a;
  border-radius: 0px 0px 5px 5px;
  box-shadow: 0px 0px 5px rgb(0 0 0 / 20%);
  clip-path: inset(0px -5px -5px -5px);
  transition: 0.2s;
}

#favorites-btn-text::after {
  content: "7";
  position: absolute;
  top: -5px;
  left: 100px;
  border-radius: 10px;
  padding: 4px;
  background-color: #a4dbff;
}
<div >
  <div >
  </div>
  <div >
    <div id="user-btn-1" title="Favorites">
      <i aria-hidden="true"  title="Your favorites"> <span id="favorites-btn-text">Favorites</span></i>
    </div>
  </div>
</div>

CodePudding user response:

Replace left: 100px; with right: 0;, apply a transform moving the element right by half its width (to accomodate for two-digit numbers as well) using transform, and make sure there's enough gap between your buttons. Consider using the column-gap CSS property to control the gap.

Also move the content to a data-attribute: data-notification-count.

.user-container {
  display: flex;
  flex-flow: column;
  width: 300px;
  float: left;
}

.user-welcome-message-container {
  border-bottom: solid 2px #ffe398;
  display: flex;
  float: right;
  text-align: right;
  padding-top: 15px;
  justify-content: flex-end;
}

.user-action-btns-container {
  display: flex;
  justify-content: flex-end;
}

#user-btn-1:hover {
  cursor: pointer;
  background-color: #c6e5ff;
  border-top: solid 0px;
  border-left: solid 1px #5babff;
  border-bottom: solid 1px #5babff;
  border-right: solid 1px #5babff;
  box-sizing: border-box;
  transition: 0.2s;
}

#user-btn-1 {
  font-family: sans-serif;
  font-style: normal;
  position: relative;
  width: 110px;
  font-size: 0.8em;
  color: black;
  background-color: #fff3d3;
  padding: 7px;
  margin-right: 15px;
  text-align: center;
  box-sizing: border-box;
  border-top: solid 0px;
  border-left: solid 1px #e8cb2a;
  border-bottom: solid 1px #e8cb2a;
  border-right: solid 1px #e8cb2a;
  border-radius: 0px 0px 5px 5px;
  box-shadow: 0px 0px 5px rgb(0 0 0 / 20%);
  clip-path: inset(0px -5px -5px -5px);
  transition: 0.2s;
}

#user-btn-1::after {
  content: attr(data-notification-count);
  position: absolute;
  top: 0;
  right: 0;
  border-radius: 10px;
  padding: 4px;
  background-color: #a4dbff;
  transform: translate(50%, -5px);
}
<div >
  <div >
  </div>
  <div >
    <div id="user-btn-1" title="Favorites" data-notification-count="14">
      <i aria-hidden="true"  title="Your favorites"> <span id="favorites-btn-text">Favorites</span></i>
    </div>
  </div>
</div>

CodePudding user response:

If you want to show the notifications above all, you can use z-index property.

  • Related