0

I have some dropdown in css that is now made in css like this

.menu ul ul,
.menu .mega-menu,
.menu .mega-menu ol li {
    opacity: 0;
    visibility: hidden;
}


.menu li:hover > ul,
.menu li:hover > .mega-menu,
.menu li:hover > .mega-menu ol li {
    opacity: 1;
    visibility: visible;
}

Can i change it css to be on click maybe with :target?

Tried with

.menu li:focus > ul,
.menu li:focus > .mega-menu,
.menu li:focus > .mega-menu ol li {
    opacity: 1;
    visibility: visible;
}

Does not work

           <div class="menu menu-style">
                <ul class="menu brand-background">
                    <li class="menu-active">A</li>
                    <li>B</li>
                    <li>
                        <a href="#">C</a>
                        <!-- Full Width Mega-Menu / Start -->
                        <div class="mega-menu full-width">
                            <div class="col-md-1">
                                <h4>Content</h4>
                                <ol>
                                    <li><a href="#">1</a></li>
                                    <li><a href="#">2</a></li>
                                    <li><a href="#">3</a></li>
                                    <li><a href="#">4</a></li>
                                    <li><a href="#">5</a></li>
                                    <li><a href="#">6</a></li>
                                    <li><a href="#">7</a></li>
                                </ol>
                            </div>
                        </div>
                        <!-- Full Width Mega-Menu / End -->
                    </li>
                    <li><a href="#">D</a></li>                            
                </ul>
            </div>
1
  • paste your html as well Commented Dec 8, 2014 at 10:58

1 Answer 1

2

Solution using the checkbox hack

FIDDLE (Click on 'C' to show/hide the content))

.mega-menu {
  opacity: 0;
  visibility: hidden;
}
label {
  cursor: pointer;
}
#cb {
  display: none;
}
#cb:checked ~ .mega-menu {
  opacity: 1;
  visibility: visible;
}
<div class="menu menu-style">
  <ul class="menu brand-background">
    <li class="menu-active">A</li>
    <li>B</li>
    <li>
      <input type="checkbox" id="cb" />
      <label for="cb">C</label>

      <!-- Full Width Mega-Menu / Start -->
      <div class="mega-menu full-width">
        <div class="col-md-1">
          <h4>Content</h4>

          <ol>
            <li><a href="#">1</a>
            </li>
            <li><a href="#">2</a>
            </li>
            <li><a href="#">3</a>
            </li>
            <li><a href="#">4</a>
            </li>
            <li><a href="#">5</a>
            </li>
            <li><a href="#">6</a>
            </li>
            <li><a href="#">7</a>
            </li>
          </ol>
        </div>
      </div>
      <!-- Full Width Mega-Menu / End -->
    </li>
    <li><a href="#">D</a>
    </li>
  </ul>
</div>

Solution using :target

1) Add an id to the element in your markup you want to target. eg give the mega-menu an id="dropdown"

2) Target the id of the element in the href of the link. eg <a href="#dropdown">

3) Use the :target selector in your css like:

#dropdown:target {
    opacity: 1;
    visibility: visible;
}

DEMO (Click on 'C' to show the content)

#dropdown {
  opacity: 0;
  visibility: hidden;
}
#dropdown:target {
  opacity: 1;
  visibility: visible;
}
<div class="menu menu-style">
  <ul class="menu brand-background">
    <li class="menu-active">A</li>
    <li>B</li>
    <li> <a href="#dropdown">C</a>

      <!-- Full Width Mega-Menu / Start -->
      <div id="dropdown" class="mega-menu full-width">
        <div class="col-md-1">
          <h4>Content</h4>

          <ol>
            <li><a href="#">1</a>
            </li>
            <li><a href="#">2</a>
            </li>
            <li><a href="#">3</a>
            </li>
            <li><a href="#">4</a>
            </li>
            <li><a href="#">5</a>
            </li>
            <li><a href="#">6</a>
            </li>
            <li><a href="#">7</a>
            </li>
          </ol>
        </div>
      </div>
      <!-- Full Width Mega-Menu / End -->
    </li>
    <li><a href="#">D</a>
    </li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

Nice, but now is problem to remove this dropdown, when i move mouse from it:(
If you want to remove it - then don't use the :target selector. If you are determined to use just CSS - then there is still a way to do this. It's called the checkbox hack
Great, can you make it without target, can you make a fiddle with that hack
@Gorostas - take a look now - I added the checkbox hack solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.