Toggle divs without using Javascript

后端 未结 11 2025
日久生厌
日久生厌 2020-12-06 10:19

I\'d like to toggle a

, but my requirement is that it must work with javascript turned off. I would like to select a hyperlink that sta
相关标签:
11条回答
  • 2020-12-06 11:04

    For a little more polished version of the accepted answer, a common practice is to combine a hidden checkbox + label to be able to have a clickable label on screen that maps to a hidden backing value that is available to both JavaScript (.checked) and to CSS (:checked)

    <input type='checkbox' id='css-toggle-switch' checked='checked' class='css-toggle-switch'>
    <label for='css-toggle-switch' class='btn'>Error Details</label>
    <div class='css-toggle-content'>
       <pre><code>Unexpected StackOverflow</code></pre>
    </div >
    

    By putting our checkbox first, we can drive CSS decisions based on the :checked selector. We can grab subsequent elements with the adjacent sibling select + or the general sibling selector ~

     /* always hide the checkbox */
    .css-toggle-switch { display: none; }
    
    /* update label text to reflect state */
    .css-toggle-switch         + label:before { content: "Hide "; }
    .css-toggle-switch:checked + label:before { content: "Show "; }
    
     /* conditionally hide content when checked */
    .css-toggle-switch:checked ~ .css-toggle-content  { display: none; }
    
    /* make the label look clickable */
    .css-toggle-switch + label {
       cursor: pointer;
      -webkit-touch-callout: none;
        -webkit-user-select: none;
           -moz-user-select: none;
            -ms-user-select: none;
                user-select: none;
    }
    

    Working Demo in jsFiddle & StackSnippets

    .css-toggle-switch { display: none; }
    .css-toggle-switch         + label:before { content: "Hide "; }
    .css-toggle-switch:checked + label:before { content: "Show "; }
    .css-toggle-switch:checked ~ .css-toggle-content  { display: none; }
    
    .css-toggle-switch + label {
       cursor: pointer;
      -webkit-touch-callout: none;
        -webkit-user-select: none;
           -moz-user-select: none;
            -ms-user-select: none;
                user-select: none;
    }
    
    
    
    /* just some styles to make the demo a little more pleasant */
    .btn {
      padding: 5px 10px;
      background: white;
      border: 1px solid grey;
      border-radius: 3px;
      width: 130px;
      display: inline-block;
      text-align: center;
    }
    .btn:hover {
      background: #e6e6e6;
      -webkit-box-shadow: inset 0 -1px 0 #ffffdffffd;
         -moz-box-shadow: inset 0 -1px 0 #ffffdffffd;
              box-shadow: inset 0 -1px 0 #ffffdffffd;
    }
    .panel {
        padding: 15px;
        background: #ffe06d;
        border: 1px solid #d69e01;
        border-radius: 3px;
    }
    pre {
      padding: 5px;
      margin-bottom: 0;
      background: #eaeaea;
      border: 1px solid grey;
        
    }
    <div class="panel">
      <input type='checkbox' id='css-toggle-switch' 
              checked='checked' class='css-toggle-switch'>
      <label for='css-toggle-switch' class='btn'>
        Error Details
      </label>
      <div class='css-toggle-content'>
         <pre><code>Unexpected StackOverflow</code></pre>
      </div >
    </div>

    0 讨论(0)
  • 2020-12-06 11:08

    You can't toggle on clicks without javascript. End.

    Update:

    If you can use CSS 3 selectors, you'll have to change your DOM structure and use CSS 3 selectors without a library that covers old browsers which are probably a lot more common than users with javascript off, You can usee @pointy answer with :selected.

    So I would say, practically it's still impossible...!

    0 讨论(0)
  • 2020-12-06 11:13

    About three years late to the party, but I found this when I was looking to do the same thing, for a mobile menu, and subsequently found a very good solution, so I thought I'd post it for whoever else comes a-hunting.

    The basic idea was from this article: http://www.creativebloq.com/css3/build-smart-mobile-navigation-without-hacks-6122800. I think I've used a slightly simpler approach (stumbled on while setting it all up). This hides / displays the navigation, by clicking the Menu button.

    In the CSS, nav is set as "hidden" and nav:target is set to display. The page has two menu buttons, using the same image, both have class menubutton, absolute position in the same place. menbuttonON has index 1000, sits outside nav in the html menubuttonOFF has index 1001, but sits inside nav, so it's invisible at first

    In the HTML, clicking menubuttonON links to nav, which is then target, so it displays. Inside that nav is menubuttonOFF, with a higher z-index than menubutton ON, so that's on top now. Clicking menubuttonOFF links back to menubuttonON, so nav isn't the target, and disappears, taking menubuttonOFF with it.

    Simplified CSS (without site-specific formatting):

    nav {display: none;}
    nav:target {display: block !important;}
    .menubutton { position: absolute;
    text-align: right;
    top: 0;
    margin-top: 14%;} /* This margin puts it below the header logo */
    .menubuttonON {z-index: 1000;}
    .menubuttonOFF {z-index: 1001;}
    

    HTML

    <header> <!-- logo here --> </header>
    <div class="menubutton menubuttonON" name="buttonON"><a href="#nav"><img src="../Images/menubutton.png">MENU</a></div>
    
    <nav id="nav" name="nav">
    
    <div class="menubutton menubuttonOFF"><a
    href="#buttonON"><img src="../Images/menubutton.png">CLOSE</a></div>
    <ul> <!-- all the navigation stuff --> </ul>
    </nav>
    

    You can see it working here: http://www.thewritersgreenhouse.co.uk/storyelements_resources/storyelements.htm.

    0 讨论(0)
  • 2020-12-06 11:14

    As others have said, you must use JS to achieve toggling of divs. If you want your website to work with javascript disabled, you need to design your website to fail gracefully when javascript isn't available. In other words, your website should NOT rely on JavaScript to function. Ex: AJAX forms should fall back to HTTP submit, etc.

    0 讨论(0)
  • 2020-12-06 11:15

    No Javascript, no toggling. There are some pseudo CSS3 methods, but if you have to support JS off, you're certainly not supporting CSS3.

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