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>
Related
I've searched around and checked various answers, but I'm having trouble with the following:
There are a couple caveats
Can't use Javascript or Jquery.
has to be pure CSS.
I want the background color of the label to change from Blue to Orange after the input is selected. It does not seem to work and I've checked around and read several answers from people, but none of them have worked for me and I'm not sure what I'm doing wrong.
The CSS:
.header-tabs-container {
position: relative;
float: center;
left: 25%;
clear: both;
z-index: 2;
border-collapse: collapse;
white-space: normal;
border: unset;
}
/* tabs names */
.header-tabs-container .header-label {
position: relative;
padding: clamp(-1.5rem, -3.2rem + 8.8889vw, 3rem);
font-size: clamp(0.95rem, -0.925rem + 8.333vw, 3rem);
background-color: blue;
color: #fff;
cursor: pointer;
user-select: none;
text-align: center;
z-index: 1;
margin: 0px;
border: white 1px solid;
white-space: nowrap;
border-radius: 40px 40px 0px 0px;
}
/* Hover effect on tabs names */
.header-tabs-container .header-label:hover {
background: orange;
color: blue;
transition: 0.2s;
}
/* Content area for tabs */
.header-tab-content {
position: relative;
background: #eee;
margin-top: -10px;
width: 100%;
min-height: 100vh;
padding: 0px;
float: left;
box-sizing: border-box;
z-index: 2;
display: none;
white-space: nowrap;
border: 1px solid blue;
}
.header-tab-content:after {
content: "";
clear: both;
}
/* Hide input radio from users */
input[name="header-tab"] {
display: none;
}
/* Show tab when input checked */
input[name="header-tab"]:checked + .header-tab-content {
display: block;
transition: 0.5s ease-out;
}
input[name="header-tab"]::after + .header-label {
background-color: orange;
}
The HTML
<section class="header-tabs-container">
<label class="header-label" for="header-tab1">Tab1</label><!--
--><label class="header-label" for="header-tab2">Tab2</label>
</section>
<input name="header-tab" id="header-tab1" type="radio" checked/>
<section class="header-tab-content">
<h3>Test</h3>
Content
</section>
<input name="header-tab" id="header-tab2" type="radio" />
<section class="header-tab-content">
<h3> test</h3>
content
</section>
</section>
Basically everything works as expected... Except I cannot, for the life of me, get the following to work at all.
input[name="header-tab"]:checked + header-label {
background-color: orange;
}
Any ideas or advice would be greatly appreciated.
Thank you in advance.
In CSS you cannot select previous siblings, therefore you'll need move input above your tabs and use ~ for sibling selections for the content:
.header-tabs-container {
position: relative;
float: center;
/* left: 25%;*/
clear: both;
z-index: 2;
border-collapse: collapse;
white-space: normal;
border: unset;
}
/* tabs names */
.header-tabs-container .header-label {
position: relative;
padding: clamp(-1.5rem, -3.2rem + 8.8889vw, 3rem);
font-size: clamp(0.95rem, -0.925rem + 8.333vw, 3rem);
background-color: blue;
color: #fff;
cursor: pointer;
user-select: none;
text-align: center;
z-index: 1;
margin: 0px;
border: white 1px solid;
white-space: nowrap;
border-radius: 40px 40px 0px 0px;
}
/* Hover effect on tabs names */
.header-tabs-container .header-label:hover {
background: orange;
color: blue;
transition: 0.2s;
}
/* Content area for tabs */
.header-tab-content {
position: relative;
background: #eee;
margin-top: -10px;
width: 100%;
min-height: 100vh;
padding: 0px;
float: left;
box-sizing: border-box;
z-index: 2;
display: none;
white-space: nowrap;
border: 1px solid blue;
}
.header-tab-content:after {
content: "";
clear: both;
}
/* Hide input radio from users */
input[name="header-tab"] {
display: none;
}
/* Show tab when input checked */
input[name="header-tab"]:nth-of-type(1):checked ~ .header-tab-content:nth-of-type(1),
input[name="header-tab"]:nth-of-type(2):checked ~ .header-tab-content:nth-of-type(2) {
display: block;
transition: 0.5s ease-out;
}
input[name="header-tab"]:checked + .header-label {
background-color: orange;
}
<section class="header-tabs-container">
<input name="header-tab" id="header-tab1" type="radio" checked/>
<label class="header-label" for="header-tab1">Tab1</label>
<input name="header-tab" id="header-tab2" type="radio" />
<label class="header-label" for="header-tab2">Tab2</label>
<section class="header-tab-content">
<h3>Test</h3>
Content
</section>
<section class="header-tab-content">
<h3> test</h3>
content
</section>
</section>
The + combinator matches the second element only if it immediately
follows the first element.
CSS_Selectors
Move the labels and inputs inside the same section and then try ~ or +.
input must be first.
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>
I'm trying to discover a way that doesn't use javascript that allows you to hover over the smaller divs (or images) to change the background of the larger div. Is this possible purely with HTML & CSS?
The example has 2 problems:
1. Only rolling over one of the divs works (because it's straight after)
2. When rolling over that div, the background of the main div reverts after moving the mouse off, so it's not a permanent change
I'm very curious and appreciate any advice here, thanks!
UPDATE:
I've just created this: https://jsfiddle.net/ehzsmusr/
The backgrounds seem to change, but don't stay when you hover over something else. Can this be fixed?
#main {
width: 300px;
height: 200px;
background: red;
float: left;
}
.hover1 {
float: left;
background: blue;
width: 100px;
height: 75px;
}
.hover2 {
float: left;
background: green;
width: 100px;
height: 75px;
}
.hover1:hover + #main {
background: #ccc
}
.hover2:hover + #main {
background: #ccc
}
<div class='container'>
<div class='hover1'></div>
<div class='hover2'></div>
<div id='main'></div>
</div>
if you don't mind clicking as you mentioned in the comment, here's one implementation of the checkbox hack mentioned by #kabanus ( using radio buttons instead )
body {
margin: 0;
padding: 0;
}
#container {
width: 100vw;
height: 100vh;
background: #eee;
}
input {
display: none;
}
label {
display: block;
width: 50px;
height: 50px;
float: left;
margin: 20px;
}
#redL {
background: red;
}
#greenL {
background: green;
}
#blueL {
background: blue;
}
#red:checked ~ #big {
background: red;
}
#green:checked ~ #big {
background: green;
}
#blue:checked ~ #big {
background: blue;
}
#big {
width: 50vw;
height: 50vh;
background: #fff;
clear: both;
margin: auto;
}
<div id="container">
<input type="radio" id="red" name="color" />
<label for="red" id="redL"></label>
<input type="radio" id="green" name="color" />
<label for="green" id="greenL"></label>
<input type="radio" id="blue" name="color" />
<label for="blue" id="blueL"></label>
<div id="big">
</div>
</div>
Another hack would be setting the transition-delay to 604800s ( or more ) so the color changes and goes back after that numbers of seconds ( one week later ).
body {
margin: 0;
padding: 0;
}
#container {
width: 100vw;
height: 100vh;
background: #eee;
}
#redL {
background: red;
}
#greenL {
background: green;
}
#blueL {
background: blue;
}
label {
display: block;
width: 50px;
height: 50px;
float: left;
margin: 20px;
}
#redL:hover ~ #big {
background: red;
transition-delay: 0s;
}
#greenL:hover ~ #big {
background: green;
transition-delay: 0s;
}
#blueL:hover ~ #big {
background: blue;
transition-delay: 0s;
}
#big {
width: 50vw;
height: 50vh;
background: #fff;
clear: both;
margin: auto;
transition: all .1s 604800s;
}
<div id="container">
<label id="redL"></label>
<label id="greenL"></label>
<label id="blueL"></label>
<div id="big">
</div>
</div>
I need the radio button below to be composed of two separate boxes, rather than one:
<!DOCTYPE html>
<html>
<head>
<style>
.navText {
font-family: Helvetica, sans-serif;
font-size: 15px;
font-weight: 400;
display: table-cell;
text-align: center;
vertical-align: middle;
color: white;
}
input[type=radio] {
display: none;
}
.overlay {
position: relative;
float: left;
width: 100px;
height: 50px;
display: table;
background-color: grey;
}
input[type=radio]:hover + .overlay {
background-color: red;
}
input[type=radio]:checked + .overlay {
background-color: red;
}
</style>
</head>
<body>
<label><input type="radio" name="nav" onclick="document.location='#';">
<div class="overlay"><h1 class="navText">Home</h1></div></label>
</body>
</html>
I would like it to work like this (but still as a radio button):
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 153px;
height: 50px;
float: left;
}
.container:hover div {
background-color: red;
}
.box1 {
width: 50px;
height: 50px;
float: left;
margin-right: 3px;
background-color: grey;
}
.box2 {
width: 100px;
height: 50px;
float: left;
background-color: grey;
}
</style>
</head>
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
How can I make the radio button be composed of two separate boxes?
You could add another box using pseudo-element :after
.overlay:after {
content:'';
position: absolute;
width: 100px;
height: 50px;
left:104px;
display: block;
background-color: yellow;
}
.overlay:hover,
.overlay:hover:after {
background-color: red;
}
input[type=radio]:checked + .overlay,
input[type=radio]:checked + .overlay:after {
background-color: red;
}
Also, if you want the overlay to change color on a :hover, you need to apply the styling to the overlay, not to the radio button (as it's not possible to hover the radio button itself).
Fiddle here:
http://jsfiddle.net/0r1tefr8/2/
I am working on a project with a video player. I want to add play/pause and skip buttons together but one of the buttons is always invisible, however working. The codes I am using:
in .css file:
.buttons { position:absolute; top: 326px; left:150px; }
.buttons DIV { width: 26px; height: 20px; cursor: pointer; }
.buttons .pause { background-image: url("button_pause.png"); }
.buttons .play { background-image: url("button_play.png"); }
.buttons .skip { background-image: url("button_skip.png"); }
in html file:
<div class="buttons">
<div id="skip" onclick="skipCurrentSong();"></div>
<div id="playpause" onclick="setPlayPause();"></div>
</div>
the functions in js file work properly but the skip button is invisible. I have tried to create a different class in css file for the skip button and updated the html file accordingly but this gave the same output also. Can anyone say what mistake I am making and how to correct it?
Thanks in advance.
Some extra codes:
.css file:
#CHARSET "UTF-8";
BODY { height: 530px; overflow: hidden; }
#tv { width: 532px; height: 443px; background: url("tv.png") no-repeat; margin: 0 auto; margin-top: 20px; z-index: 3; position: relative; }
#title { color: #dddddd; text-align: right; float: right; margin-top: 320px; margin-right: 120px; }
.buttons { position:absolute; top: 326px; left:150px; }
.buttons DIV { width: 26px; height: 20px; cursor: pointer; background-color: white;}
.buttons .pause { background-image: url("button_pause.png"); }
.buttons .play { background-image: url("button_play.png"); }
.buttons .skip { background-image: url("button_skip.png"); }
FORM { display: block; margin: 0 auto; background: url("player.png"); height: 295px; width: 482px; clear: both; position: relative; top: -421px; margin-bottom: -295px; z-index: 4; }
FORM LABEL { color: #00aad4; margin-bottom: 10px; padding-top: 40px; }
FORM INPUT { border: none; border-bottom: 3px solid #00aad4; font-size: 24px; width: 200px; }
FORM * { display: block; margin: 0 auto; text-align: center; }
FORM .loader { margin-top: 10px; }
.loader { background: url("load.gif"); width: 16px; height: 16px; margin: 0 auto; visibility: hidden; }
.load .loader { visibility: visible; }
in html file:
<div id="tv">
<div id="title"></div>
<div class="buttons">
<div id="playpause" onclick="setPlayPause();"></div>
<div id="skip" onclick="skipCurrentSong();"></div>
</div>
</div>
Updated: This will give you three buttons. Do you want pause/play combined?
CSS:
.buttons { position:absolute; top: 326px; left:150px; }
.buttons div { width: 26px; height: 20px; cursor: pointer; background-color: white;}
.buttons #pause { background-image: url("button_pause.png"); }
.buttons #play { background-image: url("button_play.png"); }
.buttons #skip { background-image: url("button_skip.png"); }
HTML:
<div id="tv">
<div id="title"></div>
<div class="buttons">
<div id="play" onclick="setPlayPause();"></div>
<div id="pause" onclick="setPlayPause();"></div>
<div id="skip" onclick="skipCurrentSong();"></div>
</div>
</div>
I changed the .css code as:
.skipbutton { position:absolute; top:326px; left:120px; }
.skipbutton DIV { width: 26px; height: 20px; cursor: pointer; background-color: gray;}
.skipbutton .skip { background-image: url("button_skip.png"); }
.buttons { position:absolute; top: 326px; left:90px; }
.buttons DIV { width: 26px; height: 20px; cursor: pointer; }
.buttons .pause { background-image: url("button_pause.png"); }
.buttons .play { background-image: url("button_play.png"); }
and changed .html as:
<div id="tv">
<div id="title"></div>
<div class="skipbutton">
<div class="skip" onclick="skipCurrentSong();"></div>
</div>
<div class="buttons">
<div id="playpause" onclick="setPlayPause();"></div>
</div>
</div>
surpsiringly for skip button div class worked while for playpause button, div id works and div class just kills the button. It is a little awkward as the two buttons have the same structe in css file.
I tried to seperate the classes for two buttons earlier but this time it finally worked.
Thanks to lasseespeholt.