CSS inheritance : ul style not applied to nested div - html

I ran into a problem trying to build a dropdown menu in a navbar, using nested lists.
Here's a snippet of the code :
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
}
.dropdown:hover + .dropdown-content {
display: block;
}
.dropdown-content a {
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
<nav class="navbar navbar-default" role="navigation" data-600='opacity:0' data-1000='opacity:1'>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul id=navbar class="nav navbar-nav">
<li class="dropdown"><a href=index.html>Jeux de vilains ▼</a></li>
<div class="dropdown-content">
<ul class="nav navbar-nav navbar-right">
<li class=secondary><a href=/carto2016-jeuxvideos/article-1.html>Tomb Raider</a></li>
<li class=secondary><a href=/carto2016-jeuxvideos/article-2.html>GTA</a></li>
<li class=secondary><a href=/carto2016-jeuxvideos/article-3.html>SimCity</a></li>
<li class=secondary><a href=/carto2016-jeuxvideos/article-4.html>Call of Duty</a></li>
<li class=secondary><a href=/carto2016-jeuxvideos/article-5.html>Resident Evil 5</a></li>
<li class=secondary><a href=/carto2016-jeuxvideos/article-6.html>Civilization</a></li>
</ul>
</div>
</ul>
</div>
This part was solved :
Basically, my problem is that I want my "dropdown" div to have no
style of its own, but to inherit the style of the navbar.
The weird thing is that the nested navbar is properly styled, but the
dropdown div follows the style of general a and hover:a elements.
If you look at the beginning of my HTML, you'll see the culprit: <div
class="dropdown">, which appears completely differently from the
rest. I don't understand why the class in which it is nested (the nav
class) is not considered the parent, especially since the following
list is properly styled.
I attached a screenshot of what it looks like: "Jeux de vilains"
looks weird, when it should look like "Règles du jeu".
This has been solved too
Well thank you all for you swift and kind help, I took out the
unwanted div and put the dropdown class in the corresponding li.
However, while the list does appear on hover, it disappears as soon as
the mouse is not hovered. I'd like to make it so the secondary menu
appears as long as either the parent li or the secondary menu itself
are hovered. How could I do that ?
Here's the website :
http://medialab.github.io/carto2016-jeuxvideos/index.html
Thanks in advance for everyone's help.
Question solved.

Your first instinct would be to use JavaScript but for this but there's a slick way to accomplish this with what are essentially operators in CSS...
Use the :hover pseudo-class on a parent element and look for the instance of the dropdown.
.nav:hover .dropdown-content {
display: block;
}
Because you're selecting the parent element, it applies to both the dropdown and what you hover over to show it over so it's displayed whenever it's within those realms. Make more and they'll work just the same respective to their individual dropdowns.
https://jsfiddle.net/ksxk33w4/1/

Related

Add icons to list with ::before

I'm working with Bootstrap. I'm trying to add an image as number icon to my list. It is kind of a bad practice to add <div> tag inside <ul> tag, so is it possible to add icon with ::before?
Here's my code:
<ul class="lizt" id="sub-lizt">
<li class="item">1. Bunch of geebrish</li>
<li class="item">2. Bunch of geebrish</li >
</ul>
ul {
list-style-image: url('/your_img_path.jpg');
}
You can use this in your CSS. For in-depth just take a look Here.
Putting a div inside an li tag is not a bad practice. It would be wrong to put a div directly inside a list ol/ul, but not inside a list item.
That being said, you don't need a div to put a numbered image as bullet for your list, because, in fact, list-style property allows using an image as bullet.
.item {
list-style: url('http://via.placeholder.com/32x32/000/fff?text=1');
}
.item:nth-child(2) {
list-style: url('http://via.placeholder.com/32x32/000/fff?text=2');
}
.item:nth-child(3) {
list-style: url('http://via.placeholder.com/32x32/000/fff?text=3');
}
<ol class="lizt" id="sub-lizt">
<li class="item">Bunch of geebrish</li>
<li class="item">Bunch of geebrish</li >
<li class="item">Bunch of geebrish</li >
</ol>
You'd probably want your images vertically aligned in the middle with your text. In that case, you have many options, like moving up your a elements from its relative position.
.item a {
display: inline-block;
position: relative;
top: -0.75em;
}

Stretching the component in CSS to the full page width

I have the following code: https://jsfiddle.net/u8db2j75/1/ and it works fine, I have the effect I wanted - a picture and some text next to it. But now I want to add another component, a navigation bar - and I want to add it on top of the page. So what I followed the example given here http://css-snippets.com/simple-horizontal-navigation/ and I created the code like this:
<div class="nav">
<ul>
<li class="home">Home</li>
<li class="tutorials"><a class="active" href="#">Tutorials</a></li>
<li class="about">About</li>
<li class="news">Newsletter</li>
<li class="contact">Contact</li>
</ul>
</div>
https://jsfiddle.net/u8db2j75/2/ however, after modifying css as well -as you can see - the effect is far from what I expected... What did go wrong here?
Give your .nav ul and .nav a min-width of 100%.
Example:
.nav {
min-width:100% !important;
}
.nav ul {
list-style: none;
background-color: #444;
text-align: center;
position: absolute;
top: 0;
padding: 0;
margin: 0;
min-width: 100%;
}
https://jsfiddle.net/u8db2j75/4/
I don't have 50 reputation to comment the answer above, but:
List item needs to be displayed inline, or floated to the left so the result will be a horizontal navigation as per the examble shown in the issue.

Why can I not force ul to behave as a table-row?

I am using bootstrap3, and I would like to have a navbar, where all nav items are spaced through the whole width of the bar.
To achieve this my only option was to make the bar behave like a table, -row, respectively -cell.
I've got the right markup & css, but apparently somehow I cannot force display: table-row; on my ul.
Live Example
Odd thing is that:
Both firefox & chromium report that although table-row is the most important rule, they apply block
This only happens when I include bootstrap3 (try toggling it in the live example)
HTML
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="">
Homeen
</li>
<li class="">
News
</li>
</ul>
</div>
CSS
.collapse.navbar-collapse {
display: table !important;
width: 100%;
}
.navbar .nav {
display: table-row !important;
width: 100%;
background: #fff;
}
.navbar .nav > li {
display: table-cell !important;
background: #39f;
float: none;
}
Just make it a table man.
All the crazy code you're going to have to write to make this work without any browser bugs is going to be far more ugly than a table tag.
I've figured it out
I just had to unfloat .nav
.nav {
float: none;
}
Working example
Sigh, another lost hour. But I will leave the question up, maybe it saves someone's time.

Better method than float:right in navigation bar?

I have made a horizontal navigation bar using styles, but I've encountered a major issue... Since <li> is a block element, I can't align it using text-align:right, which makes me unable to align it properly. I've tried using the display:inline; syntax for the list-item element, but that doesn't make any difference either (which makes sense actually).
My question being, is there any way of aligning horizontal <li>, without having to use float:right;? I want it to fit the current list's format (which I've adjusted to fit a parent div), and using float isn't really a good or safe method. Here's a screenshot of what I got so far (layout is slightly messed up due to recent addition of image). As you can see, I have managed to get the "My page" and "Log out" properly placed, but as soon as I add something more "complex" (like the "+", which now is placed in the normal list), it gets screwed up... I really don't get how other websites manages to get this right.
You must define text-align: right for the containing element
HTML:
<ul class="nav">
<li class="menu">1</li>
<li class="menu">2</li>
<li class="menu">3</li>
<li class="menu">4</li>
<li class="menu">5</li>
</ul>
CSS:
.nav {
text-align: right;
}
.menu {
display: inline;
}
JSFiddle
You can split the menu to a left and right part, if you like. Add or remove padding and margin as needed
HTML:
<ul class="nav left-nav">
<li class="menu">1</li>
<li class="menu">2</li>
<li class="menu">3</li>
</ul>
<ul class="nav right-nav">
<li class="menu">4</li>
<li class="menu">5</li>
</ul>
CSS:
.nav {
margin: 0;
padding: 0;
}
.left-nav {
text-align: left;
}
.right-nav {
text-align: right;
}
.menu {
display: inline;
}
JSFiddle
Here you go i think this is what you are looking for:
jsfiddle.net/Sdw5h/

parallelogram menu

I've stumbled upon a problem here. I created a menubar which consists several clickable parallelogram menu-items with list and link tags.
Unfortunately, the menubar is showing a problem now. Whenever I try to hover the overlapping part of menu-item, it will automatically choose the next-right menu-item. For example, when I tried to click the pointy part of HOME (which is overlapping with menu-item ABOUT), the menu-item ABOUT would be chosen instead.
I want all of area of each menu-item clickable respectively without any interference from neighboring menu-items. Any helps and suggestions please?
http://i.stack.imgur.com/NDKNF.png
http://i.stack.imgur.com/gnhzt.png
Here's the HTML code:
<ul class="header" id="navlist">
<li id="home"></li>
<li id="about"></li>
<li id="benchmark"></li>
<li id="service"></li>
<li id="work"></li>
<li id="client"></li>
<li id="contact"></li>
</ul>
Here's the CSS:
#navlist {
position:relative;
}
#navlist li {
margin:0;padding:0;list-style:none;position:absolute;top:0;
}
#navlist li,
#navlist a {
height:31px;display:block;
}
#home {
left:0px;
width:112px;
background:URL('../headbar/navmap.png') 0px 0px;
}
#home a:hover {
background:URL('../headbar/navmap.png') 0px -31px;
}
and for another menu-item such as about, client, and others follow the same rule as home.