I have this code and I would like to have the list be collapsed by default. Here is what it does now.
<!DOCTYPE html>
<head>
<title>menu mockup</title>
<style type="text/css">
.show {display: none; }
.hide:focus + .show {display: inline; }
.hide:focus { display: none; }
.hide:focus ~ #list { display:none; }
@media print { .hide, .show { display: none; } }
</style>
</head>
<body>
<p>Here's a list</p>
<div>
<a href="#" class="hide">[hide]</a>
<a href="#" class="show">[show]</a>
<ol id="list">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ol>
</div>
<p>How about that?</p>
</body>
</html>
For this to work in pure CSS requires a change to the HTML (since CSS can only target elements that appear later in the DOM (either as later-siblings, or descendants, or a combination of those two) than the elements upon which they’re styled. So, the HTML is now:
Note also that the
div
now has anid
in order to allow for the list to be hidden (again, using just CSS and basic HTML).The CSS is somewhat convoluted, though explained through
/* comments in the CSS itself */
:JS Fiddle demo.
A slightly revised (another element added to wrap the links) version, to allow for more easily positioning them (rather than having to manually work out how wide each link is before setting the
left
position):With the CSS:
JS Fiddle demo.
Or, instead, using
visibility: hidden
/visibility: visible;
.This html code collapses the list by default