I am trying to create one link, Buying Tips, in my navigation menu as a drop down menu. I am running into a few problems and nothing I do seems to fix these issues.
For some reason when I scroll over Buying Tips, the first option of my drop down menu overlaps the navigation menu. I am not sure why this is happening and how to correct this.
I would like the drop down arrow to show but not sure how to code this in CSS.
I would like the navigation links to be equally spaced out and Buying Tips is all on one line. (please view image to see problem) I tried changing the width of the navbar and change the font sizes of navigation links but that does not help.
Printscreen of navigation issues on webpage
.navbar {
width: 100%;
background-color: black;
overflow: auto;
}
.navbar a {
float: left;
padding: 5px;
color: white;
text-align: center;
text-decoration: none;
font-size: 20px;
width: 24%;
}
.navbar a:hover {
color: #FFA500
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .BuyTip {
float: left;
color: white;
text-align: center;
text-decoration: none;
font-size: 20px;
}
.dropdown-content {
display: none;
position: absolute;
min-width: 200px;
}
.dropdown-content a {
float: none;
color: black;
display: block;
text-align: left;
font-size: 10px;
}
.dropdown:hover .dropdown-content {
display: block;
}
#media screen and (max-width: 480px) {
.navbar a {
float: none;
display: block;
width: 100%;
}
}
<div class="navbar">
Chade's Bicycle Company
<div class="dropdown">
<a class="BuyTip">Buying Tips</a>
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Choosing The Correct Bike
Setting A Budget
Test-Ride Before Buying
Choosing The Correct Size
The Essential Accessories
</div>
</div>
Company Calendar
Contact Us
</div>
I've changed your code to achieve the desired result.
As for the questions you've asked:
Since you've made the element bearing the .dropdown class absolutely positioned it will stick to the top since it has no relative parent.
I've used a CSS border property to create an arrow. You can customize it as you wish. (even replace it with the caret class you've used originally, as long as you position it appropriately.)
You can control the spacing between the navigation links using CSS margin or padding properties. read-more about padding and margin.
.navbar {
width: 100%;
background-color: black;
overflow: auto;
font-size: 0;
}
.navbar li {
display: inline-block;
margin: 0 10px;
}
.navbar li a {
display: block;
padding: 5px;
color: white;
text-align: center;
text-decoration: none;
font-size: 20px;
}
.navbar a:hover {
color: #FFA500
}
.dropdown>a {
position: relative;
padding-right: 10px;
}
.dropdown>a:after {
content: "";
display: block;
border-width: 6px;
border-style: solid;
border-right: 6px solid transparent;
border-color: red transparent transparent;
width: 0;
height: 0;
position: absolute;
right: -14px;
top: 14px;
}
.dropdown-content {
display: none;
position: absolute;
padding: 10px 0;
}
.navbar li .dropdown-content a {
color: black;
display: block;
text-align: left;
font-size: 10px;
}
.dropdown:hover .dropdown-content {
display: block;
}
#media screen and (max-width: 800px) {
.navbar li a {
font-size: 16px;
}
.dropdown>a:after {
border-width: 5px;
border-right: 5px solid transparent;
right: -10px;
top: 12px;
}
}
<div class="navbar">
<ul>
<li>Chade's Bicycle Company</li>
<li class="dropdown">
<a>Buying Tips</a>
<div class="dropdown-content">
Choosing The Correct Bike
Setting A Budget
Test-Ride Before Buying
Choosing The Correct Size
The Essential Accessories
</div>
</li>
<li>Company Calendar</li>
<li>Contact Us </li>
</ul>
</div>
Related
I need to create menu like shown in this screenshot:
So as you can, the cursor hover opens large sub menu with two sub sections. Will be glad for any similar examples to my issues. Thanks for your answers!
Here is a very simple example to get you started. It needs more styling of course and more content, but this should give you all the tools to have a dropdown on hover in your menu
HTML:
<header>
<a href="/url">
Hover to see dropdown
</a>
<div>
<section></section>
<section></section>
</div>
</header>
CSS:
header {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 50px;
}
header > a {
padding: 0 2em;
height: 50px;
display: grid;
place-content: center;
}
header > div {
display: hidden;
background-color: white;
}
a:hover + div {
display: inherit;
}
Something like this should work. It utilizes the :hover attribute in the navbar tab 'Dropdown' to reveal more content (in this case it'll be just some links).
HTML:
<div class="navbar">
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
CSS:
.navbar {
overflow: hidden;
background-color: #333;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
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;
}
You can find more information on the W3School website for more!
I am working on a horizontal navigation bar with a dropdown menu. I'm quite new to making codes so this is maybe a stupid question. My navigation is sticking to the left of my website, but I need it to stay in line with the text and I can't get the navigation bar threw my whole webpage how do I fix this?
photo of my website with the 2 problems:
enter image description here
nav {
position: absolute;
}
.horizontal {
list-style-type: none;
margin: 40 auto;
width: 640px;
padding: 0;
overflow: hidden;
}
.horizontal>li {
float: left;
}
.horizontal li ul {
display: none;
margin: 0;
padding: 0;
list-style: none;
position: relative;
width: 100%;
}
.horizontal li:hover ul {
display: inline-block;
}
.horizontal li a {
display: block;
text-decoration: none;
text-align: center;
padding: 22px 10px;
font-family: arial;
font-size: 8pt;
font-weight: bold;
color: #FFFFFF;
text-transform: uppercase;
border-right: 1px solid #607987;
background-color: #006600;
letter-spacing: .08em;
width: 70px;
}
.horizontal li a:hover {
background-color: darkorange;
color: #a2becf
}
.horizontal li:first-child a {
border-left: 0;
}
.horizontal li:last-child a {
border-right: 0;
}
h1 {
margin-top: 80px;
}
<nav id="mainnav">
<ul class="horizontal">
<li>Home</li>
<li>Planning</li>
<li>Takken
<ul>
<li>Kapoenen</li>
<li>Kawellen</li>
<li>Kajoo's</li>
<li>Jojoo's</li>
<li>Givers</li>
<li>Jin</li>
<li>Akabe</li>
</ul>
</li>
<li>Kleding</li>
<li>Contact
<ul>
<li>Leiding</li>
<li>Verhuur</li>
</ul>
</li>
<li>Inschrijven</li>
</ul>
</nav>
Two things in your css are giving you trouble.
nav{ position: absolute; } means this div will not fill the width.
horizontal{ margin: 40 auto;} 40 is not valid.
You MUST specify a measurement unit in CSS, so it should be 40px if I'm guessing your intention, but other units are available.
Here is amended css you can try.
nav {
width: 100%;
background-color: #006600;
}
.horizontal {
list-style-type: none;
margin: 40px auto;
width: 640px;
padding: 0;
overflow: hidden;
}
Step 1) Add HTML:
Example
<!-- The navigation menu -->
<div class="navbar">
<a class="active" href="#">Home</a>
Planning
Takken
Kleding
Contact
Inschrijven
</div>
And CSS:
.navbar {
width: 100%;
background-color: #555;
overflow: auto;
}
.navbar a {
float: left;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
width: 15%;; /* Four links of equal widths */
text-align: center;
}
I coded this website entirely in HTML and CSS without using any libraries. The dropdown menu works perfectly fine on any computer or Android phone and I have tested in using multiple web browsers. On iPhones, the dropdown menu doesn't appear on the screen.
I thought it had to do with the issue of iPhone not supporting :hover, but after a lot of testing and attempts to fix it, I have found that the hover is not the problem. After clicking the nav, I have found that although not visible, the links in the dropdown menu work if I click on the correct place where it should be. Essentially, the dropdown menu and the links are there, but they can't be seen. I have tried adding visiblity: visible, z-index: 999 and several different display values but the problem persists.
nav {
position: relative;
background: white;
border: .05em solid #004EA8;
width: 100%;
}
nav .logo {
margin-bottom: .3em;
}
nav a {
color: white;
text-decoration: none;
display: block;
text-align: center;
}
nav ul,
nav:active ul {
list-style: none;
display: flex;
flex-direction: column;
position: fixed;
padding: 20px;
background: white;
top: 3.8em;
width: 100%;
}
.nav-sections {
display: none;
width: 100%;
}
.navbar-link {
display: flex;
flex-direction: column;
font-style: none;
text-decoration: none;
color: #004EA8;
}
.dropdown {
display: inline-block;
}
nav li,
.dropbtn {
background-color: white;
font: inherit;
text-align: center;
width: 100%;
padding: 5px 0;
margin: 0;
width: 100%;
}
.dropbtn a {
color: #004EA8;
}
nav:hover ul {
display: block;
}
.dropbtn {
display: flex;
flex-direction: column;
font-style: none;
color: white;
padding: 0;
}
.dropdown-content {
display: none;
}
.nav-button {
display: inline-block;
float: right;
color: white;
margin: .75em .6em .4em 1em;
}
.hamburger {
position: relative;
display: inline-block;
width: 1.25em;
height: 1.2em;
margin-top: .85em;
margin-right: 0.3em;
border-top: 0.2em solid #004EA8;
border-bottom: 0.2em solid #004EA8;
}
.hamburger:before {
content: "";
position: absolute;
top: 0.3em;
left: 0px;
width: 100%;
border-top: 0.2em solid #004EA8;
}
<nav class="nav">
<nav class="navbar-items items-left">
<img class="logo" src="images/NELS Logo 2145C.png" alt="New England Language School Building">
<div class="nav-button navbar-link navbar-link-toggle">
<div class="hamburger"></div>
</div>
<ul class="nav-sections">
<li class="single-line-link">Home</li>
<li class="single-line-link">About</li>
<li class="single-line-link">
<div class="dropdown navbar-link">
<button class="dropbtn">Languages</button>
<div class="dropdown-content">
English
French
German
Italian
Japanese
Mandarin
Portuguese
Spanish
Request a Language
</div>
</div>
</li>
<li class="single-line-link">Corporate</li>
<li class="double-line-link">Session<br>Dates</li>
<li class="single-line-link">Events</li>
<li class="double-line-link">Language<br>Partner</li>
<li class="single-line-link">Photos</li>
<li class="single-line-link">Location</li>
<li class="single-line-link">Policies</li>
<li class="single-line-link">Contact</li>
</ul>
</nav>
</nav>
Again, this is only an issue on iPhones. Any help would be hugely appreciated as I have spent hours/days searching for and trying different solutions that don't work.
I am including the code for the nav in HTML and the CSS to make the drop down menu work. To see the full code, you can visit the website at https://www.newenglandlanguage.com/
I have tested this on Safari and the behaviour of the menu shows the same issue as you have described. To fix it just remove the overflow: hidden property from the nav element selector in your media queries.
I am a beginner in HTML/CSS, So I am facing a problem while trying to integrate the user interface on the Navbar like this: .
I don't know how to style it, which tags should I use and how to put icon of user...
/* Add a background color to the top navigation */
.topnav {
background-color: #e61a26;
overflow: hidden;
position: relative;
top: 0;
width: 100%;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #f1f3f2;
color: #000;
}
/* Add a gray right border to all a items, except the last item (last-child) */
a {
border-right: 1px solid #fff;
}
a:last-child {
border-right: none;
}
<p>Welcome <strong>Username></strong></p>
<p>logout</p>
<div class="topnav">
<a class="active" href="/WWW/home.php">Home</a>
Shortcode
Work Order
</div>
I tried to solve this with flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
There are also some special cases like the border on the last, which is invisible. For this use the :last-child selector to remove it from the last item. And to style the link to look like a normal text, use text-decoration: none; For the user image you can use the img tag. After you set your custom width and height use and border-radius: 50%; to make it automaticlly a circle.
Just study the flexbox guide and you'll see it isn't that hard. Hope this helps.
body {
margin: 0;
}
.navbar {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
background-color: red;
}
.navbar__left,
.navbar__right {
display: flex;
flex-direction: row;
align-items: center;
}
.navbar__item {
padding: 20px;
color: white;
border-right: 1px solid white;
text-decoration: none;
}
.navbar__item:last-child {
border-right: none;
}
.navbar__item--active {
background-color: white;
color: black;
}
.navbar__user-image {
width: 45px;
height: 45px;
border-radius: 50%;
}
.navbar__user-image,
.navbar__user-logout {
margin-right: 15px;
}
<nav class="navbar">
<div class="navbar__left">
Home
About us
Contact
</div>
<div class="navbar__right">
<img class="navbar__user-image" src="https://www.telegraph.co.uk/content/dam/news/2016/08/23/106598324PandawaveNEWS_trans_NvBQzQNjv4Bqeo_i_u9APj8RuoebjoAHt0k9u7HhRJvuo-ZLenGRumA.jpg?imwidth=450" alt="user img">
<span class="navbar__user-logout">user logout</span>
</div>
</nav>
So after a few tries I succeeded it's really simple here is my code now :
/* Add a background color to the top navigation */
.topnav {
background-color: #e61a26;
overflow: hidden;
position: relative;
top: 0;
width: 100%;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #f1f3f2;
color: #000;
}
/* Add a gray right border to all a items, except the last item (last-child) */
a {
border-right: 1px solid #fff;
}
.workorder_link {
border-right: none;
}
a:last-child {
border-right: none;
}
<nav class="navbar">
<div class="topnav">
Home
Shortcode
Work Order
Logout
<a style="float: right; text-decoration: none;">Welcome <strong>Username</strong><img src="img/user.png" style="margin-bottom: 5px; margin-left: 5px; width: 20px; height: 20px;"></a>
</div>
</nav>
I want to make a dropdown menu in CSS ... I already have a menu with "fixed" position(this first menu has to be fixed and nothing else)
but when I try to design my dropdown menu, items in it don't go to the position I want and always align to the first menu...
plz, tell how to do it correctly....
#menu {
overflow: hidden;
position: fixed;
top: 0;
width: 50%;
margin-left: 25%;
background-color: #333;
color: #f2f2f2;
}
.menu-content {
float: left;
display: block;
text-decoration: none;
padding: 25px 20px;
font-size: 17px;
}
#products {
padding-top: 25px;
font-size: 22px;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropdown-btn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: center ;
}
.dropdown:hover .dropdown-content {
display: block;
}
<body>
<div id="menu">
<div class="dropdown">
<button id="products" class="dropdown-btn menu-content"> <i class="fa fa-bars"></i> products </button>
<div class="dropdown-content">
<a> speaker </a>
</div>
</div>
</div
</body>
this is the result I get:
enter image description here
Just remove
#menu {
overflow: hidden; <--remove!!
}
and add
.dropdown:hover .dropdown-content {
display: block;
margin-top: 65px; <- Add
}
If you use overflow:hidden on your #menu your dropdownlist will never show up, because your list will be limited by the external div, since it's nested in.
Also, use margin-top to add some distance between each item.
a way to fix it is to change the positioning from absolute to relative. also add a way to position it
.dropdown-content {
display: none;
position: absolute;<-- change to relative-->
background-color: #f9f9f9;
min-width: 160px;
z-index: 1;