css active state for many elements - html

I have a div and inside this div there is an icon with background and text.
when clicking on this div I want to change 3 things:
the div background, the icon background and the text color.
how can I do it with CSS only?
http://jsfiddle.net/g1nrye8e/
<div class="click">
<div class="icon"></div>
<div class="text">text</div>
</div>
.click{
width: 200px;
height: 100px;
background: blue;
}
.icon{
width: 50px;
height:50px;
background: yellow;
display: inline-block;
}
.text{
color: #fff;
display: inline-block;
}

Preserve clicked state using pure CSS
The best way to preserve the clicked state, without JavaScript is to
wrap your elements inside a <label>
immediately before the element you want to target place an invisible input checkbox
when the input becomes :checked target any first next sibling element using +* and change styles accordingly
Repeat the same rule for +*'s inner elements:
/* DEFAULT STYLES */
.div{
width: 200px;
height: 100px;
background: blue;
margin:10px;
}
.icon{
width: 50px;
height:50px;
background: yellow;
display: inline-block;
}
.text{
color: #fff;
display: inline-block;
}
/* HIDE CHECKBOX HELPER */
label.click > input{ /* hide the input checkbox */
position:absolute;
visibility:hidden;
}
/* ACTIVE STYLES */
label.click > input:checked +* { /* (the next .div) */
background: #000;
}
label.click > input:checked +* .icon{
background: #c0ffee;
}
label.click > input:checked +* .text{
color: #f00ba4;
}
<label class="click">
<input type="checkbox"> <!-- :checked state changes +div styles -->
<div class="div">
<div class="icon"></div>
<div class="text">text</div>
</div>
</label>
<label class="click">
<input type="checkbox">
<div class="div">
<div class="icon"></div>
<div class="text">text</div>
</div>
</label>

Just add a class to the parent when it is clicked, then write more specific CS for that class.
Here's a running example.
document.querySelector(".click").addEventListener("click", function() {
this.classList.toggle("clicked");
});
.click{
width: 200px;
height: 100px;
background: blue;
}
.icon{
width: 50px;
height:50px;
background: yellow;
display: inline-block;
}
.text{
color: #fff;
display: inline-block;
}
.clicked.click{
background: green;
}
.clicked .icon{
background: red;
}
.clicked .text{
color: blue;
}
<div class="click">
<div class="icon"></div>
<div class="text">text</div>
</div>

This is a different solution with only css, but you have to click to every element:
.click{
width: 200px;
height: 100px;
background: blue;
}
.icon{
width: 50px;
height:50px;
background: yellow;
display: inline-block;
}
.text{
color: #fff;
display: inline-block;
}
.click:focus{
background: red;
}
.icon:focus{
background: grey;
}
.text:focus{
background: black;
}
<div class="click" tabindex="1">
<div class="icon" tabindex="2"></div>
<div class="text" tabindex="3">text</div>
</div>
There is a great explanations about the tabindex and :focus without using JS. Check it out impressivewebs

Related

A hover on top of a hover

I already have a css hover where when hovering over someones name, a card to the side appears with more information about that user.
Is it possible to have another hover on top of the first hover? So another card appears with even more information.
Name (hover on name) > d.o.b, address , etc (hover on their d.o.b for example) > second card appears with further info.
Thanks,
Jack
At the moment I just have the initial as a radio button which brings up the first info card, then I have a hover based off of that to show the second info card.
Here's an simple example I made:
#a {
width: 100px;
background: blue;
height: 100px;
}
#a:hover {
background: yellow;
}
#b {
width: 50px;
background: black;
height: 50px;
}
#b:hover {
background: red;
}
<div id="a">
<div id="b">
</div>
</div>
.parent {
width: 100px;
height: 100px;
background-color: lightpink;
}
.child,
.sub-child {
display: none;
width: 100px;
height: 100px;
position: relative;
right: -100px;
}
.child {
background-color: lightgreen;
}
.sub-child {
background-color: lightblue;
}
/* Show the child when hovering on the parent */
.parent:hover .child {
display: block;
}
/* Show the sub-child when hovering on the child */
.child:hover .sub-child {
display: block;
}
/* Not needed, just styling */
div:hover {
outline: 2px solid red;
}
<div class="parent">
<div class="child">
<div class="sub-child"></div>
</div>
</div>

How do I change sass property dynamically when element is focused?

I have an Input type search and a div just below it.
I want to change the color of the div from blue to red when the search box is focused.
my Div
<div id="demo-2">
<input type="search" placeholder="Search By Title, Author" />
<div class="autocomplete">
hello
</div>
</div>
the classes I have applied
#demo-2 input[type="search"]:focus {
width: 275px;
padding-left: 32px;
color: #000;
background-color: #fff;
cursor: auto;
}
.autocomplete {
background-color: blue;
height: 350px;
width: 275px;
position: absolute;
display: block;
right: 1em;
top: 1em;
z-index: -50;
margin-right: 20px;
}
My requirement is that the background-color of class autocomplete should change from blue to red when the search is focused without using any javascript.
Tried below code but didn't work
#demo-2 input[type="search"]:focus {
width: 275px;
padding-left: 32px;
color: #000;
background-color: #fff;
cursor: auto;
.autocomplete {
background-color: red;
}
}
You can use the adjacent sibling combinator (+) or the general sibling combinator (~)
#demo-2 input[type="search"]:focus {
width: 275px;
padding-left: 32px;
color: #000;
background-color: #fff;
cursor: auto;
~ .autocomplete {
background-color: red;
}
}
Instead of div use label and make the label block for example
input{
&:focus{
& ~ label{
background-color:red;
}
}
}
label{
display:block;
background-color:blue;
color:white;
padding:.5rem;
margin-top:1rem;
}
<input type="text" id="user" autocomplete="off">
<label for="user">Username</label>

JS: not able to understand behaviour of hover css selector

When I hover the element with class top_bottom_b1, the element with class top_bottom_b2 have to hide. I need to achieve this using css selector. I'm not sure why below code doesn't work.
.top_bottom_b1{
display: block;
width:50px;
height:50px;
background-color:red;
}
.top_bottom_b2{
display: block;
width:50px;
height:50px;
background-color:yellow;
top: 8px;
}
.top_bottom_b1:hover .top_bottom_b2{
display: none;
}
<body>
<div class="top_bottom_b1"></div>
<div class="top_bottom_b2"></div>
</body>
EDIT:
Even if there are multiple elements(as shown below) between and , the css selector (hover) should work.
<div class="top_bottom_b1"></div>
<div></div>
<div></div>
<div></div>
... <!-- N number of divs -->
<div class="top_bottom_b2"></div>
Use the adjacent sibling selector + or if their not director siblings use the general sibling selectors - ~:
.top_bottom_b1 {
display: block;
width: 50px;
height: 50px;
background-color: red;
}
.top_bottom_b2 {
display: block;
width: 50px;
height: 50px;
background-color: yellow;
top: 8px;
}
div > .top_bottom_b2 {
background: blue;
}
.top_bottom_b1:hover ~ .top_bottom_b2 {
display: none;
}
.top_bottom_b1:hover ~ div > .top_bottom_b2 {
display: none;
}
<div class="top_bottom_b1"></div>
<div>I'm in the middle</div>
<div class="top_bottom_b2"></div>
<div>
<div class="top_bottom_b2"></div>
</div>

How to target two elements with one click?

I want to change the background-color of a box after clicking on it and at the same time create another box with pure CSS. I tried it with the target selector. But I only can manage to do one of them asks and not both at the same time.
Here is a DEMO of my try.
/* fonts */
p {
font-size: 10px;
}
#school::after,
#work::after {
font-size: 10px;
content: "Second box";
color: white
}
/* white boxes */
.panel {
height: 50px;
width: 50px;
background-color: white;
border: 1px solid #262626;
position: relative;
}
/* span (100%, 100%) inside the white-boxes */
.panel span {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
/* second-box */
.panel div {
display: none;
}
/* if white-box is targeted, this lets the second-box appear */
.panel div:target {
display: block;
height: 50px;
width: 50px;
background-color: blue;
border: 1px solid black;
border-radius: 4px;
position: absolute;
left: 70px;
}
/* for testing purposes */
.panel:active span {
background-color: black;
}
<p>White Boxes</p>
<div class="panel">
<a href="#school">
<span></span>
</a>
<div id="school"></div>
</div>
<div class="panel">
<a href="#work">
<span></span>
</a>
<div id="work"></div>
</div>
You can modify the presentation of any number of elements using the :target pseudo-class, so long as each one is nested within the element with the id which is :targeted:
/* fonts */
p {
font-size: 10px;
}
#school div::after, #work div::after {
font-size: 10px;
content: "Second box";
color: white
}
/* white boxes */
.panel {
height: 50px;
width: 50px;
background-color: white;
border: 1px solid #262626;
position: relative;
}
/* span (100%, 100%) inside the white-boxes */
.panel span {
position:absolute;
width:100%;
height:100%;
top:0;
left: 0;
}
/* second-box */
.panel a div {
display: none;
}
/* if white-box is targeted, this lets the second-box appear */
.panel a:target div {
display: block;
height: 50px;
width: 50px;
background-color: blue;
border: 1px solid black;
border-radius: 4px;
position: absolute;
left: 70px;
}
/* if white-box is targeted, this gives the box a blue background */
.panel a:target span {
background-color: blue;
}
/* for testing purposes */
.panel:active span {
background-color: black;
}
<p>White Boxes</p>
<div class="panel">
<a href="#school" id="school">
<span></span>
<div></div>
</a>
</div>
<div class="panel">
<a href="#work" id="work">
<span></span>
<div></div>
</a>
</div>
:target is only for one element at same time, because you can't target two anchors at same time. You need javascript or a css trick with :checked
Solution with pure css :checked
http://jsfiddle.net/KNG6n/78/
What I make:
<div class="panel">
<label>
<input type="checkbox">
<div></div>
</label>
</div>
CSS
label {
display:block;
position: relative;
width: 100%;
height: 100%;
}
label input {
visibility: hidden;
}
label input:checked + div {
display: block;
}
I see an answer was accepted already, but will post my solution if anyone else is interested.
I changed the location of the div to be before the link and added a css rule.
new code:
<p>White Boxes</p>
<div class="panel">
<div id="school"></div>
<a href="#school">
<span></span>
</a>
</div>
<div class="panel">
<div id="work"></div>
<a href="#work">
<span></span>
</a>
</div>
CSS added:
.panel div:target+a span{
background-color: black;
}
Demo:
http://jsfiddle.net/KNG6n/81/

How to style object when the user clicks it

In css for the links I can style the as before user clicks and after user clicks. But how can I do this for simple text or an object that when a user clicks that object then change its color.
For Example
<style>
.object:onmouseclick {
background-color:green;
padding:5px;
}
</style>
What we have to write in place of onmouseclick.
If you were styling a link you would use :active or :focus but as you're using this on a dom element, you would have to use jQuery to add a class to the clicked item and apply the style through that class...
$('.object').click(function() {
$(this).toggleClass('clicked');
});
.object {
background-color: green;
padding: 5px;
cursor: pointer;
width: 300px;
color: orange;
}
.object.clicked {
background-color: blue;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="object">
<h1>The Mutants Are Revolting</h1>
<p>Bender?! You stole the atom. That's the ONLY thing about being a slave. I am Singing Wind, Chief of the Martians. Anyhoo, your net-suits will allow you to experience Fry's worm infested bowels as if you were actually wriggling through them.</p>
</div>
button:active or button:focus should do the trick.
a {
background-color: red;
}
a:active {
background-color: #efefef;
}
myButton
and optionally
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.object{
background:green;
padding:5px;
width: 300px;
height: 300px;
position: relative;
}
input{
display: none;
}
label{
display: block;
width: 300px;
height: 300px;
}
input:checked ~ label{
position: absolute; top: 0; left: 0;
width: 100%;
height: 100%;
background: #00f url(http://www.pubzi.com/f/sm-chess-horse.png) no-repeat center center;
border: 1px solid #000;
}
<div class="object">
<input type="checkbox" id="c1" name="checkbox" />
<label for="c1"></label>
</div>