Prevent the dropdown menu overlap the div below just push it - html

I have a dropDown menu and when the drop list is open is overlapping the div below, I wish to push the div below down when the dropdown is open, I've try with flex, grid, flexgrow, grid rows, positions relative/absolute, and I can't find any solution.
here is the HTML code:
<div class="container">
<div class="dropContainer">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
Home
About
Contact
</div>
</div>
</div>
<div class="inputContainer">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" />
</div>
</div>
and the CSS:
.container{
display: flex;
flex-direction: column;
height: 100vh;
}
.dropContainer{
display: block;
height: 100%;
margin-bottom: 2rem;
}
.inputContainer{
display: block;
height: 100%;
top: 5rem;
}
/* dropDown Menu */
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
padding-bottom: 140px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.show {display: block; }
and here is the code example working: https://codepen.io/raulcosalux/pen/VwzGyYO
kind regards,

Using absolute positioning will not regard for content underneath. With that being said, you have to use position: relative; on your dropdown menu in order to allow the input or any other content below to adjust when the menu opens.
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.container{
display: flex;
flex-direction: column;
height: 100vh;
}
.dropContainer{
display: block;
height: 100%;
margin-bottom: 2rem;
}
.inputContainer{
display: block;
height: 100%;
position: sticky;
}
/* dropDown Menu */
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: relative;
background-color: #f1f1f1;
min-width: 160px;
padding-bottom: 140px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.show {display: block; }
<div class="container">
<div class="dropContainer">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
Home
About
Contact
</div>
</div>
</div>
<div class="inputContainer">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" />
</div>
</div>

Change the position of the dropdown container that holds the menu to relative, then remove the padding you have on it. Remove this => padding-bottom: 140px; and change this => .dropdown-content { position: absolute; } to .dropdown-content { position: relative; }, this will push the content down below the menu element and the padding will no longer cover its content.
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.container{
display: flex;
flex-direction: column;
height: 100vh;
}
.dropContainer{
display: block;
margin-bottom: 2rem;
}
.inputContainer{
display: block;
height: 100%;
top: 5rem;
}
/* dropDown Menu */
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.show {
display: block;
}
<div class="container">
<div class="dropContainer">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
Home
About
Contact
</div>
</div>
</div>
<div class="inputContainer">
<label for="fname">First name: </label>
<input type="text" id="fname" name="fname" />
</div>
</div>
I also removed the height on the dropContainer as well.

Related

Dropdown list to Include Images or Icons

I want to add specific icons or png's to the dropdown list next to the text. Firefox no longer supports a background-image style inline to do this. I have looked around but all the ways are outdated.
This is the codepen link with an example Link
The code is this if needed.
HTML
<div>
<select>
<option value="Cat">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="cat icon">
Cat</option>
<option value="Dog">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="dog icon">
Dog</option>
<option value="Bird">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="bird icon">
Bird</option>
<option value="Mouse">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="mouse icon">
Mouse</option>
</select>
</div>
CSS
select {
padding: 5px 25px;
font-size: 20px;
border: none;
background:grey;
border-radius: 5px;
color: azure;
}
div{
position: absolute;
top:20%;
width:100%;
left: 50%;
margin: auto 0;
}
What i would advice you to do is not to use a <select> as a menu, but a <div> with <a> tags or a <ul> with <li> tags, here's a working example:
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
image<br><img src="https://www.w3schools.com/images/lamp.jpg">
font awesome<br><i class="fas fa-phone"></i>
</div>
</div>
Here is the whole code:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
</style>
</head>
<body>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
image<br><img src="https://www.w3schools.com/images/lamp.jpg">
font awesome<br><i class="fas fa-phone"></i>
</div>
</div>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
</body>
</html>
You could use divs and Javascript:
let dropdown = document.querySelector(".dropdown");
let selectCountry = dropdown.querySelector(".select-country");
let options = dropdown.querySelector(".options");
let optionItems = options.querySelectorAll("div");
const toggleDropdown = () => {
if (window.getComputedStyle(options).display == "none") {
options.style.display = "block";
}
else {
options.style.display = "none";
}
}
selectCountry.addEventListener("click", toggleDropdown);
const chooseOption = (option) => {
console.log(option.getAttribute("data-value"));
toggleDropdown();
}
optionItems.forEach(function(option, i) {
option.addEventListener("click", function(){chooseOption(option)});
});
const closeDropdown = (e) => {
if (e.target.parentNode.className !== "dropdown") {
options.style.display = "none";
}
}
document.addEventListener("click", closeDropdown);
.dropdown {
width: 200px;
background-color: #fff;
border: 1px solid #e2e2e2;
}
.dropdown .options {
display: none;
}
.dropdown .options div {
position: relative;
height: 30px;
padding: 5px 5px 5px 50px;
cursor: pointer;
line-height: 30px;
}
.dropdown .options div:nth-of-type(2n+1){
background-color: #efefef;
}
.dropdown .options div:before {
content: "";
width: 32px;
height: 24px;
background-image: url(https://i.ibb.co/3C5F68J/flags.png);
background-repeat: no-repeat;
position: absolute;
left: 5px;
top: 8px;
}
.dropdown .options div.sp:before {
background-position: 0 0;
}
.dropdown .options div.en:before {
background-position: 0 -47px;;
}
.dropdown .options div.fr:before {
background-position: 0 -95px;
}
.dropdown .options div.it:before {
background-position: 0 -143px;
}
.dropdown div.select-country {
position: relative;
height: 30px;
padding: 5px 50px 5px 5px;
cursor: pointer;
line-height: 30px;
border: 1px solid #e2e2e2;
}
.dropdown div.select-country:before {
content: "";
width: 32px;
height: 24px;
background-image: url(https://i.ibb.co/3C5F68J/flags.png);
background-repeat: no-repeat;
background-position: 0 -191px;
position: absolute;
right: 5px;
top: 8px;
}
<div class="dropdown">
<div class="select-country">-- select contry --</div>
<div class="options">
<div class="sp" data-value="sp">Spain</div>
<div class="en" data-value="en">England</div>
<div class="fr" data-value="fr">France</div>
<div class="it" data-value="it">Italy</div>
</div>
</div>

3 dropdown separate drop down lists, only select top one

I have created a website (i'm fairly new to using html on a website.... i normally just use images and text) and i want 3 drop down boxes independently selectable which will go to 3 different pages
the problem is, all the lists work in coding, but when i click on any buttons it only selects the top list
3 drop down boxes
how the drop looks when you select any button
this is the coding ive used....there are 120 different class links, for each dropdown
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
background-color: #005eb8;
color: white;
margin-left: 98px;
padding: 5px;
font-size: 17px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #004a90;
}
.dropdown {
position: absolute;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
margin-left: 98px;
background-color: #f4f7f8;
min-width: 50px;
height: 440px;
overflow-y: scroll;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 1px 6px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #005eb8; color:white;}
.show {display: block;}
</style>
</head>
<body>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">--- Select ---</button>
<div id="myDropdown1" class="dropdown-content">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20t.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91t.html">1890-91</a>
</div>
</div>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown1").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
</body>
</html>
thanks in advance
Chris
UPDATE
ok. many thanks for the help. i've manages to get the 3 different buttons to go to the 3 different sections, however the last issue is that the top 2 buttons do not close when clicking anywhere else on the page.... but the bottom button does. this is the code.... Can anyone suggest script to close the top two? the webpage is https://www.glossopnorthendafc.co.uk/history.html
<br>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn1 {
background-color: #005eb8;
color: white;
margin-left: 86px;
padding: 5px;
font-size: 17px;
border: none;
cursor: pointer;
}
.dropbtn1:hover, .dropbtn1:focus {
background-color: #004a90;
}
.dropdown1 {
position: absolute;
display: inline-block;
}
.dropdown-content1 {
display: none;
position: absolute;
margin-left: 91px;
background-color: #f4f7f8;
min-width: 50px;
height: 440px;
overflow-y: scroll;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content1 a {
color: black;
padding: 1px 6px;
text-decoration: none;
display: block;
}
.dropdown1 a:hover {background-color: #005eb8; color:white;}
.show {display: block;}
</style>
</head>
<body>
<div class="dropdown1">
<button onclick="myFunction1()" class="dropbtn1">--- Select ---</button>
<div id="myDropdown1" class="dropdown-content1">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20r.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/2018-19r.html">2018-19</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91r.html">1890-91</a>
</div>
</div>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction1() {
document.getElementById("myDropdown1").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown-content1");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
</body>
</html>
<br>
<br>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn2 {
background-color: #005eb8;
color: white;
margin-left: 86px;
padding: 5px;
font-size: 17px;
border: none;
cursor: pointer;
}
.dropbtn2:hover, .dropbtn2:focus {
background-color: #004a90;
}
.dropdown2 {
position: absolute;
display: inline-block;
}
.dropdown-content2 {
display: none;
position: absolute;
margin-left: 91px;
background-color: #f4f7f8;
min-width: 50px;
height: 440px;
overflow-y: scroll;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content2 a {
color: black;
padding: 1px 6px;
text-decoration: none;
display: block;
}
.dropdown2 a:hover {background-color: #005eb8; color:white;}
.show {display: block;}
</style>
</head>
<body>
<div class="dropdown2">
<button onclick="myFunction2()" class="dropbtn2">--- Select ---</button>
<div id="myDropdown2" class="dropdown-content2">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20t.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/2018-19t.html">2018-19</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91t.html">1890-91</a>
</div>
</div>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction2() {
document.getElementById("myDropdown2").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn2')) {
var dropdowns = document.getElementsByClassName("dropdown-content2");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
</body>
</html>
<br>
<br>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn3 {
background-color: #005eb8;
color: white;
margin-left: 86px;
padding: 5px;
font-size: 17px;
border: none;
cursor: pointer;
}
.dropbtn3:hover, .dropbtn3:focus {
background-color: #004a90;
}
.dropdown3 {
position: absolute;
display: inline-block;
}
.dropdown-content3 {
display: none;
position: absolute;
margin-left: 91px;
background-color: #f4f7f8;
min-width: 50px;
height: 440px;
overflow-y: scroll;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content3 a {
color: black;
padding: 1px 6px;
text-decoration: none;
display: block;
}
.dropdown3 a:hover {background-color: #005eb8; color:white;}
.show {display: block;}
</style>
</head>
<body>
<div class="dropdown3">
<button onclick="myFunction3()" class="dropbtn3">--- Select ---</button>
<div id="myDropdown3" class="dropdown-content3">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20s.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/2018-19s.html">2018-19</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91s.html">1890-91</a>
</div>
</div>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction3() {
document.getElementById("myDropdown3").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn3')) {
var dropdowns = document.getElementsByClassName("dropdown-content3");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
</body>
</html>
<br>
I only see a single <div class="dropdown"> in your example, but I assume there are more in your code. If that's the case, bear in mind HTML ids are intended to be unique. Try changing the myDropdown1 to a class and selecting in in your JavaScript using getElementsByClassName.
Reach out on Twitter if you'd like direct help. Up The Hillmen!

Pure HTML and CSS hamburger menu does not work

I'm making a hamburger menu right now with html and css (no js), and the :checked + .something does not work. I'm searching for solutions for about 3 hours but i can't find any. If you would help me that would be nice.
Maybe I messed up somewhere because i watched it from a video, but i did the exact same thing like him but i doesn't work :(
Here is the code:
* {
margin: 0;
padding: 0;
}
body {
background-color: teal;
}
.navigations {
right: 0;
z-index: 10;
display: block;
position: fixed;
top: 15px;
padding-bottom: 20px;
padding-left: 25px;
padding-right: 150px;
border-radius: 50px 10px 10px 50px;
background: rgba(0, 0, 0, 0.5);
}
.navigations div {
display: inline-block;
font-size: 25px;
}
.navigations a {
text-decoration: none;
color: white;
}
.burger {
z-index: 100;
position: fixed;
right: 25px;
display: block;
top: 25px;
cursor: pointer;
}
.burger div {
width: 45px;
height: 5px;
background-color: white;
margin: 12px;
border-radius: 10px;
}
#toggle {
display: none;
position: fixed;
}
#toggle:checked+.navigations {
display: none;
}
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale = 1.0">
<link rel="stylesheet" type="text/css" href="something.css">
</head>
<body>
<div class="navigations">
<div class="nav">
About us
</div>
<div class="nav">
Tours
</div>
<div class="nav">
Contacts
</div>
</div>
<label for="toggle">
<div class="burger">
<div class="burgerelem"></div>
<div class="burgerelem"></div>
<div class="burgerelem"></div>
</div>
</label>
</body>
</html>
It seems like you're actually missing the checkbox element. Since you're using the adjacent sibling selector, (+ in #toggle:checked + .navigations) you should put the checkbox with the .navigations div immediately before the #toggle input.
* {
margin: 0;
padding: 0;
}
body {
background-color: teal;
}
.navigations {
right: 0;
z-index: 10;
display: block;
position: fixed;
top: 15px;
padding-bottom: 20px;
padding-left: 25px;
padding-right: 150px;
border-radius: 50px 10px 10px 50px;
background: rgba(0, 0, 0, 0.5);
}
.navigations div {
display: inline-block;
font-size: 25px;
}
.navigations a {
text-decoration: none;
color: white;
}
.burger {
z-index: 100;
position: fixed;
right: 25px;
display: block;
top: 25px;
cursor: pointer;
}
.burger div {
width: 45px;
height: 5px;
background-color: white;
margin: 12px;
border-radius: 10px;
}
#toggle {
display: none;
position: fixed;
}
/*
Since the .navigations is the next sibling element after #toggle,
the + selector works here
*/
#toggle:checked+.navigations {
display: none;
}
<input type="checkbox" id="toggle" /> <!-- Add this! -->
<div class="navigations">
<div class="nav">
About us
</div>
<div class="nav">
Tours
</div>
<div class="nav">
Contacts
</div>
</div>
<label for="toggle">
<div class="burger">
<div class="burgerelem"></div>
<div class="burgerelem"></div>
<div class="burgerelem"></div>
</div>
</label>

Menu toggle with options inside using only CSS

I'm trying to create a menu with options inside. I'm using only CSS with checkbox and radio inputs.
By changing one of the options, I also want the menu to close. I tried this using label inside label, but it doesn't work.
My prototype code:
input {
display: none;
}
label {
cursor: pointer;
}
label span:hover {
font-weight: 600;
}
.opener .menu {
background-color: #f3f3f3;
display: flex;
flex-direction: column;
color: #4d4d4d;
padding: 12px 4px;
width: 270px;
}
#menu:checked~.opener .menu {
display: none;
}
#menu~.opener>span:nth-of-type(1) {
display: none;
}
#menu:~.opener>span:nth-of-type(2) {
display: block;
}
#menu:checked~.opener>span:nth-of-type(1) {
display: block;
}
#menu:checked~.opener>span:nth-of-type(2) {
display: none;
}
.box {
height: 50px;
width: 50px;
margin: 20px 0;
}
#red:checked~.box {
background-color: red;
}
#blue:checked~.box {
background-color: blue;
}
#green:checked~.box {
background-color: green;
}
<input id="menu" type="checkbox"></input>
<input id="red" type="radio" name="opcoes" checked></input>
<input id="blue" type="radio" name="opcoes"></input>
<input id="green" type="radio" name="opcoes"></input>
<label class="opener" for="menu"><span>Open</span><span>Close</span>
<div class="menu">
<label for="red"><span>red</span></label>
<label for="blue"><span>blue</span></label>
<label for="green"><span>green</span></label>
</div>
</label>
<div class="box"></div>
Or you can check here: https://codepen.io/anon/pen/JxzPKR
Is there a way to close the menu when you click on one of the options without JavaScript?
Sometimes, contrary to popular opinion, it's just more dev friendly to use Javascript.
There is too much conditional logic for this to be a pure CSS solution. There are ~3 if-then-else conditions you would have to satisfy, while keeping the styles cascading. I think the most arduous task to satisfy is that you have a toggle header, in addition to other controls toggling it.
This will inherently get more complex and convoluted the more components you add. But here is an example using :target. This is a work-around and provides a solution to the question at hand. You won't be able to 'toggle' the menu this way so I had to add the header under the elements so it can be accessed via some kind of sibling selector:
.menu {
position: relative;
width: 45%;
}
input[type="radio"] {
position: absolute;
opacity: 0;
height: 0;
width: 0;
}
a:any-link {
all: unset;
}
.menu-header {
position: absolute;
top: 0;
padding: 5px 10px;
color: white;
width: 100%;
background-color: cornflowerblue;
}
.menu-header a {
font-weight: bold;
cursor: pointer;
color: white;
font-size: 22px;
}
.menu-header .close {
display: none;
}
#menu-body {
display: none;
flex-flow: column nowrap;
position: absolute;
top: 34px;
background-color: rgba(220,220,220,1);
height: 100px;
color: black;
width: 100%;
padding: 10px;
}
.menu-header a,
#menu-body label {
cursor: pointer;
}
#menu-body:not(:target) {
display: none;
}
#menu-body:not(:target) + .menu-header > a:not(.close) {
display: inline-block;
}
#menu-body:target {
display: flex;
}
#menu-body:target + .menu-header > a {
display: none;
}
#menu-body:target + .menu-header > a.close {
display: inline-block;
}
<div class="menu">
<div id="menu-body">
<input id="red" type="radio" name="opcoes" checked/>
<label for="red">Red</label>
<input id="blue" type="radio" name="opcoes"/>
<label for="blue">Blue</label>
<input id="green" type="radio" name="opcoes"/>
<label for="green">Green</label>
</div>
<div class="menu-header">≡ Open≡ Close</div>
</div>
You should consider accessability using this method, or at minimum, how this effects site navigation.
Edit: A demo in regards to discussion in comments:
.menu {
position: relative;
width: 45%;
}
input[type="radio"] {
position: absolute;
opacity: 0;
height: 0;
width: 0;
}
a:any-link {
all: unset;
}
#menu-header {
position: absolute;
top: 0;
padding: 5px 10px;
color: white;
width: 100%;
background-color: cornflowerblue;
}
#menu-header a {
font-weight: bold;
cursor: pointer;
color: white;
font-size: 22px;
}
#menu-header .close {
display: none;
}
#menu-body {
display: none;
flex-flow: column nowrap;
position: absolute;
top: 34px;
background-color: rgba(220,220,220,1);
height: 100px;
color: black;
width: 100%;
padding: 10px;
}
.menu-header a,
#menu-body label {
cursor: pointer;
}
#menu-body:not(:target) {
display: none;
}
#menu-body:not(:target) ~ .menu-header > a:not(.close) {
display: inline-block;
}
#menu-body:target {
display: flex;
}
#menu-body:target ~ #menu-header > a {
display: none;
}
#menu-body:target ~ #menu-header > a.close {
display: inline-block;
}
#red:target ~ .box {
background-color: red;
}
#blue:target ~ .box {
background-color: blue;
}
#green:target ~ .box {
background-color: green;
}
.box {
background-color: black;
width: 75px; height : 75px;
}
<div class="menu">
<input id="red" type="radio" name="opcoes" checked/>
<input id="blue" type="radio" name="opcoes"/>
<input id="green" type="radio" name="opcoes"/>
<div id="menu-body">
Red
Blue
Green
</div>
<div class="box"></div>
<div id="menu-header">
≡ Open
≡ Close
</div>
</div>

HTML and CSS Make Several Divs Aligned On The Same Line

I am making a website with a navigation menu on the top. I have multiple buttons with dropdown menus. I want to make the buttons in all in a row at the same height and I want the buttons to be in the middle. Here is my code:
/* Links CSS */
a,
a:visited,
a:active {
text-decoration: underline;
color: white;
}
a:hover {
color: red;
text-decoration: underline;
}
/* Element CSS */
html,
body {
margin: 0px;
background-color: white;
}
html {
height: 100%;
width: 100%;
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
position: relative;
padding-bottom: 6rem;
min-height: 100%;
}
.button,
.games,
.programs,
.apps,
.misc {
font-family: 'Arsenal';
font-size: 18px;
text-decoration: underline;
background: transparent;
padding-top: 1%;
padding-bottom: 1%;
padding-left: 1.5%;
padding-right: 1.5%;
cursor: pointer;
border: 0px;
}
.content {
font-family: 'Arsenal';
font-size: 15px;
text-decoration: none;
background-color: white;
border: 0px;
cursor: pointer;
}
.button:hover,
.content:hover,
.games:hover,
.programs:hover,
.apps:hover,
.misc:hover {
background-color: #f0efef;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
padding: 0.5%;
}
.games-dropdown {
position: relative;
display: inline-block;
}
.progams-dropdown {
position: relative;
display: inline-block;
}
.apps-dropdown {
position: relative;
display: inline-block;
}
.misc-dropdown {
position: relative;
display: inline-block;
}
.show {
display: block;
}
/* Div CSS */
#page {
margin-top: 0px;
}
#title {
font-family: 'Arsenal';
font-size: 37px;
padding-top: 0.5%;
color: black;
display: inline-block;
cursor: pointer;
}
#links {
margin: auto;
display: inline-block;
overflow: auto;
}
#content {
background-color: white;
border: 1px solid black;
border-radius: 3px;
margin-left: 10%;
margin-right: 10%;
box-shadow: 1px 1.5px #8f8f8f;
}
#footer {
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding: 1rem;
background-color: #f0efef;
text-align: center;
border-top: 1px solid black;
font-family: 'Arsenal';
font-size: 15px;
}
<!-- HTML -->
<center>
<div id="links">
<!-- Code For Dropdown Menus | Code From http://www.w3schools.com/ | Thanks To Them!-->
<!-- Games Dropdown Menu: Browser and Downloadable-->
<div class="programs-dropdown">
<button class="programs" onclick="games()" title="Games">Games</button>
<div id="gamesDropdown" class="dropdown-content">
<button class="content" onclick="download()">Download</button>
<button class="content" onclick="online()">Online</button>
</div>
</div>
<!-- Programs Dropdown Menu: Windows and Max OS X-->
<div class="programs-dropdown">
<button class="programs" onclick="programs()" title="Programs">Programs</button>
<div id="programsDropdown" class="dropdown-content">
<button class="content" onclick="windows()">Windows</button>
<button class="content" onclick="mac()">Mac OS X</button>
</div>
</div>
<button class="button" onclick="websites()" title="Websites">Websites</button>
<button class="button" onclick="home()" title="Home">Home</button>
<!-- Apps Dropdown Menu: IOS and Android -->
<div id="apps-dropdown">
<button class="apps" onclick="apps()" title="Apps">Apps</button>
<div id="appsDropdown" class="dropdown-content">
<button class="content" onclick="ios()">IOS</button>
<button class="content" onclick="android()">Android</button>
</div>
</div>
<button class="button" onclick="blog()" title="Blog">Blog</button>
<!-- Misc. Dropdown Menu: Chrome Extensions, GitHub, Etc.-->
<div id="misc-dropdown">
<button class="misc" onclick="misc()" title="Misc.">Misc.</button>
<div id="miscDropdown" class="dropdown-content">
<button class="content" onclick="chrome()">Chrome Extensions</button>
<button class="content" onclick="github()">GitHub</button>
</div>
</div>
</div>
</center>
The easiest way to center elements is to use flexbox.
You can learn about it more at: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Using flexbox to center elements is actually quite simple. Assuming you have set of elements:
<div class="elements">
<div class="element__item">Element one</div>
<div class="element__item">Element two</div>
<div class="element__item">Some other element</div>
</div>
You can center them horizontally & vertically using css like this:
.elements {
display: flex;
justify-content: center;
align-items: center;
}
You can check this on JSfiddle: https://jsfiddle.net/kf405784/