CSS Drop Down Menu - List elements are static - html

I may seem really silly or outright wrong in the way I code. However, when I create a drop down menu in CSS the new li elements get pushed to the other side of the page and not in the container box. How would I fix this?
Here is the code:
<nav>
<div class="wrapper">
<div class="brand">
<img class="UKLogo" src="images/logo.png" alt="">
</div> <!-- brand -->
<div class="navigation">
<ul class="nav-ul">
<li> HOME </li>
<li> ABOUT </li>
<a href="#">
<li class="course-li">
COURSES
<ul class="drop-down">
<li class="list-item"> Driver CPC </li>
<li> First Aid </li>
<li> Other </li>
</ul>
</li>
<li> CONTACT </li>
<!-- <li> TESTOMONIALS </li> -->
<!-- <li> FAQs </li> -->
</ul>
</div> <!-- Navigation -->
</div> <!-- Wrapper -->
</nav>
nav {
margin: 0px;
padding: 0px;
height: 75px;
background-color: #FFF;
}
.brand {
margin: auto;
width: 960px;
}
.company-name {
padding: 0px;
margin: 0px;
}
.UKLogo {
padding: 0px;
margin: 0px;
position: relative;
top: 11px;
}
.navigation ul li {
display: inline-block;
margin: 10px;
position: relative;
left: 380px;
top: -46px;
}
.navigation ul a {
color: black;
margin-left: 40px;
text-decoration: none;
font-family: Lato;
font-weight: 300;
}
.navigation ul a:hover {
color: #169ec5;
font-weight: 300;
}
.course-li:hover .drop-down {
left: 0px;
}
.drop-down {
position: absolute;
left: -5px;
z-index: 1;
background-color: white;
left: -9999px;
}
Thank you ever so much for looking and helping. Always open to criticism whether its the way I code or anything else.
Here is a JSFiddle https://jsfiddle.net/vj41qLts/
Many Thanks!

You need to declare a position in the parent, for the child to reside in. An element with position: absolute; will position itself to the first parent with position: relative;. If there is no parent with position: relative;, it will use the browser window instead.
See fix example here: https://jsfiddle.net/vj41qLts/1/
I think there are two thing you need to change:
ul li will select everything li in the navigation even the dropdown, ul>li will only select the immediate child, instead of running down the nested elements.
you need to add position:relative; in your dropdown's parent.

One of the first issues I see is the fact that your markup for your main links isn't setup correctly. Following a structure more link the below should give make it work the way you want it to:
<nav>
<ul>
<li><a href="#">Home<a></li>
<li><a href="#">About<a></li>
<li>
<a href="#">Courses<a>
<div>
<ul>
<li>A link</li>
<li>A link</li>
</ul>
</div>
</li>
</ul>
</nav>
Then use CSS or JS to control showing and hiding the dropdown of links.

Related

Setting max width bigger than parent in absolute positioned child doesn't work when parent is positioned relatively

I'm trying to make a basic navigation bar where a child dropdown appears when hovering over a list item. I want to position this dropdown starting at the right most edge of the list item I am hovering over, but I want it this dropdown be able to scale bigger than the list item you're hovering over.
The trouble is that when I position the parent relative, the dropdown's width is constricted to the width of the list item you're hovering over, when I remove postion relative I lose the ability to position it the way I want it.
When the parent List item doesn't have position relative it looks like this:
But I want the right edge of that dropdown to align with the right side of the list item I'm hovering on. When I add position relative to the list items, the width of the dropdown is contsrained like this:
The markup looks like follows:
<nav>
<ul class="outer-list">
<li>
<a>
Work
</a>
<ul class="inner-list">
<li>Sub1</li>
<li>Sub2</li>
<li>Sub3</li>
</ul>
</li>
</ul>
<ul class="outer-list">
<li>
<a>
Contact
</a>
</li>
</ul>
<ul class="outer-list">
<li>
<a>
Helpdesk
</a>
<ul class="inner-list">
<li>Sub1</li>
<li>Sub2</li>
<li>Sub3</li>
</ul>
</li>
</ul>
<ul class="outer-list">
<li>
<a>
Subscriptions
</a>
<ul class="inner-list">
<li>Sub1</li>
<li>Sub2</li>
<li>Sub3</li>
</ul>
</li>
</ul>
I am not in charge of the markup, but if it needs to change to allow for a solution that is fine.
My CSS is as follows:
.outer-list{
.dropdown{
padding-right: 20px;
a{
color: black;
text-decoration: none;
position: relative;
.icon-dropdown{
position: absolute;
display: inline-block;
width: 6px;
height: 4px;
background-image: url('./Assets/BlueArrowIcon.svg');
background-size: cover;
background-position: center;
top: 50%;
right: -11px;
transform: translateY(-50%)
}
}
.inner-list{
padding: 25px 20px;
display: none;
position: absolute;
background-color: $color-white;
box-shadow: 5px 0px 50px rgba(0,0,0,0.16);
z-index: 1;
max-width: 310px;
li{
margin-bottom: 20px;
&:hover{
a{
color: $color-dark-red;
}
}
}
}
&:hover{
a{
color: $color-blue;
}
.inner-list{
display: block;
a{
color: black;
}
}
}
}
&:last-of-type{
.dropdown{
padding-right: 0px;
}
}
}
If anyone could help me that would be much appreciated.

How can I keep horizontal element alignment inside an expanding list item?

I have been tasked with styling a website, where I have encountered a hurdle regarding the horizontal alignment of elements inside list items.
I have replicated the structure of the menu in question with this JSFiddle.
I want to know how I can keep the position of the green divs (as shown from the start) when I expand the menu, using the button in the fiddle. I need them to keep horizontal alignment with the first <a> element in their respective <li> element, when the submenus are displayed.
you can do it like this for example:
<html>
<script>
function clickFunction(){
var elements = document.getElementsByClassName("submenu");
for(var i = 0; i < elements.length; i++){
elements[i].classList.toggle("display-sublist");
}
}
</script>
<style>
ul{
list-style-type: none;
margin: 0px;
padding: 0px;
}
ul li{
width: 100%;
background-color: blue;
}
.submenu{
display: none;
}
.display-sublist{
display: block;
}
ul li a{
width: 95%;
background-color: red;
}
.main-test {
display: inline-block;
float: left;
width: 90%;
}
.cancel-test{
display: inline-block;
background-color: green;
float: right;
width: 10%;
}
.expand-button{
clear: both;
display: block;
}
</style>
<body>
<ul>
<li>
<a class="main-test" href="#">Something</a>
<a class="cancel-test">x</div>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
<a class="main-test"href="#">Something</a>
<a class="cancel-test">x</a>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
Something
</li>
<li>
Something
</li>
</ul>
<button onclick="clickFunction()" class="expand-button">Expand</button>
</body>
</html>

How to create a CNN Menu Navigation using HTML, CSS and JQuery

I would like to implement this kind of menu navigation (http://cnn.com), when you click the hamburger icon it open the menu nav. I'm not sure if this is modal, anyone knows how to create the html, css, and jquery for this kind of menu nav?
Here's something to get you started:
$hamberger = $("#hambergerMenu");
$("#triggerMenu").click(function() {
$hamberger.show(500);
});
$("#closeHamberger").click(function() {
$hamberger.hide(500);
});
#hambergerMenu {
position: fixed;
display: none;
width: 100%;
height: 100%;
left: 0;
top: 0;
background-color: #a6a6a6;
}
#closeHamberger {
margin: 10px;
position: fixed;
right: 0;
font-size: 2em;
top: 0;
text-decoration: none;
color: white;
}
#hambergerMenu ul {
list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<img id="triggerMenu" src="http://neil.computer/stack/3line.png" />
<div id="hambergerMenu">
<a id="closeHamberger" href="javascript:void(0)">X</a>
<ul>
<li>
Content
</li>
<li>
Good Content
</li>
<li>
Bad Content
</li>
<li>
Meh Content
</li>
<li>
SO Content
</li>
</ul>
</div>

Why does my dropdown menus move left to right (HTML & CSS)

The problem I have is when I go to the navigation menu and have the menu come down, instead of being one item per line it goes across the screen from left to right
I get [Item 1] [Item 2] [Item 3] instead of:
[Item 1]
[Item 2]
[Item 3]
I think it has something to do with the navigation bar with my drop down menu (or lack thereof) want to leave. Any suggestions?
#navbar {
position: fixed;
top: 0px;
left: 0px;
z-index: 999;
width: 100%;
}
.nav {
width: 1800px;
height: 70px;
margin: auto;
position: absolute;
left: 0px;
top: 0px;
background-color: #DD0205;
}
.margin {
margin: 0;
display: inline-block;
}
#searchbox {
padding 5px;
font-size: 1em;
line-height: 100px;
}
#magnify-search {
text-indent: -99999px;
width: 25px;
height: 25px;
display: block;
background: transparent url(magnify.jpg) 0 0 no-repeat;
}
ul.cssMenu,
ul.cssMenu ul {
list-style: none;
margin: 1;
padding: 0;
position: relative;
}
ul.cssMenu ul {
display: none;
;
/*initially menu item is hidden*/
position: absolute;
}
/* Hover effect for menu*/
ul.cssMenu li:hover > ul {
display: block;
}
li {
display: inline;
}
<input type="search" id="searchbox" value placeholder="Search">
<div class="margin">
<input type="button" id="magnify-search" />
</div>
<ul class="cssMenu">
<li class="Eco-Fashion">
<b>Eco-Fashion</b>
<ul>
<li>Tops
</li>
<li>Bottoms
</li>
<li>Outwear
</li>
<li>Shoes
</li>
<li>Jewelry
</li>
</ul>
</li>
<li>&nbsp &nbsp </li>
<li class="GreenBeauty">
<b>Green Beauty</b>
<ul>
<li>Soy Makeup
</li>
<li>Soy Blush
</li>
<li>Soy
</li>
</ul>
</li>
<li>&nbsp &nbsp </li>
<li class="GreenLifestyle">
<b>Green Lifestyle</b>
<ul>
<li>Leaf Pants
</li>
<li>Coconut Bra
</li>
<li>Wilson Volleyball
</li>
</ul>
</li>
<li>&nbsp &nbsp </li>
<li class="Sale">
<b>Sale</b>
</li>
</ul>
</div>
</div>
What happened?
You have specified <li> to display as inline elements. This rule force all <li> elements to behave like a normal text. It means that it will appear in one line and will have white spaces between element (like between words in a paragraph).
Where is that piece of code?
You have CSS rule that looks like this
li {
display: inline;
}
in a very bottom of your code snippet.
What to do?
If you want them to be under each other use display: block instead.
Anything else?
Yes. Your menu have very strange behavior and I'd recommend you to take a look at jQuery Accordion

Links not working in unordered list

I have a weird problem where my links work fine on one page or fail to do so on another. Here is my code for the non-working page:
<div id="wrapper">
<a href="frontPage.html"><header>
<img src="img/MOBILAX-LOGO.png" height="100" alt="logo">
<h1>MOBI & LAX</h1>
<p>CELLULAR REPAIR CENTER</p>
</header></a>
<nav>
<ul>
<li>
ABOUT US
</li>
<li>
SERVICES
</li>
<li>
IPHONE REPAIR
</li>
<li>
BLOG
</li>
<li>
CONTACTS
</li>
</ul>
</nav>
And the code for the working page:
<div id="wrapper">
<a href="frontPage.html"><header>
<img src="img/MOBILAX-LOGO.png" height="100" alt="logo">
<h1>MOBI & LAX</h1>
<p>CELLULAR REPAIR CENTER</p>
</header></a>
<nav>
<ul>
<li>
<a class="activeLink" href="side2.html">ABOUT US</a>
</li>
<li>
SERVICES
</li>
<li>
IPHONE REPAIR
</li>
<li>
BLOG
</li>
<li>
CONTACTS
</li>
</ul>
</nav>
I am able to see the links fine, but they are not clickable.
Here is the CSS for the nav, ul and wrapper:
nav {
background-color: #2a2a2a;
width: 50%;
height: 200px;
float: left;
}
nav ul {
list-style: none;
height: 200px;
}
nav ul li {
float: left;
margin-top: 86px;
margin-left: 25px;
}
nav a {
text-decoration: none;
color: white;
font-weight: bold;
}
nav a:hover {
color: #f25e44;
}
.activeLink {
color: #f25e44;
}
#wrapper {
width: 1400px;
/*border: 1px solid black;*/
margin-left: auto;
margin-right: auto;
box-shadow: 10px 5px 5px 10px #888888;
}
EDIT: I figured out the issue. I had a div overlapping my ul.
FIDDLE
The # would normally reference an anchor on your page and scroll there. Since you are just using the # it links to itself, so the page wouldn't reload, and would stay in the same place.
Your CSS also specifies not to decorate (underline) the hyperlinks, giving the impression that the link does nothing.
Edit: http://jsfiddle.net/2L3hL7w6/
I've added some CSS to highlight in red if a link has been visited - you'll see if you click on one of your links on the page it changes to red, showing the link does in fact work.
nav a:visited {
color: #ff0000;
}
All the links are the same in your example code. So when you once clicked one link....nothing more will happen since you are already there.