I had in my mind that it would not be hard to add an anchor tag that, when hovered or clicked, would cause a CSS flyout with more links in it to appear.
As it is now, a set of normal anchor tags are inside of a span which is inside of an li. I want to add this hover flyout link to be in the same location, the same as one of the links but instead of being a normal link, do the flyout. I found all kinds of code online but none of it seems to work in this location:
<li>
Introduction to Something
<span>
<a target="_blank" href="http://http...file.html">Watch Slideshow</a>
View File
<a target="_blank" href="http://file....pdf">Print</a>
FLY OUT MENU ITEM
</span>
</li>
I've posted a demo, that's not hugely different to #jeffkee's answer, over at jsbin, to show how deep it's possible to go with flyout menus and how simple they can be.
The (x)html is copied below (with notes):
<ul>
<li>home</li>
<li>products
<ul>
<li>CPUs
<ul>
<li><a href="#">AMD<a>
<ul>
<li>AM2</li>
<li>AM3</li>
</ul>
</li>
<li>Intel</li>
</ul>
</li>
<li>Motherboards</li>
<li>PSUs</li>
<li>Hard drives
<ul>
<li>HDD</li>
<li>SSD</li>
</ul>
</li>
</ul>
</li>
<li>Tracking</li>
</ul>
The CSS is as below:
ul {list-style-type: none; width: 8em; border: 1px solid #000; padding: 0;}
/* just to set the base-line for the ul, but note the width. It's important. */
ul li {position: relative; border-top: 1px solid #000; margin: 0; padding: 0; }
/* the position: relative is used to allow its child elements to be absolutely positioned */
ul li:first-child {border-top: 0 none transparent; }
/* to avoid a two-pixel border on the first li (1px li-border + 1px ul-border) */
ul li:hover {background-color: #f90; }
/* just to aid visually */
ul ul {position: absolute; top: -1px; left: 8em; display: none; }
/* sets up all ul elements beneath the parent ul, the -px is to counter the movement forced by the border, bear in mind that the li:first-child doesn't *have* a border, so adjust to taste */
ul > li:hover > ul {display: block; }
/* makes the nested list appear if the parent-li is hovered */
I don't really see how the fly out is structured. Flyouts are generally set within a link so that when the mother link is hovered, the child shows up..
Check out the most recent flyout menu I did on http://www.feelfabulous.ca/oldindex.php and break down the HTML/CSS. You can do it without any javascript etc. Here's hte HTML structure I have (simplified):
<ul id="menu">
<li>Home</li>
<li>About
<div class="submenu_container">
<ul>
<li>our story</li>
<li>meet our team</li>
<!--<li>press</li>-->
</ul>
</div>
</li>
<li>Spa Menu</li>
<li>Party Packages</li>
<li>Beauty Kits</li>
<li>Goody Bags</li>
</ul>
So .submenu_container is set to display:none, and then #menu li:hover .submenu_container is set to display:block. that's the basic idea of a flyout tyep fo menu. And of course the .submenu_container is absolute positioned so it doesn't affect the shape and form of the page when it pops up.
Related
i am a newbie to CSS,HTML and trying to understand lists.however something confuses me .As you can see below my HTML i am trying to create a drop down navigation bar.what i don't understand is why would display property won't work on a single li element.
.block1{background-color:#736570;margin:0px;}
ul a {color:white;}
ul li{list-style-type: none; padding:5px;}
.hidden {display:none;}
.home:hover .hidden{display:block;}
.hidden a:hover{background-color: #f1f1f1;}
<body>
<ul class="block1">
<li class="home">Home
<li class="hidden">
contact us
</li>
<li>about<li>
<li>Investor</li>
<li> what we do</li>
</li>
</ul>
</body>
Here is the new css you should use:
.block1{background-color:#736570;margin:0px;}
ul a {color:white;}
ul li{list-style-type: none; padding:5px;}
.hidden{display:none;}
.home:hover + .hidden{display:block;}
li:hover{background-color: #f1f1f1;}
Then your html should look like this:
<body>
<ul class="block1">
<li class="home">Home</li>
<li class="hidden" >
contact us
</li>
<li>about</li>
<li>Investor</li>
<li> what we do</li>
</ul>
</body>
Nothing too wrong with your html, just a mismatch <li>, and the css you want to look at this post: Using only CSS, show div on hover over <a>
Here is the JSFiddle: Example of OP Code
i don't understand is why would display property won't work on a
single li element.
The div with class .home is not the parent of li tag with class hidden. Hence it will never trigger a hover over that. Whenever you trigger a hover over a parent container it trickles down and find its children and does some sort of styling.
In your case, you are trying to use display:none to hide a li and make it display by means of hover.
Consider the snippet below, whenever you hover over the parent container, the li tag is being displayed. (This approach below does not make a drop down menu for you but it is give you some insight how to make that display property change on hover)
.block1 {
background-color: #736570;
margin: 0px;
}
ul a {
color: white;
}
ul li {
list-style-type: none;
padding: 5px;
}
.hidden {
display: none;
}
.block1:hover .hidden {
display: block;
}
.hidden a:hover {
background-color: #f1f1f1;
}
.home
<html>
<body>
<ul class="block1">
<li class="home">Home
<li class="hidden">
contact us
</li>
<li>about
<li>
<li>Investor</li>
<li> what we do</li>
</li>
</ul>
</body>
</html>
I'm doing some practice with layout using css, and I've come across a weird thing that I don't know how to explain. Where does the space highlighted in red in the following image come from, and how do I eliminate it?
HTML:
<body>
<div class="menu-bar">
<ul>
<li>
Home
<ul>
<li>Beach House</li>
<li>Ski Cabin</li>
<li>Country Cottage</li>
</ul>
</li>
<li>
News
<ul>
<li>World News</li>
<li>National News</li>
<li>Local News</li>
</ul>
</li>
<li>
Contact
<ul>
<li>Email Address</li>
<li>Phone Number</li>
<li>Postal Address</li>
</ul>
</li>
<li>
About
<ul>
<li>About Me</li>
<li>About You</li>
<li>About Us</li>
</ul>
</li>
</ul>
</div>
</body>
CSS:
body {background: #77c4d3; padding:1%; }
div.menu-bar{position: relative; max-width: 700px;}
/*Styles for both menu and submenus: */
div.menu-bar ul { list-style-type: none; margin: 0; padding:20px; background: gray;}
div.menu-bar li { background:white; text-align:center; display:inline-block; padding:10px;}
/*Menu-specific styles: */
div.menu-bar > ul {width: 100%;}
div.menu-bar > ul > li {width:20%; border:0; margin: 0; padding-top: 10px; padding-bottom: 10px;}
/* Submenu-specific styles */
div.menu-bar ul ul {background-color: blue; padding-left: 10px; padding-right: 10px;}
/*Hide any unodered lists that are descendents of a list element by default*/
div.menu-bar li ul {
display: none;
}
/*Select any unordered lists that are children of list elements that are being hovered on.*/
/* The <ul> is display:block, but the individual <li> elements are inline-block, which is what matters.*/
div.menu-bar li:hover > ul {
display: block;
position: absolute;
}
That comes from the wrapping <ul> below <div class="menu-bar">. It's width is set to 100% in your css where you say:
div.menu-bar > ul {
width: 100%;
}
Since the buttons don't fully take up the space in that <ul> there is some extra grey space. If you add a text-align: center; to that style, it will nicely center your buttons, as so:
div.menu-bar > ul {
width: 100%;
text-align: center;
}
Or check out my JSFiddle for this.
There are a couple of things going on here to be aware of.
1.) You have space in your code, between your top-most <li>'s. This is a funky issue with whitespace. CSS-Tricks has a great summary of the issue. Essentially, you have to put your closing </li> tag back-to-back with the next opening <li> tag, to get rid of those tiny gaps.
2.) Secondly, your width is set to 20%. You can bump this up to even quarters, at 25%...although you'll notice that this pushes the last of your <li>s down a line. This is because...
3.) There is a 10px padding on div.menu-bar li which applies 10px of padding to the left, right, top and bottom. Your div.menu-bar > ul > li rules specify a top and bottom padding, but the left and right are left the same. My personal approach for this?
4.) Use box-sizing. By setting this CSS property to border-box, your padding is included in the width of your elements. In other words, you can set the following CSS:
div.menu-bar > ul > li {
width: 25%;
margin: 0;
padding: 12px;
box-sizing: border-box;
}
...and you'll end up with a set of list items that have a) no funky, tiny space between them and b) are all on the same line.
Hope that helps!
Each nav item's width is dependent on it's text content. It has no knowledge of how wide it's parent is. Each nav item is just shoved as far left as it can go next to it's neighbor.
If you are looking to have the nav items fill the bar evenly, you will need to use a flex solution. See here for a complete explanation.
I created the drop-down menu by using CSS and HTML.
I just can't figure out what am I making wrong. When I hover the mouse over the Social it doesn’t pop-up me the drop-down menu.
Entire fiddle here
Js Fidle Example
A part of code where I think its mistake.
#nav ul li a:hover {
color: #ccc;
text-decoration: none;
}
#nav ul li:hover ul{
display : block;
}
#nav ul ul {
display: none;
position : absolute ;
background-color: #333;
border: 5px solid #222;
border-top : 0;
margin-left: -5px;
}
youo have <li> Social</li> and it should be
<li> Social
<ul>
<li> Facebook</li>
<li> Twitter </li>
<li> Youtube </li>
</ul>
</li>
JSFIDDLE
You need to put the sub UL inside the li
<li> Social
<ul>
<li> Facebook</li>
<li> Twitter </li>
<li> Youtube </li>
</ul>
</li>
See fidde: http://jsfiddle.net/2j55uthz/1/
The reason is because in your CSS this line:
#nav ul li:hover ul {
display: block
}
Is target the UL element inside of the hovered li
The <ul> containing facebook, youtube, twitter needs to be within the social <li>. It works with that change.
I'm a newbie to html and trying to figure it out through online tutorials. I have a menubar that goes horizontally across the top of the page. Right now I have the menubar in a div tag, and within the contents, I have
<nav>
<li>
<a id="l1" href="whatever.com/about/">About</a>
<a id="l2" href="whatever.com/content/">Content</a>
<a id="l3" href="whatever.com/history/">History</a>
<a id="l4" href="whatever.com/Team/">Team</a>
</li>
</nav>
I want to position the links and change the font, and I was under the impression that I would do so using a format along the lines of:
<style>
.l1
{
position:relative;
top:5px;
right:30px;
}
</style>
However, that does not seem to be working, and I can't find any helpful tutorials. Can anyone give me advice on how to appropriately format & style my links?
The dot notation you've used in CSS is for classes, not IDs, this should work:
<nav>
<ul>
<li><a class="l1" href="whatever.com/about/">About</a></li>
<li><a class="l2" href="whatever.com/content/">Content</a></li>
<li><a class="l3" href="whatever.com/history/">History</a></li>
<li><a class="l4" href="yabidu.com/Team/">Team</a></li>
</ul>
</nav>
ID's id="foo" on an element are accessed in CSS with #foo, also they supposed to be completely unique, therefore no element IDs on a page should be the same. Classes on the other hand class="bar" are allowed to be used multiple times and are access in CSS using .bar.
You've also used invalid syntax, <li> (list items) are always supposed to be directly inside either <ul> (unordered list) or <ol> (ordered list), I have fixed your markup for you as well.
Your HTML5 systax is wrong..
<nav>
<ul>
<li><a href="#">LINK<a></li>
.....
</ul>
</nav>
and to aceess the li element, use
nav ul li a {
font-size:20px;
font-weight:bold;
position:relative;
top:XX;
left:XX;
}
Solution 1:
HTML:
<nav>
About
Content
History
Team
</nav>
CSS:
nav a {
float: left;
padding: 0 20px;
}
DEMO 1
Solution 2:
HTML:
<nav>
<ul>
<li>
About
</li>
<li>
Content
</li>
<li>
History
</li>
<li>
Team
</li>
</ul>
</nav>
CSS:
nav ul {
margin: 0px;
padding: 0px;
list-style-type: none;
}
nav ul li {
float: left;
padding: 0 20px;
list-style: none;
}
DEMO 2
Element li not allowed as child of element nav in this context.
Contexts in which element li may be used:
inside ol elements.
inside ul elements.
inside menu elements
HTML:
<nav>
<ul>
<li>About</li>
<li>Content</li>
<li>History</li>
<li>Team</li>
</ul>
</nav>
Don't use id's for styling elements (Use only where it is necessary).
Use css selectors:
nav ul li a { ... }
or if you interested to style only childs a in li element:
nav li > a { ... }
For display li elements inline you must add next style
nav li {
display: inline-block;
*display: inline; // fix for ie7
}
or float:left instead display:inline-block;
I am styling my top level <li> to look like tabs. and on rollover a div shows but if there are nested <ul> <li>'s in the div they inherit the same tab style as the top level <li>'s
below is my style:
#menu li a {
font-family:Arial, Helvetica, sans-serif;
font-size:13px;
color: #ffffff;
display:block;
outline:0;
text-decoration:none;
padding:10px 9px 2px 9px;
/* Background color and gradients */
background: #da0000;
background: -moz-linear-gradient(top, #b80202, #da0000);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#b80202), to(#da0000));
/* Rounded corners */
-moz-border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
border-radius: 5px 5px 0px 0px;
}
This is my HTML
<li>
Headquarters
<div class="dropdown_2columns">
<div class="col_2">
<ul>
<li>Board</li>
<li>Staff</li>
</ul>
</div>
</div>
</li>
I thought adding a class to the top level <li> would help but no luck. Is there something I am missing? when the code above runs "Board" and "Staff" both have a red tab effect on them.
You are targeting all As that are in LIs, so this behavior is as it should be.
There are many solutions to this "problem". The easiest way would be to target (with your CSS selector) just the first level of LIs with the "child selector":
#menu > li > a {
...
}
This should only affect the first level of As in the LIs.
Well, adding a class to the top level <li> won't work - because the inner <a>'s will still be affected by:
#menu li.myclass a
I.e., they're anchor elements inside a <li> with class "myclass".
Instead, you can change the rule to:
#menu > li > a
... meaning, only <a>'s that are immediate children of <li>'s, which are immediate children of #menu, will be affected (IE6 doesn't support this). This is assuming it's your <ul> that has the id "menu".
Or you could use (mostly for IE6 compatibility):
#menu li li a
{
/* Undo styles you applied to #menu li a */
}
Note that in this, you'll have to reset/undo/"overwrite" all the styles previously set on #menu li a that you don't want to apply to the inner anchors.
An alternative for IE6 - where you won't need to reset/undo styles - is to set a class on the <a>'s rather than the <li>'s:
#menu li a.tab
I think this is impossible to answer without seeing a more complete snippet of the HTML. At the moment folks answering have to assume which element has the ID of #menu.
If the HTML looks like this:
<div id="menu">
<ul>
<li>
Headquarters
<div class="dropdown_2columns">
<div class="col_2">
<ul>
<li>Board</li>
<li>Staff</li>
</ul>
</div>
</div>
</li>
</ul>
</div>
Then the solution would be:
#menu > ul > li > a { ... }
If the code looks like this:
<ul id="menu">
<li>
Headquarters
<div class="dropdown_2columns">
<div class="col_2">
<ul>
<li>Board</li>
<li>Staff</li>
</ul>
</div>
</div>
</li>
</ul>
Then the correct selector would be:
#menu > li > a { ... }
If it doesn't look like either of those snippets, then I'll need to see more of your code in order to better answer your question!
One more thing -- if you want to be even more specific you can also use first-child which would be implemented as:
#menu > ul > li > a:first-child { ... }
or...
#menu > li > a:first-child { ... }
...depending on your HTML.