How to make a pure CSS bootstrap onclick and hover dropdown menu? - html

There is a pure CSS (no JavaScript) dropdown menu activated on hover, and the menu stays open if you click it.
It's here: Making Animated Dropdown Menu by Using Pure CSS Like Bootstrap does
Here's the code:
html, body {
margin:0;
}
.acn-menu {
text-align: center;
background-color: rgba(0, 0, 0, 0.9);
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
min-height: 74px;
width: 100%;
}
.label_openclose {
display: none;
}
.menu-tabs {
height: 100%;
}
.menu-tabs .elem {
box-sizing: border-box;
padding: 0 20px;
float: left;
height: 100%;
line-height: 70px;
background-color: rgb(30, 30, 30);
color: white;
}
#-webkit-keyframes spin {
0% {
transform: rotate(-180deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(-180deg);
}
}
#keyframes spin {
0% {
transform: rotate(-180deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(-180deg);
}
}
.menu-check:checked ~ .label_openclose {
-webkit-animation-play-state: paused;
animation-play-state: paused;
}
.menu-check {
display: none;
}
.menu-tabs .elem:hover {
background-color: rgba(255, 255, 255, 0.2);
}
/*#media screen and (max-width:55%)*/
#media screen and (max-width:768px) {
.label_openclose {
-webkit-animation: spin 2s;
animation: spin 2s;
display: inline-block;
transform: rotate(-180deg);
transition-duration: 1s;
margin: 10px;
width: 30px;
height: 30px;
border-radius: 50%;
border-top: 10px solid rgb(50, 50, 50);
border-right: 10px solid rgb(100, 100, 100);
border-bottom: 10px solid rgb(150, 150, 150);
border-left: 10px solid rgb(200, 200, 200);
background-color: transparent;
cursor: pointer;
}
.label_openclose:hover {
transform: rotate(180deg);
}
.menu-tabs .elem {
transition: border 1s linear, height 1s;
line-height: initial;
float: initial;
height: 0px;
cursor: pointer;
border-top: 0px solid #000;
overflow: hidden;
}
.menu-tabs:hover .elem {
height: 25px;
}
.menu-check:checked ~ .menu-tabs .elem {
height: 25px;
color: white;
border-top: 2px solid rgba(255, 255, 255, 0.2);
}
.label_openclose:hover ~ .menu-tabs .elem {
border-top: 2px solid rgba(255, 255, 255, 0.2);
height: 25px;
}
}
<div class="acn-menu">
<input type="checkbox" id="openclose" class="menu-check" />
<label class="label_openclose" for="openclose"></label>
<div class="menu-tabs">
<div class="elem">test</div>
<div class="elem">nav</div>
<div class="elem">bar</div>
<div class="elem">with</div>
<div class="elem">transitions</div>
</div>
</div>
<main>
test content of main page</br>The navbar menu stays open when you click on the circle</br>and it even opens on hover, not just on click.
</main>
I would put the drop down where it says "Solutions" in the navbar:
How could I make this work with the default bootstrap 3 navbar menu?

The trick is in the :checked and :hover CSS psuedo-class selectors on a checkbox in combination with ~ (the general sibling combinator). The combinator (~) sounds complicated, but it basically just means select any sibling after the ~ if it is present in html after the selector before the ~. For example:
.before ~ .after {
background-color: orange;
}
...
<div>
<p class = "before">Before</p>
<p class = "after">After</p> <!-- I'll be orange -->
<p class = "after">After</p> <!-- Me too! -->
<p class = "after">After</p> <!-- You get the point -->
</div>
So basically all you need is (1) a checkbox element (2) a label for said checkbox, and (3) a menu (with as many children as you want). And all three have to be siblings so that checking / unchecking the checkbox can toggle the classes of the other two elements via the psuedo-class selectors and the ~ combinator.
In the example you showed, the checkbox display is set to none, but that's just because its ugly. It could easily still be there and the menu toggle would function the same. You can toggle the check with the label alone, so it's doesn't really matter. But the invisible checkbox is what makes everything happen. You could style it directly and forget the label if you wanted to.
So all you need to do is set the menu to hidden, and the menu after the ~ combinator to show if the checkbox is either checked or hovered over:
.menu {
display: none;
}
.check-toggle:checked ~ .menu, .check-toggle:hover ~ .menu {
display: block;
}
...
<input id="checkBox" class="check-toggle" type="checkbox"/>
<label for="checkBox">MENU</label>
<div class="menu">
<div>Menu Items</div>
<div>Menu Items</div>
<div>Menu Items</div>
<div>Menu Items</div>
</div>
It might be an absolute pain to find a perfect replica of this in bootstrap, or it might be easy, I'm not sure. But you don't really need it. You can just add the invisible checkbox, the label, and the menu with the toggling selectors, and then style everything else with bootstrap.You might need to over-power the cascade, but worst comes to worst you can make special toggling selectors with an id instead of a class.
Here is a minimalist working example:
<style>
.check-toggle {
/*display: none;*/
}
.menu {
display: none;
position: absolute;
background-color: white;
border: 2px solid black;
margin-top: -2px;
}
.menu:hover {
display: block;
}
.check-toggle:checked ~ label, .check-toggle:hover ~ label {
color: orange;
}
.check-toggle:checked ~ .menu, .check-toggle:hover ~ .menu {
display: block;
}
</style>
<div>
<input id="checkBox" class="check-toggle" type="checkbox"/>
<label for="checkBox">MENU</label>
<div class="menu">
<div>Menu Items</div>
<div>Menu Items</div>
<div>Menu Items</div>
<div>Menu Items</div>
</div>
</div>

You can use :focus for this. Check out the example code.
Drawback: You cannot toggle the dropdown (You can open it but cannot close it).

Related

CSS element automatically closing when active / selected

I have a searchbar, which is initially hidden until the user "hovers" over the div "searchbar". The issue I had was if the user did not stay hovered over the searchbar, it would then close and be hidden again. I wanted to change this to :active, meaning the user has to click to show and hide ... however, when changing the CSS to :active, the searchbar opens and instantly closes on itself. Also if I press once and hold down the mouse, it stays open...
Any suggestions where I am going wrong?
https://codepen.io/richag_ff/pen/bGayzeP
<div class="searchbar">
#Html.TextBox("SearchText", ViewBag.SearchText as String, new { #class = "search_input", placeholder = "Search by part or reference" })
<i class="fa fa-search search_icon_i"></i>
</div>
.searchbar{
margin-bottom: auto;
margin-top: auto;
height: 60px;
border-radius: 30px;
padding: 10px;
display: flex;
}
.search_input{
color: #858585;
border: 0;
outline: 0;
background: none;
width: 0;
caret-color:transparent;
line-height: 40px;
transition: width 0.4s linear;
}
.searchbar:hover > .search_input{
padding: 0 10px;
width: 215px;
caret-color:#000;
transition: width 0.4s linear;
}
.searchbar:hover > .search_icon{
background: white;
color: #000;
}
.search_icon{
height: 40px;
width: 40px;
float: right;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
color:#858585;
text-decoration:none;
}
Update
OP also needs the input to stay in the "open" state after it has been clicked and returned back to the "closed" state when clicked again. There were changes to the markup:
Add a hidden checkbox
.searchbar is a <label>
.search-icon is a <b> because an interactive tag like <a> will usually result in unexpected behavior when it is in another interactive tag (like <label>).
The toggling feature is possible by leveraging the checkbox/radio button association with <label>:
Figure I
// checkbox is display: none
<input id='switch' type='checkbox'>
// ⇳ id and for must match
<label for='switch' class='searchbar'>
<input id='search' type='search'><b
...
</label>
when a chk/rad input is associated to a <label> -- whenever one is clicked by the user, the other is also clicked remotely. In oder to enable an association, the chk/rad must have an id and the <label> must have a [for] attribute with the chk/rad id (see figure I).
When the <label> is clicked so is the checkbox which in turn changes it's state to :checked. Once checked, it can change all tags that proceed it by the use of adjacent sibling combinator, general sibling combinators, and descendant combinators. Unfortunately, it's not perfect -- because the <label> is not clickable where the input#search resides. Only the areas to the left and right of #search is clickable. I made some outlines to popup whenever the <label> is clicked to indicate to the user that it's in a "locked" state.
:active state only happens when the user keeps the mouse button down. Use :focus on the input. The user clicks the input once and it's in the full length state until the user clicks elsewhere. The .search-icon can be controlled as well be using the adjacent sibling combinator:
Figure II
#search:focus + .search-icon {...
/* If input is focused by user then if the next tag has class
.search-icon, apply the styles on .search-icon */
html {
font: 2ch/1.25 'Segoe UI'
}
.searchbar {
display: flex;
justify-content: center;
align-items: center;
margin: 50px auto;
padding: 0;
line-height: 40px;
border: 4px groove lightgrey;
border-radius: 5px;
cursor: pointer;
}
#search {
display: inline-block;
font: inherit;
width: 0;
border: 0;
outline: 0;
background: none;
caret-color: transparent;
height: 40px;
transition: width 0.4s linear;
}
.searchbar:hover #search,
#search:focus,
#switch:checked+.searchbar #search {
width: 75%;
margin: 0 12px;
padding: 2px 4px;
border: 3px inset rgba(129, 129, 129, 0.3);
border-radius: 5px;
caret-color: #000;
}
#switch:checked+.searchbar #search {
outline: 3px navy solid;
}
#switch:checked+.searchbar {
background: #ddd;
}
.search-icon {
display: flex;
justify-content: center;
align-items: center;
margin-left: -5%;
color: #858585;
text-decoration: none;
cursor: pointer;
}
.searchbar:hover .search-icon,
#search:focus+.search-icon,
#switch:checked+.searchbar .search-icon {
margin-left: 0;
padding: 5px;
border: 2px groove grey;
border-radius: 50%;
transition: border 0.3s linear;
}
#switch:checked+.searchbar .search-icon {
outline: 3px navy solid;
color: navy;
}
.fa-lg {
display: inline-block;
padding: 10px 2.5px;
}
#switch {
display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" rel="stylesheet">
<input id='switch' type='checkbox'>
<label for='switch' class="searchbar">
<input id='search' name='search' type='search'>
<b class="search-icon"><i class="fa fa-search fa-lg"></i></b>
</label>
You need to use :focus for this. But to get focus to work on a div you need to add tabindex="-1" to the div.
Because focus only works for 1 element. You can't focus on the input. Therefor we have to add a jQuery solution to fix it.
See snippet below! ✌️
$('#TmInM').on('focus', function () {
$('#TmInM').addClass('focus');
$('#search').focus();
}).on('blur', function (e) {
$('#TmInM').removeClass('focus');
$('#search').blur();
});
$('#search').on('focus', function () {
$('#TmInM').addClass('focus');
$('#search').focus();
}).on('blur', function (e) {
$('#TmInM').removeClass('focus');
$('#search').blur();
});
#TmInM {
width:40vw;
height:3.4vh;
background: #FFF;
display: inline-block;
margin-left:10vw;
margin-top:0.9vh;
color:#777;
border:2px solid transparent;
outline:none;
border-radius:4px;
-webkit-box-shadow:0px 0px 1px 1px #BBB;
-moz-box-shadow:0px 0px 1px 1px #BBB;
box-shadow:0px 0px 1px 1px #BBB;
}
#TmInM.focus {
border:2px solid #00b646;
outline:none;
}
#TmInM img {
float: left;
margin-top:0.4vh;
margin-left:0.4vw;
opacity: 0.2;
}
#TmInM input {
width:30vw;
height:1.8vh;
padding:0.2vw;
margin-top:0.2vh;
margin-left:0.2vw;
font-size:0.8vw;
border:0;
}
#TmInM input::placeholder {
color:#CCC;
font-style:italic;
}
#TmInM input:focus {
border:2px solid transparent;
outline:none;
-webkit-box-shadow:0px 0px 1px 1px #FFF;
-moz-box-shadow:0px 0px 1px 1px #FFF;
box-shadow:0px 0px 1px 1px #FFF;
}
#TmInM input:focus::placeholder {
color:#999;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="TmInM" tabindex="-1">
<img src="<?php echo $baseURL; ?>images/search.png" alt="" /><input type="text" name="search" id="search" class="inputmain" placeholder="Send out a leprechaun to go search what u are looking for..." value="" />
</div>

button:hover and cursor pointer not working?

The cursor: pointer and background-color change on hover are both not working on my buttons. I have tried the solutions i have seen to similar questions (swapped from a button tag to a anchor or div tag, changed the z-index) but none of them have helped. The rest of the styling is working fine so i don't understand why this is happening, please help.
.btn {
cursor: pointer;
position: relative;
display: inline-block;
width: 80px;
height: 30px;
background-color: #15181c;
border: none;
margin: auto 10px;
border-radius: 5px;
font-weight: bold;
color: white;
letter-spacing: 1.5px;
transition-duration: 0.3s;
padding: 5px 10px;
}
.btn:hover {
background-color: lightslategray;
}
.disabled {
opacity: 0.5;
}
<div class ="buttons">
<div class="btn">DEAL</div>
<div class="btn disabled">HIT</div>
<div class="btn disabled">STAND</div>
</div>
In case anyone else has this issue. I had a box shadow overlay to add a shadow on the background which was stopping the buttons from registering the mouse hover. Changing the button z-index to 100 didn't help, but when i changed the overlay index to -1 it solved the issue.
It's a bug on webkit browsers that doesn't allow you to see the hover effect.
You can do it in 2 ways: (You can do both)
Set the div.btn inside another div called for example div.btn-container and apply the hover to the parent element. This will surely work for you. (You can apply the hover to both of them)
Change the display: inline-block to display: inline. Inline sometimes causes some issues. (Sometimes)
.buttons {
display: flex
}
.btn {
position: relative;
/* SET THE DISPLAY TO INLINE-BLOCK */
display: inline;
width: 80px;
height: 30px;
background-color: #15181c;
border: none;
border-radius: 5px;
font-weight: bold;
color: white;
letter-spacing: 1.5px;
transition-duration: 0.3s;
padding: 5px 10px;
}
.btn-container {
margin: auto 10px
}
/* YOU CAN WRITE BOTH IN YOUR CSS FILE HERE */
.btn:hover {
cursor: pointer;
background-color: lightslategray;
}
.btn-container:hover btn {
background-color: lightslategray;
cursor: pointer;
border: 1px solid red
}
/* UNTIL HERE */
.disabled {
opacity: 0.5;
}
<div class="buttons">
<div class='btn-container'>
<div class="btn">DEAL</div>
</div>
<div class='btn-container'>
<div class="btn disabled">HIT</div>
</div>
<div class='btn-container'>
<div class="btn disabled">STAND</div>
</div>
</div>
The cursor: pointer and background-color change on hover are both not working on my buttons.
Is the code in the snippet the one you are using?
Since in the code snippet .btn:hover is working just fine?
Have you tried force-refreshing your browser.
Control + F5
You can also try running the page with a different browser.
Edit:
Based on the answers you have given to the other comments, I think it might be a system or browser bug. Here's a Jquery alternative if the pure css does not work in your system (if you are fine with using it).
.buttons {
display: flex
}
.btn {
position: relative;
display: inline;
width: 80px;
height: 30px;
background-color: #15181c;
border: none;
border-radius: 5px;
font-weight: bold;
color: white;
letter-spacing: 1.5px;
transition-duration: 0.3s;
padding: 5px 10px;
}
.disabled {
opacity: 0.5;
}
.btn:hover, .btn.jqHover {
background-color: lightslategray;
cursor: pointer;
border: 1px solid red
}
.activeElement {
background:#bfbfbf;
}
<div class="buttons">
<div class='btn-container'>
<div class="btn">DEAL</div>
</div>
<div class='btn-container'>
<div class="btn disabled">HIT</div>
</div>
<div class='btn-container'>
<div class="btn disabled">STAND</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function () {
var hoveredElement;
var clickedElement;
$(document).mousemove(function (event) {
hoveredElement=event.target.nodeName;
// comment this section in between to see issue
$(hoveredElement).mouseenter(function () {
$(this).addClass('jqHover');
}).mouseleave(function () {
$(this).removeClass('jqHover');
});
return hoveredElement;
});
$(document).click(function (event) {
clickedElement = event.target.nodeName;
return clickedElement;
});
});
</script>
Original Jquery answer by Sai

Custom CSS dropdown to focus on dropdown elements

I am trying to get the dropdown elements to focus while the list is in open state using CSS. Right now, the autofocus only selects the All, then closes the list dropdown. I want it to focus on cheese in an open state and focus tab through Toast and any components that could be after. I have tried placing tabIndex on each element, but the list dropdown closes so I don't see the focus.
Please, see CodeSandBox here.
.select-box {
position: relative;
display: block;
width: 100%;
height: 48px;
margin: 0 auto;
}
.select-box-current:hover {
border: 1px solid #D4D4D4;
}
.select-box-current:focus {
box-shadow: 0 0 4px 2px #8CB6FD, 0 1px 3px 0 rgba(102, 102, 102, 0.1), 0 1px 3px 0 rgba(102, 102, 102, 0.2);
}
.select-box-current {
position: relative;
cursor: pointer;
outline: none;
}
.select-box-current:focus+.select-box-list {
opacity: 1;
animation-name: none;
}
.select-box-current:focus+.select-box-list .select-box-option {
cursor: pointer;
}
.select-box-value {
display: flex;
}
.select-box-input {
display: none;
}
.select-box-input:checked+.select-box-input-text {
display: block;
}
.select-box-input-text {
display: none;
width: 100%;
margin: 0;
padding: 15px;
background-color: #fff;
}
.select-box-list {
position: absolute;
width: 100%;
padding: 0;
margin-top: 8px;
margin-bottom: 8px;
list-style: none;
opacity: 0;
animation-name: HideList;
animation-duration: 0.5s;
animation-delay: 0.5s;
animation-fill-mode: forwards;
animation-timing-function: step-start;
background-color: #FFFFFF;
}
.select-box-option {
display: block;
padding: 15px;
background-color: #fff;
}
.select-box-option:hover {
background-color: rgba(102, 102, 102, 0.05);
}
.select-box-option:focus {
background-color: rgba(102, 102, 102, 0.05);
box-shadow: 0 0 4px 2px #8CB6FD;
}
#keyframes HideList {
from {
transform: scaleY(1);
}
to {
transform: scaleY(0);
}
}
<div className="select-box">
<div className="select-box-current" tabIndex="0" autoFocus={true}>
<div className="select-box-value">
<input className="select-box-input" type="radio" id={0} value="all" name="Ben" defaultChecked />
<p className="select-box-input-text">
All
</p>
</div>
<div key={idx} className="select-box-value">
<input className="select-box-input" type="radio" id={1} value="Cheese" name="Ben" />
<p className="select-box-input-text">
Cheese
</p>
</div>
<div className="select-box-value">
<input className="select-box-input" type="radio" id={2} value="Toast" name="Ben" />
<p className="select-box-input-text">
Toast
</p>
</div>
</div>
<ul className="select-box-list">
<li>
<label className="select-box-option" htmlFor={0} aria-hidden={false}>
All
</label>
</li>
<li>
<label className="select-box-option" htmlFor={1} aria-hidden={false}>
Cheese
</label>
</li>
<li>
<label className="select-box-option" htmlFor={2} aria-hidden={false}>
Toast
</label>
</li>
</ul>
</div>
If you want to use a little you can easily do this by just switch its class back and forth.
<script>
document.getElementById("something").classList.toggle(".open")
</script>
I don't think you should force this pure CSS focus implementation. The reason is: you can only have 1 element focused. That is why the dropdown closes when 1 of the dropdown items is focused on.
I recommend to solve this by switching from psuedo class :focus to class active for the dropdown items
.select-box-option.active {
background-color: rgba(102, 102, 102, 0.05);
box-shadow: 0 0 4px 2px #8cb6fd;
}
Track the active item via state that updates when a different input is selected
const [activeItem, setActiveItem] = React.useState("cheese");
<input
className="select-box-input"
type="radio"
id={1}
value="toast"
name="Ben"
onChange={() => setActiveItem("toast")}
/>
And as mentioned by the others, you can use arrow navigation for the User Experience, because again, using tab will cause issues because it will change focus and close your dropdown.
window.addEventListener("keydown", handleKeydown);
function handleKeydown(e) {
if (e.keyCode === 40) {
// down arrow key
} else if (e.keyCode === 38) {
// up arrow key
}
}

Custom style radiobuttons

I want to style my radio buttons so that that black dot inside the circle is for example red.
There are several examples available on the internet like this one: JSFIDDLE. The thing with this example is that it does not work in Internet Explorer.
Another point that makes my situation harder is that I can not, due to implementation requirements, add any other html objects to the following code:
<span class="custom-radio">
<input id="id4" type="radio" name="id_test" value="">
<label for="id4">No</label>
</span>
My question is: how can I create a custom radiobutton without adding extra HTML to the code above and still make it work in most browsers (IE, FF, Chrome)
You can hide the input itself with display:none and the use a pseudo-element on the label
input[type='radio'] {
display: none;
}
input[type='radio'] + label {
position: relative;
line-height: 1em;
}
input[type='radio'] + label:before {
content: '';
width: .5em;
height: .5em;
border-radius:100%;
margin-right: .5em;
display: inline-block;
background: red;
vertical-align: middle;
box-shadow: 0 0 0 .1em white, 0 0 0 .2em black;
}
input[type='radio']:checked + label:before {
background: green;
vertical-align: middle;
}
<span class="custom-radio">
<input id="id4" type="radio" name="id_test" value=""/>
<label for="id4">No</label>
</span>
After that it's just a matter of styling the pseudo-element to taste,
for something like changing the colour of the tick/ball you can use ::before:
input:checked ~ label::before{
content: "";
background: #F90 none repeat scroll 0% 0%;
width: 8px;
height: 8px;
position: absolute;
border-radius: 20px;
left: 7px;
top: 5px;
}
here's a fiddle
n.b. You'd be positioning the ::before element, so this would need tweeking to the correct position when used in your application
You cannot do that in IE because it does not allow you to use :before on input elements, at least according to this answer. I think the only thing you can do in your situation is to try adding :before on the label and position it over the checkbox.
Try this link
Styling Radio Buttons with CSS
Update : code (copy/past from above link):
<input id="choice-a" type="radio" name="g" />
<label for='choice-a'>
<span><span></span></span>
Choice A
</label>
input[type="radio"] {
opacity: 0;
position: absolute;
}
/* Matches the direct descendant of a label preceded by a
radio button */
input[type="radio"] + label > span {
position: relative;
border-radius: 14px;
width: 20px;
height: 20px;
background-color: #f0f0f0;
border: 1px solid #bcbcbc;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
margin: 0 1em 0 0;
display: inline-block;
vertical-align: middle;
}
/* Matches the direct descendant of a label preceded by a
checked radio button */
input[type="radio"]:checked + label > span {
background: linear-gradient(#a0e5f8, #75c7dc);
background: -webkit-linear-gradient(#a0e5f8, #75c7dc);
border-color: #41a6bf;
box-shadow: 0px 1px 2px rgba(65, 166, 191, 0.9) inset;
}
/* Matches a span contained by the direct descendant
of a label preceded by a checked radio button */
input[type="radio"]:checked + label > span span {
display: inline-block;
width: 8px;
height: 8px;
position: absolute;
left: 6px;
top: 6px;
border-radius: 5px;
border: none;
background: #167c95;
box-shadow: 0px 1px rgba(255, 255, 255, 0.3);
}
input[type="radio"]:focus + label > span {
box-shadow: 0px 0px 6px rgba(63, 165, 190, 1);
}
Fiddle example
Simply use a combination of hidden radio input elements and styled span elements wrapped in label elements, styled accordingly:
input[type=radio] {
display: none;
}
span {
border-radius: 100%;
height: 20px;
width: 20px;
display: inline-block;
border: 1px solid;
position: relative;
}
input[type=radio]:checked + span:before {
content: '';
position: absolute;
height: 50%;
width: 50%;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
background: green;
border-radius: 100%;
}
<label>
<input type="radio" name="myRadio" />
<span></span>Item 1
</label>
<label>
<input type="radio" name="myRadio" />
<span></span>Item 2
</label>
<label>
<input type="radio" name="myRadio" />
<span></span>Item 3
</label>
You cannot change the properties of radio buttons or checkboxes, but you can simulate them. Keep your HTML the same, and add this to your CSS.
.custom-radio input[type=radio] {
display:none;
}
.custom-radio label {
display:inline-block;
}
.custom-radio label:before {
content:"";
display:inline-block;
width:16px;
height:16px;
border-radius:50%;
background:url("http://s17.postimg.org/p1q2imsln/radio.png");
background-position:0% 0%;
}
.custom-radio label:hover:before {
background-position:0% 100%
}
.custom-radio input[type=radio]:checked~label:before {
background-position:100% 0%;
}
.custom-radio input[type=radio]:checked~label:hover:before {
background-position:100% 100%;
}
Simply provide an image that follows the template in the link that has red radio buttons.
Why even use the input[type="radio"] element at all? You can recreate in in html, css, and javascript and offer better browser support than any crazy css :before, :after stuff. Here is the code and a working jsfiddle:
Here is a link to the fiddle: https://jsfiddle.net/www139/ba5jn2e6/19/
Hope this helps anyone else with the issue. I experienced the same dilemma, so I decided to create it from scratch!
window.onload = function(){
var radioButtonGroups = document.getElementsByClassName('radioGroupContainer');
for(var i = 0; i != radioButtonGroups.length; i++)
{
var radioButtons = radioButtonGroups[i].getElementsByClassName('radioButtonContainer');
for(var i = 0; i != radioButtons.length; i++)
{
radioButtons[i].onclick = function(){
var value = this.children[0].getAttribute('name');
for(var i = 0; i != radioButtons.length; i++)
{
radioButtons[i].children[0].setAttribute('class','radioButtonDot');
}
this.children[0].setAttribute('class','radioButtonDotActive');
this.parentNode.setAttribute('name',value);
};
}
}
};
/*
* Created by William Green.
* Questions or comments? Email william.green#protonmail.com
* I would appreciate credit for this code if you use it; but it is not required.
* Last updated July 26, 2015
* Created July 26, 2015
*
*/
.radioButtonContainer {
background-color:#eee;
padding:5px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
display:table;
margin-top:5px;
margin-bottom:5px;
}
.radioButtonContainer .radioButtonDot {
width:16px;
height:16px;
background-color:transparent;
border:1px solid #000;
display:inline-block;
vertical-align:middle;
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%;
-o-transition:all .5s ease;
-moz-transition:all .5s ease;
-webkit-transition:all .5s ease;
-ms-transition:all .5s ease;
transition:all .5s ease;
}
.radioButtonContainer .radioButtonDotActive {
width:16px;
height:16px;
background-color:#1396DE;
border:1px solid transparent;
display:inline-block;
vertical-align:middle;
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%;
-o-transition:all .5s ease;
-moz-transition:all .5s ease;
-webkit-transition:all .5s ease;
-ms-transition:all .5s ease;
transition:all .5s ease;
}
.radioButtonContainer .radioButtonLabel {
background-color:transparent;
display:inline-block;
vertidal-align:middle;
border:0;
}
<div class="radioGroupContainer" id="radioChoicesOne">
<div class="radioButtonContainer">
<div class="radioButtonDot" name="optionOne"></div>
<input type="button" class="radioButtonLabel" value="Option One">
</div>
<div class="radioButtonContainer">
<div class="radioButtonDot" name="optionTwo"></div>
<input type="button" class="radioButtonLabel" value="Option Two">
</div>
<div class="radioButtonContainer">
<div class="radioButtonDot" name="optionThree"></div>
<input type="button" class="radioButtonLabel" value="Option Three">
</div>
</div>
<div id="radioButtonGroupOneValue"></div>
<input type="button" value="Get radio button value..." onclick="document.getElementById('radioButtonGroupOneValue').innerHTML = document.getElementById('radioChoicesOne').getAttribute('name');">

CSS3 fold and unfold effect

I need a little bit of help.
I am using the following html and css in CodePen. Basically I need it to start in a folded state rather than open.
I need the folder cover (top div) to always be unfolded as this will show information on how to open the content as the cover needs to contain a fold/unfold link. Also when I enter content in to each of the fold divs the content runs over to the next div, rather than expanding the current div as you will see in my edited CodePen below.
This would also need to be resolved so that it does not cause an issue when it is folded as the divs become misaligned.
Original CodePen
http://codepen.io/boxabrain/pen/Hhugb/
My edited CodePen
http://codepen.io/anon/pen/gmAnK
HTML
<div id="folder">
<input type="checkbox" id="toggle"/> <label for="toggle" id="toggle- label">fold/unfold</label>
<div class="fold">
Element 1
</div>
<div class="fold">
Element 2
</div>
<div class="fold">
Element 3
</div>
<div class="fold">
Element 4
</div>
<div class="fold">
Element 5
</div>
<div class="fold">
Element 6
</div>
<div class="fold">
Element 7
</div>
<div class="fold">
Element 8
</div>
</div>
CSS
body {
padding: 50px;
font-family: Arial, sans-serif;
}
#folder {
width: 300px;
padding: 10px;
}
.fold {
background: #000;
background: #000;
padding: 10px;
width: 280px;
height: 80px;
color: #999;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
transition: all 0.3s linear;
}
#toggle { display: none; }
#toggle-label {
display: inline-block;
cursor: pointer;
margin-bottom: 50px;
border: 1px solid #e5e5e5;
font-size: 11px;
color: #999;
background: #fff;
text-transform: uppercase;
border-radius: 5px;
padding: 5px;
}
#toggle:checked ~ .fold:nth-child(odd) {
margin-top: -82px;
-webkit-transform: perspective(800px) rotateX(-80deg);
-moz-transform: perspective(800px) rotateX(-80deg);
transform: perspective(800px) rotateX(-80deg);
}
#toggle:checked ~ .fold:nth-child(even) {
margin-top: -84px;
-webkit-transform: perspective(800px) rotateX(80deg);
-moz-transform: perspective(800px) rotateX(80deg);
transform: perspective(800px) rotateX(80deg);
}
Can anyone help me?
Thanks in advance
To start out in a folded state, just add :not selector like this:
#toggle:not(:checked) ~ .fold:nth-child(odd)
#toggle:not(:checked) ~ .fold:nth-child(even)
About content overflow - you need to adjust it by hands if you don't want to use Js (play with individual .folds heights and rotateX).