0

Just wondering if a div can be called without using javascript.

such as

my_div:hover{ add new layout}

is there a version for click eg

my_div:click{add new layout}

Thanks

LinkinTED
  • 17,122
  • 4
  • 31
  • 55
  • Nope, but you can go the other way. You might also be interested in something like: https://github.com/krasimir/cssx – jmargolisvt Dec 04 '17 at 15:31

2 Answers2

3

Yes, if you add tabindex="0" to your div, you make it clickable and can then use the :focus pseudo-class to apply styles.

<div class="clickable" tabindex="0"></div>

.clickable {
  height: 100px;
  background: blue;
}

.clickable:focus {
  background: red;
}

Codepen example. Clicking the div should give it focus and apply the :focus CSS to it. Clicking away from it will unfocus (blur) it and reset the default styles.

delinear
  • 2,575
  • 1
  • 8
  • 20
0

Not directly, but you can fake it using checkboxes:

input[type=checkbox] {
  display: none;
}
.content {
  display: none;
  padding: 20px;
  background-color: #dadada;
}
input[type=checkbox]:checked+label+.content {
  display: block;
}
<input type="checkbox" id="check">
<label for="check">Click me</label>
<div class="content">
  <h3>Content</h3>
  <p>lorem20</p>
</div>
Jonas Grumann
  • 10,058
  • 2
  • 18
  • 37