Skip to content

Instantly share code, notes, and snippets.

@stamaniorec
Created June 16, 2016 06:10
Show Gist options
  • Save stamaniorec/34a98f55c6318253ba29a85606fd722a to your computer and use it in GitHub Desktop.
Save stamaniorec/34a98f55c6318253ba29a85606fd722a to your computer and use it in GitHub Desktop.
A simple CSS accordion
<!DOCTYPE html>
<html>
<head>
<title>Accordion</title>
<style type="text/css">
p { margin: 0; }
.accordion h2 { cursor: pointer; }
.accordion-content {
background-color: #999;
color: white;
}
.accordion-close {
display: block;
margin-top: 20px;
text-decoration: underline;
cursor: pointer;
}
.accordion .accordion-content {
max-height: 0; /* height property cannot be transitioned */
overflow: hidden;
padding: 0 20px; /* height doesn't take padding into account*/
transition: all 0.3s;
opacity: 0; /* flashy effect */
}
.accordion.open .accordion-content {
max-height: 300px; /* how tall it *could* get */
padding: 10px 20px;
opacity: 1;
}
</style>
</head>
<body>
<div class="accordion">
<h2>Press me to reveal content</h2>
<div class="accordion-content">
<p >Hidden content in here</p>
<span class="accordion-close">Close</span>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript">
(function() {
$(".accordion h2").on('click', function() {
$('.accordion').addClass('open');
});
$(".accordion-close").on('click', function() {
$(".accordion").removeClass('open');
});
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment