0

I have coded the following jQuery, that makes a div slide down when a link is clicked. Is it possible in pure css3 without jQuery?

http://jsbin.com/exakuj/5

HTML

<header>
  <h1>Heading</h1>
</header>
<section>
  <a href='#' id='click'>Lorem ipsum dolor sit amet, consectetur adipiscing elit vestibulum convallis.</a>
  <div id='show'></div>
</section>

jQuery

$('#click').click(function() {
$('#show').slideDown('slow', function() {
// Animation complete.
  });
});

4 Answers4

1

DEMO FIDDLE WITHOUT JQUERY

here is the code -

html -

<label for="test1" class="test">Click Me ;)</label>
<input type="checkbox" id="test1"/>
<div class="test2">
    Yo!! you clicked it, now I am visible to you with a smooth animation
</div>

css-

.test{
    background: yellow;
    padding:10px;
    display:block;
    font-family: tahoma;
    text-align:center;
    width:100px;
    font-weight:bold;
    clear: both;
}
.test2{
    width:100px;
    height:0px;
    text-align:center;
    font-size:12px;
    font-family: tahoma;
    color:#fff;
    font-weight:bold;
    padding:10px;
    opacity:0;
    float:left;
    overflow:hidden;
    background:red;
    -webkit-transition: all 0.7s ease;
    -moz-transition: all 0.7s ease;
    -ms-transition: all 0.7s ease;
    -o-transition: all 0.7s ease;
    transition: all 0.7s ease;
}
#test1:checked + .test2{
    height: 70px;
    opacity:1;
}
#test1{
    display:none;
}
Rohit Agrawal
  • 5,372
  • 5
  • 19
  • 30
1

Yes, but you probably would want some javascript anyway.

#show {
   padding: 20px;
   background: #ccc;
   max-height:0;
   overflow:hidden;
   -webkit-transition:max-height 1s;
   -moz-transition:max-height 1s;
   transition:max-height 1s;
}

#show:target {
    max-height:1000px; /* you can't transistion to height:auto */
}

http://jsfiddle.net/richbradshaw/hgdrw/1/

There are a few things here:

  1. the :target selector
  2. The page will jump to the ID if you don't use JS to cancel that behaviour
  3. The only way to close it is to change the URL so it doesn't include #show at the end

If I were doing this, I would use what I have here, but instead of the target selector, I'd add a class to #show called 'active' using JS, and change #show:target to #show.active.

http://jsfiddle.net/richbradshaw/hgdrw/2/

I'm using jQuery here because it's so simple,

The target selector isn't really great for use in practice in my experience.

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
0

If it's helpful I've adapted Rohit's answer into an accordion menu inside a nav tag, each div with content: see this JSFiddle.

The fixed height of the divs is necessary for a pure CSS version, which is an issue - you can't use height: auto;.

Dave Everitt
  • 17,193
  • 6
  • 67
  • 97
0

You can get a similar feel to a slide transition by using the top padding/margin:

http://jsbin.com/bicoseroze/1/

Ben Boyle
  • 1,686
  • 16
  • 13