Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapse and expand elements with CSS

Tags:

html

css

I'm trying to build a page where only headlines are visible when it is loaded, and the tables beneath each title toggle between hidden and displayed state when the user clicks on the title. The constraint I have is to do this in CSS only.

This is what I came up with so far:

https://jsfiddle.net/Argoron/c1ypx24c/6/

It doesn't work the way it should because every time I click on a title, the tables beneath the other titles are hidden. What I'm trying to accomplish is that each section behaves independently, meaning that for example table 1 should change its display state only when title 1 is being clicked.

Also, not sure why both alternative titles are displayed in section 3.

like image 555
Argoron Avatar asked Feb 18 '15 13:02

Argoron


People also ask

How do you collapse a div in CSS?

The . collapse class indicates a collapsible element (a <div> in our example); this is the content that will be shown or hidden with a click of a button. To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element.

What is collapsed in CSS?

The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0 . Given how CSS handles animations, you cannot use padding on a .

How do you collapse a menu in CSS?

Collapse & Expand Now we just have to set the default state of the menu to be collapsed when when checkbox is not checked. We do this by changing the max-height of . menu-content to 0 , but have it display a max-height of 100% when the checkbox is checked. That's it!


1 Answers

I will suggest use checkbox input and :checked instead of a and :target tags to trigger the event since the target will change always you click another link. Try this:

.tb {
    margin:10px 0;
}
.tb span+span, .collapsible {
    display:none;
}
.tb input[type="checkbox"]:checked ~ span {
    display:none;
}
.tb input[type="checkbox"]:checked ~ span+span{
    display:inline;
}
.tb input[type="checkbox"]:checked ~ .collapsible {
    display:table;
}
<div class="tb">
    <input type="checkbox"/>
    <span>Show</span><span>Hide</span>
    <table class="collapsible" id="collapsible1">
        <tr>
            <td>Hello 1</td>
        </tr>   
    </table>    
</div>
<div class="tb">
    <input type="checkbox"/>
    <span>Show</span><span>Hide</span>
    <table class="collapsible" id="collapsible1">
        <tr>
            <td>Hello 2</td>
        </tr>   
    </table>    
</div>
<div class="tb">
    <input type="checkbox"/>
    <span>Show</span><span>Hide</span>
    <table class="collapsible" id="collapsible1">
        <tr>
            <td>Hello 3</td>
        </tr>   
    </table>    
</div>

Now if you don't want to see the checkbox you can use CSS. Check this Snippet

.tb {
  margin: 10px 0;
  position: relative;
}
input[type="checkbox"] {
  width: 100%;
  height: 23px;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  z-index: 10;
  cursor: pointer;
}
input[type="checkbox"]:hover ~ span {
  background: black;
}
.tb span {
  position: relative;
  height: 23px;
  line-height: 23px;
  display: inline-block;
  background: red;
  color: white;
  transition: all .3s ease;
  padding: 0 10px;
  width: 100%;
}
.tb span+span,
.collapsible {
  display: none;
}
.tb input[type="checkbox"]:checked ~ span {
  display: none;
}
.tb input[type="checkbox"]:checked ~ span+span {
  display: inline-block;
}
.tb input[type="checkbox"]:checked ~ .collapsible {
  display: table;
}
<div class="tb">
  <input type="checkbox" />
  <span>Show</span><span>Hide</span>
  <table class="collapsible" id="collapsible1">
    <tr>
      <td>Hello 1</td>
    </tr>
  </table>
</div>
<div class="tb">
  <input type="checkbox" />
  <span>Show</span><span>Hide</span>
  <table class="collapsible" id="collapsible1">
    <tr>
      <td>Hello 2</td>
    </tr>
  </table>
</div>
<div class="tb">
  <input type="checkbox" />
  <span>Show</span><span>Hide</span>
  <table class="collapsible" id="collapsible1">
    <tr>
      <td>Hello 3</td>
    </tr>
  </table>
</div>

Or the DemoFiddle

like image 177
DaniP Avatar answered Oct 05 '22 06:10

DaniP