Custom CSS dropdown to focus on dropdown elements - html

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
}
}

Related

Radio Button Method - HTML Accordion

I would like some assistance with my accordion code,
My idea is to get something like this:
The Radio Button Method adds a hidden radio input and a label tag to each accordion tab.
The logic is straightforward:
when a user selects a tab, they essentially check the radio button associated with that tab.
when a user clicks the next tab in the accordion, the next radio button is selected, and so on.
Only one tab can be open at a time using this method.
I'd like some advice on how to incorporate this into my current accordion code.
<!doctype html>
<html>
<head>
<style>
input {
display: none;
}
label {
display: block;
padding: 8px 22px;
margin: 0 0 1px 0;
cursor: pointer;
background: #6AAB95;
border-radius: 3px;
color: #FFF;
transition: ease .5s;
position: relative; /* ADDING THIS IS REQUIRED */
}
label:hover {
background: #4E8774;
}
label::after {
content: '+';
font-size: 22px;
font-weight: bold;
position: absolute;
right: 10px;
top: 2px;
}
input:checked + label::after {
content: '-';
right: 14px;
top: 3px;
}
.content {
background: #E2E5F6;
padding: 10px 25px;
border: 1px solid #A7A7A7;
margin: 0 0 1px 0;
border-radius: 3px;
}
input + label + .collapse {
display: none;
}
input:checked + label + .collapse {
display: block;
}
</style>
</head>
<body>
<input type="checkbox" id="title1" />
<label for="title1">Accordion 1</label>
<div class="collapse">
<p>Your content goes here inside this division with the class "content".</p>
</div>
<input type="checkbox" id="title2" />
<label for="title2">Accordion 2</label>
<div class="collapse">
<p>Your content goes here inside this division with the class "content".</p>
</div>
</body>
</html>
No need to change the CSS (at least the part handling the accordion functionality) but you'd have to change a bit in your HTML.
To get the desired accordion effect where only one tab can be open at a time you should:
use radio buttons instead of checkboxes (input[type="radio"]).
And the important part is to give those radio buttons the same name (the attribute name must be the same for all the accordion component's radio buttons) in order to achieve the desired outcome.
Here's a a live demo:
/** nothing changed on the CSS part, see the HTML part for the required changes */
input {
display: none;
}
label {
display: block;
padding: 8px 22px;
margin: 0 0 1px 0;
cursor: pointer;
background: #6AAB95;
border-radius: 3px;
color: #FFF;
transition: ease .5s;
position: relative;
/* ADDING THIS IS REQUIRED */
}
label:hover {
background: #4E8774;
}
label::after {
content: '+';
font-size: 22px;
font-weight: bold;
position: absolute;
right: 10px;
top: 2px;
}
input:checked+label::after {
content: '-';
right: 14px;
top: 3px;
}
.content {
background: #E2E5F6;
padding: 10px 25px;
border: 1px solid #A7A7A7;
margin: 0 0 1px 0;
border-radius: 3px;
}
input+label+.collapse {
display: none;
}
input:checked+label+.collapse {
display: block;
}
<!-- changed "type=checkbox" to "type=radio" -->
<!-- added the same "name" attribute value for all the radio buttons -->
<input type="radio" name="radio-btn" id="title1" />
<label for="title1">Accordion 1</label>
<div class="collapse">
<p>Your content goes here inside this division with the class "content".</p>
</div>
<!-- changed "type=checkbox" to "type=radio" -->
<!-- added the same "name" attribute value for all the radio buttons -->
<input type="radio" name="radio-btn" id="title2" />
<label for="title2">Accordion 2</label>
<div class="collapse">
<p>Your content goes here inside this division with the class "content".</p>
</div>
CAUTION: Even though the radio buttons hack works as needed, there is no way you can close all the accordion items after interacting for the first time (you can have a closed accordion initially though).
I have found this example using Sass that looks exactly like what you need: https://codepen.io/alvarotrigo/pen/dyJbqpd.
The example uses radio buttons, such as <input type="radio" id="title1" name="select"/>. Because they have the same name, you can only select one at a time.
In your example, you have checkboxes like in this example at w3schools.com. Using checkboxes, you can tick any number of checkboxes at a time, therefore the current accordion behavior.
Here's a stripped-down version (converted to CSS):
input {
position: absolute;
opacity: 0;
z-index: -1;
}
.tab {
overflow: hidden;
}
.tab-label {
display: flex;
justify-content: space-between;
padding: 1em;
background: #2c3e50;
color: white;
cursor: pointer;
}
.tab-content {
max-height: 0;
padding: 0 1em;
color: #2c3e50;
background: white;
}
input:checked ~ .tab-content {
max-height: 100vh;
padding: 1em;
}
<div class="tab">
<input type="radio" id="rd1" name="rd">
<label class="tab-label" for="rd1">Item 1</label>
<div class="tab-content">Content</div>
</div>
<div class="tab">
<input type="radio" id="rd2" name="rd">
<label class="tab-label" for="rd2">Item 2</label>
<div class="tab-content">Content</div>
</div>
I have slightly changed your code and added another div with overflow: hidden:
/** nothing changed on the CSS part, see the HTML part for the required changes */
input {
display: none;
}
label {
display: block;
padding: 8px 22px;
margin: 0 0 1px 0;
cursor: pointer;
background: #6AAB95;
border-radius: 3px;
color: #FFF;
transition: ease .5s;
position: relative;
/* ADDING THIS IS REQUIRED */
}
label:hover {
background: #4E8774;
}
label::after {
content: '+';
font-size: 22px;
font-weight: bold;
position: absolute;
right: 10px;
top: 2px;
}
input:checked+label::after {
content: '-';
right: 14px;
top: 3px;
}
.content {
background: #E2E5F6;
padding: 10px 25px;
border: 1px solid #A7A7A7;
margin: 0 0 1px 0;
border-radius: 3px;
}
input+label+.collapse {
display: none;
}
input:checked+label+.collapse {
display: block;
}
<div class="tab">
<input type="radio" id="title1" name="select"/>
<label for="title1">Accordion 1</label>
<div class="collapse">
Your content goes here inside this division with the class "content".
</div>
</div>
<div class="tab">
<input type="radio" id="title2" name="select" />
<label for="title2">Accordion 2</label>
<div class="collapse">
Your content goes here inside this division with the class "content".
</div>
</div>

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>

Hyperlinks inside accordion are translucid (HTML CSS ONLY)

I am working on this code which I am adapting from https://codepen.io/dandiws/pen/qqyeed
But when I try to include a hyperlink inside the accordion it is seen even when the accordion its closed.
How can i make the links not to be visible when the accordion is closed?
Do you know how to fix?
translucent hyperlinks
<html>
<head>
<style>
body {
background-color:#d3d0c9;
font-family:'Arial';
padding:2em 6em;
}
h1{
color:#fff;
text-align:center;
}
/*--------Accordion-------*/
.acc-kontainer {
width: 100%;
margin: auto;
}
.acc-kontainer .acc-body {
width: 98%;
width: calc(100% - 20px);
margin: 0 auto;
height: 0;
color: rgba(0, 0, 0, 0);;
background-color: rgba(42, 41, 92, 0.9);
line-height: 28px;
padding: 0 20px;
box-sizing: border-box;
transition: 0.5s;
}
.acc-kontainer label {
cursor: pointer;
background-color: rgba(101, 103, 106, 0.3);
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
display: block;
padding: 15px;
width: 100%;
color: #fff;
font-weight: 400;
box-sizing: border-box;
z-index: 100;
}
.acc-kontainer input{
display: none;
}
.acc-kontainer label:before {
font-family: 'FontAwesome';
content: '\f067';
font-weight: bolder;
float: right;
}
.acc-kontainer input:checked+label {
background-color: rgba(255, 255, 255, 0.15);
}
.acc-kontainer input:checked+label:before {
font-family: 'FontAwesome';
content: '\f00d';
transition: 0.5s;
}
.acc-kontainer input:checked~.acc-body {
height: auto;
color: #fff;
font-size: 16px;
padding: 20px;
transition: 0.5s;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>
<body>
<h1><img src="file:///Macintosh HD/Users/Matias/Desktop/banner.jpg" width="950" height="260" alt=""/></h1>
<p> </p>
<div class="acc-kontainer">
<div>
<input type="radio" name="acc" id="acc1" checked>
<label for="acc1">REGISTER to your SESSIONS</label>
<div class="acc-body">
Link for registering to session Register <br>
</div>
</div>
<div>
<input type="radio" name="acc" id="acc2">
<label for="acc2">COMPLETE your PRE-WORK</label>
<div class="acc-body">
Pre work links link
</div>
</div>
<div>
<input type="radio" name="acc" id="acc3">
<label for="acc3"> TECH CHECK & TROUBLESHOOTING</label>
<div class="acc-body">
Please follow this link to download Mursion software
Download this file which guides you how to install ...
</div>
</div>
<div>
<input type="radio" name="acc" id="acc4">
<label for="acc4">CONTINOUS LEARNING TOOLKIT</label>
<div class="acc-body">
Links
bada di
badad da
</div>
</div>
</div>
</body>
</html>
Try adding this for links
.acc-kontainer input:checked~.acc-body a {
color: blue; /* whatever link color you want */
}
.acc-kontainer input:not(:checked)~.acc-body a {
color: rgba(0, 0, 0, 0); /* font color set to transparent like other content in .acc-body */
}

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

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).

Div/button LOOKS wider than it should be by 1 pixel

As you can see on the image below, the dropdown div is a pixel wider than it should have been. The strangest thing is that on hover it only changes its color (line 22 of SCSS fiddle) and it's wider no more!
Apparently, its width is equal to its parent, but as you can see, it's not.
Maybe it's tied with button hover somehow?
Could somebody explain me the thing with this situation?
The code is like this.
https://jsfiddle.net/can528p2/12/
<div class="home">
<div class="submit">
<button class="btn-search">
Search Items
</button>
<div class="menu">
<ul role="menu">
<li>
<button>
Items1
</button>
</li>
<li>
<button>
Items2
</button>
</li>
</ul>
</div> <!-- /.menu -->
</div> <!-- /.submit -->
</div> <!-- /.home -->
SCSS
.home {
height: 200px;
padding: 20px;
background-color: rgba(0, 0, 0, 0.7);
button {
height: 40px;
line-height: 40px;
width: 100%;
}
.submit {
width: 20%;
position: relative;
.btn-search {
background: #ff530d;
color: #fff;
outline: none;
border: none;
&:hover,
&:focus,
&:active {
background-color: #f26202;
outline: none;
}
} //.btn-search
.menu {
position: absolute;
width: 100%;
border-radius: 0;
ul {
margin: 0;
padding: 0;
list-style-type: none;
background-color: transparent;
button {
background: #fff;
border: none;
outline: none;
&:hover,
&:focus,
&:active {
background: #ff530d;
border: none;
outline: none;
}
}
}
} //.menu
} //.submit
}
It's an optical illusion. It's tricking your brain because the dividing edges of the pixels on your monitor are dark and your brain contrasts them with a dark background. Try changing the whole background color to be a lighter shade and the effect diminishes:
.home {
height: 200px;
padding: 20px;
background-color: rgba(250, 250, 250, 0.7); <--
button {
height: 40px;
line-height: 40px;
width: 100%;
}
https://jsfiddle.net/can528p2/13/