I was wondering if anyone could see the bug in my code I am clearly not seeing. I am trying to create a simple navigation bar with a sub menu under 'brands' that appears on hover. Not sure if I am targeting the menu wrong, or what is happening. Any help would be appreciated. Thank you
<div class="nav">
<ul>
<li> Home </li>
<li> History </li>
<li> Brands </li>
<ul>
<li> Proprietary Brands </li>
<li> Licenced Brands </li>
<li> Distributed Brands </li>
</ul>
<li> Corp. Info </li>
<li> Investors </li>
</ul>
</div>
.nav{
width: 100%;
}
.nav ul{
font-size: 1.2em;
text-align: right;
padding-right: 5%;
padding-top: 2%;
list-style: none;
}
.nav li {
text-decoration: none;
padding-left: 2%;
display: inline-block;
}
.nav li li{
font-size: 1em;
}
.nav li ul{
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul{
display: block;
}
.nav li ul li{
display: block;
}
Your html just a bit wrong, it should be
...
<li>
<ul> ... </ul>
</li>
...
instead
...
<li> ... </li>
<ul> ... </ul>
<li> ... </li>
...
That mean you close li tag that should be the container of too soon, that make your csss for .nav li ul and anything with that become invalid because no structure like that exist in your html
Related
I have to create left section for a website ".leftpart" in which I have parent navigation class ".nav-links" with CSS properties. I have also have same class ".nav-links" as a sub section under parent ".nav-links". I want to give a different background color "red" to child ".nav-links" [Edit and Save]. Can you anyone suggest best way to do this ?
My code is below, thanks in advance :
.leftpart {
width: 100%;
display: block;
margin: 20px;
}
.leftpart .nav-links ul {
list-style: none;
margin: 0;
padding: 0;
display: block;
}
.leftpart .nav-links ul li {
margin-bottom:20px;
}
.leftpart .nav-links ul li a {
background:green;
color:#fff;
padding: 20px;
display: inline-block;
}
<div class="leftpart">
<div class="nav-links">
<h1> Primary Links </h1>
<ul>
<li>
Profile
</li>
<li>
Friends
</li>
<li>
Milestones
</li>
<li>
Groups
</li>
</ul>
<div class="sub-seciton">
<div class="nav-links">
<ul>
<li>
Edit
</li>
<li>
Save
</li>
</ul>
</div>
</div>
</div>
<div class="nav-links">
<h1> Secondary Links </h1>
<ul>
<li>
Privacy
</li>
<li>
Settings
</li>
</ul>
</div>
</div>
Just use an additional decedent selector only changing the background property:
.leftpart .nav-links .nav-links ul li a {
background: red;
}
.leftpart {
width: 100%;
display: block;
margin: 20px;
}
.leftpart .nav-links ul {
list-style: none;
margin: 0;
padding: 0;
display: block;
}
.leftpart .nav-links ul li {
margin-bottom: 20px;
}
.leftpart .nav-links ul li a {
background: green;
color: #fff;
padding: 20px;
display: inline-block;
}
.leftpart .nav-links .nav-links ul li a {
background: red;
}
<div class="leftpart">
<div class="nav-links">
<h1> Primary Links </h1>
<ul>
<li>
Profile
</li>
<li>
Friends
</li>
<li>
Milestones
</li>
<li>
Groups
</li>
</ul>
<div class="sub-seciton">
<div class="nav-links">
<ul>
<li>
Edit
</li>
<li>
Save
</li>
</ul>
</div>
</div>
</div>
<div class="nav-links">
<h1> Secondary Links </h1>
<ul>
<li>
Privacy
</li>
<li>
Settings
</li>
</ul>
</div>
</div>
You need to target .nav-links inside the sub-section. You can also style your nav-links siblings separately. See my example:
.leftpart {
width: 100%;
display: block;
margin: 20px;
}
.leftpart .nav-links ul {
list-style: none;
margin: 0;
padding: 0;
display: block;
}
.leftpart .nav-links ul li {
margin-bottom:20px;
}
.leftpart > .nav-links ul li a {
background:green;
color:#fff;
padding: 20px;
display: inline-block;
}
.leftpart .nav-links .sub-seciton .nav-links ul li a {
background:red ;
}
.leftpart > .nav-links:nth-child(2) ul li a {
background: blue;
}
<div class="leftpart">
<div class="nav-links">
<h1> Primary Links </h1>
<ul>
<li>
Profile
</li>
<li>
Friends
</li>
<li>
Milestones
</li>
<li>
Groups
</li>
</ul>
<div class="sub-seciton">
<div class="nav-links">
<ul>
<li>
Edit
</li>
<li>
Save
</li>
</ul>
</div>
</div>
</div>
<div class="nav-links">
<h1> Secondary Links </h1>
<ul>
<li>
Privacy
</li>
<li>
Settings
</li>
</ul>
</div>
</div>
Just set your CSS code more specific for it:
.leftpart {
width: 100%;
display: block;
margin: 20px;
}
.leftpart .nav-links ul {
list-style: none;
margin: 0;
padding: 0;
display: block;
}
.leftpart .nav-links ul li {
margin-bottom:20px;
}
.leftpart .nav-links ul li a {
background:green;
color:#fff;
padding: 20px;
display: inline-block;
}
.leftpart .nav-links .sub-seciton .nav-links ul li a {
background:red;
color:#fff;
padding: 20px;
display: inline-block;
}
<div class="leftpart">
<div class="nav-links">
<h1> Primary Links </h1>
<ul>
<li>
Profile
</li>
<li>
Friends
</li>
<li>
Milestones
</li>
<li>
Groups
</li>
</ul>
<div class="sub-seciton">
<div class="nav-links">
<ul>
<li>
Edit
</li>
<li>
Save
</li>
</ul>
</div>
</div>
</div>
<div class="nav-links">
<h1> Secondary Links </h1>
<ul>
<li>
Privacy
</li>
<li>
Settings
</li>
</ul>
</div>
</div>
.leftpart {
width: 100%;
display: block;
margin: 20px;
}
.leftpart .sub-seciton>.nav-links ul li:nth-child(1) a,
.leftpart .sub-seciton>.nav-links ul li:nth-child(2) a{
background:red;
}
.leftpart .nav-links ul {
list-style: none;
margin: 0;
padding: 0;
display: block;
}
.leftpart .nav-links ul li {
margin-bottom:20px;
}
.leftpart .nav-links ul li a {
background:green;
color:#fff;
padding: 20px;
display: inline-block;
}
<div class="leftpart">
<div class="nav-links">
<h1> Primary Links </h1>
<ul>
<li>
Profile
</li>
<li>
Friends
</li>
<li>
Milestones
</li>
<li>
Groups
</li>
</ul>
<div class="sub-seciton">
<div class="nav-links">
<ul>
<li>
Edit
</li>
<li>
Save
</li>
</ul>
</div>
</div>
</div>
<div class="nav-links">
<h1> Secondary Links </h1>
<ul>
<li>
Privacy
</li>
<li>
Settings
</li>
</ul>
</div>
</div>
I am trying to follow a tutorial on YouTube to create a toolbar, but I am using Vuejs instead of regular HTML like on the video and the content of the views (.vue files) goes beside the toolbar instead of below it and another thing, I try to make center the toolbar.
<template>
<div id="app">
<div class="toolbar">
<ul>
<li>
Home
</li>
<li>
About
<ul>
<li>
<a href>Our Team</a>
</li>
<li>
<a href>Camp Sites</a>
</li>
<li>
<a href>Mission & Vision</a>
</li>
<li>
<a href>Resources</a>
</li>
</ul>
</li>
<li>
Things to do
<ul>
<li>
<a href>Activities</a>
</li>
<li>
<a href>Parks</a>
</li>
<li>
<a href>Shops</a>
</li>
<li>
<a href>Events</a>
</li>
</ul>
</li>
<li>
Contact
<ul>
<li>
<a href>Map</a>
</li>
<li>
<a href>Directions</a>
</li>
</ul>
</li>
<li>
News
</li>
</ul>
</div>
<div>
<router-view/>
</div>
</div>
</template>
#app {
margin: 0;
padding: 0;
}
body {
background-color: black;
background-size: none;
margin-left: 10%;
}
.toolbar ul {
margin: 0px;
padding: 0px;
list-style: none;
font-family: arial;
}
.toolbar ul li {
float: left;
width: 200px;
height: 40px;
background-color: black;
opacity: 0.8;
line-height: 40px;
text-align: center;
font-size: 20px;
}
.toolbar ul li a {
text-decoration: none;
color: white;
display: block;
}
.toolbar ul li a:hover {
background-color: green;
}
.toolbar ul li ul li {
display: none;
}
.toolbar ul li:hover ul li {
display: block;
}
Whatever what I tried today, either the view was correctly below the toolbar but then the submenus were unclickable, or the views were below and centered but with a big margin-top to make the menu clickable or just beside the toolbar.
I am trying to replicate this kind of toolbar with the views right below it.
I guess the problem is 'float'. You will need a div with clear:both style after the toolbar and before the div containing router-view.
...
div class="toolbar"
...
/div
div style="clear:both"/
div
router-view
...
I am able to display all the nested li's inline. What I don't understand is why there is a gap between About and Our Team li elements. I have already tried setting the margin and padding of both ul and li elements to 0.
<div ng-show = "buttonDisplay" id = "buttonDisplayContent">
<ul>
<li> Home </li>
<li class = "subLi"> About </li>
<ul class = "nested">
<li> Our Team </li>
<li> Our efforts </li>
</ul>
<li class = "nextToNested"> Blog </li>
<li class = "subLi"> Services </li>
<ul class = "nested">
<li> Design </li>
<li> Web </li>
<li> Learn </li>
<li> Invent </li>
</ul>
<li class = "nextToNested"> Portfolio </li>
<li> Contact </li>
</ul>
</div>
CSS
#buttonDisplayContent ul {
list-style-type: none;
padding: 0;
}
#buttonDisplayContent ul ul {
list-style-type: none;
padding: 0;
}
#buttonDisplayContent ul a {
text-decoration: none;
color: #000;
font-size: 25px;
font-weight: bold;
}
#buttonDisplayContent ul ul a {
text-decoration: none;
color: lightgray;
font-size: 15px;
font-weight: bold;
}
.subLi {
float: left;
margin: 0;
padding: 0;
list-style-type: none;
}
.nested {
margin-left: 0;
}
.nested li {
display: inline-block;
padding-bottom: 6px;
padding-right: 1%;
padding-left: 1%;
padding-top: 8px;
}
#buttonDisplayContent ul li:hover {
background-color: black;
}
UPDATE
Thanks for all your help. I changed my HTML and CSS as follows. Li elements are still not aligning as desired. Here is a fiddle: https://jsfiddle.net/qvq87ke1/2/
HTML
<div ng-show = "buttonDisplay" id = "buttonDisplayContent" >
<ul>
<li> Home </li>
<li class = "subLi">About
<ul class = "nested">
<li> Our Team </li>
<li> Our efforts </li>
</ul>
</li>
<li class = "nextToNested"> Blog </li>
<li class = "subLi"> Services
<ul class = "nested">
<li> Design </li>
<li> Web </li>
<li> Learn </li>
<li> Invent </li>
</ul>
</li>
<li class = "nextToNested"> Portfolio </li>
<li> Contact </li>
</ul>
</div>
CSS
#buttonDisplayContent {
background-color: #141516;
width: 100%;
margin-top: 9%;
text-align: center;
position: absolute;
opacity: 0.9;
}
#buttonDisplayContent ul {
list-style-type: none;
padding: 0;
}
#buttonDisplayContent ul ul {
list-style-type: none;
padding: 0;
}
#buttonDisplayContent ul a {
text-decoration: none;
color: #fff;
font-size: 50px;
font-weight: bold;
}
#buttonDisplayContent ul ul a {
text-decoration: none;
color: lightgray;
font-size: 40px;
font-weight: bold;
}
.subLi {
float: left;
margin: 0;
padding: 0;
list-style-type: none;
}
.nested {
float: right;
margin-left: 0;
}
.nested li {
display: inline-block;
padding-bottom: 6px;
padding-right: 1%;
padding-left: 1%;
padding-top: 8px;
}
#buttonDisplayContent ul li:hover {
background-color: black;
}
Your markup looks like it is incorrect. When nesting un-ordered or ordered lists they should be contained withing the li they will be creating a sub list for.
Like this:
<ul>
<li>One</li>
<li>Two
<ul>
<li>One - Sub Two</li>
<li>Two - Sub Two</li>
</ul>
</li>
<li>Three</li>
</ul>
You are adding your ul between the li:
<li class="subLi">About</li>
<ul class="nested">
<li>Our Team</li>
<li>Our efforts</li>
</ul>
<li class="nextToNested">Blog</li>
Updated answer based on updated question..
Try setting display:inside; on your nested <ul> and put your nested <ul> inside of the <li>
P.S. A screenshot or a fiddle (or at least your plain CSS) would be really helpful..
You should post a JSFiddle with your code, that would be easier to help you.
Otherwise, as a good practice, the UL nested nested should be in the previous LI (which should it parent so).
<li class = "subLi">
About
<ul class = "nested">
<li> Our Team </li>
<li> Our efforts </li>
</ul>
</li>
Maybe this is where the problem comes from.
If it can helps you:
Proper way to make HTML nested list?
Good Luck'
It's possible there is some padding/margin on the "a" element as well from the browser. Best practices is to import a reset.css file at the beginning of your project to clear all browser-applied CSS and style it on your own or using a framework.
I figured it out. I was floating the li elements unnecessarily.
<div ng-show = "buttonDisplay" id = "buttonDisplayContent" >
<ul>
<li> Home </li>
<li class = "subLi">About
<ul class = "nested">
<li> Our Team </li>
<li> Our efforts </li>
</ul>
</li>
<li class = "nextToNested"> Blog </li>
<li class = "subLi"> Services
<ul class = "nested">
<li> Design </li>
<li> Web </li>
<li> Learn </li>
<li> Invent </li>
</ul>
</li>
<li class = "nextToNested"> Portfolio </li>
<li> Contact </li>
</ul>
</div>
SASS
#buttonDisplayContent
background-color: #141516
width: 100%
margin-top: 9%
text-align: center
position: absolute
opacity: 0.9
#buttonDisplayContent
ul
list-style-type: none
padding: 0
#buttonDisplayContent
ul
ul
list-style-type: none
padding: 0
#buttonDisplayContent
ul
a
text-decoration: none
color: #fff
font-size: 50px
font-weight: bold
#buttonDisplayContent
ul
ul
a
text-decoration: none
color: lightgray
font-size: 40px
font-weight: bold
.subLi
//float: left
margin: 0
padding: 0
list-style-type: none
.nested
//float: right
margin-left: 0
display: inline
.nested
li
display: inline
padding-bottom: 6px
padding-right: 1%
padding-left: 1%
padding-top: 8px
#buttonDisplayContent
ul
li:hover
background-color: black
I have learnt CSS online and I am new to web designing. Need some expert opinion here, I may have written something wrong or stupid. Please forgive that as I am a beginner.
Here are my CSS and HTML codes:
#menu {
float: left;
width: 1000px;
height: 30px;
background-color:#0066FF;
border-bottom: 1px solid #333;``
}
#menu ul {
float: left;
width: 1000px;
margin: 0;
padding: 7px 0 0 0;
list-style: none;
}
#menu ul li{
display:inline;
}
li ul {display: none;}
li:hover ul {display: block; position:relative;}
li:hover li a{background: #0066FF;}
#menu ul li a{
float: left;
padding: 0 20px;
font-size: 12px;
font-weight: bold;
text-align: center;
text-decoration: none;
color: #FFFFFF;
}
#menu li a:hover, #menu li .current{
color: #FFFF00;
}
#menu ul li:hover ul{
width: 150px;
white-space: nowrap
height: 10px;
text-align: center;
background:#0066FF;
}
<div id="menu">
<ul>
<li>Home</li>
<li>Quran
<ul>
<li>Translation</li>
<li>Tajweed</li>
<li>Tafseer</li>
<li>Qoutes</li>
</ul>
</li>
<li>Ahadees
<ul>
<li>Sahih Al-Bukhari</li>
<li>Sahih Muslim</li>
<li> Sunan Abu-Dawud</li>
<li>al-Tirmidhi</li>
<li>al-Nasa'i</li>
<li>Ibn Maja </li>
</ul></li>
<li>Wazaif
<ul>
<li>Allah's help</li>
<li>Rizzaq</li>
<li>Aulaad</li>
<li>Marriage</li>
</ul></li>
<li>Rights & Duties
<ul>
<li>As Parents</li>
<li>As Husband</li>
<li>As Wife</li>
<li>As Son</li>
<li>As Daughter</li>
</ul></li>
<li>Videos
<ul>
<li>Molana Tariq Jameel</li>
<li>Dr Zakir Naik</li>
<li>Dr Farhat Hashmi</li>
<li>Naat videos</li>
</ul></li>
<li>Quran & Science</li>
<li>Library
<ul>
<li>Masnoon Duain</li>
<li>Tib-e-Nabvi</li>
<li>Tafseer</li>
<li>Islamic comerace</li>
</ul></li>
<li>FAQs</li>
<li>Blogs</li>
<li>Contacts</li>
</ul>
</div>
It looks like the problem is that you haven't positioned the sub-menus properly.
Because the sub-menu have not been given position:absolute they remain in the documents flow and so disturb other elements when shown.
Adding position:absolute removes them from the flow and solves the problem.
In order to be positioned according to the parent li, that li needs to be a block (hence display:inline-block)(you could float the li too if that's your choice) and be given position:relative.
Here's a suggestion that should help you along the way.
#menu ul li {
display:inline-block;
position: relative;
}
li ul {
display: none;
position: absolute;
top:100%;
left:0;
}
li:hover ul {
display: block;
}
JSfiddle Demo
#menu {
float: left;
width: 1000px;
height: 30px;
background-color: #0066FF;
border-bottom: 1px solid #333;
``
}
#menu ul {
float: left;
width: 1000px;
margin: 0;
padding: 7px 0 0 0;
list-style: none;
}
#menu ul li {
display: inline-block;
position: relative;
}
li ul {
display: none;
position: absolute;
top: 100%;
left: 0;
}
li:hover ul {
display: block;
}
li:hover li a {
background: #0066FF;
}
#menu ul li a {
float: left;
padding: 0 20px;
font-size: 12px;
font-weight: bold;
text-align: center;
text-decoration: none;
color: #FFFFFF;
}
#menu li a:hover,
#menu li .current {
color: #FFFF00;
}
#menu ul li:hover ul {
width: 150px;
white-space: nowrap height: 10px;
text-align: center;
background: #0066FF;
}
<div id="menu">
<ul>
<li>Home
</li>
<li>Quran
<ul>
<li>Translation
</li>
<li>Tajweed
</li>
<li>Tafseer
</li>
<li>Qoutes
</li>
</ul>
</li>
<li>Ahadees
<ul>
<li>Sahih Al-Bukhari
</li>
<li>Sahih Muslim
</li>
<li> Sunan Abu-Dawud
</li>
<li>al-Tirmidhi
</li>
<li>al-Nasa'i
</li>
<li>Ibn Maja
</li>
</ul>
</li>
<li>Wazaif
<ul>
<li>Allah's help
</li>
<li>Rizzaq
</li>
<li>Aulaad
</li>
<li>Marriage
</li>
</ul>
</li>
<li>Rights & Duties
<ul>
<li>As Parents
</li>
<li>As Husband
</li>
<li>As Wife
</li>
<li>As Son
</li>
<li>As Daughter
</li>
</ul>
</li>
<li>Videos
<ul>
<li>Molana Tariq Jameel
</li>
<li>Dr Zakir Naik
</li>
<li>Dr Farhat Hashmi
</li>
<li>Naat videos
</li>
</ul>
</li>
<li>Quran & Science
</li>
<li>Library
<ul>
<li>Masnoon Duain
</li>
<li>Tib-e-Nabvi
</li>
<li>Tafseer
</li>
<li>Islamic comerace
</li>
</ul>
</li>
<li>FAQs
</li>
<li>Blogs
</li>
<li>Contacts
</li>
</ul>
</div>
I want to make my navigation bar with dropdown, but there is always distance on left side of the div dropdown. How do I remove that distance?
Here is the code of the navigation bar:
.navbar {
background-color: #6b6b6b;
margin:10px;
}
.navbar ul {
display: inline;
color: white;
font-family: "Agency FB";
font-weight: bold;
text-transform: uppercase;
list-style-type: none;
font-size: 24px;
}
.navbar ul li {
display: inline-block;
padding-right: 10px;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
}
.navbar ul li:hover {
background-color: #cccccc;
}
.navbar ul li ul {
display: none;
position: absolute;
margin-left:-10px;
margin-top:5px;
background-color:white;
color:#6b6b6b;
padding-left:-20px;
font-size:20px;
}
.navbar ul li ul li {
display: block;
}
.navbar ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
And here is the code of html navigation bar (without link):
<div class="navbar">
<ul>
<li>
Home
</li>
<li>
Category
<ul>
<li>Batik</li>
<li>Party</li>
<li>Office</li>
<li>Casual</li>
<li>Sport</li>
</ul>
</li>
<li>
Information
<ul>
<li>
About Us
</li>
<li>
Cara Belanja
</li>
<li>
Our Location
</li>
</ul>
</li>
<li>
Contact Us
</li>
</ul>
</div>
Thanks before :)
I remade it for you kinda, I'm sure you'll get the jist of it
<div id="nav">
<ul>
<li>Home</li>
<li>Category
<ul>
<li>Blah</li>
<li>Blah</li>
<li>Blah</li>
</ul>
</li>
<li>Information
<ul>
<li>Blah</li>
<li>Blah</li>
<li>Blah</li>
</ul>
</li>
<li>Contact Us</li>
</ul>
</div>
http://jsfiddle.net/un64F/3/ for css
I'm not sure I understand what you are asking for exactly but does adding padding-left: 0; to the .navbar ul style have the effect you want?