how to make border when hover on menu element - css

I want to make border around hovered menu element, i have a problem that when i hover the rest menu elements is moving right, since i used padding, how can i make it stay fixed?
.menu ul li {
display: inline-block;
}
.menu li {
line-height: 152px;
font-size: 14px;
padding-left: 40px;
text-transform: uppercase;
}
.menu a {
color: #000;
}
.menu li a.active {
color: #4bcaff;
}
.menu li a:hover {
border: 1px solid red;
padding: 5px;
}
<nav class="menu">
<ul>
<li>Home</li>
<li>Services</li>
<li>Features</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>

You should set the padding on the regular state of the <a> element instead of :hover. The :hover state inherits the padding of the regular state. So the padding and "big" flickering issue should be gone.
To avoid a "small" flickering issue on the :hover state, caused by the additional border you have to set a not visible border (with same border-width) to the regular state.
See the following solution, using the "placeholder-border" and border:
.menu ul li {
display: inline-block;
line-height: 152px;
font-size: 14px;
padding-left: 40px;
text-transform: uppercase;
}
.menu a {
color: #000;
padding: 5px;
border: 1px solid transparent;
}
.menu li a.active {
color: #4bcaff;
}
.menu li a:hover {
border: 1px solid red;
}
<nav class="menu">
<ul>
<li>Home</li>
<li>Services</li>
<li>Features</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>
Another solution without the "placeholder-border" and border, using box-shadow:
.menu ul li {
display: inline-block;
line-height: 152px;
font-size: 14px;
padding-left: 40px;
text-transform: uppercase;
}
.menu a {
color: #000;
padding: 5px;
}
.menu li a.active {
color: #4bcaff;
}
.menu li a:hover {
-webkit-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
-moz-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
}
<nav class="menu">
<ul>
<li>Home</li>
<li>Services</li>
<li>Features</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>

I suggest three changes (the third is what really solves your issue):
box-sizing: border-box for all elements - makes styling more consistent and intuitive.
Remove padding and instead add margin for the li elements. Remember that padding is additional space from the content to the border of the element; margin however is additional space outside the border so it is a better fit to put some space between items.
Add the padding for your a elements not only on hover, but also for their regular state; then on hover, make the padding 1px smaller in order to level the added 1px border.
* {
box-sizing: border-box;
}
.menu ul li {
display: inline-block;
}
.menu li {
line-height: 152px;
font-size: 14px;
margin-left: 40px;
text-transform: uppercase;
}
.menu a {
color: #000;
padding: 5px;
}
.menu li a.active {
color: #4bcaff;
}
.menu li a:hover {
border: 1px solid red;
padding: 4px;
}
<nav class="menu">
<ul>
<li>Home</li>
<li>Services</li>
<li>Features</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>

Related

My a:hover is not working for some reason

Not sure why it's not working, but here's my code.
.tm-nav ul li{
display: inline-block;
margin-top: 20px;}
.tm-nav ul li a {
padding: 10px 20px;
text-decoration: none;
font-family: Trebuchet MS;
font-size: 22px;
margin-left: 50px;
color: #8e7e6b;
}
.tm-nav a:hover: {
color: black;}
And here is my HTML:
<nav class="tm-nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Products</li>
<li>Privacy</li>
</ul>
</nav>
Whenever, I hover over my navigation links, they don't change color, even adding text-decoration: underline under a:hover does not take effect.
.tm-nav a:hover: {
color: black;}
should be
.tm-nav a:hover {
color: black;}
you have an extra colon sitting on ".tm-nav a:hover {}"
I removed the colon and tried it on my computer and it worked.

w3schools example for overflow:hidden [duplicate]

This question already has answers here:
CSS overflow:hidden with floats
(5 answers)
Closed 6 years ago.
I already know about overflow:hidden that It hide text when text can't be filled in border. but I don't know why the following coding need it.
This code creates navigation bar but if I erase overflow:hidden, It doesn't work. I want to know about this happening. hidden works for what?
please help.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
That is because you are not clearing float left.Please remove overflow:hidden; from code and add
ul:after{
Content:"";
display:block;
clear:both;
}
This will work properly.
its about float left when child has this property then parent leave child's height property so then ul tag invisible becouse that have no height
so us like this
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
Hope this helps you
THANKS
Actually it works well if you give ul a height.
Here the overflow is well explained: W3C
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
height:46px;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

Dropdown menu: Submenu name is below the menu bar and not within the nav box

I've coded a css dropdown menu and cant seem to get the name "LOGOS" to stay within the green box when I hover over the word "Illustration". I've made the word 5em so I can see it. Cant get it to stay in the box...no control of it's position. Can you help?
Thanks,
T.
<div id="nav-bar-sm-cont">
<ul id="sm-nav">
<li id="home">HOME</li>
<li id="about">PROFILE</li>
<li id="illustration">ILLUSTRATION
<ul>
<li id="logos">LOGOS</li>
</ul>
</li>
<li id="billboards">BILLBOARDS</li>
<li id="six-mo">6 MO BREAKFAST</li>
<li id="cal">ARTSHOW</li>
<li id="church">CHURCH</li>
<li id="contact">CONTACT</li>
<li id="cat-ill">CAT ILLUSTRATION</li>
<li id="contact-cat">CONTACT CAT</li>
</ul>
</div>
<!--end nav bar sm container -->
/* START small NAV bar **************************/
#nav-bar-sm-cont { position: absolute;
width: 1000px;
height: 100px;
}
#sm-nav li { position: relative;
top: 30px;
left: 35px;
font-size: .6em;
line-height: 250%;
letter-spacing: 0.3em;
list-style-type: none;
float: left;
}
#sm-nav a:link{ text-decoration:none;
color:silver;
background-color:transparent;
padding: 5px 5px;
}
#sm-nav a:visited {text-decoration:none;
color: #9781B7;
padding: 5px 5px;
background-color: transparent;
}
#sm-nav a:hover {text-decoration:none;
color: #fff;
background-color:#a7d6d5;
padding: 5px 5px;
}
#sm-nav a:active {text-decoration:none;
color:#fff;
background-color: green;
padding: 5px 5px;
}
/*start drop down************************************/
#sm-nav li ul { position:relative;
list-style-type: none;
display: none;
}
#sm-nav li:hover ul { position: absolute;
background: green;
padding: 5px 5px;
display:block;
font-size: 5em;
width: 103px;
height: 10px;
}
/*end drop down*****************************************/
/* END small NAV BAR *****************************/
The problem is that your CSS for the li is affecting both the parent li and the child. To fix that just change:
#sm-nav li {
to
#sm-nav > li {
Now that CSS will only affect the parent li and you're free to adjust the CSS for the child however you want like this:
#sm-nav li:hover ul li { }
JSFiddle

How do I stop CSS inheritance?

please take a look at the CSS below the subMenu li keep inheriting all the stylings from the MainMenu even when I gave it it own ones!
For example there are padding on the MainMenu, the subMenu applies them to itself as well even if I gave it it own padding it won't use them and it will still uses the ones that are on the MainMenu, Please help, thanks in advance...
#headerMenu{
float: right;
}
/*Main Menu*/
#MainMenu li{
padding: 8px 12px 5px 12px;
position: relative;
list-style: none;
float: left;
}
#MainMenu a{
color: #757575;
}
#MainMenu a:hover{
color: #7FA0BA;
}
#MainMenu a.active{
color: #305067;
}
#MainMenu li a.getStartedButton{
color: #A3C182;
}
#MainMenu li a.getStartedButton:hover{
color: #7FA0BA;
}
/*SubMenu*/
ul.subMenu{
background-color: #FFFFFF;
border: 1px solid black;
position: absolute;
top: 49px;
-webkit-box-shadow: 0px 4px 15px -5px rgba(0,0,0,0.43);
-moz-box-shadow: 0px 4px 15px -5px rgba(0,0,0,0.43);
box-shadow: 0px 4px 15px -5px rgba(0,0,0,0.43);
}
.subMenu li{
background-color: orange;
width: 95px;
padding: 2px 2px 2px 3px;
position: relative;
text-align: left;
list-style: none;
}
.subMenu li a{
background-color: black;
}
/*Tablets & Smart Phones (Must Be Hidden From Widescreen)*/
.SecondHeader-button,#SecondHeader{
display: none;
}
/*////////////////////////////*/
The HTML is as follows:
<header>
<div id="wrapperHeader">
<div class="scrollable" id="headerContent">
<section class="headerLogo">
<img id="logoImage" src="assets/elements/logo.png" alt="LOAI Design Studio Logo"/>
</section>
<section id="headerMenu">
<nav>
<ul id="MainMenu">
<li><a class="active" href="index.html">Home</a></li>
<li><a class="SubMenu" href="#">Portfolio</a>
<ul class="subMenu">
<li>Web Design</li>
<li>Visual Identity</li>
<li>Photography</li>
</ul>
</li>
<li>Testimonials</li>
<li>About Me</li>
<li>Get In Touch</li>
<li><a class="getStartedButton" href="get-started.html">Get Started</a></li>
</ul>
</nav>
</section>
</div>
</div>
</header>
According to CSS Specificity Rules
#MainMenu li
Is more specific than
.subMenu li
And that is why the latter (.subMenu li) doesn't override the former (#MainMenu li).
Try:
#mainMenu .subMenu li
And read on http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

How to set child unordered list position?

I have a nav bar with children unordered lists nested under the main navbar items. My problem is on the inner list, when I position absolute, the inner list is centered to the page, and when I position relative, it is positioning inline with the parent list.
I'm trying to get the first child item to line up directly under it's parent.
/* OUTER LIST STYLING */
div#navbar2 {
position:relative;
width: 100%;
border-top: solid #000 1px;
border-bottom: solid #546F8B 1px;
background-color: #546F8B;
}
div#navbar2 ul#navbar {
padding: 0;
margin: 10px 0;
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
letter-spacing:1px;
color: #FFF;
white-space: nowrap;
}
div#navbar2 ul#navbar li {
margin: 0 2px;
list-style-type: none;
display: inline;
}
div#navbar2 li a {
text-decoration: none;
color: #fff;
padding: 10px 10px;
}
div#navbar2 li a:link {
color: #FFF:
}
div#navbar2 li a:visited {
color: #ffffff;
}
div#navbar2 li a:hover {
color: #000;
background-color: #FDFFC9;
}
/* INNER LIST STYLING */
div#navbar2 ul#navbar li ul.innerlist{
display: none;
color:#000;
}
div#navbar2 ul#navbar li ul.innerlist li{
color:#000;
}
div#navbar2 ul#navbar li:hover ul.innerlist {
position: absolute;
display: inline;
left: 0;
width: 100%;
margin: 40px 0 0px 0px;
padding: 0px;
color:#000;
}
div#navbar2 li.innerlist a {
text-decoration: none;
color: #000;
padding: 10px 10px;
}
div#navbar2 li.innerlist a:link {
color: #000:
}
div#navbar2 li.innerlist a:visited {
color: #000;
}
div#navbar2 li.innerlist a:hover {
color: #000;
background-color: #FDFFC9;
}
And my html:
<div id="navbar2">
<ul id="navbar">
<li id="index">About Rattletree</li>
<li id="upcomingshows">Calendar</li>
<li id="booking">Contact
<ul class="innerlist">
<li class="innerlist">Booking</li>
<li class="innerlist">press</li>
</ul>
</li>
<li id="instruments">The Band
<ul class="innerlist">
<li class="innerlist">The Instruments</li>
<li class="innerlist">The Players</li>
</ul>
</li>
<li id="classes">Sights & Sounds
<ul class="innerlist">
<li class="innerlist">Listen</li>
<li class="innerlist">photos</li>
<li class="innerlist">video</li>
</ul>
</li>
<li id"classes">Workshops & Classes</li>
</ul>
</div>
Thanks for any help!
If you set the #navbar > li {position: relative;} and the ul.innerlist {position: absolute; } then it, and its descendant li elements, will be positioned in relation to the #navbar > lis instead of the page.
If I understood correctly what do you want, just add this to your CSS
#navbar .innerlist {
display: block;
margin-left: 0px;
padding-left: 0px;
}

Resources