I have a structure like the following:
<div class="navigation_sub_item_background" id="sub_nav_2">
<div class="navigation_sub_item" id="2_1">
<a class="navigation__sub__link" href="?p=2_1">
<span>
Test 3
</span>
</a>
</div>
<div class="navigation_sub_item" id="2_2">
<a class="navigation__sub__link" href="?p=2_2">
<span>
Test 4
</span>
</a>
</div>
<div class="navigation_sub_item" id="2_3">
<a class="navigation__sub__link" href="?p=2_3">
<span>
Test 5
</span>
</a>
</div>
<div class="navigation_sub_item" id="2_4">
<a class="navigation__sub__link" href="?p=2_4">
<span>
Test 6
</span>
</a>
</div>
</div>
And I want to select only the first link and the last link. I tried div.navigation_sub_item > a.navigation__sub__link:first-of-type but this selects all of the links.
Anyone a suggestion?
Selecting the first and last link
As your links are wrapped in div elements you'll need to select the first and last div and target the first or last a element within those:
.navigation_sub_item:first-child a:first-of-type {
/* The very first link. */
}
.navigation_sub_item:last-child a:last-of-type {
/* The very last link. */
}
Note that I've specified the first-of-type and last-of-type on the a elements themselves just in case more than one a element exists within your div elements. You may want to replace these with first-child and last-child if you do not care about other elements coming before or after them.
A special case for where only one element is present
I want them all, thats cool. But what should I do if there is only one .navigation__sub__link-element? then the :last-child rule will be applied but I want another rule to be used.
In that case you can override the specificity of these selectors by specifying both first-child and last-child:
.navigation_sub_item:first-child:last-child a {
/* This will override the above selectors. */
}
Or if you're not specifying :first-of-type or :last-of-type on your a elements, you could equally use the :only-child selector:
.navigation_sub_item:only-child a {
/* This will also override the above selectors. */
}
This should work.
.navigation_sub_item:first-child .navigation__sub__link {}
.navigation_sub_item:last-child .navigation__sub__link {}
Try to this demo example here used to first-child, last-child
.navigation_sub_item:first-child .navigation__sub__link,
.navigation_sub_item:last-child .navigation__sub__link{
// here style
}
.navigation_sub_item:first-child .navigation__sub__link{
color:red;
}
.navigation_sub_item:last-child .navigation__sub__link{
color:green;
}
<div class="navigation_sub_item_background" id="sub_nav_2">
<div class="navigation_sub_item" id="2_1">
<a class="navigation__sub__link" href="?p=2_1">
<span>
Test 3
</span>
</a>
</div>
<div class="navigation_sub_item" id="2_2">
<a class="navigation__sub__link" href="?p=2_2">
<span>
Test 4
</span>
</a>
</div>
<div class="navigation_sub_item" id="2_3">
<a class="navigation__sub__link" href="?p=2_3">
<span>
Test 5
</span>
</a>
</div>
<div class="navigation_sub_item" id="2_4">
<a class="navigation__sub__link" href="?p=2_4">
<span>
Test 6
</span>
</a>
</div>
</div>
You can select the first and last child in the following way
.navigation_sub_item:first-child .navigation__sub__link,
.navigation_sub_item:last-child .navigation__sub__link {
/*CSS rules*/
}
You can read more about CSS3 first-child and last-child selectors
by clicking on the links.
You should use :first-child and :last-child
.navigation_sub_item:first-child a,
.navigation_sub_item:last-child a {
color: red;
}
DEMO
Try this:
.navigation_sub_item:first-child .navigation__sub__link
and
.navigation_sub_item:last-child .navigation__sub__link
to cover your comment consider this:
.navigation_sub_item:first-child .navigation__sub__link{
font-weight: bold
}
.navigation_sub_item:last-child .navigation__sub__link{
color: blue
}
<div class="navigation_sub_item_background" id="sub_nav_2">
<div class="navigation_sub_item" id="2_4">
<a class="navigation__sub__link" href="?p=2_4">Test 6</a>
</div>
</div>
i think the best method and working for me
.div-selecter a:last-of-type { color:red;}
Related
Here are two potential situations in my HTML:
<div class="ptb_sold">
<span class="ptb_one_line">Sold</span>
</div>
<div class="ptb_link_button">
<a class="ptb_link_button" target="_blank" href="https://test.html">Click to Register</a>
</div>
<div class="ptb_sold"></div>
<div class="ptb_link_button">
<a class="ptb_link_button" target="_blank" href="https://test.html">Click to Register</a>
</div>
My goal is to hide "ptb_link_button" in the first one, but allow the "ptb_link_button" div to display in the second example. Basically, if the item is sold, I do not want to display the "Register" button.
I initially thought this would work:
div.ptb_sold:empty+.ptb_link_button {
display: none;
}
<div class="ptb_sold">
<span class="ptb_one_line">Sold</span>
</div>
<div class="ptb_link_button">
<a class="ptb_link_button" target="_blank" href="https://test.html">Click to Register - Link one</a>
</div>
<div class="ptb_sold"></div>
<div class="ptb_link_button">
<a class="ptb_link_button" target="_blank" href="https://test.html">Click to Register - Link two</a>
</div>
But unfortunately it does not. Any other suggestions?
If I understood you correctly, then I think this would be the approach:
// Hides the button if sold
div.ptb_sold + .ptb_link_button a.ptb_link_button {
display: none;
}
// Show the button if not sold
div.ptb_sold:empty + .ptb_link_button a.ptb_link_button {
display: block;
}
Example - https://jsfiddle.net/4o1gvnjx/
Also, I would consider changing the class name of the container for the button to be something like 'ptb_link_button-container' to avoid confusion. That way, the CSS selector would be more streamlined.
How to hide with CSS every <div class="rpwwt-post-excerpt"> in my UL/LI list except first one, but to keep all <span class="rpwwt-post-title">
<div id="rpwwt-recent-posts-widget-with-thumbnails-2" class="rpwwt-widget">
<h4 class="widget-title penci-border-arrow"><span class="inner-arrow">WIDGET TITLE</span></h4>
<ul>
<li>
<span class="rpwwt-post-title">TITLE #1</span>
<div class="rpwwt-post-excerpt">POST EXCERPT TO SHOW</div>
</li>
<li>
<span class="rpwwt-post-title">TITLE #2</span>
<div class="rpwwt-post-excerpt">POST EXCERPT TO HIDE</div>
</li>
<li>
<span class="rpwwt-post-title">TITLE #3</span>
<div class="rpwwt-post-excerpt">POST EXCERPT TO HIDE</div>
</li>
<li>
<span class="rpwwt-post-title">TITLE #4</span>
<div class="rpwwt-post-excerpt">POST EXCERPT TO HIDE</div>
</li>
</ul>
</div>
Inside your selector use the :not(:first-child) pseudo-class on the ancestor li to exclude the first <li> in a list from matching the selector - even though the style-rule ultimately affects only div.rpwwt-post-excerpt elements.
Like so:
li:not(:first-child) div.rpwwt-post-excerpt {
display: none;
}
Just select li is not first-child.
use :not for is not , and use first-child to select first element.
so , ul li:not(:first-child) { display:none; }
I've got a menu with 3 levels of deepness. It starts with the categories, then the subcategories, and after all, the final links. Some of these links are already in the second or even the first level, but that's not a problem. The menu is working fine.
The problem is that I'm trying to make it look fancy, so I added to each div a class that designates the menu level. You can see the full Angular template here. Mind that these classes are the "lvl0", "lvl1", "lvl2":
<div class="menu-container">
<div class="row header">
<img class="logo" src="../../../assets/menu-header.PNG">
</div>
<div class="row menu-btn">
<div class="inner-menu-btn" (click)="openMenu()">
<span class="menu-span" [#menuStringAnim]="active">MENU</span>
<i class="fa fa-bars menu-icon" [#menuIconAnim]="active"></i>
</div>
</div>
</div>
<div class="menu-list" [#menuListAnim]="active">
<div class="row row-fix lvl0" *ngFor="let category of getCategories()" (click)="openCategory(category)">
<div class="little-menu-bar-toplvl" *ngIf="categoriesNavigator.lvl0 == category.key"></div>
<span class="menu-top-level">{{ category?.title?.toUpperCase() }} </span>
<div *ngIf="categoriesNavigator.lvl0 == category.key">
<br>
<div class="row row-fix lvl1" *ngFor="let subcategory of getSubcategories(category.key)" (click)="openSubcategory(subcategory)">
<div class="little-menu-bar-midlvl"></div>
<span class="menu-second-level">{{ subcategory?.title?.toUpperCase() }} </span>
<div *ngIf="categoriesNavigator.lvl1 == subcategory.key">
<br>
<div class="row row-fix lvl2" *ngFor="let thirdLevel of getThirdLevel(category.key, subcategory.key)" (click)="openUrl(thirdLevel)">
<div class="little-menu-bar-lowlvl" *ngIf="categoriesNavigator.lvl0 == category.key"></div>
<span class="menu-third-level">{{ thirdLevel?.title?.toUpperCase() }} </span>
</div>
</div>
</div>
</div>
</div>
</div>
So these classes are very simple. I'm not very good at CSS (I prefer designing logic rather than designing), and maybe I'm doing some stupid thing here:
.lvl0 :hover{
color: orange;
}
.lvl1 :hover{
color: orange;
}
.lvl2 :hover{
color: orange;
clear: both;
}
So the behavior works nice for first level, but as you can see, all the rows with the second level get highlighted instead of just the one I'm hovering on:
Same happens with the third level.
Do you have any idea on what I'm doing wrong? I'm adding the Angular tag just in case it has something to do with my template code. Thank you!
The problem is that you have applied the style to your div and as the divs are nested, the styles will cascade and turn everything inside it the colour - you can try to apply the styles directly to the spans to avoid this. Also I have removed the space before your hover colon
.lvl0:hover>span { /* leave hover on div but style the span */
color: orange;
}
.lvl1:hover>span {
color: red;
}
.lvl2:hover>span {
color: green;
}
<div class="lvl0">
<span>test 0</span>
<div class="lvl1">
<span>test 1</span>
<div class="lvl2">
<span>test 2</span>
</div>
</div>
</div>
The :hover is basically propagating down to other levels. Do not use CSS on the parent directly. Instead, use it on something like span etc.
Check pen here to solve your issue. In your case, you can have <div> tag too instead of the span which closes there and is basically a sibling of next level.
.lvl:hover {
//common for all
color: orange;
}
I need the style to alternate when classes are nested in a repeating pattern. Sadly right now, all the code does is override based on the order of the CSS and not the order of the HTML.
In the example below, each word needs to be the color it names. This needs to work for an indefinite number of nestings (could be a crazy huge number of nestings), and also needs to work when other CSS styles are applied, which means that the HTML cannot be changed. Also, there is no guarantee that there won't be anything between those elements which means they won't be direct parents all the time (So the > selector will not work).
Anyone know how to do this? (Or if it is even possible?)
span {
color: black;
}
.foo a {
color: red;
}
.bar a {
color: green;
}
<html>
<body>
<span class="foo">
<span class="bar">
<span class="foo">
<span class="bar">
<span class="foo">
<span class="bar">
<strong>black green</strong>
</span>
<strong><br>black red</strong>
</span>
<strong><br>black green</strong>
</span>
<strong><br>black red</strong>
</span>
<strong><br>black green</strong>
</span>
<strong><br>black red</strong>
</span>
</body>
</html>
By putting class within a class, the proper way of calling it in CSS is through the > operator.
If the CSS is as such ( see fiddle or below), there will be a yellow and green element. This is because the CSS is only at parent/first level. I put a new line of CSS below and you see 4 red elements because it only reached until the second level CSS. The rest will follow the parent elements because they do not have any style. Therefore the closest parent that have a style defined in CSS is .bar > .foo a, resulting in red for the remaining 3 elements.
span {
color: black;
}
.foo a {
color: yellow;
}
.bar a {
color: green;
}
.bar > .foo a {
color: red;
}
Html code:
<span class="foo">
<span class="bar">
<span class="foo">
<span class="bar">
<span class="foo">
<span class="bar">
<strong>1black green</strong>
</span>
<strong><br>2black red</strong>
</span> <strong><br>3black green</strong>
</span> <strong><br>4black red</strong>
</span> <strong><br>5black green</strong>
</span> <strong><br>6black red</strong>
</span>
http://jsfiddle.net/de9ppead/
I want to display a div on hovering the anchor tag using css. Below is my html code
<td class="cellStyle">
<a href="#" class="linkStyle">
<div>Home</div>
<div style="display:none;">I'm here... (some html) </div>
</a></td>
Please tell me how i can achieve this using css.
Thanks
Give your hidden div a class:
<td class="cellStyle">
<a href="#" class="linkStyle">
<div>Home</div>
<div class="demo">I'm here... (some html) </div>
</a></td>
And use these styles:
<style>
a.linkStyle div.demo { display:none; }
a.linkStyle:hover div.demo { display:block; }
</style>
Working jsfiddle: http://jsfiddle.net/6pB8G/
how to make sub menu appear when hover over link?
CSS
a.linkStyle:hover div.here {
display:block;
}
div.here {
display : none;
}
HTML
<a href="#" class="linkStyle">
<div>Home</div>
<div class="here">I'm here... (some html) </div>
</a>
(Added a class to div which is to be hidden first)
JSFIDDLE DEMO
Try this:
CSS:
.hiddendiv {display:none;}
.linkStyle:hover > .hiddendiv { display:block; }
HTML:
<a href="#" class="linkStyle">
<div>Home</div>
<div class="hiddendiv">I'm here... (some html) </div>
</a>
here is a working demo
It's better to keep block level elements outside from inline elements. So, try keeping DIV outside from A.
here is demo:
HTML
<td>
<a class="tooltip">Home</a>
<div class="tip">I'm here... (some html)</div>
</td>
CSS:
.tip{display:none}
.tooltip:hover + .tip{display:block}
Working Demo