0

I made a burger-menu with a pseudo-element. Now I need to show a menu, if a pseudo-element is hover. I can choose it this way :.header:hover:before But I can't force the menu to depends on it.

It's a structure of html:

<header class="header">
    <a href="#"><img src="img/logo.svg" alt="logo" class="logo logo_header"></a>
    <nav class="menu menu_header">It's a menu</nav>
    <div class="contacts-header">Some contacts here</div>
</header>

It's CSS:

.header:before {
        content: '';
        display: block;
        width: 40px;
        height: 30px;
        background-image: url("../img/icon-burger-menu.svg");
        background-repeat: no-repeat;
        background-position: center;
        background-size: contain;
    }
.menu {
        display: none;
    }

    .header:hover:before **???** {
        display: block;
    }

Is it possible to use a selector in this case?

enter image description here

4
  • What is the exact effect you want to achieve? I don't think you can set a :hover on a pseudo element directly with just css.
    – JasperZelf
    Commented Dec 18, 2019 at 8:45
  • I'd like to hover on a burger-menu and to show a menu block. I just don't want to use JS for such an easy case. It looks so easy, if a pseudo-element hover, change display:none on display: block for a menu.
    – Marina
    Commented Dec 18, 2019 at 8:50
  • 1
    Use a <button> containing an <img> with alt text and JavaScript to trigger then. Then it will work for people who can't use a mouse or who depend on screen readers. CSS hacks for "easy" interactions are just causes of accessibility problems.
    – Quentin
    Commented Dec 18, 2019 at 9:07
  • It's good advice, but I can't change html...
    – Marina
    Commented Dec 18, 2019 at 9:30

2 Answers 2

2

I think it would be best here to have your hamburger menu as an actual element, and not a pseudo element.

Pseudo elements have some limitations. One of them is that you can't add ':hover' states to them.

As an added bonus, I think that would be a more developer friendly solution. If an other developer needs to alter your code (of you a year from now) he can see the menu in your HTML code. HTML is for markup, CSS is for styling.

The :before and :after are generally used for minor additions to your code. Not for the important bits like navigation.

2
  • But I can to add a hover behavior ".header:hover:before", or what do you mean? could you explain please? I just can't to connect a pseudo-element with a menu-block. This version for an adaptive, I just don't want to change html-structure for an adaptive version, so I used a pseudo-element here
    – Marina
    Commented Dec 18, 2019 at 9:24
  • When you target .header:hover:before you target the :before element when you are hovered over the header. You're not targeting a :hover on only the :before element.
    – JasperZelf
    Commented Dec 18, 2019 at 10:04
0

You can add :hover to pseudo-elements but pseudo-elements can't affect to their own elements (not with CSS).

Here is a quick summary with the possibilities of elements and pseudo-elements hover states.

body {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 600px;
}

div {
  width: 100px;
  height: 100px;
  background-color: lightgreen;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  cursor: pointer;
}
div:not(.pseudo-hover):hover::before {
  background-color: pink;
}
div.pseudo-hover:hover::before {
  background-color: pink;
}
div.pseudo-hover::before, div.pseudo-hover-all::before {
  pointer-events: initial;
}
div.pseudo-hover-all {
  pointer-events: none;
}
div::before {
  content: '::before';
  position: absolute;
  width: 60%;
  height: 50%;
  background-color: aquamarine;
  top: 0;
  right: -60%;
  cursor: pointer;
  pointer-events: none;
}
<h3>Hover on element affects to pseudo-element</h3>
<div>Element</div>
<h3>Hover on pseudo-element or element affects to pseudo-element</h3>
<div class="pseudo-hover">Element</div>
<h3>Hover on pseudo-element affects only to pseudo-element</h3>
<div class="pseudo-hover-all">Element</div>

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.