I'm putting together a WP website, and on the menu bar the active menu item is underlined. All of the menu items (li) had a padding-right set on them, but I need to take it off the active element so that the border on the bottom only extends to the end of the text. To make up for it, I need to somehow target the next li but I can't quite figure out how to do it. I'm working in the context of someone else's WP theme which is always a challenge but I think this is probably doable, I just don't know quite how to combine the CSS selectors. Here's the basic menu structure:
<nav id="top-menu">
<ul id="menu-top">
<li id="menu-item">
<a>ITEM 1</a>
</li>
<li id="menu-item current-menu-item">
<a>ITEM 2</a>
</li>
<li id="menu-item">
<a>ITEM 3</a>
</li>
</ul>
</nav>
To target the padding on the element, I have to use #top-menu ul > li#current-menu-item > a
So if I wanted to target the 3rd element in this example, what code would I use? Is this even possible, or am I trying to be too specific? I'm hoping some combination of child and descendant selectors, but I'm not sure how I can make it all work together. Thanks for your help!
Usually in situations like this I resort to a bit of jQuery. With jQuery, you can use next() to get the li after the one you've targeted.
First, you've mixed up ids and classes. You need to be using classes here (ids cannot be repeated):
<nav id="top-menu">
<ul id="menu-top">
<li class="menu-item">
<a>ITEM 1</a>
</li>
<li class="menu-item current-menu-item">
<a>ITEM 2</a>
</li>
<li class="menu-item">
<a>ITEM 3</a>
</li>
</ul>
</nav>
Then this would give you the li after the current one:
$('.current-menu-item').next()
So you could do something like add another class to it (obviously my class name is waaaayyy longer than it really needs to be!):
$('.current-menu-item').next().addClass('the-one-after-the-current-menu-item');
Which you could then style via css.
.the-one-after-the-current-menu-item { // styling here.... }
Yes, the "+" in CSS selects the element after whatever. In this case, something like this:
#current-menu-item + li {
background: red;
}
Although, you may have another problem in that you can't use spaces in IDs. If the IDs in your example were classes instead, you'd be completely fine.
as pointed out by russtuck91, that current-menu-item needs to be a class, then his answer is the one i would implement, it is the cleanest.
however if you cant change that (which you have to as you cant have spaces in id's), you can use :nth-child(x) where x is the number of child elements down:
#top-menu li:nth-child(3) a {
color:red;
}
will still recommend russtuck91's answer though
Related
I am trying to put the BEM naming convention in action but having some confusions about naming the HTML elements. I really want to understand do I really need to provide the class name for each HTML element.
Let's say I have the following code and for example:
<ul class="nav">
<li class="nav__list"><a href="#" class="nav__link>Home</a></li>
<li class="nav__list"><a href="#" class="nav__link>Services</a></li>
</ul>
I don't want to apply CSS to the <li> elements.
So, in that case, do I need to use the element name for the <li> tag. i.e. <li class="nav__list">...</li> ?
Can I just use the element name for the anchor tag without giving element name nav__list to the <li> element?
Here is what I am thinking to do because I don't want to apply styles to the CSS to <li>:
<ul class="nav">
<li><a href="#" class="nav__link>Home</a></li>
<li><a href="#" class="nav__link>Services</a></li>
</ul>
first of all, you should have to follow BEM most of the developer followers BEM only because BEM is good at the naming convention and it's shows the standard naming convention for coding. it depends on you if you want to use BEM you can use or else it's your wish but I suggest you follow BEM it's good in standard.
you can use this
<ul class="nav">
<li class="nav__list"></li>
<li class="nav__list"></li>
</ul>
as well as this one
<ul class="nav">
<li></li>
<li></li>
</ul>
now you don't want to give style to li but in future client say you to give style to li that time what you will do again you will change the code so you have to use this below HTML code
<ul class="nav">
<li class="nav__list"></li>
<li class="nav__list"></li>
</ul>
i have a list
<ul>
<li class="range1">Entry</li>
<li class="range1">Entry</li>
<li class="range1">Entry</li>
<li class="range2">Entry</li>
<li class="range2">Entry</li>
<li class="range2">Entry</li>
</ul>
Now i want to select the last li with the class "range1". Problem is, that this list is dynamic cause of a sql database output, so i cant work with nth-child.
li.range1:last-child
doesnt work. I dont want use Javascript so is it possible to just use CSS?
You cannot apply last-of-type or last-child to a CSS class.
You can either us Javascript or change your HTML a little bit (preferable solution):
<ul class="range">
<li class="range1">Entry</li>
<li class="range1">Entry</li>
<li class="range1 range__last">Entry</li>
<li class="range2">Entry</li>
<li class="range2">Entry</li>
<li class="range2 range__last">Entry</li>
</ul>
last-child works only for the last element of its parent irrespective of the class name differentiation that you specify.
alternatively li.range1:nth-child(3) can be used. otherwise it should have separate container classes for both the lists that u specify
In the code below how to remove the li based on the value of hyperlink?
<ul class="clean menu sub">
<li>
<a cat="tech" class="sec_net_1" href="javascript:void(0);" name="&lid=choose_category_internet_fios" rel="1">FiOS Internet</a>
</li>
<li>
<a cat="tech" class="sec_net_1" href="javascript:void(0);" name="&lid=choose_category_internet_hsi" rel="1">High Speed Internet</a>
</li>
<li class="last" style="margin-bottom:7px;">
<a cat="tech" class="sec_net_1" href="javascript:void(0);" name="&lid=choose_category_internet_dialup" rel="1">Dial-up</a>
</li>
</ul>
I get the valu of the hyperlink in a variable. Now based on the value other 2 li's should be deleted. How can we achieve this using mootools?
Now if I get flag="Dial-up", Other 2 li's should be deleted and the code should look like this:
<ul class="clean menu sub">
<li class="last" style="margin-bottom:7px;">
<a cat="tech" class="sec_net_1" href="javascript:void(0);" name="&lid=choose_category_internet_dialup" rel="1">Dial-up</a>
</li>
</ul>
Here I get the value of Flag from server.
If you can leverage on link names the solution is a single line:
http://jsfiddle.net/5axJc/
$$("ul.menu :not(a[name='&li=choose_category_internet_dialup']) !> li").destroy()
calling destroy on a collection (I would have used document.getElements in production code) will call your method on every elements in it.
In the selection expression there are several esoteric selector and maybe it worth some deeper explanation:
ul.menu is a plain class based selector
:not(a[name='&li=choose_category_internet_dialup'])
select links based on the name attributes that do :not() match the value
!> is a parent selector
hence you can read the selector as:
select all the li into the menu ul that are father of an anchor whose name is NOT your desired value.
EDIT
To select only the li that are parents of the searched one just select it with
ul.sub a[name='&lid=choose_category_internet_fios'] !> li and based on that
select all siblings with ~ li. the result is like this.
$$("ul.sub a[name='&lid=choose_category_internet_fios'] !> li ~ li").dispose()
Note we have get rid of the :not() selector. To me it is a more linear way to accomplish the task.
I have a strange problem with a custom select. I used a simple "ul li ul" to create that select and it expand on focus but if I want to use some links when select is expanded on that hidden li's when I click on them nothing happens and I don't understand why :|
if someone can help me with this.
Fiddle example: http://jsfiddle.net/RwtHn/1128/
The default target for the link(_self) didnt work. (dont know why)
Try with _blank or _parent
<ul id="main">
<li class="username" tabindex="1" > <a>USERNAME</a>
<ul class="curent_buser">
<li>HELP NEEDED</li>
<li><a wicket:id="logoff" href="http://www.youtube.com/watch?v=nOGsB9dORBg" target="_blank">LOG OFF</a></li>
</ul>
</li>
</ul>
I am currently trying to style my navigation bar with different colors for each list item.
Here is the html code
<ul>
<li id="item1">Home </li>
<li id="item2"> About </li>
<li id="item3"> News </li>
<li id="item4"> Video </li>
<li id="item5"> Donate </li>
<li class="part">Contact </li>
</ul>
And here is the css code
#item1{background:#7375D8};
#item2{background:#4E51D8};
#item3{background:#1A1EB2};
#item4{background:#303285};
#item5{background:#080b74};
#header ul li.part
{
background:#689AD3;
}
For some reason only item 1 and li.part are actually displaying the colors correctly , the rest display nothing in all browsers.
Dreamweaver however is displaying all the colors in the preview section so I have no clue what the problem is with them .
Many thanks for your contributions.
It's just a syntax error! The semicolons should be inside the curly braces!
#item1{background-color:#7375D8;}
#item2{background-color:#4E51D8;}
#item3{background-color:#1A1EB2;}
#item4{background-color:#303285;}
#item5{background-color:#080b74;}
#header ul li.part
{
background-color:#689AD3;
}
You have not closed the semicolons properly, it was outside the braces so thats the reason it was not working. See the css rules below for correct one.
Write your css like below:
#item1{background:#7375D8;}
#item2{background:#4E51D8;}
#item3{background:#1A1EB2;}
#item4{background:#303285;}
#item5{background:#080b74;}
#header ul li.part{background:#689AD3;}
the semi colons should be before the closing brace for each style. the unclosed tags are causing the problem