Hide Show content-list with only CSS, no javascript used

前端 未结 12 1596
长发绾君心
长发绾君心 2020-11-28 23:18

I\'ve been searching for a good trick to make a Hide/Show content or a list with only CSS and no javascript. I\'ve managed to make this action:



        
12条回答
  •  感动是毒
    2020-11-29 00:01

    The answer below includes changing text for "show/hide", and uses a single checkbox, two labels, a total of four lines of html and five lines of css. It also starts out with the content hidden.

    Try it in JSFiddle

    HTML

    
    
        
    

    Hidden Content

    CSS

    label {
      background-color: #ccc;
      color: brown;
      padding: 15px;
      text-decoration: none;
      font-size: 16px;
      border: 2px solid brown;
      border-radius: 5px;
      display: block;
      width: 200px;
      text-align: center;
    }
    
    input,
    label#hide-button,
    #hidden-content {
      display: none;
    }
    
    input#display-toggle:checked ~ label#display-button {
      display: none;
    }
    
    input#display-toggle:checked ~ label#hide-button {
      display: block;
      background-color: #aaa;
      color: #333
    }
    
    input#display-toggle:checked ~ #hidden-content {
      display: block;
    } 
    

提交回复
热议问题