This question already has answers here:
Make CSS Hover state remain after "unhovering"
(11 answers)
Hover CSS styling that persists after mouse away
(3 answers)
Closed 3 years ago.
I have created a navigation bar with a :hover effect and I'd like to know how to make that effect persistent, even after the cursor is removed from the area. The effect is a background color under the given word. For example, when Home is selected I'd like for that effect to stay on the element until another element (for example, Register) is clicked.
.navbar {
list-style-type: none;
background-color: black;
display: flex;
justify-content: flex-start;
align-items: center;
margin: 0;
height: 50px;
padding: 0;
}
.navbar a {
text-decoration: none;
color: gray;
padding: 17px;
transition: background-color 0.5s linear;
}
.navbar a:hover {
background-color: green;
color: honeydew;
}
body {
margin: 0;
}
<header>
<nav>
<ul class='navbar'>
<li><a href='#home'>Home</a></li>
<li><a href='#download'>Download</a></li>
<li><a href='#register'>Register</a></li>
<li><a href='#contact'>Contact</a></li>
<li><a href='#FAQ'>FAQ</a></li>
</ul>
</nav>
</header>
The desired effect can be achieved with just CSS but it's tricky and the only way I know of would cause your links to become non-functional (since it requires replacing your links with <input type="radio">). So overall, I would recommend to use JavaScript for this.
The JavaScript way would be, using jQuery, to add a class when the mouseover event is triggered and to remove it from all other elements on another desired event getting triggered.
Example: (I have replaced the :hover pseudoselector with a .hover class selector and added the jQuery library and required code.)
jQuery is not required to achieve this effect, you can do this just fine with vanilla JavaScript.
$(".navbar a").on("mouseover", function () {
$(".navbar a").removeClass("hover");
$(this).addClass("hover");
});
.navbar {
list-style-type: none;
background-color: black;
display: flex;
justify-content: flex-start;
align-items: center;
margin: 0;
height: 50px;
padding: 0;
}
.navbar a {
text-decoration: none;
color: gray;
padding: 17px;
transition: background-color 0.5s linear;
}
.navbar a.hover {
background-color: green;
color: honeydew;
}
body {
margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<nav>
<ul class='navbar'>
<li><a href='#home'>Home</a></li>
<li><a href='#download'>Download</a></li>
<li><a href='#register'>Register</a></li>
<li><a href='#contact'>Contact</a></li>
<li><a href='#FAQ'>FAQ</a></li>
</ul>
</nav>
</header>
Related
I posted a photo for you to see exactly what problem I have. I don't understand why the style does not apply to the ul and li. I apply the background style to the nav-container so I believe that should be applied to every element right?
I could use a bit of help and some explanation to understand this thing.
header {
background-image: url('Logos\ and\ Assets/cover.jpg');
height: 100vh;
background-size: cover;
background-position: center;
}
.nav-container {
display: flex;
justify-content: space-between;
padding: 10px 10%;
background-color: rgba(153, 217, 140, 0.3);
}
.nav-ul-2 {
list-style-type: none;
}
.nav-ul-2 li {
display: inline-block;
padding: 0% 20px;
}
.nav-ul-1 {
list-style-type: none;
}
.nav-ul-1 li {
transition: all 0.3s ease 0s;
}
.nav-ul-1 li:hover {
color: yellowgreen;
}
.nav-ul-2 li {
[enter image description here][1] transition: all 0.3s ease 0s;
}
.nav-ul-2 li:hover {
color: yellowgreen;
}
<header>
<nav class="nav-container">
<ul class="nav-ul-1">
<li>Acasa</li>
</ul>
<ul class="nav-ul-2">
<li>Servicii</li>
<li>Despre Noi</li>
<li>Contact</li>
</ul>
<div>
<img src="Logos and Assets/roFlag.svg" class="flag" alt="ro-flag">
<img src="Logos and Assets/englFlag.svg" class="flag" alt="eng-flag">
</div>
</nav>
</header>
i dont understant exactly what you are trying here because
your navigation seems to me like this with background color :
Typically your containers are going to be div elements that surround certain areas of the page (a nav-container, a .container for the whole body element, etc.) Try using a div to wrap your nav element and apply your background style to it.
I want to have a border-bottom effect on hover.
I am using transition property on hover, however after the transition-duration is over the border size changes/reduces by a pixel or so, which seems weird. Please help fix this and also would love to know the reason for this effect.
HTML -
<nav class="navbar">
<ul>
<li>
Home
</li>
<li>
About us
</li>
<li>
Contact us
</li>
</ul>
</nav>
CSS -
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.navbar {
width: 90vw;
margin: 0 auto;
font-size: 1.2rem;
background-color: #f1f1f1;
}
.navbar ul {
display: flex;
list-style: none;
justify-content: center;
}
.navbar ul li {
padding: 10px 20px;
margin: 5px;
color: #333;
cursor: pointer;
border-bottom: 4px solid transparent;
}
.navbar ul li:hover {
color: orangered;
border-bottom: 4px solid orangered;
transition: all 1s;
}
Here is the code - https://codepen.io/rawn01/pen/bGBmBdK
The reason for this happening is how the browser optimizes rendering. For animating / transitioning effects it often separates the affected element into a separate "composition layer" for performance reasons.
You can try several ways to attempt to fix the graphical issue:
add will-change attribute in order to tell the browser to keep the element on a separate layer
modify your transition: all 1s to a more specific one (e.g. transition: color 1s, border-bottom-color 1s). Also make sure to only modify the border-bottom-color attribute, not the whole border-bottom attribute with size and type (even though it stays the same)
This question already has an answer here:
Why does clip-path (and other properties) affect the stacking order (z-index) of elements later in DOM?
(1 answer)
Closed 2 years ago.
I made my navigation bar and positioned it (fixed), and it works fine. I was able to scroll down and all. As soon has I added filter (brightness) to it the image on my page, the navigation bar disappeared. I have tried using pseudo-elements and setting the position (absolute/relative), I set the filter property to the container of the child element of the image, it still didn't work. Can someone help me on how to have my navigation bar display on fixed position and still have the image filtered. Thanks in Advance.
nav {
position: fixed;
background-color: #fff;
}
nav ul {
margin: 0;
padding: 0;
}
.navbar-brand {
padding-right: 20px;
}
nav li {
display: inline-block;
padding: 10px;
}
nav a {
color: #000;
text-decoration: none;
}
nav a:hover {
color: #ff6600;
text-decoration: none;
}
.title-image img {
width: 100%;
height: 300px;
filter: brightness(60%);
}
<nav>
<ul>
<li>
<a class="navbar-brand" href="#">Navbar Brand</a>
</li>
<li>
Home
</li>
<li>
About
</li>
<li>
services
</li>
</ul>
</nav>
<div class="title-image">
<img src="https://images.unsplash.com/photo-1599546824091-f49550ce8cbc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60">
</div>
JSfiddle Demo
Just add z-index to your nav element as follow
nav{
position: fixed;
background-color: #fff;
z-index:999;
}
nav ul{
margin: 0;
padding: 0;
}
.navbar-brand{
padding-right: 20px;
}
nav li{
display: inline-block;
padding: 10px;
}
nav a{
color: #000;
text-decoration: none;
}
nav a:hover{
color: #ff6600;
text-decoration: none;
}
.title-image img{
width: 100%;
height: 300px;
filter: brightness(60%);
}
<nav>
<ul>
<li>
<a class="navbar-brand" href="#">Navbar Brand</a>
</li>
<li>
Home
</li>
<li>
About
</li>
<li>
services
</li>
</ul>
</nav>
<div class="title-image">
<img src="https://images.unsplash.com/photo-1599546824091-f49550ce8cbc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60">
</div>
The navbar didn't disappear, it is just beneath the image. To have it in front, you should use z-index: 10; (or any value greater than 0).
See more at : https://developer.mozilla.org/en-US/docs/Web/CSS/z-index
In addition, keep in mind that your image - or any element after your navbar - will be positioned on top of your page. May be you'll want to let the equivalent of the navbar height as space before any content.
Hello this is my first time using Sass on a project so im not sure if it is because I setup the code in correctly in sass or if its the particles js causing issues.
Logo - For some reason im able to change the size of the logo but I'm not able to change the color.
Menu Links - Not sure why but for some reason the menu's underline is not working and I cant even click on them if I wanted to but when I change the size of links it works.
Here is a link in case you want see it live
http://lonestarwebandgraphics.com/
<div id="header-home">
<!-- <div id="particles-js"></div> -->
<div class="container">
<nav id="main-nav">
<img src="img/RE-LOGO_01.png" alt="My Portfolio Logo" id="logo">
<ul>
<li>Home</li>
<li>About Me</li>
<li>Capabilities</li>
<li>Skills</li>
<li>Projects</li>
<li>Contact Me</li>
</ul>
</nav>
<div class="header-content">
<h1>DESIGNER</h1>
<h1>+</h1>
<h1>DEVELOPER</h1>
<p class="lead">Welcome to my website. I am a Graphic / Web Designer & Web Developer</p>
</div>
</div>
<div id="particles-js"></div>
</div>
CSS CODE
#logo {
width: 70px;
height: 70px;
color: #fff;
}
#main-nav {
z-index: 20;
display: flex;
justify-content: space-between;
padding-top: 1rem;
background: none;
ul {
display: flex;
}
li {
padding: 1rem 1.5rem;
}
a {
text-decoration: none;
color: #fff;
text-transform: uppercase;
// CSS Below is For Border Bottom Hover Effect
border-bottom: 3px transparent solid;
padding-bottom: 0.5rem;
transition: border-color 1s;
&:hover {
border-color: $light-blue;
}
&.current {
border-color: $yellow-color;
}
}
}
The div's with classes .particles-js-canvas-el, .header-content, #header-home #particles-js, #header-home are above the nav bar which are making to click on those items instead of actual nav bar elements, so you have to send them back and get the navbar to front so that you can click on the items.
Add the follwing code at the end of the main.css file
css/main.css
.white-logo { filter: brightness(100); }
body
{
background: #00111e;
}
.particles-js-canvas-el, .header-content, #header-home #particles-js
{
position: absolute;
z-index: -100;
height: 100%;
width: 100%;
}
#header-home
{
background-color: rgba(0, 0, 0, 0);
z-index: -1;
}
To change color of the logo, apply the required class from the css
<img src="img/RE-LOGO_01.png" alt="My Portfolio Logo" id="logo" class="white-logo">
Thank you
I was able to fix my problems. By playing with the code and getting some advice on here.
for the logo I just uploaded a white svg to save time. At first I was try to upload svg at original colro and change it with css but after looking at examples it looked like you need to add a lot of code or use some javascript so to make it easy I just changed the color in photoshop.
to fix the navbar I add position Relative in css
HTML
<nav id="main-nav">
<img src="img/logo-white-1.svg" alt="My Portfolio Logo" id="logo">
<ul>
<li>Home</li>
<li>About Me</li>
<li>Capabilities</li>
<li>Skills</li>
<li>Projects</li>
<li>Contact Me</li>
</ul>
</nav>
SASS
#main-nav {
z-index: 20;
display: flex;
justify-content: space-between;
padding-top: 1rem;
background: none;
position: relative;
ul {
display: flex;
}
li {
padding: 1rem 1.5rem;
}
a {
text-decoration: none;
color: #fff;
text-transform: uppercase;
// CSS Below is For Border Bottom Hover Effect
border-bottom: 3px transparent solid;
padding-bottom: 0.5rem;
transition: border-color 1s;
&:hover {
border-color: $light-blue;
}
&.current {
border-color: $yellow-color;
}
}
}
I uploaded to this domain for now but I plan on moving the website to another domain later (in case this link doesnt work in the future)
http://lonestarwebandgraphics.com/
I'm running into issues on my first ever website. I have successfully created a navigation bar at the top which looks and acts somewhat how I want it to (other than the color scheme but that can come later). The issue is that whenever I click a different link on the bar I want that box to change color, but it is currently stuck highlighted on the homepage. I think this is something super simple but i cannot find it. Thank you for the help.
body
{
font-family:sans-serif;
width: 100%;
margin: 0px;
}
/* upper strip holding the tabs*/
ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
position:fixed;
top: 0px;
width: 100%;
background-color: #328CC1
}
li
{
float:left;
border-right:3px solid #30FFE3;
}
li a
{
display: block;
color: whitesmoke;
text-decoration: none;
padding: 14px 16px;
text-align: center;
}
li a:hover:not(.active)
{
background-color: #111;
}
a.active
{
background-color: #EAB126
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>Academics</li>
<li>Athletics</li>
<li>Contact</li>
</ul>
Change this:
a.active
background-color: #EAB126
}
to this:
li a:focus {
background-color: #EAB126
}
Here is the JSFiddle demo
This does what you ask BUT if this is a navigation bar then bear in mind that the control will lose focus as soon as you change page. It would be better if you use Javascript/JQuery to handle that much more easily.
A typical way of handling this is to have each page have a class that includes the page name: <div class="academics"> for example.
Now modify your header (within the page div) as follows:
<ul>
<li><a class="for_home" href="#home">Home</a></li>
<li><a class="for_academics" href="#academics">Academics</a></li>
<li><a class="for_athletics" href="#athletics">Athletics</a></li>
<li><a class="forcontact" href="#contact">Contact</a></li>
</ul>
this would be followed by the css as follows:
.home .for_home, .academics .for_academics, .athletics .for_athletics, .contact .for_contact {
background-color: #EAB126;
}
Then the menu item for the current page will be highlighted.
Congratulations on your first website!