Creating a clickable dropdown menu with HTML & CSS [duplicate] - html

This question already has answers here:
Javascript onclick simulation using only css and html (without javascript)
(3 answers)
Closed last year.
I am currently working on a project using only HTML & CSS. I am wanting to make the menu button only display the dropdown items when it is clicked, and have an "x" button that will close them when it is clicked. I am able to make the menu dropdown on hover but am having no luck with it on click without JavaScript.
Does anyone know how I might be able to do this with just HTML & CSS? Here is the HTML & CSS that I have as of right now:
.header-menu {
background-color: #fff;
font-family: "Noto Sans", sans-serif;
font-size: 18px;
}
.header-menu-items {
display: none;
z-index: 1;
max-width: 100%;
overflow-wrap: break-word;
font-weight: 900;
}
.a {
color: #000;
padding: 10px;
text-decoration: none;
}
.search {
display: none;
}
.search-icon {
display: none;
}
.header-menu-items hr {
color: #d0d0d0;
background-color: #d0d0d0;
height: 1px;
border: none;
}
.header-menu-btn {
text-align: left;
background-color: #ff3b3b;
color: #fff;
font-size: 20px;
font-weight: 600;
padding: 10px 0;
border: none;
cursor: pointer;
width: 100%;
margin-bottom: 10px;
padding-left: 10px;
}
.header-menu:hover .header-menu-items {
display: block;
}
<div className="header-menu">
<button className="header-menu-btn">Menu</button>
<div className="header-menu-items">
<div>
<a className="a" href="">
Shirts
</a>
<hr />
<a className="a" href="">
Pants
</a>
<hr />
<a className="a" href="">
Shoes
</a>
<hr />
<a className="a" href="">
T-shirts
</a>
<hr />
<a className="a" href="">
Accessories
</a>
<hr />
</div>
</div>
</div>

Javascript onclick simulation using only css and html (without javascript)
I know your answer is in the answer to this post but I can't really figure out how to do it myself.
It would require changing your button to an input tag of checkbox type and going from there.

First of all it has to be class and not className.
What you want to do can be achieved with a checkbox and the sibling selector ~.
.header-menu {
background-color: #fff;
font-family: "Noto Sans", sans-serif;
font-size: 18px;
}
.header-menu-items {
display: none;
z-index: 1;
max-width: 100%;
overflow-wrap: break-word;
font-weight: 900;
}
.a {
color: #000;
padding: 10px;
text-decoration: none;
}
.search {
display: none;
}
.search-icon {
display: none;
}
.header-menu-items hr {
color: #d0d0d0;
background-color: #d0d0d0;
height: 1px;
border: none;
}
.header-menu-btn {
text-align: left;
background-color: #ff3b3b;
color: #fff;
font-size: 20px;
font-weight: 600;
padding: 10px 0;
border: none;
cursor: pointer;
width: 100%;
margin-bottom: 10px;
padding-left: 10px;
display: block;
}
#toggle-menu {
display: none;
}
#toggle-menu:checked ~ .header-menu-items {
display: block;
}
#toggle-menu ~ label .hide {
display: none;
}
#toggle-menu:checked ~ label .show {
display: none;
}
#toggle-menu:checked ~ label .hide {
display: inline;
}
<div class="header-menu">
<input type="checkbox" id="toggle-menu"><label for="toggle-menu" class="header-menu-btn">Menu <span class="show">show</span><span class="hide">hide<span></label>
<div class="header-menu-items">
<div>
<a class="a" href="">
Shirts
</a>
<hr />
<a class="a" href="">
Pants
</a>
<hr />
<a class="a" href="">
Shoes
</a>
<hr />
<a class="a" href="">
T-shirts
</a>
<hr />
<a class="a" href="">
Accessories
</a>
<hr />
</div>
</div>
</div>

Related

CSS & HTML Button Under each other adding text in between [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I'm trying to create 2 buttons with different links and have the second button be a clickable image. For some reason the text in between the buttons acts as a hyperlink. How do I fix this?
.button1 {
background-color: #E44040;
border: none;
color: white;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button2 {
border: none;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button1 {
border-radius: 2px;
}
.button2 {
border-radius: 12px;
}
<body style="font-family: arial">
<p>Click the button below For youtube</p>
<div>
<a href="https://www.youtube.com/" method="get" target="_blank">
<button class="button button1">Youtube</button>
</div>
<div>
<p>Click the button below fork Reddit</p>
<a href="https://www.reddit.com/" method="get" target="_blank">
<button class="button button2" style="border: 0; background: transparent">
<img src="https://toppng.com/uploads/preview/reddit-icon-reddit-logo-transparent-115628752708pqmsy4kgm.png" width="90" height="90"></button>
</div>
You forgot to close the a tags. Try this:
Edit: changed the buttons to divs, see comment of user cloned.
body {
font-family: arial, sans-serif;
}
.button1 {
background-color: #E44040;
border: none;
color: white;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button2 {
border: none;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button1 {border-radius: 2px;}
.button2 {border-radius: 12px;}
<p>Click the button below For youtube</p>
<div>
<a href="https://www.youtube.com/" method="get" target="_blank">
<div class="button button1">Youtube</div>
</a>
</div>
<div>
<p>Click the button below fork Reddit</p>
<a href="https://www.reddit.com/" method="get" target="_blank">
<div class="button button2" style="border: 0; background: transparent">
<img src="https://toppng.com/uploads/preview/reddit-icon-reddit-logo-transparent-115628752708pqmsy4kgm.png" width="90" height="90">
</div>
</a>
</div>
You have not closed the <a> tag.
This is the corrected code:
.button1 {
background-color: #E44040;
border: none;
color: white;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button2 {
border: none;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button1 {border-radius: 2px;}
.button2 {border-radius: 12px;}
<body style="font-family: arial">
<p>Click the button below For youtube</p>
<div>
<a href="https://www.youtube.com/" method="get" target="_blank">
<button class="button button1">Youtube</button>
</a>
</div>
<div>
<p>Click the button below fork Reddit</p>
<a href="https://www.reddit.com/" method="get" target="_blank">
<button class="button button2" style="border: 0; background: transparent">
<img src="https://toppng.com/uploads/preview/reddit-icon-reddit-logo-transparent-115628752708pqmsy4kgm.png" width="90" height="90">
</button>
</a>
</div>
</body>

An <a> tag with an <img> tag inside lines up differently that an <a> tag with just text. How can I fix this?

I am working on a website, and I am trying to make a top navigation bar. The far left link should be an image with my logo, and the other ones should navigate to different parts of the site. The button with the image lines up differently that the other ones. How can I fix that?
<div id='topbar'>
<a href='index.html' class='topbar-button-home'>
<img src='camelCaseCo_centered_cropped_notext_bold_small.png'>
</a>
<a href='index.html' class='topbar-button'>Home</a>
<a href='index.html' class='topbar-button'>2</a>
<a href='index.html' class='topbar-button'>3</a>
<a href='index.html' class='topbar-button'>4</a>
</div>
<style>
#topbar {
padding: 0;
margin: 0;
display: inline-block;
font-size: 0;
}
.topbar-button {
width: 200px;
margin: 0px;
padding: 20px;
display: inline-block;
border: solid black 1px;
border-radius: 0px;
text-decoration: none;
color: black;
text-align: center;
font-family: "Google Sans", sans-serif;
font-size: 14px;
}
.topbar-button-home {
width: 50px;
margin: 0px;
padding: 8px;
border: solid black 1px;
display: inline-block;
text-decoration: none;
color: black;
text-align: center;
font-family: "Google Sans", sans-serif;
font-size: 14px;
}
</style>
Thanks in advance!
What about Flexbox?
#topbar {
padding: 0;
margin: 0;
display: flex;
}
.topbar-button {
width: 200px;
margin: 0px;
padding: 20px;
display: inline-block;
border: solid black 1px;
border-radius: 0px;
text-decoration: none;
color: black;
text-align: center;
font-family: "Google Sans", sans-serif;
font-size: 14px;
}
.topbar-button-home {
width: 50px;
margin: 0px;
padding: 8px;
border: solid black 1px;
display: inline-block;
text-decoration: none;
color: black;
text-align: center;
font-family: "Google Sans", sans-serif;
font-size: 14px;
}
<div id='topbar'>
<a href='index.html' class='topbar-button-home'><img src='camelCaseCo_centered_cropped_notext_bold_small.png'></a>
<a href='index.html' class='topbar-button'>Home</a>
<a href='index.html' class='topbar-button'>2</a>
<a href='index.html' class='topbar-button'>3</a>
<a href='index.html' class='topbar-button'>4</a>
<a href='index.html' class='topbar-button'><img src='camelCaseCo_centered_cropped_notext_bold_small.png'></a>
</div>
In the example above, all flex children will have the same height regardless of their content.
Remember, images are inline elements and so they are affected by line height. Try setting your image to diasplay block or inline-block. I see you have it on the link— move it to the image.

How to stack social icons

I'm having a brain fart. I apologize if this question has been answered before, but I don't really know developer lingo so I'm not sure what to type in.
I have built my own social icons, which will sit on the right of the screen. Currently, I'm using a seperate class for each icon with height: number vh; to make them stacked on top. I'm sure this isn't the correct way. Could anyone point me in the right direction?
Here is my current code:
.fa-facebook {
background: purple;
opacity: 0.5;
color: white !important;
border: 2px solid white;
}
.fa-twitter {
background: purple;
opacity: 0.5;
color: white !important;
border: 2px solid white;
}
/*social*/
.fa-social {
padding: 10px;
font-size: 30px;
width: 50px;
text-align: center;
text-decoration: none;
margin: 0;
position: fixed;
right: 10px;
}
p.social {
position: fixed;
right: 13px;
top:30vh;
text-align: center;
font-weight: 800;
}
.fa-social-1{
top:35vh;
}
.fa-social-2{
top:45vh;
}
.fa:hover {
opacity: 0.9;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-social-1 fa-facebook"></a>
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-social-2 fa-twitter"></a>
You can use flexbox. It will clean up most of your css.
<div class="social-group">
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-social-1 fa-facebook"></a>
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-social-2 fa-twitter"></a>
</div>
Css..
/* Only CSS */
.fa-facebook, .fa-twitter {
background: purple;
opacity: 0.5;
color: white !important;
border: 2px solid white;
padding: 10px;
font-size: 30px;
width: 50px;
text-align: center;
text-decoration: none;
margin: 0;
}
/* Extra CSS */
.social-group {
display: flex;
justify-content: flex-end;
margin-top: 30vh;
}
.fa:hover {
opacity: 0.9;
}
Working example
https://jsfiddle.net/khvrLyon/
Best not to use fixed position for the items individually - better to put them in a container, position that and let the browser handle the layout.
.fa-facebook {
background: purple;
opacity: 0.5;
color: white !important;
border: 2px solid white;
}
.fa-twitter {
background: purple;
opacity: 0.5;
color: white !important;
border: 2px solid white;
}
/*social*/
.fa-social {
padding: 10px;
font-size: 30px;
width: 50px;
text-align: center;
text-decoration: none;
margin: 0;
right: 10px;
}
.fa:hover {
opacity: 0.9;
}
#social-container{
position:fixed;
right:0;
top:35vh;
display:flex;
flex-direction:column;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div id="social-container">
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-social-1 fa-facebook"></a>
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-social-2 fa-twitter"></a>
</div>
You need to add padding in the Tags, here is an example:
<a style="text-decoration: none; padding-bottom: 10px" target="_blank"
class="fa fa-social fa-social-1 fa-facebook"></a>
<a style="text-decoration: none; padding-top: 10px" target="_blank"
class="fa fa-social fa-social-2 fa-twitter"></a>
If this does not work try:
<div class="container" style="padding-bottom: 10px;">
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-
social-1 fa-facebook"></a>
</div>
<div class="container" style="padding-top: 10px;">
<a style="text-decoration: none;" target="_blank" class="fa fa-social fa-
social-2 fa-twitter"></a>
</div>

How to change the colour of the icons in my navigation bar when hovering over the area, not just the image.

I'm trying to change the colour of the icons from White to Black in my navigation bar when hovering over the area, not just the image. I've got the image to change when I hover purely over the image, but I would like to expand this to the area around it. Any help would be appreciated, my code is below.
HTML:
.navbar {
width: 100%;
height: auto;
text-align: center;
background-color: transparent;
overflow: auto;
display: block;
}
.navbar a {
width: 20%;
padding: 2px 0;
float: left;
transition: all 0.3s ease;
color: white;
font-size: 12px;
text-decoration: none;
display: block;
}
.navbar a:hover {
background-color: white;
color: black;
display: block;
}
.active {
background-color: #4CAF50;
}
.menubar {
width: 32px;
height: 32px;
}
<div class="navbar">
<a href="homepage.html">
<figure>
<img class="menubar" src="../images/icons/home.png" onmouseover="this.src='../images/icons/home_black.png'" onmouseout="this.src='../images/icons/home.png'" />
<figcaption>Homepage</figcaption>
</figure>
</a>
<a href="car.html">
<figure>
<img class="menubar" src="../images/icons/car.png" onmouseover="this.src='../images/icons/car_black.png'" onmouseout="this.src='../images/icons/car.png'" />
<figcaption>Cars</figcaption>
</figure>
</a>
<a href="motorbike.html">
<figure>
<img class="menubar" src="../images/icons/motorcycle.png" onmouseover="this.src='../images/icons/motorcycle_black.png'" onmouseout="this.src='../images/icons/motorcycle.png'" />
<figcaption>Motorcycles</figcaption>
</figure>
</a>
<a href="cycle.html">
<figure>
<img class="menubar" src="../images/icons/bicycle.png" onmouseover="this.src='../images/icons/bicycle_black.png'" onmouseout="this.src='../images/icons/bicycle.png'" />
<figcaption>Bicycles</figcaption>
</figure>
</a>
<a href="boat.html">
<figure>
<img class="menubar" src="../images/icons/boat.png" onmouseover="this.src='../images/icons/boat_black.png'" onmouseout="this.src='../images/icons/boat.png'" />
<figcaption>Boats</figcaption>
</figure>
</a>
</div>
Homepage
Cars
Motorcycles
Bicycles
Boats
CSS
Looking at your current code I came up with several solutions for you.
You can move your onmouseover and onmouseout events to the a tag and use function to change your images.
function changeImageSrc(target, newSrc) {
var img = target.getElementsByTagName('img')[0];
img.src = newSrc;
}
.navbar {
width: 100%;
height: auto;
text-align: center;
background-color: transparent;
overflow: auto;
display: block;
}
.navbar a {
width: 20%;
padding: 2px 0;
float: left;
transition: all 0.3s ease;
color: white;
font-size: 12px;
text-decoration: none;
display: block;
}
.navbar a:hover {
background-color: white;
color: black;
display: block;
}
.active {
background-color: #4CAF50;
}
.menubar {
width: 32px;
height: 32px;
}
<a href="homepage.html" onmouseover="changeImageSrc(this, '../images/icons/home_black.png')" onmouseout="changeImageSrc(this, '../images/icons/home.png')">
<figure>
<img class="menubar" src="../images/icons/home.png" />
<figcaption>Homepage</figcaption>
</figure>
</a>
It will do, but I don't think it is the best solution.
You can achieve the desired result without using javascript at all. Add two images to the figure element and show only one at a time using :hover css class.
.navbar {
width: 100%;
height: auto;
text-align: center;
background-color: transparent;
overflow: auto;
display: block;
}
.navbar a {
width: 20%;
padding: 2px 0;
float: left;
transition: all 0.3s ease;
color: white;
font-size: 12px;
text-decoration: none;
display: block;
}
.navbar a:hover {
background-color: white;
color: black;
display: block;
}
.active {
background-color: #4CAF50;
}
.menubar {
width: 32px;
height: 32px;
}
.show-on-hover {
display: none;
}
.product:hover .hide-on-hover {
display: none;
}
.product:hover .show-on-hover {
display: inline;
}
<a class="product" href="motorbike.html">
<figure>
<img class="menubar hide-on-hover" title="Hidden on hover" src="../images/icons/motorcycle.png" />
<img class="menubar show-on-hover" title="Visible on hover" src="../images/icons/motorcycle_black.png" />
<figcaption>Motorcycles</figcaption>
</figure>
</a>
The best solution, imo, would be to use vector icons from font. Then you would be able to change icon color with css. If you have svg's of your icons you can create the font with them using icomoon.io or similar service.

How to make outer div doesn't react when inner div is :active in css

I have two div's inside each other. The outer one is bigger When I click on the outer it changes as far as I used .outer:active in css. When I click on the inner it also changes (I used .outer .inner:active), but the outer div changes also.
Here's jsfiddle: https://jsfiddle.net/hetjwnxa/2/
HTML:
<div class="testimonials-content">
<div class="testimonials-square">
<a href="">
<div class="translation-button">
<p>Translate</p>
</div>
</a>
<a href="">
<div class="testimonials-square-text">
<p>TITLE</p>
</div>
</a>
</div>
<div class="testimonials-square">
<a href="">
<div class="translation-button">
<p>Translate</p>
</div>
</a>
<a href="">
<div class="testimonials-square-text">
<p>TITLE</p>
</div>
</a>
</div>
<div class="testimonials-square">
<a href="">
<div class="translation-button">
<p>Translate</p>
</div>
</a>
<a href="">
<div class="testimonials-square-text">
<p>TITLE</p>
</div>
</a>
</div>
</div>
And here CSS:
.testimonials-content {
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
width: 750px;
background-color: #FFFFFF;
}
.testimonials-square {
width: 214px;
height: 214px;
border-radius: 10px;
border: 5px solid #282165;
color: #282165;
font-size: 22px;
text-align: center;
float: left;
margin-right: 26px;
margin-bottom: 27px;
background-color: #FFFFFF;
text-align: center;
color: #282165;
font-weight: bold;
}
.translation-button {
width: auto;
height: auto;
border-radius: 2px;
border: 2px solid #282165;
color: #282165;
font-size: 22px;
text-align: center;
float: left;
margin: 5px;
background-color: #FFFFFF;
text-align: center;
color: #282165;
font-weight: bold;
}
.testimonials-square .translation-button p {
color: #FFFFFF;
background-color: #282165;
}
.testimonials-square-text {
margin-top: 80px;
margin-left: 20px;
margin-right: 20px;
text-align: center;
}
.testimonials-square p {
color: #282165;
}
.testimonials-square:active {
background-color: #282165;
color: #FFFFFF;
}
.testimonials-square .translation-button:active p {
color: #282165;
background-color: #FFFFFF;
}
.testimonials-square:active p {
background-color: #282165;
color: #FFFFFF;
}
.testimonials-square:nth-child(2n+1) {
background-color: #282165;
color: #FFFFFF;
}
.testimonials-square:nth-child(2n+1) .translation-button {
border: 2px solid #FFFFFF;
}
.testimonials-square:nth-child(2n+1) .translation-button p {
background-color: #FFFFFF;
color: #282165;
}
.testimonials-square:nth-child(2n+1) .translation-button:active p {
background-color: #282165;
color: #FFFFFF;
}
.testimonials-square:nth-child(2n+1) p {
background-color: #282165;
color: #FFFFFF;
}
.testimonials-square:nth-child(2n+1):active {
background-color: #FFFFFF;
color: #282165;
}
.testimonials-square:nth-child(2n+1):active p {
background-color: #FFFFFF;
color: #282165;
}
p {
margin: 0px;
}
a {
text-decoration: none;
}
How can I make the outer div not to change after clicking on inner div?
Any help will be appreciated. Thanks!
When an element is :active, all it's ancestor elements will be :active too, for obvious reasons.
In your case, if you have access to the HTML, you could add an extra element (e.g. .testimonials-bg) inside the .testimonials-square as a sibling to .translation-button and style it so it stretches to fit .testimonials-square and add the :active style to it.
HTML
<div class="testimonials-content">
<div class="testimonials-bg"></div>
<div class="testimonials-square">
<a href="">
<div class="translation-button">
<p>Translate</p>
</div>
</a>
<a href="">
<div class="testimonials-square-text">
<p>TITLE</p>
</div>
</a>
</div>
CSS
.testimonials-bg {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.testimonials-bg:active {
background-color: #282165;
}
Another way of doing it is:
onmousedown event of the inner div, set the styles of outer div property back to normal.
onmouseup event of inner div, set style of inner div = ""; otherwise the outer div active styles will not work.