0

I've been using a pure CSS navigation for a few years now, and lately we've started building a bunch of mobile sites at the company I work for. I'd really like to keep using pure CSS, and note rely on jQuery, but on a mobile site, drop down menu's don't work correctly.

Is there something similar to jQuery's .click(); event in CSS3? Basically, when a user clicks on a navigation link, I want the drop down to open and stay open. I've tried looking around, but I can't seem to find anything.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
JacobTheDev
  • 17,318
  • 25
  • 95
  • 158

4 Answers4

6

You could use the :target selector for some basic functionality. See Chris Coyier's post about it. Note, brower support.

EDIT: Made a quick demo

Adam Simpson
  • 3,591
  • 2
  • 22
  • 27
4

Given some basic HTML structures, you can recreate the toggle (show/hide) capacities of JavaScript implementations:

Using :target:

HTML:

<nav id="nav">
    <h2>This would be the 'navigation' element.</h2>
</nav>
<a href="#nav">show navigation</a>
<a href="#">hide navigation</a>

CSS:

nav {
    height: 0;
    overflow: hidden;
    -moz-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -o-transition: all 1s linear;
    -webkit-transition: all 1s linear;
    transition: all 1s linear;
}
nav:target {
    height: 4em;
    color: #000;
    background-color: #ffa;
    -moz-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -o-transition: all 1s linear;
    -webkit-transition: all 1s linear;
    transition: all 1s linear;
}

JS Fiddle demo.

Using :checked:

HTML:

<input type="checkbox" id="switch" />
<nav>
    <h2>This would be the 'navigation' element.</h2>
</nav>
<label for="switch">Toggle navigation</label>

CSS:

#switch {
    display: none;
}
#switch + nav {
    height: 0;
    overflow: hidden;
    -moz-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -o-transition: all 1s linear;
    -webkit-transition: all 1s linear;
    transition: all 1s linear;
}
#switch:checked + nav {
    height: 4em;
    color: #000;
    background-color: #ffa;
    -moz-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -o-transition: all 1s linear;
    -webkit-transition: all 1s linear;
    transition: all 1s linear;
}

label {
    cursor: pointer;
}

JS Fiddle demo.

Unfortunately the closest alternative native CSS has, to a ':clicked' selector is the :active or :focus pseudo-classes, the first selector of which will cease to match once the mouse-button is released, the second of which will cease to match once the given element is no longer focused (by either keyboard or mouse focusing elsewhere in the document).

Updated the demos, to allow for toggling the text of the label (using CSS generated content):

#switch {
    display: none;
}
#switch + nav {
    height: 0;
    overflow: hidden;
    -moz-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -o-transition: all 1s linear;
    -webkit-transition: all 1s linear;
    transition: all 1s linear;
}
#switch:checked + nav {
    height: 4em;
    color: #000;
    background-color: #ffa;
    -moz-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -o-transition: all 1s linear;
    -webkit-transition: all 1s linear;
    transition: all 1s linear;
}

label {
    display: inline-block;
    cursor: pointer;
}

#switch ~ label::before {
    content: 'Show ';
}

#switch:checked ~ label::before {
    content: 'Hide ';
}

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • This looks pretty nice, too. Seems a bit more complex than the `:target` method that I accepted, but I'll try this out too. – JacobTheDev Jun 20 '13 at 15:37
  • The `:target` method's *included* in the answer... ;) however *both* approaches carry the inevitable limited-browser support (primarily Internet Explorer's failure to implement `:checked` until version 9, and `:target` is 'incomplete' in versions 9 *and* 10). – David Thomas Jun 20 '13 at 15:40
  • @DavidThomas - If he can limit this to run only for mobile then that won't be a problem as mobile browsers generally support all the latest fun CSS3 stuff. – webnoob Jun 20 '13 at 15:48
  • Missed that the `:target` version was in here, lol. Just say the `:chedked` stuff. For my purposes, `:target` should work fine. Windows Phone 7 came with IE9, and my plan is to only use this method once the site gets down to a mobile size (for responsive sites). Should be fun to code :) – JacobTheDev Jun 20 '13 at 15:49
  • You're welcome - it's important to use the correct terms because the term "pseudo-selector" is meaningless, whereas "pseudo-class" means an element in a specific state (and therefore [not an event](http://stackoverflow.com/questions/11703241/does-css-have-a-blur-selector-pseudo-class/11703334#11703334)), and in this case such a meaning can be paramount to understanding the question/answer at hand. – BoltClock Jun 20 '13 at 15:53
  • 1
    @Rev: just a caution as one downside to the `:target` method is that it messes with the "back" and "forward" functioning of many (all?) browsers since it is adding and removing parts to the url string. To see the "issue" just hit the "back" arrow on the browser after you have clicked the show/hide navigation a few times in the sample fiddle. For user, that experience could be very annoying. – ScottS Jun 20 '13 at 17:05
2

Try the :active psuedo-class. It's not completely bulletproof, but you should be able to get at least some basic functionality from it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

Try something like this in your CSS Code...

  selector:hover, selector:active {
     display:block;
     height:100px;//test
     width:200px; //test
     border:solid 1px #000; //test
    }
klewis
  • 7,459
  • 15
  • 58
  • 102