can't select the desired ul element - CSS - html

Been reading and leaving out the advanced stuffs for future but this one seems to be fairly easy and straight forward but still can't get it.
<div class="sidenav">
<h3>Photo Archives</h3>
<ul>
<li><a href="../photos.html" class="selected" >2015</a>
<ul>
<li>Singapore</li><li>England</li>
</ul>
</li><li>2014
</li><li>2013
</li><li>2012
</li><li>2011
</li><li>2010
<ul>
<li>Philippines</li>
</ul>
</li><li>2009
</li>
</ul>
</div>
CSS bit here. This one is in primary css file.
.sidenav {
padding-top: 5%;
padding-left: 2%;
display: table-cell;
position: relative;
width: 20%;
vertical-align: top;
background-color: #c01f2c;
text-align: center;
/*border: 1px solid green;*/
}
.sidenav h3 { padding-bottom: 15px;}
.sidenav ul{ position:relative; }
.sidenav ul li:hover, .selected{
background-color: #2e2e2e;
}
.sidenav ul li a {
color:white;
display: block;
line-height: 3rem;
font-size: 1.25rem;
}
.sidenav ul li:hover ul {
display: block;
position: relative;
}
.sidenav ul li ul li:hover, .selected{
border-left: 5px solid white;
}
.sidenav ul ul {
position: absolute;
display: none;
width: inherit;
background-color: #c01f2c;
padding-right: 10px;
}
.sidenav ul ul a {
font-size: 1rem;
text-align: right;
padding-right: 10px;
Following CSS is exclusively for this page - 2015.html - which I have made reference to later than the main css file in the header.
.sidenav ul ul {
position: relative;
display: block;
width: inherit;
background-color: #c01f2c;
padding-right: 10px;
}
Problem is I can't seem to select just the first ul which is first submenu with 2015.html. If I leave it as it is now, it will select both ul from 2015 and 2010. I want to select just the 2015 submenu. I've tried child, descendant, combinations all kind of selecting techniques but to no avail.
I know the thing I'm trying to do can be done with simple java but I don't want to jump to it yet. Want to keep it css and simple.

You may want to consider using first-of-type, assuming 2015 is always in the first child li of the menu ul:
.sidenav ul li:first-of-type ul {
// styles
}
The :first-of-type CSS pseudo-class represents the first sibling of
its type in the list of children of its parent element.
With that said, given that the only valid children of a ul is li you can substitute with :first-child

Your rule .sidenav ul ul targets all ul sub-children.
You need to use the :first-of-type pseudo-class if you only want to target the first child
.sidenav ul ul:first-of-type

Try this out ,
As you need to select the first ul in the first sub menu
.sidenav > ul ul:first-child {
position: relative;
display: block;
width: inherit;
background-color: #c01f2c;
padding-right: 10px;
}

To make something dynamic use this:
.sidenav ul li a.selected + ul {
position: relative;
display: block;
width: inherit;
background-color: #c01f2c;
padding-right: 10px;
}
Working fiddle: http://jsfiddle.net/alessiozuccotti/uannpmf6/

Related

Dropdown part of navbar displaying through other li elements when set to responsive

I have been trying to make my navbar responsive. So far I managed to display all li items underneath each other when the screen gets to small. However some li's of my navbar have sub items which normally drops down (Classes has 3 sub li's). When hovering one of the li's which has subitems it renders them through the main li items of the nav bar.
What I want is that the submenus dont get triggered by hovering, but only when the user taps their screen. Afterwards it should display the submenu items underneath the parent li and then continue with the other main li's.
The code underneath is the working part for the fullscreen nav bar.
Demo
.nav a {
text-decoration: none;
color: #fff;
display: inline-block;
padding-left: 15px;
padding-right: 15px;
border-bottom: none;
transition: .5s background-color;
}
.nav ul {
list-style: none;
background-color: #522d54;
text-align: center;
padding: 0;
margin: 0;
display: block;
}
.nav li {
font-family: 'Allerta', Helvetica, Arial, sans-serif;
width: auto;
line-height: 40px;
border-bottom: none;
font-size: 1em;
display: inline-block;
text-align: left;
}
.nav a:hover {
background-color: #39203b;
}
.nav a.active {
cursor: default;
background-color: #824885;
box-shadow: inset 0em -.2em #b084b3;
}
.nav ul li {
position: relative;
display: inline-block;
}
.nav ul li ul li {
text-align: center;
padding: 0;
width: 100%;
}
.nav li ul li a {
text-align: left;
font-size: .8em;
white-space: nowrap;
padding-left: 15px;
padding-right: 15px;
display: block;
}
.nav ul li ul {
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul {
display: inline-block;
}
You need to override your hover display with none, because you no longer want it to show on hover.
#media screen and (max-width: 650px) {
.nav li:hover ul {
display: none;
}
}
Then you will need to use javascript or jquery to add a class to the li on click:
jQuery could be something like (assumes you have class 'sub' on items with sub menus):
$('.nav li.sub').click(function() {
$(this).toggleClass('open-sub-menu');
});
New class is:
.nav li.sub.open-sub-menu ul {
position:relative;
display:block;
}
Thank you so much that truly did the trick. I am a complete jquery/js noob so this was extremely helpful. Somehow it took me 15 minutes to figure out I had not linked to jquery in the head tags.
For those with the same problem as me make sure to put this in between the tags of your html.
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

drop down menu is hardly staying open

This is a frequently addressed problem in SO, but almost 5 of the threads here didn't offer any help. The dropdown menu items are not staying open unless I hover over the initial item many times. I can't locate the problem in a specific class, any help would be appreciated.
Here is the CSS Code:
nav ul {
list-style: none;
}
nav ul li {
display: block;
float: right;
}
nav ul ul {
display: none;
position: relative;
margin-top: 60px;
}
nav ul li:hover > ul {
display: block;
position: absolute;
margin-left: -30px;
}
nav ul ul li {
width: 170px;
float: none;
display: block;
}
#navigation_bar a {
display: block;
float: right;
padding: 5px;
font-size: 25px;
color: black;
}
#navigation_bar a:hover {
border-radius: 20px;
color: #FFFFFF;
transition: color 0.2s;
}
div nav {
display: block;
}
And here is the corresponding HTML code:
<div id="navigation_bar">
<nav>
<ul>
<li>شركتنا</li>
<li>الخدمات</li>
<li>المركز الإعلامي
<ul>
<li>آخر الأخبار</li>
<li>معرض الصور</li>
</ul>
</li>
<li>التحميلات</li>
<li>اتصل بنا</li>
</ul>
</nav>
</div>
It's because there's a bit of a gap between the parent nav and the child dropdown. When you try to hover on the child dropdown and your cursor passes over this gap, you lose the li:hover state hence the dropdown hides.
nav ul ul {
...
margin-top: 43px;
...
}
https://jsfiddle.net/mq29ac39/1/
Now compare this one with the original margin-top: 63px
https://jsfiddle.net/mq29ac39/2/
I added background a color to give a better visual idea about the gap

How can i make a dropdown menu

CSS
.nav {
margin-top: 0px;
margin-left: 0%;
}
.nav ul {
width: 100%;
height: auto;
background-color: #fff;
margin-bottom: 0px;
margin-top: 0px;
display: absolute;
z-index: 2;
/*top: -124px;*/
position: fixed;
border-bottom: 2px #009759 solid;
}
.nav ul li {
margin-top: 5px;
color: white;
list-style-type: none;
display: inline;
padding-left: 15px;
padding-right: 15px;
padding-top: 20px;
padding-bottom: 22px;
border-radius: 0px;
}
.nav ul li a
{
color: #009759;
text-decoration:none;
}
.nav ul li active {
color: #ff0000;
}
What is the best way to make a dropdown menu from one of my header menu buttons? I inlcuded my CSS file for the header but now I need you to tell me how to make a dropdown.
You can make a dropdown in many ways.. the usual method is by making javascript codes that will handle the hover event on the selected menu..
there are many good tutorials online that can help you.. like this dropdown with javascript
but if you are looking for pure css dropdown, you can also look at this link: dropdown pure css
You could use Jquery for this. I have done this before, and while it may seem a bit crude with the code, it works wonderfully.
$(".nav ul, .nav ul li, .nav ul li a").hide();
$(".nav").hover(function(){
$(".nav ul, .nav ul li, .nav ul li a").show(500);
});

responsive tab menu with centered dropdown area

This is what it should look like:
There is a tab-menu and a dropdown area. This should always have the same position (but different content and the respective tab choosen) as in the picture. Meaning it should always be as wide as the tabs(-menu).
But I can not figure out how:
to get this responsive
how to have the dropdown area stay where it is
how to style the subitems (in the dropdown area)
Here is what I got so far (sorry for the huge css it is not cleaned yet!), the menu starts at line 1559.
http://jsfiddle.net/pxpHw/
How do I do this properly?
THANKS!
code:
// css
nav {
cursor: default;
}
a {
text-decoration: none;
color: #000000;
}
#menu ul {
margin: 0px;
padding: 0px;
left: 0;
list-style-type: none;
background:green;
z-index: 100;
max-width: 60em;
}
#menu li {
float: left;
width: 20%;
text-align: center;
}
#menu li ul {
display: none;
/*display: block;*/
padding-top: 3px;
}
#menu li:hover ul {
display: block;
}
#menu li ul li {
background-color: #2F2D49;
border-bottom: 1px solid #FFFFFF;
width: 100%;
max-width: 60em;
min-height: 30em;
position: absolute;
}
#menu li ul li a {
color: #FFFFFF;
}
#menu li ul li:hover {
background-color: #232323;
}
Use media queries to get the responsive design
Check the following links
http://mobile.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website/
http://mediaqueri.es/
Thanks
AB
This is what I was looking for: (Columns and Layout tab)
http://codecanyon.net/item/css3-full-responsive-dropdown-menu/full_screen_preview/4528828

Weird IE7 Hover Bug/Problem

This is my little CSS style:
#languages_menu
{
background: url('/images/langs.png') repeat-y;
bottom: 40px;
font-size: 0.9em;
right: 10px;
position: absolute;
width: 90px;
}
#languages_menu ul, #languages_menu ul li
{
list-style: none;
}
#languages_menu ul li a
{
cursor: pointer;
display: block;
line-height: 22px;
margin-left: 10px;
text-decoration: none;
width: 80px;
}
#languages_menu ul li a:hover
{
background-color: #AEBDD2;
color: #F00;
}
#languages_menu ul li a span.flag
{
float: left;
margin: 3px 5px 0 3px;
}
And this is my HTML code (copied from Developer Toolbar, so don't worry, it's valid):
<!-- DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" -->
...
<DIV id=languages_menu><DIV class=topimg></DIV>
<DIV>
<UL>
<LI><A class=en><SPAN class=flag></SPAN><SPAN class=name>English</SPAN></A></LI>
<LI><A class=fr><SPAN class=flag></SPAN><SPAN class=name>Français</SPAN></A></LI>
...
</UL>
</DIV>
</DIV>
In IE8, IE8Compatibility and IE9, and in every other browser, :HOVER is working like a charm. But if I switch to IE7 or lesser, the anchor is not changing anymore on mouse over.
The worst thing is that I have other similar codes on the same page and they are still working after I switch to IE7, like the following one:
#navigation
{
height: 22px;
position: relative;
width: 100%;
}
#navigation ul
{
float: left;
left: 50%;
}
#navigation ul, #navigation ul li
{
float: left;
list-style: none;
position: relative;
}
#navigation ul li
{
right: 50%;
}
#navigation ul li a
{
color: #889DBF;
display: block;
line-height: 22px;
padding-left: 20px;
text-decoration: none;
}
#navigation ul li a b
{
display: block;
padding-right: 21px;
}
#navigation ul li.current a, #navigation ul li a:hover
{
background: url('/images/navigation-left.png') no-repeat;
color: #111B35;
}
#navigation ul li.current a b, #navigation ul li a:hover b
{
background: url('/images/navigation-right.png') no-repeat 100% 0;
color: #111B35;
}
<DIV id=navigation>
<UL>
<LI class=current><B id=text_menu_login>Accedi</B></LI>
<LI><B id=text_menu_register>Registrati</B></LI>
...
</UL>
</DIV>
Anyone knows why this is happening and how to fix it?
[EDIT]
I just found a fix for this bug.
If i replace:
#languages_menu ul li a:hover
With:
#languages_menu ul li:hover a
The menu works great even with IE lesser than 8. But I don't think it's a good solution for cross-browser compatibility because :hover pseudo-class can not be used in IE lesser than 7.
MANY MANY THANKS ERIC!
you need to add href to your anchor, a:hover is not picked up by IE7 otherwise:
<A class=en href="#">
another trick is to add empty rule in your css:
#languages_menu ul li a:hover {
/* all your hover rules go here */
}
#languages_menu ul li:hover a {
/* keep this empty, triggers a:hover on IE7 */
}
Add a default background-color to your non-hovered link (just use white).