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

前端 未结 12 1549
长发绾君心
长发绾君心 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-28 23:44

    There is 3 rapid examples with pure CSS and without javascript where the content appears "on click", with a "maintained click" and a third "onhover" (all only tested in Chrome). Sorry for the up of this post but this question are the first seo result and maybe my contribution can help beginner like me

    I think (not tested) but the advantage of argument "content" that you can add great icon like from Font Awesome (its \f-Code) or an hexadecimal icon in place of the text "Hide" and "Show" to internationalize the trick.

    example link http://jsfiddle.net/MonkeyTime/h3E9p/2/

    <style>
    label { position: absolute; top:0; left:0}
    
    input#show, input#hide {
        display:none;
    }
    
    span#content {
        display: block;
        -webkit-transition: opacity 1s ease-out;
        transition: opacity 1s ease-out;
        opacity: 0; 
        height: 0;
        font-size: 0;
        overflow: hidden;
    }
    
    input#show:checked ~ .show:before {
        content: ""
    }
    input#show:checked ~ .hide:before {
        content: "Hide"
    }
    
    input#hide:checked ~ .hide:before {
        content: ""
    }
    input#hide:checked ~ .show:before {
        content: "Show"
    }
    input#show:checked ~ span#content {
        opacity: 1;
        font-size: 100%;
        height: auto;
    }
    input#hide:checked ~ span#content {
        display: block;
        -webkit-transition: opacity 1s ease-out;
        transition: opacity 1s ease-out;
        opacity: 0; 
        height: 0;
        font-size: 0;
        overflow: hidden;
    }
    </style>
    <input type="radio" id="show" name="group">   
    <input type="radio" id="hide" name="group" checked>
    <label for="hide" class="hide"></label>
    <label for="show" class="show"></label>
    <span id="content">Lorem iupsum dolor si amet</span>
    
    
    <style>
    #show1 { position: absolute; top:20px; left:0}
    #content1 {
        display: block;
        -webkit-transition: opacity 1s ease-out;
        transition: opacity 1s ease-out;
        opacity: 0; 
        height: 0;
        font-size: 0;
        overflow: hidden;
    }
    #show1:before {
        content: "Show"
    }
    #show1:active.show1:before {
        content: "Hide"
    }
    #show1:active ~ span#content1 {
        opacity: 1;
        font-size: 100%;
        height: auto;
    }
    </style>
    
    <div id="show1" class="show1"></div>
    <span id="content1">Ipsum Lorem</span>
    
    
    <style>
    #show2 { position: absolute; top:40px; left:0}
    #content2 {
        display: block;
        -webkit-transition: opacity 1s ease-out;
        transition: opacity 1s ease-out;
        opacity: 0; 
        height: 0;
        font-size: 0;
        overflow: hidden;
    }
    #show2:before {
        content: "Show"
    }
    #show2:hover.show2:before {
        content: "Hide"
    }
    #show2:hover ~ span#content2 {
        opacity: 1;
        font-size: 100%;
        height: auto;
    }
    
    /* extra */
    #content, #content1, #content2 {
        float: left;
        margin: 100px auto;
    }
    </style>
    
    <div id="show2" class="show2"></div>
    <span id="content2">Lorem Ipsum</span>
    
    0 讨论(0)
  • 2020-11-28 23:45

    I know it's an old post but what about this solution (I've made a JSFiddle to illustrate it)... Solution that uses the :after pseudo elements of <span> to show/hide the <span> switch link itself (in addition to the .alert message it must show/hide). When the pseudo element loses it's focus, the message is hidden.

    The initial situation is a hidden message that appears when the <span> with the :after content : "Show Me"; is focused. When this <span> is focused, it's :after content becomes empty while the :after content of the second <span> (that was initially empty) turns to "Hide Me". So, when you click this second <span> the first one loses it's focus and the situation comes back to it's initial state.

    I started on the solution offered by @Vector I kept the DOM'situation presented ky @Frederic Kizar

    HTML:

    <span class="span3" tabindex="0"></span>
    <span class="span2" tabindex="0"></span>
    <p class="alert" >Some message to show here</p>
    

    CSS:

    body {
        display: inline-block;
    }
    .span3 ~ .span2:after{
        content:"";
    }
    .span3:focus ~ .alert  {
        display:block;
    }
    .span3:focus ~ .span2:after  {
        content:"Hide Me";
    }
    .span3:after  {
        content: "Show Me";
    }
    .span3:focus:after  {
        content: "";
    }
    .alert  {
        display:none;
    }
    
    0 讨论(0)
  • 2020-11-28 23:56

    A very easy solution from cssportal.com

    If pressed [show], the text [show] will be hidden and other way around.

    This example does not work in Chrome, I don't why...

    .show {
    	display: none;
    }
    .hide:focus + .show {
    	display: inline;
    }
    .hide:focus {
    	display: none;
    }
    .hide:focus ~ #list { display:none; }
    @media print {
    .hide, .show {
    	display: none;
    }
    }
    <div><a class="hide" href="#">[hide]</a> <a class="show" href="#">[show]</a>
    <ol id="list">
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
    </ol>
    </div>

    0 讨论(0)
  • 2020-11-28 23:58

    I wouldn't use checkboxes, i'd use the code you already have

    DEMO http://jsfiddle.net/6W7XD/1/

    CSS

    body {
      display: block;
    }
    .span3:focus ~ .alert {
      display: none;
    }
    .span2:focus ~ .alert {
      display: block;
    }
    .alert{display:none;}
    

    HTML

    <span class="span3">Hide Me</span>
    <span class="span2">Show Me</span>
    <p class="alert" >Some alarming information here</p>
    

    This way the text is only hidden on click of the hide element

    0 讨论(0)
  • 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

    <input id="display-toggle" type=checkbox>
    <label id="display-button" for="display-toggle"><span>Display Content</span></label>
    <label id="hide-button" for="display-toggle"><span>Hide Content</span></label>    
    <div id="hidden-content"><br />Hidden Content</div>
    

    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;
    } 
    
    0 讨论(0)
  • 2020-11-29 00:04

    I've got another simple solution:

    HTML:

    <a href="#alert" class="span3" tabindex="0">Hide Me</a>
    <a href="#" class="span2" tabindex="0">Show Me</a>
    <p id="alert" class="alert" >Some alarming information here</p>
    

    CSS:

    body { display: block; }
    p.alert:target { display: none; }
    

    Source: http://css-tricks.com/off-canvas-menu-with-css-target/

    0 讨论(0)
提交回复
热议问题