Skip to content Skip to sidebar Skip to footer

All Css Accordion Divs Open By Default (no Jquery)

I'm using this: jsfiddle.net/wromLbq5 And am hoping to have the ability to have multiple accordion sections open at once, and on page load. By this I mean, when one opens, I don't

Solution 1:

This css is showing all of the accordions as closed using this style:

.accordionli > .sub-menuli {
      height: 0px;
    }

You could set it to 33px to show all open when the page opens - but that would break the functionality as it is the :target selector which then sets the height and thus causes the selected section to show as open.

The only solution is to move into javascript.....

Solution 2:

Can it be done? Yes

Do you need to change your HTML? Yes

Is it pretty? No

Basically instead of Target maintaining state. Use a checkbox replacing you top level a tags with label.

body {margin:50px;}

/* Reset */.accordion,
.accordionul,
.accordionli,
.accordionlabel,
.accordionspan {
  margin: 0;
  padding: 0;
  border: none;
  outline: none;
}
.accordionli {
  list-style: none;
}

.accordioninput[type="checkbox"]{display:none;}

/* Layout & Style */.accordionli > label, .accordionli > a  {
  display: block;
  position: relative;
  min-width: 110px;
  padding: 010px040px;
  height: 32px;

  color: #fdfdfd;
  font: bold 12px/32px Arial, sans-serif;
  text-decoration: none;
  text-shadow: 0px1px0pxrgba(0,0,0, .35);

  background: #6c6e74;
  background: -moz-linear-gradient(top,  #6c6e740%, #4b4d51100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6c6e74), color-stop(100%,#4b4d51));
  background: -webkit-linear-gradient(top,  #6c6e740%,#4b4d51100%);
  background: -o-linear-gradient(top,  #6c6e740%,#4b4d51100%);
  background: -ms-linear-gradient(top,  #6c6e740%,#4b4d51100%);
  background: linear-gradient(top,  #6c6e740%,#4b4d51100%);

  -webkit-box-shadow: inset 0px1px0px0pxrgba(255,255,255, .1), 0px1px0px0pxrgba(0,0,0, .1);
  -moz-box-shadow: inset 0px1px0px0pxrgba(255,255,255, .1), 0px1px0px0pxrgba(0,0,0, .1);
  box-shadow: inset 0px1px0px0pxrgba(255,255,255, .1), 0px1px0px0pxrgba(0,0,0, .1);
}

.accordion > li:hover > label,
.accordion > li:target > label,
.accordion > li > input[type="checkbox"]:checked ~ label{
  color: #3e5706;
  text-shadow: 1px1px1pxrgba(255,255,255, .2);
  
  /*background: url(../img/active.png) repeat-x;*/background: #a5cd4e;
  background: -moz-linear-gradient(top,  #a5cd4e0%, #6b8f1a100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#a5cd4e), color-stop(100%,#6b8f1a));
  background: -webkit-linear-gradient(top,  #a5cd4e0%,#6b8f1a100%);
  background: -o-linear-gradient(top,  #a5cd4e0%,#6b8f1a100%);
  background: -ms-linear-gradient(top,  #a5cd4e0%,#6b8f1a100%);
  background: linear-gradient(top,  #a5cd4e0%,#6b8f1a100%); 
}

.accordionli > labelspan, .accordionli > aspan {
  display: block;
  position: absolute;
  top: 7px;
  right: 0;
  padding: 010px;
  margin-right: 10px;
  
  font: normal bold 12px/18px Arial, sans-serif;
  background: #404247;
  
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  border-radius: 15px;

  -webkit-box-shadow: inset 1px1px1pxrgba(0,0,0, .2), 1px1px1pxrgba(255,255,255, .1);
  -moz-box-shadow: inset 1px1px1pxrgba(0,0,0, .2), 1px1px1pxrgba(255,255,255, .1);
  box-shadow: inset 1px1px1pxrgba(0,0,0, .2), 1px1px1pxrgba(255,255,255, .1);
}


.accordion > li:hover > labelspan,
.accordion > li:target > labelspan,
.accordion > li > input[type="checkbox"]:checked ~ labelspan{
  color: #fdfdfd;
  text-shadow: 0px1px0pxrgba(0,0,0, .35);
  background: #3e5706;
}

/* Images */.accordion > li > label:before {
  position: absolute;
  top: 0;
  left: 0;
  content: '';
  width: 24px;
  height: 24px;
  margin: 4px8px;

  background-repeat: no-repeat;
  background-image: url(http://designmodo.com/demo/css3accordionmenu/img/icons.png);
  background-position: 0px0px;
}

.accordionli.files > label:before { background-position: 0px0px; }
.accordionli.files:hover > labe:before,
.accordionli.files:target > label:before { background-position: 0px -24px; }

.accordionli.mail > label:before { background-position: -24px0px; }
.accordionli.mail:hover > label:before,
.accordionli.mail:target > label:before { background-position: -24px -24px; }

.accordionli.cloud > label:before { background-position: -48px0px; }
.accordionli.cloud:hover > label:before,
.accordioninput[type="checkbox"]:checked:before { background-position: -48px -24px; }

.accordionli.sign > label:before { background-position: -72px0px; }
.accordionli.sign:hover > label:before,
.accordioninput[type="checkbox"]:checked:before { background-position: -72px -24px; }

/* Sub Menu */.sub-menuli > a {
  color: #797979;
  text-shadow: 1px1px0pxrgba(255,255,255, .2);

  background: #e5e5e5;
  border-bottom: 1px solid #c9c9c9;

  -webkit-box-shadow: inset 0px1px0px0pxrgba(255,255,255, .1), 0px1px0px0pxrgba(0,0,0, .1);
  -moz-box-shadow: inset 0px1px0px0pxrgba(255,255,255, .1), 0px1px0px0pxrgba(0,0,0, .1);
  box-shadow: inset 0px1px0px0pxrgba(255,255,255, .1), 0px1px0px0pxrgba(0,0,0, .1);
}

.sub-menuli:hovera { background: #efefef; }

.sub-menuli:last-childa { border: none; }

.sub-menuli >  aspan {
  color: #797979;
  text-shadow: 1px1px0pxrgba(255,255,255, .2);
  background: transparent;
  border: 1px solid #c9c9c9;

  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
}

.sub-menuem {
  position: absolute;
  top: 0;
  left: 0;
  margin-left: 14px;
  color: #a6a6a6;
  font: normal 10px/32px Arial, sans-serif;
}

/* Functionality */.accordionli > .sub-menuli {
  height: 0;
  overflow: hidden;

  -webkit-transition: height .2s ease-in-out;
  -moz-transition: height .2s ease-in-out;
  -o-transition: height .2s ease-in-out;
  -ms-transition: height .2s ease-in-out;
  transition: height .2s ease-in-out;
}

.accordioninput[type="checkbox"]:checked ~ .sub-menuli {
  height: 33px;
}
<ulclass="accordion"><liid="one"class="files"><inputtype="checkBox"id="cbOne"checked="checked" /><labelfor="cbOne" >My Files<span>495</span></label><ulclass="sub-menu"><li><ahref="#one"><em>01</em>Dropbox<span>42</span></a></li><li><ahref="#one"><em>02</em>Skydrive<span>87</span></a></li><li><ahref="#one"><em>03</em>FTP Server<span>366</span></a></li><li><ahref="#one"><em>01</em>Dropbox<span>42</span></a></li><li><ahref="#one"><em>01</em>Dropbox<span>42</span></a></li></ul></li><liid="two"class="mail"><inputtype="checkBox"id="cbTwo"checked="checked" /><labelfor="cbTwo" >Mail<span>26</span></label><ulclass="sub-menu"><li><ahref="#two"><em>01</em>Hotmail<span>9</span></a></li><li><ahref="#two"><em>02</em>Yahoo<span>14</span></a></li><li><ahref="#two"><em>03</em>Gmail<span>3</span></a></li></ul></li><liid="three"class="cloud"><inputtype="checkBox"id="cbThree"checked="checked" /><labelfor="cbThree" >Cloud<span>58</span></label><ulclass="sub-menu"><li><ahref="#three"><em>01</em>Connect<span>12</span></a></li><li><ahref="#three"><em>02</em>Profiles<span>19</span></a></li><li><ahref="#three"><em>03</em>Options<span>27</span></a></li></ul></li><liid="four"class="sign"><inputtype="checkBox"id="cbFour"checked="checked" /><labelfor="cbFour" >Sign Out</label><ulclass="sub-menu"><li><ahref="#four"><em>01</em>Log Out</a></li><li><ahref="#four"><em>02</em>Delete Account</a></li><li><ahref="#four"><em>03</em>Freeze Account</a></li></ul></li></ul>

Look Ma! No Javascript!

Post a Comment for "All Css Accordion Divs Open By Default (no Jquery)"