Search code examples
cssbackground-colorsemantic-uifomantic-ui

Struggling to deal with parent background-color/ child: before background-color conflict


I have managed to create a pseudo :before element to add to menu items which is working as I want however when I add a background color/ image to the parent div (bottom-nav) the pseudo element seems to break, can anyone shed some light?

Heads up I'm using fomantic UI (semantic UI fork)

.bottom-nav {
  background-color: #000;
  .menu {
    margin-bottom: 30px;
    .toggle-sidebar {
      margin-top: 25px;
      color: #2f2f2f;
    }
    .ui.dropdown {
      margin-top: 25px;
    }
    .item {
      font-family: Open Sans;
      font-size: 14px;
      font-weight: 700;
      color: #fff;
      padding: 10px;
    }
    .item:before {
      content: '';
      opacity: 1;
      position: absolute;
      border-radius: 2px;
      height: 20px;
      transform: translate(-50%, 0);
      background-color: #cd2122 !important;
      overflow: hidden;
      width: 0%;
      left: 50%;
      z-index: -1;
      transition: 0.3s ease;
    }
    .item:hover:before {
      opacity: 1;
      width: 100%;
    }
  }
}
<!-- You MUST include jQuery before Fomantic -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
<div class="bottom-nav">
  <div class="ui container">
    <div class="ui grid">
      <div class="sixteen wide column mobile hidden tablet hidden menu">

        <div class="ui dropdown">
          <a class="item" href="#">Link 1</a>
        </div>
        <div class="ui dropdown">
          <a class="item " href="#">Link 2</a>
        </div>
        <div class="ui dropdown">
          <a class="item" href="#">Link 3</a>
        </div>
        <div class="ui dropdown">
          <a class="item" href="#">Link 4</a>
        </div>
      </div>
    </div>
  </div>
</div>


Solution

  • I changed 2 things which should make the pseudo element visible again. Changed z-index to a non-negative number, so it will be visible in front of the background-color. And also gave it a width which isn't 0.

    [edit: updated code after further explanation by OP]

    .item {
        font-family: Open Sans;
        font-size: 14px;
        font-weight: 700;
        color: #fff;
        padding: 10px;
        position: relative;
      }
    
    .ui.dropdown:before {
        content: '';
        opacity: 1;
        position: absolute;
        border-radius: 2px;
        height: 20px;
        transform: translate(-50%, 0);
        background-color: #cd2122 !important;
        overflow: hidden;
        width: 50px;
        left: 50%;
        z-index: 1 ;   
        transition: 0.3s ease;
      }