I wonder if someone can help me or point me in the right direction so I can resolve my problem.
I have a list which has got class assigned
<ul class="link-list nav">
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
</ul>
The link-list nav looks as below:
.link-list {
display: inline-block;
list-style: none;
margin: 0;
padding: 0;
font-size: 12px;
}
.link-list > li {
display: inline-block;
}
.link-list > li > a {
display: block;
padding: .9em 1.6em;
/*margin: top right bottom left;
border: 1px solid white;*/
text-decoration: none;
color: white;
}
.link-list > li > a:hover { /* :hover, :focus, :blur */
color: pink;
}
.link-list.nav > li > a {
font-weight: bold;
}
So now the list is being formatted as it should be
Correct colors:
but when I am trying to make the first item of the list bigger by embedding span tags and changing above code to:
<ul class="link-list nav">
<li><span style="font-size: large">Menu 1</span></li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
</ul>
The font become bigger but also all other formatting disappear
Incorrect colors:
I have been experimenting with defining another class which would inherit from link-list nav but unsuccessfully. Any suggestions please?
Thanks in advance,
Regards Tom.
you can put the span as child of a given your CSS (you are using the direct child selector >).
or
you can just use CSS selector first-child in li
or
or simply removing the >, making it a child (not direct one).
.link-list {
display: inline-block;
list-style: none;
margin: 0;
padding: 0;
font-size: 12px;
}
.link-list > li {
display: inline-block;
}
.link-list > li > a {
display: block;
padding: .9em 1.6em;
/*margin: top right bottom left;
border: 1px solid white;*/
text-decoration: none;
color: red;
}
.link-list > li > a:hover {
/* :hover, :focus, :blur */
color: pink;
}
.link-list.nav > li > a {
font-weight: 700;
}
/* use this */
.link-list.nav > li > a span {
font-size: 20px
}
/* or use this */
.link-list.nav > li:first-child > a {
color: blue
}
<ul class="link-list nav">
<li><span>Menu 1</span>
</li>
<li>Menu 2
</li>
<li>Menu 3
</li>
<li>Menu 4
</li>
</ul>
The selectors no longer match because the <a> is no longer a child of the <li>.
Use a descendant combinator (a space) instead of a child combinator (a >).
Alternatively, put the span inside the innermost element so that it doesn't disrupt the child combinators.
Alternatively, and probably the best option, don't add an extra element at all. You want to style all the text there, so apply your CSS to the whole element.
Thank you Quentin and dippas for your swift responses.
Out of both solutions, I have gone for creating:
.link-list.nav > li > a span {
font-size: 20px
}
and changing
<li><span>Menu 1</span>
to
<li><span>Menu 1</span>
This resolved my problem completely.
Clearly I need to reach out to articles about inheriting.
Thanks again and take care guys!
Related
I've written the folloning code:
<ul>
<li>Text</li>
<li>text</li>
</ul>
and styles:
list-style-type: none;
padding: 5px;
display: inline;
background-color: #A9A9A9;
But i have spacing between two li elements like the following:
How can I remove this spacing?
By put them inline
<ul>
<li>Text</li><li>text</li>
</ul>
Js Fiddle Demo
If you float your li items, it should remove the margin between li output.
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
ul {
list-style-type: none;
}
ul li {
float:left;
padding: 5px;
display: block;
background-color: #A9A9A9;
}
Here are two common ways to avoid the space:
<ul>
<li>
one</li><li> <!-- use this to avoid the linebreak -->
two</li><li>
three</li>
</ul>
Or you can use Comments:
<ul>
<li>one</li><!--
--><li>two</li><!-- Comments so there is no white-space
--><li>three</li>
</ul>
You can check it in this Demo
You get the space because there is some space between the elements.
(Tabs, Newline count as space ). With this Minimized HTML it should work :)
You can read more about it here Examples at CSS-Tricks
try floats and use list-style-type for ul:
ul {
list-style-type: none;
}
li {
padding: 5px;
float:left;
background-color: #A9A9A9;
}
Just Add float:left in your Css
ul li {
background-color: #A9A9A9;
display: inline;
float: left;
padding: 5px;
}
Demo
There are many ways as mentioned above few of them..however you can achieve with another method. using font-size:0
ul
{font-size:0; /*This will remove the space totally*/
list-style-type: none;
}
li{
padding: 5px;
display: inline;
background-color: #A9A9A9;
font-size:16px; /*This is important line so the font come again in same shape*/
}
Here is the Demo.
I created a navigation bar at for my website using an in-line list and then it has been styled. Each <li> is exactly the same but I want the last one to have a different size as i wish to change the width and padding of it.
I have no idea how I am able to do this, I've tried multiple ways but experienced lots of problems along the way. I tried adding styling in the <li> tag on the HTML page, but it changed absolutely nothing, I then tried using the last-child selector which worked to an extent. It allowed me to change the padding of it but not width. But it didn't just change it for the last one but also the first one.
CSS:
.dropdown{
position: relative;
margin: 0 auto;
float: right;
top: 20px;
font-size: 13px;
}
.dropdown li {
float: left;
width: 155px;
background-color:#373737;
position: relative;
border-bottom:1px solid #575757;
border-top:1px solid #797979;
}
.dropdown li a {
display: block;
padding: 10px 8px;
color: #fff;
position: relative;
z-index: 2000;
text-align:center;
}
.dropdown li a:hover,
.dropdown li a.hover{
background: #CF5C3F;
position: relative;
}
.dropdown :last-child li a{
padding: 0px;
width: 40px;
}
HTML
<ul class="dropdown">
<li><a id="page1" href="index.html">Home</a></li>
<li>Internet Architecture
<ul class="sub_menu">
<li>Item Two</li>
<li>Item Three</li>
<li>Item Four</li>
<li>Item Four</li>
</ul>
</li>
<li>Internet Security
<ul class="sub_menu">
<li>Laws</li>
<li>Security Risks</li>
</ul>
<li>Internet Security
<ul class="sub_menu">
<li>Laws</li>
<li>Security Risks</li>
</ul>
</li>
<li>Item One
<ul class="sub_menu">
<li>Item Two</li>
<li>Item Three</li>
<li>Item Four</li>
</ul>
</li>
<li><img src="images/contact_white.png" width="30px" height="auto"></li>
</ul>
Does anyone have any idea on how to fix this?
I want the last one to have a different size as i wish to change the width and padding of it.
So if you meant last child of level 1 than use
ul.dropdown > li:last-child {
/* Target */
}
And if you meant each last child of li on 2nd level ul, than use
ul.dropdown > li > ul > li:last-child {
/* Target */
}
Demo
Demo (Just more elements, nothing fancy)
I may be missing something here, but if I understand your question right it's as simple as giving the <li> you want to be the odd one out an id, and then using css to change li#myId..
This is the first time I am working with CSS hover in this way. But it does not work.
I am trying to show a navigation cloud when the user hovers a item in the navigation.
This is my current CSS
.menuBox li a:hover,.menuBox li.selected a { color: #fff; background-color: #068dda; }
.menuBox .cloudnav { display: none; position: absolute; width: 995px; height: 500px; background-color: #fff; z-index: 999999; border: 1px solid #4a4d4b;}
.menuBox li a:hover>.cloudnav { display: block; }
This is my current html
<nav class="menuBox">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<div class="cloudnav">test</div>
</nav>
I even made a jsfiddle for you guys: http://jsfiddle.net/tAgEj/
I also tried .menuBox li a:hover + .cloudnav { display: block; } but that didn't work either.
What does?
It won't work with the a:hover. But in newer browsers you can work it out like this
.menuBox ul:hover + .cloudnav { display: block; }
But this will only work in newer browsers, because many IE's don't support hover for something else than <a>
.menuBox li a:hover>.cloudnav { display: block; } this won't work because the div.cloudnav isn't a direct child of the a element. This would work if it would be
something<div class="cloudnav"></div>
I have this CSS:
#menuBar {
/*width: 960px;*/
height: 35px;
clear: both;
}
ul#nav {
float: left;
width: 100%;
margin: 0;
padding: 0;
list-style: none;
background-color: #0068b4;
}
ul#nav li {
display: inline;
}
ul#nav li a {
float: left;
line-height: 35px;
color: #ffffff;
text-decoration: none;
margin: 0;
padding: 0 30px;
background-color: #0068b4;
}
/* APPLIES THE ACTIVE STATE TO PARENT*/
ul#nav .currentParent a, ul#nav li:hover > a {
color: #ffffff;
text-decoration: none;
background: #005899;
}
/* THE SUBMENU LIST HIDDEN BY DEFAULT */
ul#nav ul {
display: none;
}
/* WHEN THE FIRST LEVEL MENU ITEM IS HOVERED, THE CHILD MENU APPEARS */
ul#nav li:hover > ul {
position: absolute;
display: block;
width: 97%;
height: 35px;
margin: 35px 0 0 0;
background-color: #e0e0e0;
}
ul#nav li:hover > ul li a {
float: left;
line-height: 35px;
color: #000000;
text-decoration: none;
margin: 0;
padding: 0 30px 0 30px;
background-color: #e0e0e0;
}
/* APPLIES THE ACTIVE STATE TO CHILD*/
ul#nav li ul li.currentChild a, ul#nav li:hover > ul li a:hover {
color: #000000;
text-decoration: none;
background-color: #afafaf;
}
And this HTML:
<div id="menuBar">
<ul id="nav">
<li>Menu 1
<ul>
<li>Menu 1 Submenu item 1</li>
<li>Menu 1 Submenu item 2</li>
<li>Menu 1 Submenu item 3</li>
</ul>
</li>
<li>Menu 2
<ul>
<li>Menu 2 Submenu item 1</li>
<li>Menu 2 Submenu item 2</li>
</ul>
</li>
<li class="currentParent">Menu 3
<ul>
<li>Menu 3 Submenu item 1</li>
<li class="currentChild">Menu 3 Submenu item 2</li>
<li>Menu 3 Submenu item 3</li>
</ul>
</li>
</ul>
</div>
I'm using this to display a two level horizontal menu. What I want to achieve is that when an option from the second level is clicked I want the parent and the children itself to remain highlighted. To do this I'm actually building the HTML for the menu in the code behind of my ASP.net page and using the styles: currentParent and currentChild to highlight them. That's working, what I've not been able to figure out is how to make the second level that contains the child that was clicked to stay visible. I've tried by adding display: none in a lot of places, but nothing is working. I'm wondering if somebody can help me with this and give some advise of the modifications I need to add to the CSS to achieve this?
EDIT 1:
I've completed the code and reviewed that it's working (you can see it here: http://jsfiddle.net/cesarvinas/ZQWe7/). You can see in the HTML code that I've applied the class current Parent to parent menu Menu 3 and the class currentChild to submenu item Menu 3 Submenu item 2. With this, the parent and child are now highlighted. What I want in addition is that the row that contains the submenu items (the gray one): Menu 3 Submenu item 1, Menu 3 Submenu item 2, and Menu 3 Submenu item 3 remains visible.
As I explained in the main post, I have added code to the code behind of the ASP .net page to build the HTML up and assign the CSS classes when necessary.
Is there a way to have a CSS class that I can assign to the parent <li> (in the example, the Menu 3 parent) to keep its children visible?
EDIT 2
Thanks to the help I've received here, specially from 3dgoo, this is almost done. I've updated the example here: http://jsfiddle.net/cesarvinas/ZQWe7/5/. However, to make the submenu items that are not current to remain silver (and not blue as in 3dgoo example) I've had to create one more CSS class ".notselected" that I'm applying to those submenu items that are not selected. My question is: is there a way to achieve the same without having to add an extra CSS class? This is what I've done:
ul#nav li ul li.notselected a,
ul#nav li:hover > ul li a {
float: left;
line-height: 35px;
color: #000000;
text-decoration: none;
margin: 0;
padding: 0 30px 0 30px;
background-color: #e0e0e0;
}
and the HTML:
<li class="currentParent">Menu 3
<ul>
<li class="notselected">Menu 3 Submenu item 1</li>
<li class="currentChild">Menu 3 Submenu item 2</li>
<li class="notselected">Menu 3 Submenu item 3</li>
</ul>
</li>
I've tried with this in the CSS:
ul#nav li ul li a,
ul#nav li:hover > ul li a {
float: left;
line-height: 35px;
color: #000000;
text-decoration: none;
margin: 0;
padding: 0 30px 0 30px;
background-color: #e0e0e0;
}
and removing the class="notselected" from the HTML, but it doesn't work. Why is that?
Thanks.
You can do this by using the following css selector ul#nav li.currentParent > ul like so:
CSS
...
ul#nav li.currentParent > ul, // Add this
ul#nav li:hover > ul {
position: absolute;
display: block;
width: 97%;
height: 35px;
margin: 35px 0 0 0;
background-color: #e0e0e0;
}
ul#nav li:hover > ul { // And add these three lines
z-index: 10;
}
...
Demo
So, you want the clicked item from the second row to be highlighted and the appropriate second row visible after clicking it.
If the menu will use standard links (page refreshing on each click) perhaps it would be the best to use an ASP variable to denote the current page and according to that variable make CSS modifications to make the correct submenu visible and the correct item highlighted (you can start from 3dgoo's CSS example selector ul#nav li.currentParent > ul).
The second case is that you're not refreshing the page on clicks (either using AJAX to load only parts of the page, for example). In that case, you'd have to use Javascript/jQuery to listen for clicks and change classes accordingly. This means that when you click an item, its parent li element needs to become the only currentChild class and its parent currentParent, of course.
EDIT
You can select all the currentParent submenu anchors like this: ul#nav li.currentParent ul li a and then add hover/active effects on currentChild after that.
I'm currently trying to create a drop-down menu from nested unordered lists. I have the menu working however I'm having some issues with regards to styling. The overall link that triggers the drop-down is clickable and needs to have a blue background with white text however, the drop-down elements need to have a grey background which is inherited from the overall navigation container. All I need to do is modify the text colour however whatever I method I try it always modifies the drop-down text colour as well as the heading link colour.
My CSS can be found below along with an example of the current display and the html used to generate the menu:
/*CSS*/
#coolMenu,
#coolMenu ul {
list-style: none;
}
#coolMenu {
float: right;
}
#coolMenu > li {
float: left;
}
#coolMenu li a {
display: block;
/*height: 2em;
line-height: 2em;
*/
/*padding: 0 1.5em;*/
text-decoration: none;
color: #ffffff;
}
#coolMenu ul {
position: absolute;
display: none;
z-index: 999;
}
#coolMenu li:hover ul {
display: block;
}
.dropdown a li{
color: #124162 !important;
}
#style_me > li > a{
color: #124162 !important;
}
/HTML/
<nav id="navigation" class="navigation">
<ul>
<li class="current">Home</li>
<li>Who Are We?</li>
<li>Why Join Us?</li>
<li>Contact Us</li>
</ul>
/* This is the menu element that needs styling */
<ul id="coolMenu">
/* THis should be blue background white text */
<li>Login / Register
<ul id="style_me">
/* These should be grey background blue text */
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
<li>Link 6</li>
<li>Link 7</li>
</ul>
</li>
</ul>
</nav>
Any help would be greatly appreciated. It's been a couple of years since I've had to do any CSS and my memory is a bit flakey!
This should do what you want:
#style_me li a {
color: #124162 !important;
}
(just spaces instead of >) And, perhaps, you won't need that !important.
Update: try even more specific CSS selector if what you posted is being overridden.
#coolMenu li #style_me li a {
color: #124162 !important;
}