I am building a dropdown menu for my navigation bar. The styling of the dropdown menu looks the way I want it. However, I have a problem with the spacing of the dropdown button. Basically, the button is embedded in a div that has a width of 160, which is basically the width of the open dropdown menu. However, I want the div to only have the width of the button itself, not of the expanding dropdown menu. Otherwise, the dropdown menu with its big div will shift the other elements of navbar.
.button-container {
position: relative;
/*text-align: center;*/
padding: 0;
overflow:hidden;
display:flex; /*Code Added */
justify-content:flex-end;
margin-left:auto;
/*float: center;*/
width:160px;
}
.button-container img{width:50px; height:50px; display:block; border-radius:50%} /* Code changed */
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: white;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
font-weight: 600; /*Semi-Bold = 600*/ /*Bold = 700*/
font-family:"Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
color:#666666;
padding: 12px 16px;
text-decoration: none;
display: block;
right: 4px;
left: auto;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
color:#F16852;}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
<div class="dropdown">
<div class="button-container">
<img src="http://www.fillmurray.com/g/300/300"/>
</div>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
Thanks a lot. I am happy for any clarification.
Try removing the width: 160px; for .button-container and setting right: 0; on .dropdown-content.
You don't want the .button-container to be as wide as the .dropdown-content, but you have set it to the same width!
You have positioned the .dropdown-content absolutely, but not given any further specification. Saying right: 0, means it should be aligned to the right of the closest parent element with relative positioning (in this case your .dropdown element).
Related
I have a webpage with a horizontal navigation at the top and a vertical navigation on the left. The left nav has links, that point to headers on the same page. The links worked, but the headers where on top of the page and blocked by the sticky nav. So I added this code:
<h2><span id="installation"></span>Installation</h2>
h2 {
position:relative;
}
h2 span {
position:absolute;
top:-60px;
}
The links work now and the headers move right under the top nav, when the links are clicked.
But if I scroll the page now and the headers move behind the sticky top nav, I can see the header text through the text in the nav.
picture of the problem
The nav uses this CSS:
.horizontal_menu {
position: -webkit-sticky;
position: sticky;
top: 0;
margin-bottom: 5px;
}
#ul_horizontal {
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;
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #FEC107;
}
Just add a higher z-index like z-index: 2; to the .horizontal_menu class.
Bonus: If you want to make the vertical navigation to go under the horizontal navigation, make the z-index: 3; in this way the header navigation is always on top of everything.
I'm trying to create a dropdown menu for one of the items in my nav bar. I based the code on this W3Schools example. Upon hover, the menu appears below the nav bar (as it should be) but it is 1) stacked horizontally rather than vertically and 2) appears to the far right on the page. I've look at similar questions here but haven't been able to figure out the problem in my code. Any help would be appreciated.
/* nav */
nav {
display: flex;
flex-wrap: wrap;
padding: .25rem 0;
color: #ffffff;
font: 30px 'Roboto', sans-serif;
margin: auto;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
overflow: hidden;
}
nav a {
display: block;
margin: 0 40px;
}
/* dropdown container */
.dropdown {
float: none;
position: relative;
overflow: visibile;
}
/* dropdown button */
.dropdown .dropbtn {
display: flex;
font-size: 30px;
border: none;
outline: none;
color: #ffffff;
padding: inherit;
background-color: inherit;
font-family: inherit;
margin: auto;
}
/* dropdown content (hidden by default */
.dropdown-content {
display: none;
position: absolute;
margin-top: 10px;
background-color: #ffffff;
width: 250px;
left: calc(50% - 125px);
}
.dropdown-content>a {
color: black;
text-align: left;
border-bottom: 1px solid #009EDB;
}
/* show dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
float: left;
margin: 0;
}
<nav class="justify-content-center">
About
<section class="dropdown">
<button class="dropbtn">
Work
</button>
<section class="dropdown-content">
Articles and Presentations
From Process to Flow Series
</section>
</section>
Github
Trailhead
</nav>
Your dropdown is structured of anchors (links, <a> tags), which naturally are inline elements. That means that naturally these elements are located as part of page or line flow. To make them appear vertical, you need to change them to be "block" elements, which you use by adding display: block to the styling on the dropdown a elements:
nav a {
margin: 0 40px;
display: block;
}
The 'margin' was already present in this particular element.
I've also removed all the "!important" from your styling because it's bad practice and wasn't helping at all. Since you're missing a background, I restyled the triggering element to have red text so it doesn't seem like a random white space was triggering the dropdown.
That being said, I don't see any "styled far right" behavior for the drop down. The menu is displayed directly under the triggering element (with a 40px margin, which if you have a really small screen might make it seem like it's super far right.)
/* nav */
nav {
display: flex;
flex-wrap: wrap;
padding: .25rem 0;
color: #ffffff;
font: 30px 'Roboto', sans-serif;
margin: auto;
justify-content: center;
overflow: hidden;
}
nav a {
margin: 0 40px;
display: block;
}
/* dropdown container */
.dropdown {
float: none;
overflow: hidden;
}
/* dropdown button */
.dropdown .dropbtn {
display: flex;
font-size: 30px;
border: none;
outline: none;
color: red;
padding: inherit;
background-color: inherit;
font-family: inherit;
margin: auto;
}
/* dropdown content (hidden by default */
.dropdown-content {
display: none;
position: absolute;
background-color: inherit;
width: 100%;
}
/* show dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<nav class="justify-content-center">
About
<section class="dropdown">
<button class="dropbtn">Work</button>
<section class="dropdown-content">
Articles and Presentations
From Process to Flow Series
</section>
</section>
Github
Trailhead
</nav>
Problem number 1 was solved through Rody of the Frozen Peas answer.
For Problem number 2:
You want to align the center of dropdown-content relative to it's parent.
For that you want to shift dropdown-content to the left by half of it's width and then shift it a bit to the right by half of the width of the dropdown. Also the dropdown element needs to be relatively positioned otherwise the dropdown-content would be positioned realtive to the document. To make the dropdown-content visible you need to make dropdowns and the nav bars overflow visible.
nav {
overflow: visible;
}
.dropdown {
position: relative;
overflow: visible;
}
.dropdown-content {
position: absolute;
width: 250px;
left: calc(50% - 125px);
}
The reason this works is that you align the center of the dropdown-content with the left of dropdown by specifying left: -125px as you're shifting it to the left by half of the width of dropdown-content. To then align it with the center of dropdown you need to add 50% as it is absolutely positioned and will therefore use the parents width as reference and 50% of the parents width is the parents center.
I am trying to halve the size of my sticky navbar, and have it appear overlayed rather than above of my header image. When I attempt to rescale the container/wrapper divs it seems to adjust the size of the image which I have as the navbar. I also would like to have the dropdown menu appear wherever you are located on the page, rather than just at the top.
I have tried using 'position: absolute' in my css on all of the navbar bits. This seems to work in overlaying the navbar, however it does not appear at the top of the page and the dropdown menu is not accessable.
#wrapper {
width: 100%;
}
#demoncontrast {
filter:contrast(100%) brightness(180%);
}
.navbar {
overflow: hidden;
background-color: #333;
z-index: 9999;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 0px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 657px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.content {
padding: 16px;
}
.sticky {
position: fixed;
top: 0;
width: 100%
}
.sticky + .content {
padding-top: 102px;
}
<div class="container">
<div id="wrapper">
<div class="navbar" id="myHeader">
<div class="dropdown">
<button class="dropbtn">
<img src="NONENTITYHEADERNB.png" id="opacity" align="left" style="max-width: 50%;height:auto;">
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Bio and album stream
Where to buy
Upcoming shows and links
</div>
</div>
</div>
</div>
</div>
I also would like to have the dropdown menu appear wherever you are located on the page, rather than just at the top.
You have to use the position property with fixed value: position: fixed. This will make your element (in this case is navbar) sticks on window and always moves along with viewers.
And to ensure your element are not overlaid, you may want to add z-index property to a certain level. For example: z-index: 999.
When I attempt to rescale the container/wrapper divs it seems to adjust the size of the image which I have as the navbar
The reason lies in here:
<img src="NONENTITYHEADERNB.png" id="opacity" style="max-width: 50%;height:auto;">
Your max-width:50% means this image only allow to have 50% size compare to the parent width, both container and wrapper are its parent.
If I understand you correctly, you want to make the image auto-rescaled when the container change size right? If so, then I afraid you have to put more effort with JavaScript rather then CSS.
I can't seem to center the navigation bar buttons. Is there a way to do this in the css file? I have tried centring but it hasn't worked.
HTML
<div class="navbar">
Home
News
Contact
</div>
CSS
.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 1300px; /* Full width */
z-index: 99999;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
/* Links inside the navbar */
.navbar a {
display:inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
I have modified your style for ".navbar a". Hope it will work for you.
You will love flexbox - super simple, and very useful.
Flexbox requires a parent and items.
You turn flexbox on on the parent, and then the various switches are set either on the parent (as in the case of justify-content) or on the items.
Here is a great cheatsheet for Flexbox.
Here is a fantastic YouTube tutorial.
DEMO:
.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
z-index: 99999;
text-align: center;
width: 100vw; /* Full width */
display:flex;
justify-content:center;
border:5px solid yellow;
}
/* Links inside the navbar */
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
border:1px solid pink;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
<div class="navbar">
Home
News
Contact
</div>
You can use
<div class="navbar">
<div style="display: inline-block;">
Home
News
Contact
</div>
</div>
If I understand you correctly, you need to align the links in the center of the navbar, for this you need to do:
CSS:
/* Links inside the navbar */
.navbar a {
/* float: left; remove this property */
display: inline-block; /* change display: block to inline-block */
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
You can see an example on: https://jsfiddle.net/4gy2japx/
.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%; /* Full width */
z-index: 99999;
text-align: center;
margin: 0 auto;
}
.navbar ul {
display:inline-block;
list-style-type: none;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="navbar">
Home
News
Contact
</div>
</body>
</html>
You have to remove float left and add display: inline-block;
.navbar a {
float: left;
display: block;
There are several mistakes in your elements styling. Trying to align floated elements, assigning display block to linear links and defining empirical lengths when you're aiming for full lengths are some of them.
Try this instead:
html,body {
margin: 0; /* overwrite browser defaults */
}
.navbar{
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%; /* Full width */
z-index: 99999;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
<div class="navbar">
Home
News
Contact
</div>
I have written this codepen for a Top Navbar.
This Top Navbar is a <div> element containing <a> elements:
<div class="topnav">
Home
News
Contact
About
</div>
<h1>TopNav using div and a elements</h1>
The key CSS is as follows:
.topnav {
background-color: #bbb; /* gray */
line-height: 50px; /* same as height! */
}
.topnav a {
height: 50px;
margin: 0;
padding: 15px 20px;
color: black;
text-decoration: none;
}
I have vertically centered the links in the <div> element, by:
1) giving each link a height of 50px and then
2) giving the wrapping <div> the same line-height.
This works fine.
The problem is that I am unable to precisely control the padding and margin for the links.
Right now, I have given the padding for the links an arbitrary value:
padding: 15px 20px;
However, there is a small gap at the top and bottom of each link, where the Navbar background color shows through. This can be seen when you hover over a link.
When a link is moused over, I would link the link color to cover the entire NavBar. Is there any exact calculation I can make to ensure this, rather than choosing an arbitrary value for padding-top?
Secondly, there is also a gap at the sides of each link when one hovers. It can be clearly seen when one hovers over a link that is on either side of the "active" link (the green one). I would prefer this gap to be eliminated. How can I do this?
By Default a tag is inline element, you should change it display property to display: inline-block. so that you can set margin and padding.
* {
box-sizing: border-box;
}
/* Extra small devices (phones) */
body {
margin: 0;
background-color: powderblue;
}
.topnav {
background-color: #bbb; /* gray */
line-height: 50px; /* same as height! */
}
.topnav a {
margin: 0;
padding: 10px 20px;
color: black;
text-decoration: none;
display: inline-block; /* Set inline-block for a tag */
}
.topnav {
background-color: #bbb; /* gray */
line-height: 50px; /* same as height! */
}
.topnav a.active {
background-color: mediumseagreen;
color: white;
}
.topnav a:hover:not(.active) {
background-color: #f1f1f1; /* light gray */
}
<div class="topnav">
Home
News
Contact
About
</div>
<h1>TopNav using div and a elements</h1>
Use this. I gave display:block to a and display:flex to .topnav
* {
box-sizing: border-box;
}
/* Extra small devices (phones) */
body {
margin: 0;
background-color: powderblue;
}
.topnav {
background-color: #bbb; /* gray */
height:50px;
display:flex;
line-height: 50px; /* same as height! */
}
.topnav a {
display:block;
height: 50px;
margin: 0;
padding: 0px 20px;
color: black;
text-decoration: none;
}
.topnav a.active {
background-color: mediumseagreen;
color: white;
}
.topnav a:hover:not(.active) {
background-color: #f1f1f1; /* light gray */
}
<div class="topnav">
Home
News
Contact
About
</div>
<h1>TopNav using div and a elements</h1>