hide submenus on nav - html

I'm trying to create a drop-down menu. I had it working for a minute.
My code is as follows:
<nav id="nav">
<ul>
<li class="subNav">Some Page1
<ul>
<li>Related Page1<li>
<li>Related Page2<li>
</ul>
<li>
</ul>
</nav>
My CSS is as follows:
#nav li.subNav ul{
display: none;
}
#nav li.subNav:hover ul{
display: block;
}
I have three CSS files that relate to this page. One is basically a web-kit for font, and the other two are bowlerplate.css and my custom file customFile.css. The tag <#nav li.subNav:hover ul> show up in customFile.css, and <#nav li.subNav ul> diplays in bout custom and boilerplate when I check computed styles.
There are two things I wish to fix; the submenu lines up horizontally (I need it to go vertical) and the submenu isn't hidden. I had to nest /li tag around the ul, so that took care of one problem (they're now aligned under the parent tag).
I also noticed that the height and width have changed on my parent li. I understand it expanding to accommodate the list items, but the increased height seems a little odd.

Here's my solution to the above problem
#nav li.subNav:hover ul li {
visibility: visible;
width: 171px;
padding: 0;
cursor: pointer;
float: none !important;
display: block !important;
}

Related

CSS flex property is not allowing links to be hidden and stack. Is this also not allowing my hamburger menu to function properly?

Inside my navbar, are my links. I am trying to set a media query where the links stack and center after clicking my hamburger menu with primely CSS flex-box. What do I need to change to my code to achieve this?
I was using float, absolute and relative positioning first. At this time my navbar was working almost perfectly. However, I had to place my link with its href="home," outside of the div with class "options," so that it would remain visible after my screen size approached my media query. The positioning of my links became awkward after this so I turned to CSS flex-box.
<html>
<div>
<nav class="navbar">
<label class="menu" for="toggle">&#9776</label>
<input type="checkbox" id="toggle"/>
<div class="main"><li class="home">TechReality</li></div>
<div class="options">
<li class="news"></li>
<li class="products">Categories</li>
<li class="trends">Trending</li>
<li class="forum">Customer Forum</li>
<li class="about">About Us</li>
</div>
</div>
</nav>
#media only screen and (max-width: 1135px) {
.navbar .options .home{
visibility: visible;
}
.menu {
display: block;
cursor: pointer;
}
.navbar.options{
text-align: center;
visibility: hidden;
flex-basis: 100%;
flex-wrap: wrap;
}
.news, .products, .trends, .forum, .about {
border-top: 1px solid rgba(255,255,255,0.3);
border-bottom: 1px solid rgba(0,0,0,0.1);
margin: 0;
}
ul:last-of-type a {
border-bottom: none;
}
#toggle:checked + .options {
visibility: visible;
}
}
My full code can be seen in this codepen snippet https://codepen.io/ashu121805/full/XvgRqP.
There's a lot to discuss here, but I want to point out three major problems that will get you 90% of the way there, then you can tweak the styling as you see fit afterwards.
1. Fixing invalid HTML
You have an extraneous closing div tag inside of <nav class="navbar"> that should be deleted. You also have li tags inside of div, which is incorrect; an li should always be the child of either an ol or a ul. Fixing those, and deleting the extra html opening tag from the start of your snippet above, we get this:
<div>
<nav class="navbar">
<label class="menu" for="toggle">&#9776</label>
<input type="checkbox" id="toggle"/>
<ul class="main">
<li class="home">TechReality</li>
</ul>
<ul class="options">
<li class="news"></li>
<li class="products">Categories</li>
<li class="trends">Trending</li>
<li class="forum">Customer Forum</li>
<li class="about">About Us</li>
</ul>
</nav>
</div>
(Also, just a note: In CodePen, you don't need to include the html or body tags, nor the head section. You can also delete the script tag you have and just use the JS panel in the editor view.)
2. Making the hamburger menu work
This one is very easy to fix. Right now you are toggling your menu's visibility with this rule:
#toggle:checked + .options {
visibility: visible;
}
Problem is, the + selector only selects the immediately next sibling of an element. Since .options isn't right after #toggle, the selector doesn't work.
Instead, you can use the ~ selector, which will select all siblings after an element that match the selector:
#toggle:checked ~ .options {
visibility: visible;
}
3. Basic CSS cleanup
Once you get this far, you'll have a menu that toggles when you click the hamburger, but it'll look wonky. There's a few main things we need to do:
Make our lists (ul) not actually look like lists.
Make the links stack on top of each other vertically.
Position the links in a logical place, like right below the nav bar on the left side of the screen.
#1 can be accomplished simply:
ul {
list-style: none;
padding-left: 0;
}
#2 can be accomplished by adding these additional styles:
.navbar .options {
flex-wrap: wrap;
}
.news, .products, .trends, .forum, .about {
width: 100%;
}
Also note that you currently have a typo in your media query styles – there should be a space in .navbar .options where you currently have none.
#3 is a little more involved, but a good start would be to add these styles inside your media query:
/* force .options to wrap below since it has flex-basis: 100% */
.navbar {
flex-wrap: wrap;
}
/* remove the default margin-bottom on lists – or you could do this in your main styles above your media query */
.navbar .options,
.main {
margin-bottom: 0;
}
From there you can adjust things as you like.

I'm having issues with assigning IDs properly in CSS

So, I'm having this issue with trying to make code apply to items within a sublist, which I have named "Horizontal"
<nav class="horizontal" id="horizontal">
<ul>
<li>Home</li>
<li>Menu</li>
<li>Locations</li>
<li>Catering</li>
<li>About Us</li>
</ul>
This should in turn translate over to my CSS file as so
#horizontal > li {
background-color: white;
font-size: 16px;
height: 50px;
line-height: 50px;
width: 180px;
display: inline-block;
horizontal-align: left;
I know the file is linked to my HTML doc as it has worked with other style rules.. I'd like to know why. Any help would be appreciated.
Also, side note: for this snippet
display: inline-block;
horizontal-align: left;
}
The effect I'm trying to go for here is to display the item as a block and float it to the left, I'd like to know if I'm on the right track with that code.
Finally, I seem to be having an issue with the font-color and/or text-color properties, as neither seem to work for my lists as such:
#horizontal > ul > li > a:hover {
background-color: rgb(255,101,101);
text-color: black;
The background color changes, but the text remains the same.
#horizontal > li
Where > li look for and LI element with a Parent element that has an ID horizontal
The > (greater than) operator looks for a direct descendant of a parent element. In your example, #horizontal is the parent element and the direct descendant is the UL.
Therefore, the effect you expect on the LI would not be applied.
You can use
#horizontal li, or #horizontal ul li, or #horizontal > ul > li which has a higher specificity weight.

How do I stop a horizontal list from breaking within a list item?

Assume the following list:
<div class="menublock">
<ul class="menu">
<li>Home</li>
<li>Google</li>
<li>Porn Hub</li>
<li>Toronto Star</li>
<li>Stack Overflow</li>
<li>Example Site</li>
<li>York University</li>
</ul>
</div>
with the following CSS
.menublock .menu {
margin-top: 35px;}
.menublock .menu li {
display: inline;
list-style-type: none;
padding-right: 20px;
}
.menublock .menu li a {
font-size: 16pt;
}
How do I stop the list from wrapping in the middle of a list item?
That is, I want it to line break between "Toronto Star" and "Stack Overflow" (if necessary), but not between "Stack" and "Overflow"
Line Breaks should be responsive to actual screen width, not force using child-after selectors or other such fixed points.
Make the a tag display as inline-block
.menublock .menu li a {
font-size: 16pt;
display:inline-block;
}
Instead of having every list item display as "inline", have them display as "block" and have them float "left".
This will prevent breaking of text inside of each item.
I tend to make horizontal navbars this way because it eliminates issues like these as well as awkward spacing issues that result from inline elements when they are next to each other.
Edit: floating an element automatically makes it display as "block", so just eliminate "display: inline" and have your list items float left.
.menublock .menu li a {
font-size: 16pt;
white-space:nowrap;
}

Creating a Vertical "drop" down menu in CSS

The title probably doesn't actually describe the issue properly. I want to create a menu for my website that is a vertical menu on the left side, and when you hover over an option with sub-options those sub-options pop out to the side (doesn't really matter at the moment). The issue I'm having is that when they pop out they push down all the other options, and I get this navigation bar that doesn't look good at all. If someone could help me fix this so I don't shove everything else out of the way even though they aren't overlapping, that would be appreciated.
The HTML I use.
<ul id="nav">
<li>Work</li>
<li>Imaging
<ul>
<li>Photoshop
<ul>
<li>Link</li>
<li>Link</li>
</ul>
</li>
<li>Illustrator</li>
</ul>
</li>
<li>Home</li>
The CSS I use.
ul {
list-style-type:none;
margin:0;
padding:0;
}
a {
display:block;
width:60px;
}
#nav ul {
display: none;
}
#nav li:hover > ul {
display: block;
position: relative;
float: left;
}
Thanks in advance if someone can help me with this.
that is because your document is having all the elements in a line and will always show them one after the other!
So my advice for you would be to just use
position: absolute;
This way you can align the elements over the document without interefering the current document style and element alignment!
For more: https://developer.mozilla.org/en-US/docs/Web/CSS/position
I would position the ul inside your li absolute.
position:absolute;
When you do this the element will hover above the li-parent. When you try a little with positive and negative margin you will be able to put the hover element next to it parent.
It will be something like this:
#nav li:hover > ul {
display: block;
position: absolute;
float: left;
margin-left:60px;
}

hyperlinking a list item?

i know this might seem straightforward, but i can't solve it, im trying to make the whole list item linkable, rather than just the word home
the html code:
<li class="current">Home</li>
p.s. when you hover the li, background color changes, so i want that whole list item to be hyperlinked, if you get what i mean, not just the word
Wrapping a block level element (the li) within an inline-element (a), is invalid mark-up, which has the potential to throw errors, depending on your doctype. That being the case, first change your mark-up around to:
<li>link text</li>
Assuming that your li is display: block then the following will cause the a to 'fill' the available space:
a {
display: block;
width: 100%; /* this may, or may not, be necessary */
}
If you're using this for a horizontal list, and the li are display: inline, then you can use the following:
a {
display: inline-block;
}
And style-to-taste.
Do it in reverse
<li class="current">Home</li>
not sure what your other styles are but you can change the tag with something like
li.current a{
display:block;
}
This should do it:
HTML:
<li class="current">Home</li>
CSS:
li a {
display: block;
}
// or
li.current a {
display: block;
}
​