Change colour of all elements expect the hovered element using CSS - html

I am creating a list using HTML and CSS. What I am trying to achieve is all the elements will be black until they are hovered on. When hovered, all elements except the hovered text will change color. I am attaching my code; it would be helpful if anyone could point out the mistake or tell me why it's not working.
.sub-menu-mast {
line-height: 30px;
background-color: rgb(254, 254, 254);
width: 9rem;
cursor: pointer;
}
.sub-menu-mast>li>a {
color: black;
position: relative;
left: 10%;
}
.sub-menu-mast a:not(:hover) {
color: red;
}
<nav>
<ul>
<li>
<a>XYZ </a>
<ul class="sub-menu-mast">
<li>Track</li>
<li>Return</li>
</ul>
</li>
</ul>
</nav>

Not sure, if I understood your request. Do you mean something like this?
.sub-menu-mast {
line-height: 30px;
background-color: rgb(254, 254, 254);
width: 9rem;
cursor: pointer;
}
.nav,
.menu,
.sub-menu-mast {
outline: 1px dashed teal;
}
.sub-menu-mast>li>a {
color: black;
position: relative;
left: 10%;
}
.sub-menu-mast li a {
outline: 1px dashed red;
}
.sub-menu-mast:hover a:not(a:hover) {
color: red;
}
* {
box-sizing: border-box;
}
.my-nav {
width: fit-content;
display: flex;
flex-flow: row nowrap;
outline: 1px dashed teal;
}
.my-nav ul {
padding: 0;
margin: 0;
}
.my-nav li {
width: 100%;
}
.my-nav a {
color: black;
display: block;
padding: 5px;
border-bottom: 1px solid black;
}
.my-sub-menu li a {
padding-left: 2em;
}
.my-nav li:hover,
.my-sub-menu li:hover {
background: wheat;
}
/* either this ... */
.my-sub-menu:hover li a:not(:hover) {
color: red;
}
/* ... or that */
/*
.my-sub-menu:hover li a {
color: red;
}
.my-sub-menu:hover li a:hover {
color: black;
}*/
<nav class="nav">
<ul class="menu">
<li>
<a>XYZ </a>
<ul class="sub-menu-mast">
<li>Track</li>
<li>Return</li>
<li>More</li>
<li>Fixed</li>
</ul>
</li>
</ul>
</nav>
<nav class="my-nav">
<ul class="my-menu">
<li>Item A</li>
<ul class="my-sub-menu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</ul>
</nav>

Related

How to display child HTML element over top of area cleared by parent element clip-path?

See https://jsfiddle.net/scott8035/gqn0t9a7/3/.
In this example, I have a section of a page content box displayed with a box shadow only on left & right sides. I achieve that effect by adding clip-path: inset(0 -10px); to the content box's CSS. So far, everything is good.
There is an element inside the content box. When you hover over it, a drop-down menu appears. However, the menu is also clipped by the clip-path from the parent content box instead of being displayed in its entirety.
How can I display the menu child element over the top of the clipped area so you can see the entire thing?
Note: I am somewhat hampered in how I can structure the HTML because I'm using a page builder, notably, the menu has to be a child element of the content box.
Here is the code in case the jsfiddle doesn't work:
<body>
<div id="content-box">
<div class="hoverable">
<p>
Element 1
</p>
<p>
Element 2
</p>
<p>
Element 3
</p>
<div class="menu">
<ul>
<li>Menu item 1</li>
<li>Menu item 2</li>
<li>Menu item 3</li>
<li>Menu item 4</li>
<li>Menu item 5</li>
</ul>
</div>
</div>
</div>
</body>
body {
background-color: green;
}
#content-box {
width: 50%;
margin: 40px auto;
padding: 40px;
background-color: white;
box-shadow: 0 0 10px #000000;
clip-path: inset(0 -10px);
}
.hoverable {
border: 1px solid #bbb;
padding: 10px;
position: relative;
}
p {
background-color: #eee;
padding: 5px;
}
.hoverable:hover .menu {
display: block;
}
.menu {
display: none;
width: 35%;
background-color: black;
padding: 0;
margin: 0;
position: absolute;
top: 172px;
}
.menu ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.menu ul li {
border-bottom: 1px solid #555;
}
.menu ul li a {
display: block;
padding: 5px 10px;
color: white;
text-decoration: none;
}
.menu ul li:hover a {
background-color: #333;
color: red;
}
Apply the trick to a pseudo element instead:
body {
background-color: green;
}
#content-box {
width: 50%;
margin: 40px auto;
padding: 40px;
background-color: white;
position:relative;
}
#content-box::before {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
box-shadow: 0 0 10px #000000;
clip-path: inset(0 -10px);
pointer-events:none;
}
.hoverable {
border: 1px solid #bbb;
padding: 10px;
position: relative;
}
p {
background-color: #eee;
padding: 5px;
}
.hoverable:hover .menu {
display: block;
}
.menu {
display: none;
width: 35%;
background-color: black;
padding: 0;
margin: 0;
position: absolute;
top: 172px;
}
.menu ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.menu ul li {
border-bottom: 1px solid #555;
}
.menu ul li a {
display: block;
padding: 5px 10px;
color: white;
text-decoration: none;
}
.menu ul li:hover a {
background-color: #333;
color: red;
}
<body>
<div id="content-box">
<div class="hoverable">
<p>
Element 1
</p>
<p>
Element 2
</p>
<p>
Element 3
</p>
<div class="menu">
<ul>
<li>Menu item 1</li>
<li>Menu item 2</li>
<li>Menu item 3</li>
<li>Menu item 4</li>
<li>Menu item 5</li>
</ul>
</div>
</div>
</div>
</body>

Div Not displaying on hovering

I am trying to display some content in div tag when hovering on the menu. Here I am trying to display a div when hovering on About. But it does not work.Kindly only check the commented portion. Ignore rest!
Here is my html:
<html>
<head>
<link rel="stylesheet" href="Style/styling.css">
<head>
<body>
<div class="container">
<header class="head-nav">
<img class="logo" src="Images/Logo.jpg">
<nav class="navigation">
<ul>
<li>Home</li>
<!––The hovering Menu––>
<li class="dispmenu">About</li>
<li >Center</li>
<li >Team </li>
<li>Team</li>
<li>Testing</li>
<li>Services</li>
<ul>
</nav>
</header>
<div class="submenu">
Hello
</div>
</div>
</body>
</html>
And CSS:
.container {
width: 1000px;
height: 1000px;
margin: 0 auto;
border: 1px solid gray;
}
header {
overflow: hidden;
}
.logo {
float: left;
}
.navigation ul {
list-style: none;
}
.navigation ul li {
background-color: #e0e0d1;
margin-top: 40px;
width: 96px;
border: 1px solid lightblue;
height: 40px;
text-align: center;
float: left;
}
.navigation ul li a {
font-size: 11px;
text-decoration: none;
color: black;
}
li a:hover:not(.active) {
color: #2485ba;
}
.active {
color: #2485ba;
}
/*the css for hiding and displaying*/
.dispmenu:hover .submenu {
visibility: visible;
}
.submenu {
background-color: darkblue;
color: white;
width: 684;
height: 100px;
margin-left: 312px;
visibility: hidden;
}
Here's a jQuery option if you go that route..
fiddle
https://jsfiddle.net/Hastig/ns6j55zg/
You can read about .hover here
$('.dispmenu').hover(function() {
$('.submenu').addClass('dispmenu-hover');
}, function() {
$('.submenu').removeClass('dispmenu-hover');
})
body {
margin: 0;
}
.container {
/*width: 1000px;*/
/*height: 1000px;*/
margin: 0 auto;
border: 1px solid gray;
}
header {
overflow: hidden;
}
.logo {
float: left;
}
.navigation ul {
list-style: none;
display: flex;
}
.navigation ul li {
display: flex;
background-color: #e0e0d1;
margin-top: 40px;
/*width: 96px;*/
flex: 1;
border: 1px solid lightblue;
height: 40px;
text-align: center;
float: left;
}
.navigation ul li a {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
text-align: center;
font-size: 11px;
text-decoration: none;
color: black;
}
li a:hover:not(.active) {
color: #2485ba;
}
.active {
color: #2485ba;
}
.submenu {
background-color: darkblue;
color: white;
width: 684;
height: 100px;
margin-left: 312px;
visibility: hidden;
}
/*the css for hiding and displaying*/
/* this class moved to appear after .submenu class */
.dispmenu-hover {
visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<header class="head-nav">
<img class="logo" src="Images/Logo.jpg">
<nav class="navigation">
<ul>
<li>Home</li>
<!––The hovering Menu––>
<li class="dispmenu">About</li>
<li>Center</li>
<li>Team </li>
<li>Team</li>
<li>Testing</li>
<li>Services</li>
</ul>
</nav>
</header>
<div class="submenu">
Hello
</div>
</div>
.dispmenu:hover .submenu {
You have a space between those selectors. A space is a descendant combinator.
The element <div class="submenu"> is not a descendant of the element <li class="dispmenu">. It is a sibling of the great-grandparent of the list item.
CSS selectors provide no way to describe going up the DOM tree, so you can't achieve this without either:
Changing the structure of the DOM
Using JavaScript
Ideally you should do both since:
It doesn't make much sense for a submenu to be completely disconnected from the menu item it is for. Think about how that type of data is normally represented.
You can't put in things like time delays before a menu vanishes, which makes a menu like the one you are trying to create challenging for people with some accessibility needs (e.g. sufferers of arthritis who many struggle to move the mouse in a straight line and keep the pointed inside the borders of the menu).

how to set a font size and padding percentage to be equal each boxes?

I did some research with font size and boxes sizes. I created ul li and styling them with some CSS to make them looks like a table and stay inline. after the epic code i realize that each words are different like home, tutorial, contact us, and registration. all font have a different sizes and different paddings to use.
so this is my code...
i tidy this up so it's not looks like in my localhost. what i did was made the percentage like 3.5754% to set the padding to get the exact SAME width of the boxes. so 4 of them should be the same size of boxes.
at home i created padding like 5% or so.
at tutorial i created padding like 3.something%
and etc.
because each words has a different width and number of letters.
sorry for my English, please let me know if i'm confusing u guys..
but please help about this percentage and font thing. i really don't get it. if u guys have some article to read it's also help... because i'm still looking for it. ty guys :)
body {
margin: 0;
padding: 0;
}
.navigation {
top: 0;
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
padding-top: 5%;
padding-left: 0;
width: 100%;
margin:
/*0 7.2%*/
;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
list-style: none;
display: inline;
border: solid 1px #e74c3c;
margin-left: 0;
}
#nav1 {
padding: 1% 1%;
}
#nav2 {
padding: 1% 5.74%;
}
#nav3 {
padding: 1% 3%;
}
#nav4 {
padding: 1% 3%;
}
.nav:hover {
padding: 2.2% 5%;
border: solid 1px #e74c3c;
}
.nav.currentpage {
padding: 2.2% 5%;
border: solid 1px #e74c3c;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>
2 easy option I believe:
1) display:table + table-layout:fixed;
body {
margin: 0;
padding: 0;
}
.navigation {
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
display:table;
width:100%;
padding:0;
table-layout:fixed;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
display:table-cell;
border: solid 1px #e74c3c;
}
#nav1 {
}
#nav2 {
}
#nav3 {
}
#nav4 {
}
.nav:hover {
border: solid 1px ;
}
.nav.currentpage {
border: solid 1px ;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>
2) display:flex + flex:1;
body {
margin: 0;
padding: 0;
}
.navigation {
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
display:flex;
padding:0;
list-style-type:none;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
flex:1;
border: solid 1px #e74c3c;
}
#nav1 {
}
#nav2 {
}
#nav3 {
}
#nav4 {
}
.nav:hover {
border: solid 1px ;
}
.nav.currentpage {
border: solid 1px ;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>
Using percentages is not always the best idea, specially for padding. Instead, use min-width, a normal padding and control the gutter with font-size:0 on the parent and normal padding on the LIs
Here is the code:
.nav-head{
font-size:0;
}
.nav {
padding: 2px 5px !important;
min-width: 116px;
display: inline-block;
margin: 0 !important;
font-size: 15px;
border:1px solid red;
}
And here is the DEMO
Here is the explanation on the font-zero concept link
You can use Flexbox to help you in making all the items with same width.
body {
margin: 0;
padding: 0;
}
.navigation {
top: 0;
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
display: flex;
padding-top: 5%;
padding-left: 0;
width: 100%;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
list-style: none;
flex: 1;
border: solid 1px #e74c3c;
margin-left: 0;
padding-top: 2.2%;
padding-bottom: 2.2%;
}
.nav:hover {
border: solid 1px #e74c3c;
}
.nav.currentpage {
padding-top: 2.2%;
padding-bottom: 2.2%;
border: solid 1px #e74c3c;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>

Making a Drop-Down menu

I'm trying to make a responsive menu without using bootstrap but i just can't make it
<ul class="nav">
<li class="active">Border</li>
<li>Gradient</li>
<li>Transform</li>
<li>Animations</li>
<li>Transition</li>
<li>Text Shadow</li>
<li>Box Shadow</li>
<li>Font-Face</li>
<li>RGBA</li>
</ul>
So this is the menu and on a specific viewport width it shoud become a drop down. Can you help me?
I have done my best to use javascript as less as possible and to fit to your markups. Hope it will help you!
document.querySelector(".nav").addEventListener("click", function(evt) {
console.log("nav");
if (!this.classList.contains("open")) {
this.classList.add("open");
this.classList.remove("closed");
} else {
this.classList.remove("open");
this.classList.add("closed");
}
evt.stopPropagation();
});
document.querySelector("body").addEventListener("click", function() {
var nav = document.querySelector(".nav");
if (!nav.classList.contains("open")) {
nav.classList.add("open");
nav.classList.remove("closed");
} else {
nav.classList.remove("open");
nav.classList.add("closed");
}
});
var li_elements = document.querySelectorAll(".nav li");
console.log(li_elements);
[].forEach.call(li_elements, function(li_element) {
li_element.addEventListener("click", function() {
console.log("remove actove");
document.querySelector(".nav li.active").classList.remove("active");
this.classList.add("active");
});
});
document.querySelector(".nav li.active").addEventListener("click", function(evt) {
evt.preventDefault();
});
.nav {
border: solid 1px #aaa;
background-image: linear-gradient(to bottom, #ddd, #eee);
height: 1.5em;
font-size: 1em;
padding: 0.10em 0.5em;
cursor: pointer;
width: 200px;
}
.nav.closed li.active:before {
content: "";
float: right;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 10px solid #999;
margin-top: 10px
}
.nav li {
display: none;
}
.nav.open {
height: auto;
background: white;
}
.nav li {
border-bottom: solid 1px #ddd;
padding: 0;
margin: 0
}
.nav li a {
text-decoration: none;
color: #666;
display: block;
margin: 0;
padding: 0.25em;
}
.nav.open li {
display: block;
}
.nav.open li:hover {
background: #eee;
}
.nav.closed li.active {
display: block;
color: white;
border:none;
}
<ul class="nav closed">
<li class="active">Border
</li>
<li>Gradient
</li>
<li>Transform
</li>
<li>Animations
</li>
<li>Transition
</li>
<li>Text Shadow
</li>
<li>Box Shadow
</li>
<li>Font-Face
</li>
<li>RGBA
</li>
</ul>

Getting display:table-cell and position:absolute to play nicely

By default I have a <ul> that's hidden from view which becomes visible when hovering over the parent <li>. When it does come into view the parent <li> expands its width by 1px. For some reason this only happens when using display:table-cell on the list items, which I'd prefer to keep using given the ability to center vertically. But the 1px jump is unwanted.
<ul class="nav-menu">
<li class="downloads"><a>Downloads</a>
<ul class="downloads-menu">
<li>Download 1</li>
<li>Download 2</li>
<li>Download 3</li>
</ul>
</li>
<li class="about"><a>About</a></li>
<li class="resources"><a>Resources</a></li>
<li class="contact"><a>Contact</a></li>
</ul>
ul.nav-menu {
position: relative;
height: 50px;
width: 100%;
background: black;
color: white;
}
ul.nav-menu > li {
display: table;
float: left;
list-style: none;
height: 50px;
border-right: 1px solid #333;
padding: 0 20px;
}
li a {
display: table-cell;
vertical-align: middle;
}
ul.nav-menu li.downloads:hover ul.downloads-menu {
display: block;
}
ul.downloads-menu {
display: none;
position: absolute;
top: 50px;
left: 0;
height: 250px;
width: 200px;
background: red;
}
I've setup a fiddle here: http://jsfiddle.net/azoc8c4m/1/
ul.nav-menu > li {
float: left;
list-style: none;
height: 50px;
line-height: 50px;
border-right: 1px solid #333;
padding: 0 20px;
}
li a {
}