This question already has answers here:
Add a CSS border on hover without moving the element [duplicate]
(4 answers)
Closed 2 years ago.
My primary nav li elements shift downwards whenever I hover over them. I thought this was due to use of margin causing this, but I am still receiving this issue after removing margin use, and I'm not sure what it is. I know it's most likely something simple. Any help would be appreciated. Thank you.
/*primary nav bar*/
.primarynav {
background-color: #ffffff;
border: solid 1px #f76f4d;
position: relative;
height: 50px;
width: 1430px;
top: 10px;
}
.primarynav ul {
position: relative;
padding-bottom: 10px;
text-decoration: none;
padding-left: 100px;
}
.primarynav a {
position: relative;
display: inline-block;
text-decoration: none;
color: #fd886b;
width: 115px;
height: 50px;
padding: 17px 0px 0px 0px;
font-weight: bold;
border: 1px solid orangered;
}
/*primary navigation effects*/
/*.primarynav a:hover::before {
background-color: #fd886b;
}
*/
.primarynav a:hover {
color: white;
background-color: #fd886b;
border: 2px solid orangered;
border-radius: 3px;
}
.mainnavigation li {
display: inline-block;
bottom: 51px;
padding-top: 50px;
text-align: center;
position: relative;
font-size: 15px;
left: 200px;
}
<header class="primarynav">
<div class="primaryContainer"> <!-- Main top of page navigation -->
<nav alt="worldmainnavigation"><!-- Main navigation buttons on the top of the page (6) -->
<ul class= "mainnavigation">
<li>Home</li>
<li>Items</li>
<li>Categories</li>
<li>Favourites</li>
<li>Deals</li>
<li>List An Item</li>
</ul>
</nav>
</div>
</header>
Adding margin: -2px; resolves this, its due to the border: 2px orangered.
.primarynav a:hover {
color: white;
background-color: #fd886b;
border: 2px solid orangered;
margin: -2px;
border-radius: 3px;
}
/*primary nav bar*/
.primarynav {
background-color: #ffffff;
border: solid 1px #f76f4d;
position: relative;
height: 50px;
width: 1430px;
top: 10px;
}
.primarynav ul {
position: relative;
padding-bottom: 10px;
text-decoration: none;
padding-left: 100px;
}
.primarynav a {
position: relative;
display: inline-block;
text-decoration: none;
color: #fd886b;
width: 115px;
height: 50px;
padding: 17px 0px 0px 0px;
font-weight: bold;
border: 1px solid orangered;
}
/*primary navigation effects*/
/*.primarynav a:hover::before {
background-color: #fd886b;
}
*/
.primarynav a:hover {
color: white;
background-color: #fd886b;
border: 2px solid orangered;
margin: -2px;
border-radius: 3px;
}
.mainnavigation li {
display: inline-block;
bottom: 51px;
padding-top: 50px;
text-align: center;
position: relative;
font-size: 15px;
left: 200px;
}
<header class="primarynav">
<div class="primaryContainer"> <!-- Main top of page navigation -->
<nav alt="worldmainnavigation"><!-- Main navigation buttons on the top of the page (6) -->
<ul class= "mainnavigation">
<li>Home</li>
<li>Items</li>
<li>Categories</li>
<li>Favourites</li>
<li>Deals</li>
<li>List An Item</li>
</ul>
</nav>
</div>
</header>
it is because you add 3px border on hover.
Related
I am trying to make a dropdown menu, it's my first time doing it and I'm experimenting with it. The problem that I'm facing is this:
As you can see, the principal menus are showing in a list. I have tried displaying them as flex and other attempts, but it seems like the header is making a limitation to them and I don't know how to put them beside each other. (Clarification: 'Notificaciones' and 'Usuario' are main menus and 'Mi Perfil' is a submenu that comes from 'Usuario' (parent))
Here is my code:
* {
text-decoration: none;
list-style: none;
}
body {
margin: 0px;
padding: 0px;
font-family: roboto;
}
.header-text {
position: relative;
margin: 2px 6px 2px 4px;
cursor: pointer;
}
.header-icons {
width: 32px;
margin: 2px 0px 2px 0px;
cursor: pointer;
}
header {
display: flex;
justify-content: space-between;
background-color: rgb(20, 33, 61);
color: white;
height: 30px;
align-items: center;
padding: 6px;
position: static;
}
.nav li a {
padding: 4px;
background-color: red;
list-style: none;
display: inline-block;
float: right;
}
<header>
<div class="header-text">
<h1 class="titulo-logo">Lorem</h1>
</div>
<nav class="nav">
<ul>
<li>Notificaciones</li>
<li>
Usuario
<ul>
<li>Mi Perfil</li>
</ul>
</li>
</ul>
</nav>
</header>
Thank you so much in beforehand!
First the <li> should have display: inline-block for being arranged in a row. It has nothing to do with the header.
Second, the position of the sub menu (ul ul) needs to be absolute within a li with position: relative.
white-space: nowrap will make the element not wrap when the width is larger than the parent element's width.
* {
text-decoration: none;
list-style: none;
}
body {
margin: 0px;
padding: 0px;
font-family: roboto;
}
.header-text {
position: relative;
margin: 2px 6px 2px 4px;
cursor: pointer;
}
.header-icons {
width: 32px;
margin: 2px 0px 2px 0px;
cursor: pointer;
}
header {
display: flex;
justify-content: space-between;
background-color: rgb(20, 33, 61);
color: white;
height: 30px;
align-items: center;
padding: 6px;
position: static;
}
.nav li a {
padding: 4px;
background-color: red;
list-style: none;
display: inline-block;
float: right;
}
/* added css */
ul li{
display: inline-block;
position: relative;
white-space: nowrap
}
ul ul{
position: absolute;
right:0;
top:100%
}
<header>
<div class="header-text">
<h1 class="titulo-logo">Lorem</h1>
</div>
<nav class="nav">
<ul>
<li>Notificaciones</li>
<li>
Usuario
<ul>
<li>Mi Perfil</li>
</ul>
</li>
</ul>
</nav>
</header>
I'm trying to make a Navbar with a dropdown menu. I want 'Clips' to have a dropdown menu with 3 links. But I can't seem to get it to work.
If i set the position to absolute i get them stacked on top of eachother en when i change it to relative they go inside the link. Can someone help? Do I have to change the selectors?
Thanks in advance!
.navbar{
display: flex;
justify-content: space-around;
align-items: center;
font-size: 18px;
background-color: darkred;
}
.nav-list{
list-style-type: none;
}
.dropdown{
position: relative;
display: inline-block;
list-style-type: none;
padding: 10px 10px;
border: solid black 1px;
border-radius: 10px;
margin: 2px;
}
.dropdown ul li{
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: darkred;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 10;
overflow: visible;
}
.dropdown-content a {
color: black;
padding: 12px 18px;
text-decoration: none;
display: block;
}
.dropdown:hover .dropdown-content {
display: block;
}
.nav-list .list-item {
display: inline-block;
padding: 10px 10px;
border: solid black 1px;
border-radius: 10px;
margin: 2px;
}
.navbar a{
text-decoration: none;
color: white;
font-family: 'arvo', italic;
}
.logo{
height: 60px;
position: relative;
margin: 5px 0px 5px 17px;
}
.logo_text {
position: absolute;
top: 50px;
font-size: 12pt;
color: white;
}
<body>
<header>
<nav class="navbar">
<div>
<img class="logo" src="img/FrankLogo.svg"> <h3 class="logo_text"><a href=index.html>FRANKIE</a></h3>
</div>
<ul class="nav-list">
<li class="dropdown">Clips
<ul>
<li class="dropdown-content">Frankie Anthem</li>
<li class="dropdown-content">Vrouwtjes</li>
</ul></li>
<li class="list-item">Shows</li>
<li class="list-item">Vieze Victor</li>
<li class="list-item">Contact</li>
</ul>
</nav>
</header>
Set UL position inside top menu
li.dropdown > ul {
position: absolute;
margin-left: 0 !important;
padding-left: 0;
top: 40px;
}
I am building a menu. I have this code:
<nav>
<ul>
<li>Kontakt</li>
<li>Reference</li>
<li>Moje služby</li>
<li>Kdo jsem</li>
</ul>
</nav>
And CSS:
nav{
width: 100%;
height: 90px;
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
}
nav li{
float: right;
padding: 20px 35px 0 0px;
}
nav ul{
margin-right: 100px;
height: 90px;
list-style-type: none;
}
nav a{
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
}
nav a:hover{
text-align: center;
color: 33adae;
What I am trying to do is to make the links clickable like blocks with the height of the whole navbar. The way I have done It so far, you can click only the text in the links.
Generally all that is needed is.
nav a{
display:block;
}
However for a fuller example it's generally easier to let the links determine the height of the header.
For centering, don't use floats, set the ul to text-align:center and the li to display:inline-block.
* {
margin: 0;
padding: 0;
;
box-sizing: border-box;
}
nav {
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
overflow: hidden;
/* clearfix */
}
nav li {
display: inline-block;
}
nav ul {
list-style-type: none;
text-align: center;
}
nav a {
display: block;
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
height: 90px;
line-height: 90px;
padding: 0 25px;
}
nav a:hover {
text-align: center;
color: 33adae;
background: plum;
<nav>
<ul>
<li>Kontakt
</li>
<li>Reference
</li>
<li>Moje služby
</li>
<li>Kdo jsem
</li>
</ul>
</nav>
You could remove the padding from your lis and add it to your a tags. See example http://codepen.io/anon/pen/gaGxpb
Move your <li> padding to the <a> children, and give the links a height:
See codepen
NB: Added a border to the links so as you see the boundaries.
display the links as blocks display: block; and use the line-height to give them the height you want. Try this:
nav {
width: 100%;
height: 90px;
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
}
nav li {
float: right;
padding: 0px 35px 0 0px;
}
nav ul {
margin: 0 100px 0 0;
height: 90px;
list-style-type: none;
}
nav a {
display: block;
line-height: 90px;
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
}
nav a:hover {
text-align: center;
color: 33adae;
<nav>
<ul>
<li>Kontakt</li>
<li>Reference</li>
<li>Moje služby</li>
<li>Kdo jsem</li>
</ul>
</nav>
Here is my version using height and with properties instead of padding, i used background colors so you can see how is working: http://codepen.io/aluknot/pen/wKrqaG
HTML:
<nav>
<ul>
<li>Kontakt</li>
<li>Reference</li>
<li>Moje služby</li>
<li>Kdo jsem</li>
</ul>
</nav>
CSS:
nav {
width: 100%;
height: 90px;
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
}
nav li{
float: right;
background: red;
text-align: center;
height: 100%;
width: 120px;
line-height: 90px
}
nav ul{
margin: 0;
padding-right: 100px;
height: 90px;
list-style-type: none;
background: green;
}
nav a{
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
display: block;
width: 100%;
height: 100%;
background: blue;
}
nav a:hover {
background: black;
color: white;
}
Could not really find a good title for this question...
Problem: When resizing the browser window, the menu position is not updated in Chrome, UNTIL you hover with the mouse over. In Firefox there is no problem at all.
I've made a simple fiddle http://jsfiddle.net/fHcw7/
If replacing 'position fixed' to 'position relative' there is no problem in Chrome
Html
<div id="main">
<div id="div_top" class="menu">
<nav>
<ul>
<li>
HELLO
</li>
<li>
WORLD
</li>
<li>
BANANA
</li>
</ul>
</nav>
</div>
</div>
CSS
#main
{
height: 175%;
width: 100%;
border: solid red 1px;
position: absolute; top: 0; left: 0;
overflow: hidden;
background-color: #333;
}
#div_top
{
width: 100%;
height: 100px;
margin-top: 20px;
position: fixed;
border: solid yellow 1px;
text-align: center;
font-size: 18px;
font-weight: bold;
color: #fff;
z-index: 100;
}
.menu a:link
{
color: #fff;
text-decoration: none;
}
.menu a:visited
{
color: #fff;
text-decoration: none;
}
.menu a:hover
{
background-color: rgba(100, 50, 0, 0.4);
border-radius: 5px;
border: solid white 2px;
margin: -2px;
}
.menu a:active
{
color: #fdd;
}
.menu ul
{
list-style-type: none;
margin: 0px;
padding: 0px;
text-align: center;
}
.menu li
{
display: inline;
margin: 20px;
}
I think the problem is related to the display inline of li elements.
Try to replace them with inline-block instead.
I have made a test with your fiddle and it works.
http://jsfiddle.net/notme/FA8TN/
.menu li
{
display: inline-block;
margin: 20px;
}
I would like to have a little triangle underneath the the text that points up when the user hovers over the different tabs. Here is a bit of code I'm working with.
css navbar
* {
margin: 0;
padding: 0;
list-style: none;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
margin: 10px;
}
.tab {
width: 100%;
padding: 5px;
background: #fff;
color: #000;
cursor: pointer;
font-weight: bold;
border-bottom: 1px solid black;
position: relative;
}
.tab:hover {
background: #a0a0a0;
}
.tab:hover span {
display: block;
}
.tab_child {
padding: 15px;
background: #fff;
}
.selected {
background: #a0a0a0;
}
.contain * {
float: left;
width: 100px;
}
span.triangle {
background-image: url("http://www.inner.org/torah_and_science/mathematics/images/triangle.gif");
background-repeat: none;
display: none;
height: 14px;
width: 16px;
position: absolute;
top: 25px;
left: 25%;
}
<div class="contain">
<div id="one" class="tab selected">Link1</div>
<div id="two" class="tab">Link2</div>
<div id="three" class="tab">Link3</div>
<div id="four" class="tab">Link4</div>
<div id="five" class="tab">Link5</div>
</div>
I think this is probably what you're looking for:
Fiddle
Also, please use semantic markup:
If your using HTML5 wrap your navigation in <nav> tags.
Your links (if they really are going to be links) should be <a> elements.
For a list of links like you have it is advised to use a list (<ul> & <li>).
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
float: left;
width: 20%;
}
nav a {
display: block;
padding: 5px;
border-bottom: 1px solid black;
background: #fff;
color: #000;
font-weight: bold;
position: relative;
}
nav a:hover,
.active {
background: #bbb;
}
nav a:hover:after {
content: "";
display: block;
border: 12px solid #bbb;
border-bottom-color: #000;
position: absolute;
bottom: 0px;
left: 50%;
margin-left: -12px;
}
<nav>
<ul>
<li>Link1</li>
<li>Link2</li>
<li>Link3</li>
<li>Link4</li>
<li>Link5</li>
</ul>
</nav>
Here is a modification to your jsfiddle:
I've added a <span class="arrow"></span> to contain the triangles in the HTML:
<div class="tab_container">
<div id="test1-header" class="accordion_headings header_highlight" >Home<span class="arrow"></span></div>
<div id="test2-header" class="accordion_headings" >About<span class="arrow"></span></div>
<div id="test3-header" class="accordion_headings" >Work<span class="arrow"></span></div>
<div id="test4-header" class="accordion_headings" >Social<span class="arrow"></span></div>
<div id="test5-header" class="accordion_headings" >Contact<span class="arrow"></span></div>
</div>
Here are the changes made to your menu which reduce the size of the triangle and position them at the bottom center of each menu item when hovered over:
CSS:
/*
.accordion_headings:hover{
background:#00CCFF;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid red;
}
*/
.accordion_headings{
position:relative;
}
.accordion_headings .arrow{
display:none;
}
.accordion_headings:hover .arrow{
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid red;
display:block;
position:absolute;
bottom:0;
left:49%;
}
Here is a fiddle that uses an background-image that will display over the hovered menu item. It not pretty but further css should help with that.
UPDATE
Sorry I must have misread that. Here is a working fiddle with a smaller arrow pointing in the proper direction.