I Need To Build An Accordion Without Javascript

sharingsunshine
sharingsunshine used Ask the Experts™
on
I am on a Wordpress site and I need to know how to build an accordion without using javascript.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rikin ShahMicrosoft Dynamics CRM Consultant

Commented:
Hi,

Check below code-

HTML-
<html>
<h1>CSS + HTML only Accordion Element</h1>
<ul>
  <li>
    <input type="checkbox" checked>
    <i></i>
    <h2>Languages Used</h2>
    <p>This page was written in HTML and CSS. The CSS was compiled from SASS. I used Normalize as my CSS reset and -prefix-free to save myself some headaches. I haven't quite gotten the hang of Slim for compiling into HTML, but someday I'll use it since its syntax compliments that of SASS. Regardless, this could all be done in plain HTML and CSS.</p>
  </li>
  <li>
    <input type="checkbox" checked>
    <i></i>
    <h2>How it Works</h2>
    <p>Using the sibling and checked selectors, we can determine the styling of sibling elements based on the checked state of the checkbox input element. One use, as demonstrated here, is an entirely CSS and HTML accordion element. Media queries are used to make the element responsive to different screen sizes.</p>
  </li>
  <li>
    <input type="checkbox" checked>
    <i></i>
    <h2>Points of Interest</h2>
    <p>By making the open state default for when :checked isn't detected, we can make this system accessable for browsers that don't recognize :checked. The fallback is simply an open accordion. The accordion can be manipulated with Javascript (if needed) by changing the "checked" property of the input element.</p>
  </li>
</ul>
</html>

Open in new window



CSS-
$white: rgba(254,255,250,1)
$grey: rgba(220,231,235,1)
$red: rgba(255,104,115,1)
$black: rgba(48,69,92,0.8)
$sans: 'Titillium Web', sans-serif
  
.transition
  transition: all 0.25s ease-in-out

.flipIn
  animation: flipdown 0.5s ease both
    
.no-select
  -webkit-tap-highlight-color: rgba(0,0,0,0)
  -webkit-touch-callout: none
  -webkit-user-select: none
  -khtml-user-select: none
  -moz-user-select: none
  -ms-user-select: none
  user-select: none

html
  width: 100%
  height: 100%
  perspective: 900
  overflow-y: scroll
  background-color: $grey
  font-family: $sans
  color: $black
  
body
  min-height: 0
  display: inline-block
  position: relative
  left: 50%
  margin: 90px 0
  transform: translate( -50% , 0 )
  box-shadow: 0 10px 0 0 $red inset
  background-color: $white
  max-width: 450px
  padding: 30px
  
  @media ( max-width:550px )
    box-sizing: border-box
    transform: translate( 0 , 0 )
    max-width: 100%
    min-height: 100%
    margin: 0
    left: 0

h1 , h2
  color: $red

h1
  text-transform: uppercase
  font-size: 36px
  line-height: 42px
  letter-spacing: 3px
  font-weight: 100
  @extend .flipIn
  
h2
  font-size: 26px
  line-height: 34px
  font-weight: 300
  letter-spacing: 1px
  display: block
  background-color: $white
  margin: 0
  cursor: pointer
  @extend .no-select

p
  color: $black
  font-size: 17px
  line-height: 26px
  letter-spacing: 1px
  position: relative
  overflow: hidden
  max-height: 800px
  @extend .transition
  opacity: 1
  transform: translate( 0 , 0 )  
  margin-top: 14px
  z-index: 2
  
ul
  list-style: none
  perspective: 900
  padding: 0
  margin: 0
  
  li
    position: relative
    padding: 0
    margin: 0
    padding-bottom: 4px
    padding-top: 18px
    border-top: 1px dotted $grey
    @extend .flipIn
      
    &:nth-of-type(1)
      animation-delay: 0.5s
    
    &:nth-of-type(2)
      animation-delay: 0.75s
        
    &:nth-of-type(3)
      animation-delay: 1.0s
    
    &:last-of-type
      padding-bottom: 0
      
    i
      position: absolute
      transform: translate( -6px , 0 )
      margin-top: 16px
      right: 0
      
      &:before , &:after
        content: ""
        @extend .transition
        position: absolute
        background-color: $red
        width: 3px
        height: 9px
        
      &:before
        transform: translate( -2px , 0 ) rotate( 45deg )
          
      &:after
        transform: translate( 2px , 0 ) rotate( -45deg )
    
    input[type=checkbox]
      position: absolute
      cursor: pointer
      width: 100%
      height: 100%
      z-index: 1
      opacity: 0
      
      &:checked
        &~p
          margin-top: 0
          max-height: 0
          opacity: 0
          transform: translate( 0 , 50% )
            
        &~i
          &:before
            transform: translate( 2px , 0 ) rotate( 45deg )
          
          &:after
            transform: translate( -2px , 0 ) rotate( -45deg )

@keyframes flipdown
  0%
    opacity: 0
    transform-origin: top center
    transform: rotateX(-90deg)
  5%
    opacity: 1
  80%
    transform: rotateX(8deg)
  83%
    transform: rotateX(6deg)
  92%
    transform: rotateX(-3deg)
  100%
    transform-origin: top center
    transform: rotateX(0deg) 

Open in new window


Working example here-
https://codepen.io/abergin/pen/ihlDf
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Here is a sample I put together for another question

CSS
.Accordion-switch, .Accordion-switch + .Accordion-panel {
	display: none;
}
.Accordion-switch:checked + .Accordion-panel {
	display: block;
}
.Accordion {
	border: 1px solid hsl(0, 0%, 82%);
	border-radius: .3em;
	box-shadow: 0 1px 2px hsl(0, 0%, 82%);
}
.Accordion-trigger {
	background: none;
	border: 0;
	color: hsl(0, 0%, 13%);
	display: block;
	font-size: 16px;
	font-weight: normal;
	margin: 0;
	padding: 1em 1.5em;
	position: relative;
	text-align: left;
	width: 100%;
}
.Accordion-trigger {
	border-radius: .3em .3em 0 0;
}
.Accordion-trigger:hover {
	cursor: pointer;
}
.Accordion-panel {
	margin: 0;
	padding: 1em 1.5em;
}
.Accordion > * + * {
	border-top: 1px solid hsl(0, 0%, 82%);
}

Open in new window

HTML
<div class="Accordion">
	<label class="Accordion-trigger" for="switch1">Heading</label>
	<input type="checkbox" class="Accordion-switch" id="switch1">
	<div class="Accordion-panel">Content</div>
</div>

Open in new window

Working sample here

Author

Commented:
I followed the suggestion on codepen and it works fine as long as the content isn't too long.  I don't want to use pure CSS3 because I have older users that aren't up to date on the latest browser upgrades.

So, can you tell me how to fix the problem of not being able to scroll down at http://www.henricomg.org/gardening-advice2/

Also, if the question is too long then the css doesn't display it correctly.

I am thinking it would be best to get the javascript option to work.  If only someone could tell me how to not get the <p> tags automatically placed in the code.  Then that option would work.

Julian, explains this in https://www.experts-exchange.com/questions/29158869/Why-Won't-My-Accordion-Class-Open-Up.html
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Julian,

Your example is at this page http://www.henricomg.org/gardening-advice3/ but it doesn't seem to be working.  I looked at the source but don't see what the problem could be.

Author

Commented:
Rikin,

Your example seems to work fine on codepen but when I implement it on my wordpress site it doesn't work.  You can see your page at http://www.henricomg.org/gardening-advice5/

It also is showing the checkbox and bullet which needs to be hidden.
I used UX Builder Elements.  They had an accordion element that worked flawlessly.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial