I have given an input element type checkbox, its fine but when its checked tick seems very big.
I need to make smaller that tick symbol
here is html content from browser:
<div class="defaultRoot">
<div class="defaultBody ">
<div class="checkboxWrapper">
<input id="kis-9l7v7o3vr" type="checkbox" class="kischeckbox" style="width: 24px; height: 24px; accent-color: rgb(47, 55, 93); border: 4px solid gray; border-radius: 0px; display: block; transition: border-color 0ms ease 0s, background-color 0ms ease 0s; cursor: default;">
</div>
<div class="labelWrapper">
<label class="label" data-disabled="false" for="kis-9l7v7o3vr">I agree to sell my privacy</label>
</div>
</div>
</div>
How can I make that tick smaller and thinner
Currently the browser does not (and will not) support virtual elements to the tick of the checkbox you have to customize them yourself. refer here: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_custom_checkbox
using CSS you can adjust the size of the tick inside the checkbox as follow
<!DOCTYPE html>
<html>
<head>
<title>Adjust Checkbox Tick Size</title>
<style>
.checkboxWrapper, .labelWrapper {
display: inline-block;
vertical-align: middle;
}
input[type=checkbox].kischeckbox {
display: inline-block;
width: 14px;
height: 14px;
accent-color: rgb(47, 55, 93);
border: 4px solid gray;
border-radius: 0px;
display: block;
transition: border-color 0ms ease 0s;
cursor: default;
vertical-align: middle;
}
input[type=checkbox].kischeckbox:checked + label::before {
content: "\2714"; /* tick symbol */
font-size: 16px; /* adjust the size of the tick here */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="defaultRoot">
<div class="defaultBody ">
<div class="checkboxWrapper">
<input id="kis-9l7v7o3vr" type="checkbox" class="kischeckbox">
</div>
<div class="labelWrapper">
<label class="label" data-disabled="false" for="kis-9l7v7o3vr">I agree to sell my privacy</label>
</div>
</div>
</div>
</body>
</html>
I want to create a cool animation that when the user clicks on the email input or the password the label of the input will go up and have a nice transition in the bottom border.
This is what I have:
And this is what I want to create:
My code:
.form {
position: relative;
display: flex;
flex-direction: column;
margin-bottom: 1rem;
}
.input {
background: none;
color: #c6c6c6;
font-size: 1.8rem;
padding: 1.6rem;
display: block;
border: none;
border-bottom: 1px solid #c6c6c6;
width: 100%;
}
.label {
position: absolute;
color: #c6c6c6;
font-size: 1.6rem;
left: 0.5rem;
top: 1rem;
}
<div class="form">
<input class="email input" type="email" name="email" />
<label class="label" for="email">Email Address</label>
</div>
<div class="form">
<input class="password input" type="password" name="password" />
<label class="label" for="password">Password</label>
</div>
I've searched a lot but every code example I found was with SCCS, SASS and I don't understand it. So please help me with plain CSS. Any help would be greatly appreciated!
I created an animated demo using HTML and CSS.
.main {
width: 500px;
margin: 50px 100px;
;
}
.form-group {
position: relative;
margin-bottom: 45px;
}
input {
display: block;
width: 300px;
font-size: 14pt;
padding: 10px;
border: none;
border-bottom: 1px solid #ccc;
}
input:focus {
outline: none;
}
label {
position: absolute;
top: 10px;
left: 5px;
color: #999;
font-size: 14pt;
font-weight: normal;
pointer-events: none;
transition: all 0.2s ease;
}
input:focus ~ label,
input:valid ~ label {
top: -20px;
font-size: 10pt;
color: #5264AE;
}
.bar {
display: block;
position: relative;
width: 320px;
}
.bar:before,
.bar:after {
content: "";
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #5264AE;
transition: all 0.2s ease;
}
.bar:before {
left: 50%;
}
.bar:after {
right: 50%;
}
input:focus ~ .bar:before,
input:focus ~ .bar:after {
width: 50%;
}
<div class="main">
<div class="form-group">
<input type="text" name="email" required />
<span class="highlight"></span>
<span class="bar"></span>
<label for="email">Email</label>
</div>
<div class="form-group">
<input type="password" name="password" required />
<span class="highlight"></span>
<span class="bar"></span>
<label for="password">Password</label>
</div>
</div>
I attached your code modified to work as intended, you can change any value to customize as you want but here's an explanation of how it works:
Input element
I added a z-index so that the user can click where the label is positioned and click the input instead of the label itself, also added a transition to make the animation smooth.
input:focus refers when input is active (user click or selected by pressing tab key).
And here's where the magic happens and the explanations of complex selectors:
.input:focus~.label,
.input:not([value=""]):not(:focus):invalid~.label,
.input.has-content~.label {
font-size: .7rem;
top: -.4rem;
color: blue;
}
.input:focus ~ .label selects all input's element siblings with the class .label when input is focus so that when user focus in the input the label'll be above.
.input:not([value=""]):not(:focus):invalid~.label this selector'll catch when user unfocus the input but it got content, so the label don't goes down and overlap with the username. (as the password type input doesn't have value attribute I attach you a Js snippet that do the same trick for password element, simply adding the class has-content to the element)
Hope it helps you!
document.querySelector('.password').oninput = (e) => {
e.target.value.length > 0 ?
e.target.classList.add('has-content') :
e.target.classList.remove('has-content')
}
.form {
position: relative;
display: flex;
flex-direction: column;
margin: 1rem;
}
.input {
height: 20px;
background: none;
color: #c6c6c6;
font-size: 1rem;
padding: .5rem .7rem;
display: block;
border: none;
border-bottom: 2px solid #c6c6c6;
width: 100%;
z-index: 2;
transition: all .3s ease;
}
.input:focus {
outline: transparent;
border-color: blue;
}
.input:focus~.label,
.input:not([value=""]):not(:focus):invalid~.label,
.input.has-content~.label {
font-size: .7rem;
top: -.4rem;
color: blue;
}
.label {
position: absolute;
color: #c6c6c6;
font-size: 1rem;
left: 0.5rem;
top: .6rem;
transition: all .3s ease;
}
<div class="form">
<input class="email input" type="email" name="email" />
<label class="label" for="email">Email Address</label>
</div>
<div class="form">
<input class="password input" type="password" name="password" />
<label class="label" for="password">Password</label>
</div>
I've posted about this project before. Twice, actually. And while the answers have helped me to better understand my situation, they haven't really been applicable to my situation. I blame myself because I was posting a skeletal version of the final code which didn't fully illustrate what I needed to accomplish.
Essentially: I need to integrate a relatively simple checkbox hack into a CMS, but the CMS strips id selectors. Hence, code that ought to look something like this:
<input type="checkbox" name="thisisaname" id="thisisanid"><label class="thisisanid" for="thisisanid">Type 1</label>
...ends up like this:
<input type="checkbox" name="thisisaname"><label class="thisisanid" for="thisisanid">Type 1</label>
Predictably, this breaks everything, and ostensibly any adaptation appears to be impossible. There is no CMS-specific alternative like ClientID=. Neither jQuery nor JavaScript are available to me (they're also stripped out by the CMS).
It's a case of something being extraordinarily simple to do, but being constrained by a CMS that multiplies the difficulty to the point where I'm uncertain if it's even possible. I am not a CSS maven. I know it only as much as I need to do these little projects for a few friends, and I apologize for posting about this yet again, but it's driving me crazy not knowing if this is something which ought to be shelved.
This is the code. Obviously, it's sloppy and it isn't in its fully-styled form, but it's close enough that I think it's a better example than what I've posted in the past:
#basesurround {
background: #000000cc;
margin: 0 auto;
width: 75%;
}
.information-wrap { display:flex; vertical-align:top;}
.information-wrap aside { background: #00000066; vertical-align: top; flex: 1 1 250px; min-width: 150px; padding: 0; max-width: 200px; }
.information-wrap main { vertical-align: top; display: flex; flex-direction: row; flex-wrap: wrap; width: 85%; justify-content: center; padding: 0 0 25px 25px; }
.information-wrap label { background: #000000cc; width: 100%; display: inline-block; border-bottom: 1px solid #000000; color: #9FC3C9; text-transform: capitalize; font-weight: 100; font-size: 11px; letter-spacing: 1px; cursor: pointer; transition: all 0.7s ease; position: relative; padding: 10px 10px 10px 30px; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; word-break: break-word; line-height: 125%; }
.information-wrap label:after { content: ""; width: 25px; height: 100%; position: absolute; left: 0; top: 0; background: #000000; filter: contrast(85%); }
.information-wrap details { position: relative; }
.information-wrap details summary::-webkit-details-marker { display: none; }
.information-wrap details summary::before { content: ""; position: absolute; left: 0; background: ; width: 1.5em; height: 1.5em; transition: transform 0.1s linear;}
.information-wrap summary { width: 100%; padding: 20px; padding-left: 25px; border-bottom: 1px solid #000000; background: #9FC3C9; font-family: Proxima; font-weight: 100; text-transform: uppercase; letter-spacing: 2px; color: #000000; -webkit-transition: all 1s ease; transition: all 1s ease; }
.information-wrap summary:hover { color: #ffffff4a; }
.information-wrap summary:focus {outline: none;}
.information-wrap details[open] > summary { background: #000000; filter: contrast(85%); color: #ffffff; }
.information-wrap details[open] > summary ~ * { animation: open 1s ease; }
.information-wrap details[open] summary:before {transform: rotate(90deg);}
.infocard { flex: 0 0 32.3%; display: inline-block; vertical-align: top; font-family: Proxima, Arial, Helvetica, Sans-Serif; position: relative; margin: .5%; align-items: center; justify-content: center; overflow: hidden; color: #000000; text-align: center; line-height: 160%; background-color: #141414; height: 300px; min-width: 300px; -webkit-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1); -moz-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1); -ms-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1); box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1); -webkit-border-radius: 5px; -moz-border-radius: 5px; -ms-border-radius: 5px; border-radius: 5px; min-width:250px; border: 1px solid #000000;}
.infocard figure { border: 10px solid #DADCDB; margin: 10px 10px; padding: 0; display: inline-block; position: relative;}
.infocard figure img { display: block; height: auto; max-width: 100%; }
.infocard figcaption { color: #000000; font: 400 18px/26px Proxima, Arial, Helvetica, Sans-Serif; padding: .2em 0; position: absolute; bottom: 0; text-align: center; width: 100%; }
.infocard figcaption span { font-size: 14px; color: #ffffff }
.infocard:last-of-type {margin-bottom: 200px;}
.infocard .icons { top: -7px; position: relative; color:#ffffff}
.information-wrap input { display: none; }
input:checked ~ main .infocard { display: none; }
/* INFOCARD TYPE LABELS & CHECK CONTROLS */
/* TYPE SET #1 */
#infotypeone_cont:checked ~ aside .infotypeone_cont,
#infotypetwo_cont:checked ~ aside .infotypetwo_cont,
#infotypethree_cont:checked ~ aside .infotypethree_cont,
#infotypefour_cont:checked ~ aside .infotypefour_cont,
#infotypefive_cont:checked ~ aside .infotypefive_cont
{ background: #000000; filter: contrast(85%); }
#infotypeone_cont:checked ~ main .infotypeone,
#infotypetwo_cont:checked ~ main .infotypetwo,
#infotypethree_cont:checked ~ main .infotypethree,
#infotypefour_cont:checked ~ main .infotypefour,
#infotypefive_cont:checked ~ main .infotypefive
{display: inline-block;}
/* TYPE SET #2 */
#factiontypeone_cont:checked ~ aside .factiontypeone_cont,
#factiontypetwo_cont:checked ~ aside .factiontypetwo_cont,
#factiontypethree_cont:checked ~ aside .factiontypethree_cont,
#factiontypefour_cont:checked ~ aside .factiontypefour_cont,
#factiontypefive_cont:checked ~ aside .factiontypefive_cont
{ background: #000000; filter: contrast(85%); }
#factiontypeone_cont:checked ~ main .factiontypeone,
#factiontypetwo_cont:checked ~ main .factiontypetwo,
#factiontypethree_cont:checked ~ main .factiontypethree,
#factiontypefour_cont:checked ~ main .factiontypefour,
#factiontypefive_cont:checked ~ main .factiontypefive
{display: inline-block;}
/* TYPE COLORS #1 */
.infotypeone {background-color: #ff00004d;}
.infotypetwo {background-color: #0076ff4d;}
.infotypethree {background-color: #ffac004d;}
.infotypefour {background-color: #ff00fc4d;}
.infotypefive {background-color: #d800004d;}
/* INFORMATION CARDS */
.infocard * { -webkit-box-sizing: border-box; box-sizing: border-box; -webkit-transition: all 0.25s ease; transition: all 0.25s ease; }
.infocard .background { width: 100%; vertical-align: top; opacity: 0.2; -webkit-filter: grayscale(100%) blur(10px); filter: grayscale(100%) blur(10px); -webkit-transition: all 2s ease; transition: all 2s ease; }
.infocard figcaption { width: 100%; padding: 15px 25px; position: absolute; left: 0; top: 50%; }
figure.infocard img { display: inline;}
figure.infocard .profile {border-radius: 50%; position: absolute; bottom: 50%; left: 50%; max-width: 100px; opacity: 1; box-shadow: 3px 3px 20px rgba(0, 0, 0, 0.5); border: 2px solid rgba(255, 255, 255, 0.5); -webkit-transform: translate(-50%, 0%); transform: translate(-50%, 0%); }
figure.infocard img.profile { height: 100px; width: 100px; }
figure.infocard h3 { line-height: 160%; margin: 0 0 5px; font-weight: 100; font-family: Proxima, Arial, Helvetica, Sans-Serif; text-transform: uppercase; text-indent: 0px; }
figure.infocard h3 a { text-decoration: none; letter-spacing: .3em; color: #9FC3C9cc; line-height: 18px; font-size: 15px; -webkit-transition: all 1s ease; transition: all 1s ease; }
figure.infocard h3 a:hover {opacity: .3; }
figure.infocard h3 span a, figure.infocard h3 span { font-size: 8px; opacity: 0.75; letter-spacing: 2px; display: inline-block; line-height: 10px; }
figure.infocard i { padding: 10px 5px; display: inline-block; font-size: 32px; color: #ffffff; text-align: center; opacity: 0.65;}
figure.infocard a {text-decoration: none; background-size: 0;}
figure.infocard i:hover {opacity: 1; -webkit-transition: all 0.35s ease; transition: all 0.35s ease; }
figure.infocard:hover .background, figure.infocard.hover .background {-webkit-transform: scale(1.3);transform: scale(1.3);}
<div id="basesurround">
<form>
<div class="information-wrap">
<!--- BEGIN INFORMATION CONTROLLER :: CHECKBOX --->
<input type="checkbox" name="cont" id="infotypeone_cont">
<input type="checkbox" name="cont" id="infotypetwo_cont">
<input type="checkbox" name="cont" id="infotypethree_cont">
<input type="checkbox" name="cont" id="infotypefour_cont">
<input type="checkbox" name="cont" id="infotypefive_cont">
<input type="checkbox" name="cont" id="factiontypeone_cont">
<input type="checkbox" name="cont" id="factiontypetwo_cont">
<input type="checkbox" name="cont" id="factiontypethree_cont">
<input type="checkbox" name="cont" id="factiontypefour_cont">
<input type="checkbox" name="cont" id="factiontypefive_cont">
<!--- END INFORMATION CONTROLLER :: CHECKBOX --->
<aside>
<!--- BEGIN INFORMATION CONTROLLER :: LABELS --->
<details><summary>
SUBMENU TITLE
</summary>
<label class="infotypeone_cont" for="infotypeone_cont">Check 1</label>
<label class="infotypetwo_cont" for="infotypetwo_cont">Check 2</label>
<label class="infotypethree_cont" for="infotypethree_cont">Check 3</label>
<label class="infotypefour_cont" for="infotypefour_cont">Check 4</label>
<label class="infotypefive_cont" for="infotypefive_cont">Check 5</label>
</details>
<!--- END INFORMATION CONTROLLER :: LABELS --->
<!--- BEGIN INFORMATION CONTROLLER --->
<details><summary>
SUBMENU TITLE
</summary>
<label class="factiontypeone_cont" for="factiontypeone_cont">Faction 1</label>
<label class="factiontypetwo_cont" for="factiontypetwo_cont">Faction 2</label>
<label class="factiontypethree_cont" for="factiontypethree_cont">Faction 3</label>
<label class="factiontypefour_cont" for="factiontypefour_cont">Faction 4</label>
<label class="factiontypefive_cont" for="factiontypefive_cont">Faction 5</label>
</details>
<!--- END INFORMATION CONTROLLER --->
</aside>
<main>
<!--- BEGIN INFORMATION CARD --->
<figure class="infocard infotypeone factiontypeone">
<img src="ICON" class="background"/>
<img src="ICON" class="profile"/>
<figcaption> <h3>
TITLE
<br><span>
SUBTITLE ●
MAIN LINK <br>
INFO #2 | INFO #3
</span></h3><div class="icons">
<i class="ion-ios-person-outline"></i>
<i class="ion-ios-email-outline"></i>
<i class="ion-ios-location-outline"></i>
</div></figcaption></figure>
<!--- END INFORMATION CARD --->
<!--- BEGIN INFORMATION CARD --->
<figure class="infocard infotypetwo factiontypetwo">
<img src="ICON" class="background"/>
<img src="ICON" class="profile"/>
<figcaption> <h3>
TITLE
<br><span>
SUBTITLE ●
MAIN LINK <br>
INFO #2 | INFO #3
</span></h3><div class="icons">
<i class="ion-ios-person-outline"></i>
<i class="ion-ios-email-outline"></i>
<i class="ion-ios-location-outline"></i>
</div></figcaption></figure>
<!--- END INFO CARD --->
<!--- BEGIN INFO CARD --->
<figure class="infocard infotypethree factiontypethree">
<img src="ICON" class="background"/>
<img src="ICON" class="profile"/>
<figcaption> <h3>
TITLE
<br><span>
SUBTITLE ●
MAIN LINK <br>
INFO #2 | INFO #3
</span></h3><div class="icons">
<i class="ion-ios-person-outline"></i>
<i class="ion-ios-email-outline"></i>
<i class="ion-ios-location-outline"></i>
</div></figcaption></figure>
<!--- END INFO CARD --->
<!--- BEGIN INFO CARD --->
<figure class="infocard infotypefour factiontypefour">
<img src="ICON" class="background"/>
<img src="ICON" class="profile"/>
<figcaption> <h3>
TITLE
<br><span>
SUBTITLE ●
MAIN LINK <br>
INFO #2 | INFO #3
</span></h3><div class="icons">
<i class="ion-ios-person-outline"></i>
<i class="ion-ios-email-outline"></i>
<i class="ion-ios-location-outline"></i>
</div></figcaption></figure>
<!--- END INFO CARD --->
<!--- BEGIN INFO CARD --->
<figure class="infocard infotypefive factiontypefive">
<img src="ICON" class="background"/>
<img src="ICON" class="profile"/>
<figcaption> <h3>
TITLE
<br><span>
SUBTITLE ●
MAIN LINK <br>
INFO #2 | INFO #3
</span></h3><div class="icons">
<i class="ion-ios-person-outline"></i>
<i class="ion-ios-email-outline"></i>
<i class="ion-ios-location-outline"></i>
</div></figcaption></figure>
<!--- END INFO CARD --->
</main></div>
</form></div>
I am in a quandary. I'm not very knowledgeable in CSS, and this CMS (which I must use, unfortunately) makes even the easiest of tasks inordinately difficult. Here's what I've tried:
Using nth-child / nth-of-type selectors: I was given this idea by
Temani Afif. This solution is elegant and I loved it, but
unfortunately, because of what the code will be used for, the
structure will vary often.
Placing the input above the labels: I've been advised to look
into this, but I can't find any information on how to properly code
with the input above the label, so I'm unsure if it can be used in this
situation.
I thought about mimicking the behavior I want using
[attribute|=value] selectors instead of id, but I haven't been
able to get it to work. I don't know, though, whether I'm being
limited by the code or my own incompetence, so I'm unsure if it's
something worth looking into further.
So… Is there a recommended means of overcoming the id= limitations I've missed in my research?
FWIW, the purpose of this is to create a simple(ish) code where it's easy to append the INFO CARD part of the code as many times as needed to edit this for sorting movies/books/other information of that nature, while maintaining the ease at which one can change the figure class=.
The issue here is the rather limited way in which the for attribute works inside a <label> - it looks for a form control with the corresponding id. (Only the id will do - no other attribute.)
If it cannot find one, then no form control ends up being associated with the <label>... consequently, the checkbox hack, which relies on the for attribute working, cannot produce the intended effect.
Since we can't add the id to the checkbox in the document's initial mark up, why not, instead, add the id to the checkbox dynamically, by accessing the Document Object Model (DOM)?
This is easily done in a single line:
document.querySelector('[name="thisisaname"]').id = 'thisisanid';
See the Working Example below, noting that the id is not present in the initial markup:
document.querySelector('[name="thisisaname"]').id = 'thisisanid';
label {
cursor: pointer;
}
label:hover {
font-weight: bold;
text-decoration: underline;
}
input[type=checkbox],
input[type=checkbox] + p {
opacity: 0;
}
input[type=checkbox] + p {
transition: opacity 1s linear;
}
input[type=checkbox]:checked + p {
opacity: 1;
}
<label class="thisisanid" for="thisisanid">Click Me</label>
<input type="checkbox" name="thisisaname">
<p>Checkbox hack is working</p>
I have a hidden sidebar menu on my website, if I open my website on my desktop, my hidden sidebar has 100px width (not full), so to reveal it all you must do is hover over it. If I open my website on a mobile device, my hidden sidebar has no width, and I must click the button to reveal the sidebar.
HTML:
<div id="mySidenav" class="sidenav sidenav-wrapper">
<img class="logo1" src="../img/logo1.png"><br><br>
Home
About
Contact
</div>
<div id="contoh" class="content-section-b">
<div class="mobiletoggle">
<label class="fa fa-bars" for="showblock"></label>
<input type="checkbox" id="showblock" />
</div>
</div>
In the code above I use checkbox to change 'click', because I just want to use CSS.
CSS:
.sidenav {
height: 100%;
width: 100px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 60px;
transition: 0.8s;
}
.logo1 {
height: 10%;
width: auto;
padding-left: 20px;
transition: 0.8s;
}
.sidenav:hover {
width: 250px;
transition: 0.8s;
}
.sidenav:hover ~ #contoh {
background-color: rgba(0,0,0,0.4);
filter: grayscale(100%);
}
.sidenav:hover .logo1 {
height: auto;
width: 95%;
padding-left: 10px;
transition: 0.8s;
}
.sidenav a {
float:left;
width:100%;
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 20px;
color: #818181;
display: block;
opacity: 0;
transition: opacity 1300ms
}
.sidenav a:hover, .offcanvas a:focus {
color: #f1f1f1;
background-color:#5fa2db;
font-size: 30px;
}
#showblock {
display: none;
}
#media screen and (min-width:768px) {
.mobiletoggle {
display:none;
}
}
#media screen and (max-width:767px) {
.sidenav {
width:0px;
}
#showblock:checked + .sidenav {
width: 250px;
transition: 0.8s;
}
}
In the code above I used :checked, it's just a little bit different if I use :active. The problem is why my :checked/:active did not work? I looked into this jsFiddle, and it works fine, so why can't I use it? Any suggestions?
As per your CSS, modify your HTML to:
<input type="checkbox" id="showblock">
<div id="mySidenav" class="sidenav sidenav-wrapper">
<img class="logo1" src="../img/logo1.png"><br><br>
Home
About
Contact
</div>
<div id ="contoh" class="content-section-b">
<div class="mobiletoggle">
<label class="fa fa-bars" for="showblock"></label>
</div>
</div>
The input #showblock should be above the div #mySidenav in order for your CSS to work.
#showblock:checked + .sidenav {
width: 250px;
transition: 0.8s;
}
I need that default filter value should be set, instead of showing all elements.
Once Page is loaded, By default all values are getting displayed and on click of anchor specific filter values are getting displayed (like blue color in this example).
For Given Example:
After page load, instead of showing all color, Blue color elements needs to be shown. By default all values are getting displayed, but I need a specific color data value.
CSS:
section {
display: block;
float: left;
border: 2px solid green;
min-height: 300px;
width: 100%;
border-radius: 4px;
}
a {
display: block;
float: left;
width: 25%;
text-decoration: none;
text-align: center;
padding: 5px 0;
color: white;
background: #1271C7;
}
div {
display: block;
float: left;
height: 40px;
width: 40px;
border: 1px solid black;
margin: 10px;
-webkit-transition: all .8s linear;
-moz-transition: all .8s linear;
-o-transition: all .8s linear;
-ms-transition: all .8s linear;
transition: all .8s linear;
margin-top: 20px;
}
div[data-filter="red"] {
background: red;
}
div[data-filter="green"] {
background: green;
}
div[data-filter="blue"] {
background: blue;
}
a:focus[data-filter] {
opacity: .8;
outline: none;
}
a[data-filter="red"]:focus ~ div:not([data-filter="red"]) {
height: 0px;
width: 0px;
border: none;
margin: 0;
}
a[data-filter="green"]:focus ~ div:not([data-filter="green"]) {
height: 0px;
width: 0px;
border: none;
margin: 0;
}
a[data-filter="blue"]:focus ~ div:not([data-filter="blue"]) {
height: 0px;
width: 0px;
border: none;
margin: 0;
}
HTML:
<section>
<a id="tab2" href="#" data-filter="red" tabindex="-1">RED</a>
<a id="tab3" href="#" data-filter="green" tabindex="-1">GREEN</a>
<a id="tab4" href="#" data-filter="blue" tabindex="-1">BLUE</a>
<div data-filter="red"/>
<div data-filter="blue"/>
<div data-filter="red"/>
<div data-filter="blue"/>
<div data-filter="green"/>
<div data-filter="red"/>
<div data-filter="red"/>
<div data-filter="red"/>
<div data-filter="blue"/>
<div data-filter="green"/>
<div data-filter="blue"/>
<div data-filter="green"/>
<div data-filter="green"/>
</section>
You can use jQuery to customize your app. Using css alone you can't do this. here is the solution using jquery.
$(document).ready(function(){
$('div[data-filter="red"').show(0); // only red column on page load
$("a").click(function(e){
e.preventDefault();
var x=$(this).attr("data-filter"); //returns data-filter value of a tag
if(x=="all"){
$('div[data-filter').show(0);
}
else{
$("div[ data-filter]").hide(0);
$('div[data-filter="' + x +'"]').show(0);
}
})
})