I'm really new to HTML and CSS. I have a navbar which underlines the menu item you hover with a fade in, fade out-effect from the middle,
but how can I keep an active menu item underlined with the same style?
I'm also using Typo3 / Fluid which creates me the html code and assigns the "active" class to the active menu item.
This is how it looks like so far: https://jsfiddle.net/wr5w09r0/
css
div#top_nav{
text-align: center;
}
div#top_nav li{
display: inline;
padding: 15px;
}
div#top_nav a {
display: inline-block;
position: relative;
}
div#top_nav a:hover{
color: orange;
}
div#top_nav a:before{
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: -3px;
left: 0;
background-color: orange;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.15s ease-in-out 0s;
transition: all 0.15s ease-in-out 0s;
}
div#top_nav a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
.active {
color: orange;
}
Hope this will help you. I have added this simple element.
div#top_nav a:hover:before , div#top_nav a.active:before
a {
text-decoration: none;
}
div#top_nav{
text-align: center;
}
div#top_nav li{
display: inline;
padding: 15px;
}
div#top_nav a {
display: inline-block;
position: relative;
}
div#top_nav a:hover{
color: orange;
}
div#top_nav a:before{
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: -3px;
left: 0;
background-color: orange;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.15s ease-in-out 0s;
transition: all 0.15s ease-in-out 0s;
}
div#top_nav a:hover:before , div#top_nav a.active:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
.active {
color: orange;
}
<div id="top_nav">
<ul>
<li class="mainMenu-itemLevel1">
seite1</li>
<li class="mainMenu-itemLevel1">
seite2
</li><li class="mainMenu-itemLevel1">
seite3</li>
<li class="mainMenu-itemLevel1">
seite4</li>
<li class="mainMenu-itemLevel1">
Seite5 lang Hover</li>
</ul>
</div>
Hope this will help you.
You need to use javascript to do this.
Your javascript would add a class, like active to the relevant menu item, and you can then use your css to style it appropriately.
After adding the active class to the element, to apply your orange style use this:
div#top_nav.active a{
color: orange;
}
Note the addition of .active which selects only active items, and the omission of :hover on the link, since you no longer care about hover.
You may find the , css operator useful here, for applying the same styles to different selectors (active, and hover) as shown in my example below:
var items = document.getElementsByClassName('item');
var activeClassName = 'active';
function unselectItems() {
for (var i = 0; i < items.length; i++) {
items[i].classList.remove(activeClassName);
}
}
function selectItem(item) {
unselectItems();
item.classList.add(activeClassName);
}
function onItemClick(event) {
selectItem(event.target);
}
for (var i = 0; i < items.length; i++) {
items[i].addEventListener('click', onItemClick);
}
span {
border: 1px solid black;
padding: 5px;
}
span:hover, span.active {
color: white;
background-color: black;
}
<nav>
<span class="item">Home</span>
<span class="item">About</span>
<span class="item">Contact</span>
<nav>
Related
I made a menu that appears when clicking a hamburger button with CSS and a checkbox. It works nicely so far. But now I want it to disappear when clicking elsewhere on the page, not only when clicking on the hamburger button again. How do I do that, please?
HTML:
<div class="navigatie">
<input class="nav-toggle" id="nav-toggle" type="checkbox">
<nav>
<ul>
<li>Home</li>
<li>Werkwijze</li>
<li>Over ons</li>
<li>Faq</li>
<li>Accreditatie</li>
</ul>
</nav>
<label for="nav-toggle" class="nav-toggle-label">
<span><i class="fas fa-bars"></i></span>
</label>
</div>
CSS:
.nav-toggle {
display: none;
}
nav {
position: absolute;
bottom: 100%;
left: 30%;
background-color: #f1f1f1;
width: 40%;
transform: scale(1,0);
transform-origin: bottom;
transition: transform 400ms ease-in-out;
}
nav a {
color: #65A624;
font-size: 1rem;
opacity: 0;
transition: opacity 150ms ease-in;
display: block;
}
nav a:hover:not(.active) {
background-color: #555;
color: white;
}
nav a.active {
background-color: #65A624;
color: white;
}
.nav-toggle:checked ~ nav {
transform: scale(1,1);
}
.nav-toggle:checked ~ nav a {
opacity: 1;
transition: opacity 250ms ease-in-out 250ms;
}
Greetings,
Sandra.
$(document).ready(function () {
$(document).click(function (event) {
var clickover = $(event.target);
var _opened = $(".navbar-collapse").hasClass("navbar-collapse in");
if (_opened === true && !clickover.hasClass("navbar-toggle")) {
$("button.navbar-toggle").click();
}
});
});
Check out this jsfiddle sample.
http://jsfiddle.net/52VtD/5718/
CSS Solution
If you want it purely CSS you can do the example below, I've edited a few things because your code missed some CSS and HTML to make it reproducible correctly. But the point should be clear. ;)
A overlay appears after opening, you can close the menu by clicking anywhere in the overlay (darker background)
Advice: use JavaScript
I would recommend however to make it easier and use JavaScript if that is a option.
.nav-toggle-label {
width: 3rem;
height: 3rem;
margin: 0;
display: block;
background-color: #0F0;
}
.nav-toggle {
width: 0.1rem;
height: 0.1rem;
position: absolute;
top: 0;
left: 0;
opacity: 0;
}
nav {
width: 40%;
position: absolute;
top: 0%;
left: 30%;
z-index: 110;
background-color: #f1f1f1;
transform: scale(1, 0);
transform-origin: bottom;
transition: transform 400ms ease-in-out;
}
nav a {
color: #65A624;
font-size: 1rem;
opacity: 0;
transition: opacity 150ms ease-in;
display: block;
}
nav a:hover:not(.active) {
background-color: #555;
color: white;
}
nav a.active {
background-color: #65A624;
color: white;
}
.nav-toggle:checked~.navigatie nav {
transform: scale(1, 1);
}
.nav-toggle:checked ~ .navigatie a {
opacity: 1;
transition: opacity 250ms ease-in-out 250ms;
}
.nav-toggle-close {
width: 100%;
height: 100%;
position: absolute;
top: 0%;
left: 0%;
background-color: rgba(0, 0, 0, 0.3);
display: none;
z-index: 100;
}
.nav-toggle:checked ~ .nav-toggle-close {
display: block;
}
<input class="nav-toggle" id="nav-toggle" type="checkbox">
<label for="nav-toggle" class="nav-toggle-close"></label>
<div class="navigatie">
<nav>
<ul>
<li>Home</li>
<li>Werkwijze</li>
<li>Over ons</li>
<li>Faq</li>
<li>Accreditatie</li>
</ul>
</nav>
<label for="nav-toggle" class="nav-toggle-label">
<span><i class="fas fa-bars">Menu</i></span>
</label>
</div>
I would like to know how to animate the link when I hover on it. I want the top border to move down and the bottom border to move up. The borders should fade away, when the animation ends. When I hover on the link, the borders should appear and show the animation.
HTML:
<ul>
<li class="active">Home</li>
<li>About Me</li>
<li>My Passion</li>
</ul>
CSS:
ul li a {
border-bottom: 3px transparent solid;
border-top: 3px transparent solid;
}
ul li a::before {
position: absolute;
left: 0;
top: 0;
visibility: hidden;
-moz-transition: 0.3s;
-o-transition: 0.3s;
-webkit-transition: 0.3s;
transition: 0.3s;
}
ul li a::after {
position: absolute;
left: 0;
top: 100%;
visibility: hidden;
-moz-transition: 0.3s;
-o-transition: 0.3s;
-webkit-transition: 0.3s;
transition: 0.3s;
}
ul li a:hover {
color: blue;
}
ul li a:hover::before {
visibility: visible;
top: 100%;
background: blue;
}
ul li a:hover::after {
visibility: visible;
top: 0;
background: blue;
}
Expected result:
I've re-created the example in your GIF.
For the text hover effect, make sure to use transition to ease the effect, e.g.:
a {
color: white;
text-decoration: none;
transition: 800ms ease color;
display: block;
}
a:hover {
color: gold;
}
I'm displaying the a tag as block to make sure it covers the whole li elements when hovered.
When using CSS pseudo elements always set a content property (content: '' is you want to style it):
li::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 5px;
background: gold;
opacity: 0;
}
Instead of using a border, I'm animating the pseudo before en after elements on hover. You can do this by using CSS keyframes.
li:hover::before {
opacity: 1;
animation: flyDown 300ms ease forwards;
}
li:hover::after {
opacity: 1;
animation: flyUp 300ms ease forwards;
}
#keyframes flyDown {
from {
transform: translateY(0px)
}
to {
transform: translateY(90px)
}
}
#keyframes flyUp {
from {
transform: translateY(00px)
}
to {
transform: translateY(-90px)
}
}
Please find jsfiddle here: https://jsfiddle.net/sanderdebr/fn4pgwv6/30/
---html---
<span class="a-border" onclick="menuclick(this)">ABOUT US</span>
<span class="a-border" onclick="menuclick(this)">CREATERS</span>
<span class="a-border" onclick="menuclick(this)">NEWS</span>
<span class="a-border" onclick="menuclick(this)">CONTACT</span>
---css---
a {
text-decoration: none;
}
a:hover {
cursor: pointer;
}
.a-border {
display: inline-block;
position: relative;
color: white;
padding: 0.5rem 0.25rem;
margin: 0 1.5rem;
overflow: hidden;
}
.a-border::after {
content: "";
display: block;
position: absolute;
bottom: -8px;
left: -3%;
width: 106%
border-top: 2px solid white;
transform: scale(0, 1);
transform-origin: 0% 100%;
transition: transform 0.4s;
}
a:hover .a-border::after {
transform:scale(1, 1);
}
a.focused .a-border::after {
transform: none;
}
---js---
function menuclick(underline) {
var focused = document.getElementsByClassName("focused");
var i;
for (i = 0; i < focused.length; i++) {
var under = focused[i];
if (under.classList.contains('focused')) {
under.classList.remove('focused');
}
}
if (!underline.parentElement.classList.contains('focused')) {
underline.parentElement.classList.add('focused');
}
}
I am trying to make a simple navigation menu consisting of links with an animating underline, as demonstrated by Tobias Ahlin http://tobiasahlin.com/blog/css-trick-animating-link-underlines/
I get this to work, however I can't figure out how to have the underline visible immediately if the list element is active.
Any help is welcome, thanks a lot!
Fiddle: https://jsfiddle.net/131d8q1v/5/
HTML:
<div class="container">
<ul>
<li class="active">About</li>
<li>Contact</li>
</ul>
</div>
CSS:
ul {
list-style-type:none;
}
a {
position: relative;
color: #000;
text-decoration: none;
}
a:visited {
color: #000;
text-decoration:none;
}
a:hover {
color: #000;
text-decoration:none;
}
a:before {
content: "";
position: absolute;
width: 100%;
height: 4px;
bottom: -2px;
left: 0;
background-color: #000;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
Add this rule:
li.active a:before,
a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
ul {
list-style-type: none;
}
a {
position: relative;
color: #000;
text-decoration: none;
}
a:visited {
color: #000;
text-decoration: none;
}
a:hover {
color: #000;
text-decoration: none;
}
a:before {
content: "";
position: absolute;
width: 100%;
height: 4px;
bottom: -2px;
left: 0;
background-color: #000;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
li.active a:before,
a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
<div class="container">
<ul>
<li class="active">About</li>
<li>Contact</li>
</ul>
</div>
I want to make the sliding underline to run from left to right when i hover, and also set up the width of the line from the 1st letter to the last, and not bigger. How can i do that?
.nav-bar a:hover {
color: #000;
}
.nav-bar a:before {
content: "";
position: absolute;
width: 100%;
height: 1px;
bottom: 0;
left: 0;
background-color: #000;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
.nav-bar a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
<div class="nav-bar">
<ul>
<li>RETROSPECTIVE SHOW /2006/</li>
<li>TEXTS</li>
<li>BIBLOGRAPHY</li>
</ul>
</div>
You can use display: inline-block; to keep the fixed width. And for underlining you can use pseudo-classes like :before and :after.
Have a look at the snippet below:
/* LEFT TO RIGHT */
.sliding-left-to-right {
display: inline-block;
margin: 0;
}
.sliding-left-to-right:after {
content: '';
display: block;
height: 3px;
width: 0;
background: transparent;
transition: width .5s ease, background-color .5s ease;
}
.sliding-left-to-right:hover:after {
width: 100%;
background: blue;
}
/* LAYOUT STYLING */
body {
margin: 20px;
}
a {
text-decoration: none;
font-size: 20px;
font-weight: bold;
color: blue;
}
a:hover {
color: blue;
text-decoration: none;
cursor: pointer;
}
<a class="sliding-left-to-right">Underline – Left to Right</a>
Hope this helps!
Can you underline a text on hover using css? (Like the behavior of a link but not an actual link.)
you have the following text Hello work
when you hover your mouse over the text it underlines it using css
(the text is not a link)
<span class="txt">Some Text</span>
.txt:hover {
text-decoration: underline;
}
You just need to specify text-decoration: underline; with pseudo-class :hover.
HTML
<span class="underline-on-hover">Hello world</span>
CSS
.underline-on-hover:hover {
text-decoration: underline;
}
I have whipped up a working Code Pen Demo.
Fairly simple process I am using SCSS obviously but you don't have to as it's just CSS in the end!
HTML
<span class="menu">Menu</span>
SCSS
.menu {
position: relative;
text-decoration: none;
font-weight: 400;
color: blue;
transition: all .35s ease;
&::before {
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: 0;
left: 0;
background-color: yellow;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
&:hover {
color: yellow;
&::before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
}
}
li {
display:inline-block;
padding:15px;
}
li:hover {
color: Blue;
border-bottom: 3px solid Blue;
}
<ul>
<li>Hello work</li>
</ul>
.resume_link:hover{
text-decoration: underline;
text-decoration-color: rgb(250, 114, 64);
-moz-text-decoration-color: rgb(250, 114, 64);
text-decoration-thickness: 5px;
}
<a class="resume_link">Resume</a>
<span class="text">Click Me</span>
.text:hover {
text-decoration: underline black //This colour is an example
}