1

I have header in such a way that I would like to hide my image and span's in my pheader div if "banner" exists. Is there a quick way to do this using CSS selectors? Below is the HTML code.

<header>
  <div id="banner">
    <!-- main content -->
  </div>
  <div class="pheader">
    <div class="user-panel">
      <div id="hey-user" class="d2c-user-panel">
        <img src="../images/defaultHeadshot_lg.png" class="userimg">
        <!-- Hide This -->
        <span class="caret" id="down-arrow"></span>
        <span class="hey-user d2c-header-username"><b>Hello</span>
      </div>
</header>
Neophile
  • 5,660
  • 14
  • 61
  • 107

2 Answers2

6

Yes.

#banner + .pheader img,
#banner + .pheader span {
display:none;
}

This selector only applies if .pheader is directly after #banner.

You might find this Tutsplus article useful:

The 30 CSS Selectors You Must Memorize

.pheader {
  padding: 1em;
  background: lightblue;
}
#banner + .pheader img,
#banner + .pheader span {
  display: none;
}
<header>
  <div id="banner">
    <!-- main content -->
  </div>
  <div class="pheader">
    <div class="user-panel">
      <div id="hey-user" class="d2c-user-panel">
        <img src="http://lorempixel.com/output/animals-q-c-100-100-3.jpg" class="userimg" />
        <span class="caret" id="down-arrow">Arrow</span>
        <span class="hey-user d2c-header-username"><b>Hello</b></span>
      </div>
    </div>
  </div>
</header>

It's probably worth noting that I've had to close a few divs in your original HTML structure which were missing from your code.

Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

Try this. Link https://jsfiddle.net/7bv53xqd/

#banner + .pheader img {
  display: none;
}

#banner + .pheader span {
  display: none;
}
Anandh Sp
  • 787
  • 6
  • 24