Getting a custom navbar dropdown to align properly - html

i am trying to create a nav bar for my website. the drop down is giving me trouble. how do i align the submenu so that it shows as a proper drop down?
<div id="mainhead">
<div id="title">
<h1>StudiousEnough </h1>
</div>
<li>Home</li>
<li><a href="" >class</a>
<ul>
<li> Class XI </li>
<li> CLass XII </li>
</ul>
</li>
<li><a href="#" >jee</a></li>
<li><a href="aboutus.html" >about us</a></li>
</ul>
and here is the css:
#mainhead #mainnav ul {
list-style-type: none;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-left: 0px;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
#mainhead #mainnav li {
display: block;
width: 25%;
background-color: #4D4D4D;
color: #FFFFFF;
text-align: center;
text-transform: uppercase;
float: left;
padding-bottom: 4px;
padding-top: 4px;
font-family: averia-libre;
font-style: normal;
font-weight: 700;
}
#mainhead #mainnav {
position: absolute;
width: 100%;
left: 0%;
right: 0%;
}
#mainhead #mainnav a{
display:block;
color: #ffffff;
text-decoration: none;
}
#mainhead #mainnav li:hover,#mainhead #mainnav li:focus,#mainhead #mainnav li:active {
background-color: #2535F3;
text-decoration: none;
}
#mainhead #mainnav ul li ul{
display: none;
}
#mainhead #mainnav ul li:hover ul{
display:block;
}

See the fiddle
I've added the following CSS to your CSS
CSS
#mainhead #mainnav ul li ul li{
width:100%;
clear:both;
}

First things first - check your html; you have some problems here. The list item tags need to be enclosed inside the unordered list parent tags. Also I don't see some of the ids and classes existing in the html that is used in your css.
If you always format your code, indenting enclosed blocks, that will help you spot all errors more quickly.
I went ahead and added a parent div with id = 'mainnav' and cleaned up your HTML, and it mostly renders as you'd expect. To get the submenu list items to 'stack' just make width 100%.
Clear is not necessary.
Check out the example on jsfiddle here:
https://jsfiddle.net/pianopaul/r3bb8k24/5/
<div id="mainhead">
<div id="title">
<h1>StudiousEnough </h1>
<div id="mainnav">
<ul class="menu">
<li>Home</li>
<li><a href="" >class</a>
<ul class="submenu">
<li> Class XI </li>
<li> CLass XII </li>
</ul>
</li>
<li><a href="#" >jee</a></li>
<li><a href="aboutus.html" >about us</a></li>
</ul>
</div>
</div>
</div>

Related

i have <a> links that were working but for some reason have stopped

I have a unordered list in my HTML with 5 lists that all had with links on them that were working fine. It was working fine until I added another unordered list, but i have no links inside it. I even gave it a separate class.
Here is my markup/CSS:
ul {
margin: 0;
list-style-type: none;
color: white;
diplay: block;
padding-top: 20px;
padding-left: 350px;
font-family: "Economica", sans-serif;
font-size: 28px;
text-decoration: none;
}
ul li {
padding-left: 45px;
padding-right:45px;
display: inline;
text-decoration: none;
}
ul li a:hover {
font-weight:bold;
color:grey;
}
a {
text-decoration: none;
color:white;
}
<div id="mainPage" >
<img src="http://xoio.de/al_by_xoio1.jpg" alt="concert stadium" style="width:100%; position:absolute; padding-top:70px; height:930px">
<div id="navBar">
<div id="Logo">
<h1 style="font-weight:bold;"> Ice Arena </h1>
</div>
<ul id="select">
<li>
<a style="color:#ffe700;" href="#">Home</a>
</li>
<li>
Gallery
</li>
<li>
Order Form
</li>
<li>
The Arena
</li>
<li>
Contact Us
</li>
</ul>
</div>
</div>
I've only added the code regarding the list and area of my problem.
Add position: relative; to ul, otherwise other elements are above it and prevent the link behavior.
ul {
margin: 0;
list-style-type: none;
color: white;
diplay: block;
padding-top: 20px;
padding-left: 350px;
font-family: "Economica", sans-serif;
font-size: 28px;
text-decoration: none;
/* added */
position: relative;
}
ul li {
padding-left: 45px;
padding-right: 45px;
display: inline;
text-decoration: none;
}
ul li a:hover {
font-weight: bold;
color: grey;
}
a {
text-decoration: none;
color: white;
}
<div id="mainPage">
<img src="http://xoio.de/al_by_xoio1.jpg" alt="concert stadium" style="width:100%; position:absolute; padding-top:70px; height:930px">
<div id="navBar">
<div id="Logo">
<h1 style="font-weight:bold;">Ice Arena</h1>
</div>
<ul id="select">
<li><a style="color:#ffe700;" href="#">Home</a></li>
<li>Gallery</li>
<li>Order Form</li>
<li>The Arena</li>
<li>Contact Us</li>
</ul>
</div>
</div>
Try deleting the CSS property (position:absolute;) in line 3.

How to move this menu bar to the right

I want move objects to the right but don't know how!? Any idea?
. Please help me. Here is the code in HTML and CSS
/*=====================================
= Top Bar
=====================================*/
#top-bar {
height: 42px;
line-height: 40px;
background: #f9f9f9;
color: #888;
font-size: .857em;
border-bottom: 1px solid #f5f5f5;
}
.top-nav ul li {
display: block;
float: left;
}
.top-nav ul {
margin: 0;
padding: 0;
}
.top-nav {
font-family: "Montserrat",Open Sans,Sans-serif;
}
.top-nav ul li a {
color: #111;
text-decoration: none;
display: inline-block;
padding: 0 15px;
line-height: .917em;
color: #888;
text-decoration: none;
text-transform: uppercase;
letter-spacing: .5px;
font-size: .917em;
border-right: 1px solid #eee;
}
.top-notification {
float: right;
}
.top-notification p {
margin: 0;
float: left;
font-size: 13px;
}
.top-notification a {
color: #111;
text-decoration: none;
/* display: inline-block; */
padding: 5px 9px;
line-height: .917em;
color: #888;
text-decoration: none;
text-transform: uppercase;
letter-spacing: .5px;
font-size: .917em;
border: 1px solid #D1D1D1;
margin-left: 22px;
background:#fff;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
display:inline-block;
}
nav ul li a:link {
display: inline-block;
padding: 15px 25px;
text-transform: uppercase;
letter-spacing: 1px;
position: relative;
text-decoration: none!important;
color: #fff;
width:100%;
}
nav ul li a {
color: #fff!important;
}
nav a:hover {
background-color: #2da399;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
z-index: 1000;
background: #494949;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
min-width:170px;
float:none;
display:list-item;
position: relative;
}
nav ul ul a:hover {
background-color: #656565;
}
/* Change this in order to change the Dropdown symbol */
nav li > a:after { content: ' +'; }
nav li > a:only-child:after { content: ''; }
.menu {
width: 1200px;
margin: auto;
z-index: 99999;
background: #2da399;
}
.menu.cloned {
width: 100%!important;
left: 0!important;
}
.menu.cloned nav {
width: 1200px;
margin: auto;
}
Here is HTML codes for my website. I think every thing in is fine here and the problem is in CSS codes.
<!--Main Navigation-->
<div class='menu-wrapper'>
<div class='menu'>
<nav>
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
<li><a href='#'>Nyheter </a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
<li><a href='#'>Världen</a></li>
</ul>
</li>
<li><a href='#'>Vetenskap
</a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
<li><a href='#'>Fysik </a></li>
<li><a href='#'>Matte</a>
<!-- Second Tier Drop Down -->
<ul>
<li><a href=''>Matte 1</a></li>
<li><a href='#'>Matte 2</a></li>
<li><a href='#'>Matte 3</a>
<!-- Third Tier Drop Down -->
<ul>
<li><a href='#'>Matte 4</a></li>
<li><a href='#'>Hjälpmedel
</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>فناوری </a></li>
<li><a href='#'>اجتمائی </a></li>
<li><a href='#'> سیاست</a></li>
<li><a href='#'>تاریخ </a></li>
<li><a href=''>کلاس ها</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>
</ul>
</nav>
</div>
</div>
This is menu bar
You should add some margin-left to your css. As you have not provided your full HTML I cannot say which attribute exactly needs the stylling.
I think you should add margin-left to your menus to increase space between them or to the parent div for shifting the whole navbar to the right. Hope this helps!
I'm going to assume that when you ask how to move them to the right that the ultimate goal is to center the navigation elements inside the blue-green field that is <nav>.
You might also be asking to align the navigation items to the right side of the blue-green field, I've addressed that in my second code snippet.
Center Nav Items
A common way to center something is to move it to the left by 50% and then pull it back 50% of it's width with transform. In order for this to work you'll need to make your <ul> not take up 100% of its parent element. You can do this a number of ways, for simplicity I have set it to display: inline-block;.
This solution will allow you to add/remove <li> and still be centered in <nav>.
nav ul {
display: inline-block;
position: relative;
left: 50%;
padding: 0;
margin: 0;
transform: translateX( -50%);
list-style: none;
}
nav ul li {
display: inline-block;
}
nav ul li a:link {
display: inline-block;
padding: 15px 25px;
text-transform: uppercase;
letter-spacing: 1px;
position: relative;
text-decoration: none !important;
color: #fff;
}
nav ul li a {
color: #fff !important;
}
nav a:hover {
background-color: #2da399;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
z-index: 1000;
background: #494949;
}
/* Display Dropdowns on Hover */
nav ul li:hover>ul {
display: inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
min-width: 170px;
float: none;
display: list-item;
position: relative;
}
nav ul ul a:hover {
background-color: #656565;
}
/* Change this in order to change the Dropdown symbol */
nav li>a:after {
content: ' +';
}
nav li>a:only-child:after {
content: '';
}
.menu {
width: 1200px;
margin: auto;
z-index: 99999;
background: #2da399;
}
<!--Main Navigation-->
<div class='menu-wrapper'>
<div class='menu'>
<nav>
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
<li><a href='#'>Nyheter </a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
<li><a href='#'>Världen</a></li>
</ul>
</li>
<li><a href='#'>Vetenskap
</a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
<li><a href='#'>Fysik </a></li>
<li><a href='#'>Matte</a>
<!-- Second Tier Drop Down -->
<ul>
<li><a href=''>Matte 1</a></li>
<li><a href='#'>Matte 2</a></li>
<li><a href='#'>Matte 3</a>
<!-- Third Tier Drop Down -->
<ul>
<li><a href='#'>Matte 4</a></li>
<li><a href='#'>Hjälpmedel
</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>فناوری </a></li>
<li><a href='#'>اجتمائی </a></li>
<li><a href='#'> سیاست</a></li>
<li><a href='#'>تاریخ </a></li>
<li><a href=''>کلاس ها</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>
</ul>
</nav>
</div>
</div>
I noticed you included a little more CSS than what was being used by your markup so I removed it.
Align Nav Items to Right
If you are looking to align the navigation to the right side instead of the left side you can float the <ul> to the right. The only catch here is that you will need to clear the float so you can see the background color of .menu. I used the poor man's clearfix, overflow: hidden; but you could use the more modern option of the Micro Clearfix.
nav ul {
float: right;
display: inline;
position: relative;
padding: 0;
margin: 0;
list-style: none;
}
nav ul li {
display: inline-block;
}
nav ul li a:link {
display: inline-block;
padding: 15px 25px;
text-transform: uppercase;
letter-spacing: 1px;
position: relative;
text-decoration: none !important;
color: #fff;
}
nav ul li a {
color: #fff !important;
}
nav a:hover {
background-color: #2da399;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
z-index: 1000;
background: #494949;
}
/* Display Dropdowns on Hover */
nav ul li:hover>ul {
display: inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
min-width: 170px;
float: none;
display: list-item;
position: relative;
}
nav ul ul a:hover {
background-color: #656565;
}
/* Change this in order to change the Dropdown symbol */
nav li>a:after {
content: ' +';
}
nav li>a:only-child:after {
content: '';
}
.menu {
width: 1200px;
margin: auto;
z-index: 99999;
background: #2da399;
overflow: hidden; /* Clearfix that allows the background of <nav> to be seen with floated child elements. */
}
<!--Main Navigation-->
<div class='menu-wrapper'>
<div class='menu'>
<nav>
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
<li><a href='#'>Nyheter </a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
<li><a href='#'>Världen</a></li>
</ul>
</li>
<li><a href='#'>Vetenskap
</a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
<li><a href='#'>Fysik </a></li>
<li><a href='#'>Matte</a>
<!-- Second Tier Drop Down -->
<ul>
<li><a href=''>Matte 1</a></li>
<li><a href='#'>Matte 2</a></li>
<li><a href='#'>Matte 3</a>
<!-- Third Tier Drop Down -->
<ul>
<li><a href='#'>Matte 4</a></li>
<li><a href='#'>Hjälpmedel
</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>فناوری </a></li>
<li><a href='#'>اجتمائی </a></li>
<li><a href='#'> سیاست</a></li>
<li><a href='#'>تاریخ </a></li>
<li><a href=''>کلاس ها</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>
</ul>
</nav>
</div>
</div>
Just set your nav ul li width to 100/number of items. and it will spread your menu items equally across the bar, removing the gap on the right as much as possible.
You can remove the space between inline block elements as you like by setting margin-right property accordingly.
Replace old nav ul li with this code:
nav ul li {
display:inline-block;
background-color: #2da399;
width:11%;
margin-right: -4px;
}
Doing the menu this way will allow your menu to wrap into various screen widths fully.

What makes the dropdown menu content "stick" to the menu item?

I've almost got my dropdown menu working, but I can't get the dropdown content to appear underneath the head when it's clicked. It's moved off to the side. What's causing that? Is it improperly written position?
Fiddle: https://jsfiddle.net/kiddigit/8sxj3eeg/
* {
font-family: garamond;
line-height: 1.9em;
}
.dropdownwrapper {
padding-top: 2px;
}
.dropbtn {
color: black;
padding: 13px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown-content {
/* display: none;*/
position: absolute;
}
.dropdown-content a {
color: white;
padding: 0 27.5px ;
text-decoration: none;
display: block;
background-color: #3f3f3f;
}
.dropdown-content a:hover {
color: #a9a9a9;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: black;
color: white;
}
header {
border-bottom: 5px solid;
margin-bottom: 10px;
overflow: hidden;
}
header ul {
float: right;
list-style-type: none;
margin-top: 22px;
padding:0;
width: 50%;
}
header li {
float: right;
}
header li a {
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
header li a:hover {
background-color: #111;
color: white;
}
header h1 {
float: left;
text-align: left;
line-height: 0;
font-size: 2em;
}
<header>
<h1>Father Bart Gage</h1>
<ul>
<li><a id="about" href="#">ABOUT</a></li>
<li>CONTACT</li>
<div class="dropdownwrapper">
<div class="dropdown">
<li><div class="dropbtn" onClick=”return true”>SCRIPTURE</div></li>
<div class="dropdown-content">
<a id="mark" href="#">Mark</a>
<a id="matthew" href="#">Matthew</a>
<a id="luke" href="#">Luke</a>
<a id="john" href="#">John</a>
</div>
</div>
</div>
</ul>
</header>
You have to move the dropdown-content element into the list item:
<div class="dropdown">
<li>
<div class="dropbtn" onClick=”return true”>SCRIPTURE</div>
<div class="dropdown-content">
<a id="mark" href="#">Mark</a>
<a id="matthew" href="#">Matthew</a>
<a id="luke" href="#">Luke</a>
<a id="john" href="#">John</a>
</div>
</li>
</div>
There's a couple things going on that probably need some attention.
First, a <div> element is not technically "legal" as a direct child of a ul element. The only direct child of a ul should be a li. To solve this, I've moved your dropdown divs inside the li.
Second, you may be suffering a bit of "div-itis". You probably (definitely?) don't need so many div elements to accomplish what you want. I've proposed alternate markup below your markup below.
Third, it's all about position: when you set position: absolute, the position (top, right, bottom, left) are relative to the nearest parent with position: relative. Therefore, you probably want to be sure your li elements have position: relative.
Your original markup, with the divs enclosed in the li
<header>
<h1>Father Bart Gage</h1>
<ul>
<li><a id="about" href="#">ABOUT</a></li>
<li>CONTACT</li>
<li>
<div class="dropdownwrapper">
<div class="dropdown">
<div class="dropbtn" onClick=”return true”>SCRIPTURE</div>
<div class="dropdown-content">
<a id="mark" href="#">Mark</a>
<a id="matthew" href="#">Matthew</a>
<a id="luke" href="#">Luke</a>
<a id="john" href="#">John</a>
</div>
</div>
</div>
</li>
</ul>
</header>
Alternate proposed markup
(note that this also requires altered styles - see the fiddle for those!)
<header>
<h1>Father Bart Gage</h1>
<ul>
<li><a id="about" href="#">ABOUT</a></li>
<li>CONTACT
<!-- nested ul for the dropdown, rather than divs -->
<li>
<div class="dropbtn" onClick=”return true”>SCRIPTURE</div>
<ul class="dropdown dropdown-content">
<li><a id="mark" href="#">Mark</a></li>
<li><a id="matthew" href="#">Matthew</a></li>
<li><a id="luke" href="#">Luke</a></li>
<li><a id="john" href="#">John</a></li>
</ul>
</li>
</ul>
</header>
Working Fiddle using the alternate proposed markup.

Dropdown menu without javascript

I have a menu with tabs, and on hover of a tab a list of things appear at the bottom of the tab. Then, I wanted to do that the list of thing go down with a transition( before it was display:block). My problem is that when the transition will start, the top of the list must be a certain multiplication ( the width of a tab * the number of tabs ). But I don't want any javascript.
Is there a way to do that ?
it is my sample of css dropdown menu: i hope be useful:
in HTML, and CSS:
#menu, #menu ul
{
list-style: none;
border: 1px solid #000;
background-color: #ecffec;
padding: 0 0 26px 0;
margin: 0;
}
#menu li
{
float: left;
margin-right: 3px;
border: 1px solid #ecffec;
position: relative;
}
#menu ul
{
position: absolute;
top: 25px;
left: -1px;
width: 200px;
padding: 0;
display: none;
}
#menu ul li
{
float: none;
margin: 0;
padding: 0;
line-height: 15px;
}
#menu a:link, #menu a:visited
{
display: block;
font-family: Tahoma;
font-size: 0.75em;
font-weight: bold;
text-align: left;
text-decoration: none;
color: #000;
padding: 5px;
}
#menu li:hover
{
background-color: #ffd98a;
border: 1px solid #000;
}
#menu li:hover ul
{
display: block;
}
<ul id="menu">
<li>Programming Language
<ul>
<li>Java</li>
<li>PHP</li>
<li>Python</li>
<li>Asp Classic</li>
<li>ASP.NET</li>
<li>javascript</li>
<li>Perl</li>
</ul>
</li>
<li>Database
<ul>
<li>SQL Server 2005</li>
<li>Oracle</li>
<li>MySQL</li>
<li>DB2</li>
</ul>
</li>
<li>Help</li>
</ul>
Have you seen https://codepen.io/markcaron/pen/wdVmpB?
html part
<h2>Checkbox version</h2>
<div class="dropdown">
<input type="checkbox" id="my-dropdown" value="" name="my-checkbox">
<label for="my-dropdown"
data-toggle="dropdown">
Choose one
</label>
<ul>
<li>Coffee</li>
<li>Coverage</li>
<li>Covfefe</li>
</ul>
</div>
<h2>Anchor version</h2>
<div class="dropdown">
<ul id="my-dropdown2">
<li>Coffee</li>
<li>Coverage</li>
<li>Covfefe</li>
</ul>
<a href="#my-dropdown2"
aria-controls="my-dropdown2"
role="button"
data-toggle="dropdown"
id="my-dropdown2-btn">
Choose one
</a>
<a href="#my-dropdown2-btn"
aria-controls="my-dropdown2"
role="button"
data-toggle="dropdown"
class="close">
Close dropdown
</a>
</div>
css part
better to see the link above!
check it out!

CSS - How to display dropdown text in one row?

I'm building a dropdown menu and I've got an issue showed on this img:
I want the "2.1" text to be displayed next to the "Item" text. For some reaason, every new word gets a new line.
Here's the html:
<ul id='nav'>
<li><a href='/'>Item 1</a></li>
<li><a href='/'>Item 2</a>
<ul>
<li><a href='/'>Item 2.1</a></li>
</ul>
</li>
<li><a href='/'>Item 3</a></li>
<li><a href='/'>Item 4</a></li>
<li><a href='/'>Item 5</a></li>
</ul>
And here's the CSS:
#nav {
list-style: none;
}
#nav li {
float: left;
position: relative;
}
#nav li a {
display: block;
text-decoration: none;
text-align: center;
background: #ccc;
margin-right: 5px;
}
#nav li ul {
position: absolute;
}
#nav li ul li {
display: block;
}
#nav li ul li a {
padding: 0px 10px;
height: 20px;
text-align: left;
background: #999;
}
Thanks for any help, Mike.
An easy way to fix this is adding a nowrap property to #nav li ul li a:
#nav li ul li a {
padding: 0px 10px;
height: 20px;
text-align: left;
background: #999;
white-space: nowrap; /* Forbids text wrapping */
}