selctor:hover.class applied before hover - css

I want to apply a link selector hover effect which includes bottom border underline in an unordered list. Really, I know I can do a simple underline using a border-bottom: #FFFFFF solid 2px;, but I'd like to make a custom underline with box-shadow effect. So I have two issues.
my border is showing up all the time, not just when I hover.
When I tried to add a box shadow to my .underline class it goes around the whole list item and does not create a separate line. I guess we can get to this item later.
MY CSS CODE
li {
margin: auto 0px 20px auto;
font: 1em 'Bookman Old Style', Georgia, Garamond, ‘Times New Roman’, Times, serif;
color: #FFFFFF;
}
li a {
text-decoration: none;
color: rgba(255,255,255,0.5);
display: block;
}
li a:hover .underline {
cursor: pointer;
text-decoration: none;
color: rgba(255,255,255,1);
}
.underline {
border-bottom: #FFFFFF solid 2px;
}
MY HTML CODE
<ul>
<li><a class="underline" href="http://www.sitepoint.com/">SitePoint.com</a></li>
<li>Revealing CSS3 Menu</li>
</ul>
I would really like to be pointed to a comprehensive article about how to build rich HTML elements via CSS specfically using :before and :after and being able to use multiple classes on one selector in CSS properly.
Here is a JSFiddle of what I have (broken) http://jsfiddle.net/jellis3d/a8svpwr4/2/. Also here is a picture of what I'm after. I really exaggerated the underline in order to show what I'm looking for. The line does not have to have rounded edges either.

You could do it by apply border-bottom to .underline:hover and box-shadow and display: inline-block; to li a tag.
JSFiddle - DEMO
HTML:
<ul>
<li><a class="underline" href="http://www.sitepoint.com/">SitePoint.com</a>
</li>
<li>Revealing CSS3 Menu
</li>
</ul>
CSS:
body {
background: gray; /* only for demo */
}
li {
margin: auto 0px 20px auto;
font: 1em'Bookman Old Style', Georgia, Garamond, ‘Times New Roman’, Times, serif;
color: #FFFFFF;
}
li a {
text-decoration: none;
color: rgba(255, 255, 255, 0.5);
display: inline-block;
box-shadow: 0px 5px 5px #000;
}
li a:hover .underline {
cursor: pointer;
text-decoration: none;
color: rgba(255, 255, 255, 1);
}
.underline:hover {
border-bottom: #FFFFFF solid 2px;
}

i have made a basic demo on js fiddle showing what i think your after
jsfiddle link
it uses the below css which im sure you can then see what i did and make it into what your after.
li a{
color:blue;
text-decoration:none;
}
li a:hover{
color:red;
border-bottom: black solid 1px;
box-shadow: 10px 10px 5px #888888;
}

You want to add a box-shadow effect on hovering, right? You don't need to use an extra element like .underline then. Just add it to li a:hover
li a:hover {
cursor: pointer;
text-decoration: none;
color: rgba(255,255,255,1);
box-shadow: 0 1px 0 0 rgba(0,0,0,1);
}
You can read more about pseudo-elements like :before and :after on W3C: http://www.w3schools.com/css/css_pseudo_elements.asp
I made a small demo with your code using :before on hover: http://jsfiddle.net/m9czpzs5/

Related

CSS hover and active selection for UL

Hi I have different colors for hover and active menu items. The problem is that when an item is active it changes color when hovered. I want it to maintain its active color. I mean I don't want the hover color to apply to an active menu item.
CSS:
li a:hover{
color: black;
text-shadow: 0px 1px 0px black;
}
li.active a{
color: #f37121;
font-weight: bold;
font-size: 17px;
}
You can add inheritance to active link.
Just adding:
li.active a:hover{ color: inherit;}
You could apply a :not() (CCS3 selectors are pretty well supported nowerdays) to specify you only want the changes to happen on anchor tags that do not have the .active class
li:not(.active) a:hover {
color: black;
text-shadow: 0px 1px 0px black;
}
http://jsfiddle.net/srqnuyp7/3/
Or you could simply add text-shadow:none; as an additional rule
li.active a {
text-shadow:none;
}
http://jsfiddle.net/srqnuyp7/2/
You can simply use :not(selector). This matches every element that isn't selector.
You just need to add this into your first rule for li.
li:not(.active) a:hover{ color: black; text-shadow: 0px 1px 0px black;}
Run the snippet below:
li:not(.active) a:hover{ color: black; text-shadow: 0px 1px 0px black;}
li.active a { color: #f37121; font-weight: bold; font-size: 17px;}
<li>inactive link 1</li>
<li class="active">active link</li>
<li>inactive link 2</li>

Why is my CSS for removing border-right on a class being ignored?

In my code I got border-right on all of my li elements. I'm trying to get rid of the border-right when a li element is active using an own made class named active. The problem is that my class does not remove the border-right.
This is a JSFiddle of the code as well: http://jsfiddle.net/t0a4j5tq/2/
div#navbar {
width: 250px;
height: 550px;
float: left;
}
div#navbar > ul {
text-align: center;
background-color: #EEE;
}
div#navbar > ul > li.li, div#navbar > ul > li.liLast {
list-style-type: none;
border-bottom: 1px solid black;
border-right: 1px solid black;
font-size: 25px;
font-family: Algerian, "Times New Roman";
}
div#navbar > ul > li > a {
color: black;
text-decoration: none;
display: block;
width: 100%;
padding: 38px 0;
-webkit-transition: margin-left 0.5s;
}
div#navbar > ul > li > a:hover {
color: white;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black, 0 0 5px black;
margin-left: 5px;
}
li.active {
box-shadow: inset 3px 3px 10px #333;
border-right: none;
}
<div id="navbar">
<ul>
<li class="li active">Start</li>
<li class="li">Om mig</li>
<li class="li">Vision</li>
<li class="li">Tjänster</li>
<li class="liLast">Kontakt</li>
</ul>
</div>
The root of your problem is CSS Specificity. This is important to understand, because simply using !important in your code is a hack and can cause you to have problems later on if you forget that you put it there.
!important overrides everything, and so obviously should not be used except in dire, last-resort situations (and even then, many people [myself included] would argue that you should figure out how to fix the problem anyway instead of using !important).
Earlier in your code, you have a CSS selector for the same element (and similar elements):
div#navbar > ul > li.li {
Now, you are trying to access the same element with just this:
li.active {
But the first selector is way more specific. Instead, you should use the following CSS selector, without the !important hack:
div#navbar > ul > li.active {
Hi solution is simple you just need to inform that this change is "important" :P
li.active {
box-shadow: inset 3px 3px 10px #333;
border-right-style: none !important;}
Hope this helps :)

Styling breadcrumbs using CSS

So here's the code I'm using to style my breadcrumbs.
.breadcrumbs-one{
box-shadow: 0 0 2px rgba(0,0,0,.2);
overflow: hidden;
width: 100%;
margin-top:15px;
margin-left:-20px;
}
.breadcrumbs-one li{
float: left;
}
.breadcrumbs-one a{
padding: .7em 1em .7em 2em;
float: left;
text-decoration: none;
color: #444;
position: relative;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
background-color: #fff;
background-image: linear-gradient(to right, #f5f5f5, #ddd);
}
.breadcrumbs-one li:first-child a{
padding-left: 1em;
border-radius: 5px 0 0 5px;
}
.breadcrumbs-one a:hover{
background: #fff;
}
.breadcrumbs-one a::after,
.breadcrumbs-one a::before{
content: "";
position: absolute;
top: 50%;
margin-top: -1.5em;
border-top: 1.5em solid transparent;
border-bottom: 1.5em solid transparent;
border-left: 1em solid;
right: -1em;
}
.breadcrumbs-one a::after{
z-index: 2;
border-left-color: #ddd;
}
.breadcrumbs-one a::before{
border-left-color: #ccc;
z-index: 1;
}
.breadcrumbs-one a:hover::after{
border-left-color: #fff;
}
.breadcrumbs-one .current,
.breadcrumbs-one .current:hover{
font-weight: bold;
background: none;
}
.breadcrumbs-one .current::after,
.breadcrumbs-one .current::before{
content: normal;
}
I got it from here: http://www.red-team-design.com/css3-breadcrumbs
How do I modify the code so the CSS triangle isn't appended to the last breadcrumb?
So if there is one breadcrumb I wouldn't append a triangle at the end of it.
Similarly, if there are two breadcrumbs, I don't want a triangle at the end of the second breadcrumb.
And so on and so forth.
You could select the last li element with the last-child selector. After that you delete the content of the pseudo classes after and before.
#breadcrumbs-one li:last-child a::before,
#breadcrumbs-one li:last-child a::after
{
content: normal;
}
In this example you have selected the second link and you can see that the last link has no arrow after it.
If you want select a specific index element, for example the third li element. You can use the selector nth-child(index nummer). So for example, if you want to select the third li element you could do li:nth-child(3).
In this case :
#breadcrumbs-one li:nth-child(3) a::after,
#breadcrumbs-one li:nth-child(3) a::before
{
content: normal;
}
Fiddle update
Update
Now when you use the last-child selector and you have one element, that element will be seen as the last element. But you actually want that element not the have the idicator of last. So you have to use an other idicator for this. First, one element is the first and the last. You've already defined last-child so you could easially define the first-child element.
#breadcrumbs-one li:first-child a::after,
#breadcrumbs-one li:first-child a::before{
content: "";
position: absolute;
top: 50%;
margin-top: -1.5em;
border-top: 1.5em solid transparent;
border-bottom: 1.5em solid transparent;
}
You want this code to have more priority then the last-child. Now you could use the !improtant tag of css, however i strongly recommend you to not use this tag at all costs. One way to give more priority to a code is to make the selector more specific. In this case the #breadcrumbs-one is actually a ul element, so placing a ul before it makes it more specific:
ul#breadcrumbs-one li:first-child a::after,
ul#breadcrumbs-one li:first-child a::before{
content: "";
position: absolute;
top: 50%;
margin-top: -1.5em;
border-top: 1.5em solid transparent;
border-bottom: 1.5em solid transparent;
}
Now if you don't want to make a more specific selector, you can always place this code after the last-child selector code. Css will read from top to bottom order, so you want the overlapping code to be readed after the code to be overlapped. This order is only used when the selectors are identical.
However i choose the method of a more specific path, this way it doesn't matter where you place your code.
jsFiddle
Lets add another update
First of all i would suggest you to understand what happens. Here is a little example of how the arrows are created. With this the ::before and ::after pseudo classes are also used, Here some more info about that.
I would suggest you to first try it yourself before reading my answer.
Each 'crumb' is defined by the bar with text, arrow next to it and the border of the arrow.
So what psuedo class is generating what?
Well simply, the ::after pseudo class is generating the arrow it selfs and the ::before pseudo class generates the border of the arrow.
Now you only want the arrow color to be changed(you can change the border youself). Now if you have read the border-trick you may notice that this is created with only borders. This way you don't want to use background-color but change the border color.
You can change the border color with: border: 1px solid white;, however you only want to change the color. The way you do it now is also giving the width and border-style. With border-color you can change only the color. To be even more specific: border-left-color: white;.
So would have this:
#breadcrumbs-one .current::after
{
border-left-color: white;
}
Remeber what i said earlier? A more specific selector will overwrite other css code. In this case a class is more specific as a element(anchor).
Now you have only changed the arrow color. Let's change the background of the bar itself.
There is already a css code that defines the .current element :
#breadcrumbs-one .current,
#breadcrumbs-one .current:hover{
font-weight: bold;
}
Just change the background of the element, so:
#breadcrumbs-one .current,
#breadcrumbs-one .current:hover{
font-weight: bold;
background: white;
}
There you go, the .current element is white by default!
jsFiddle
Unless I've missed the point of the question, it isn't. Even on the page you cite, it shows the last item in the list has a different CSS class
<ul id="breadcrumbs-one">
<li>Lorem ipsum</li>
<li>Vivamus nisi eros</li>
<li>Nulla sed lorem risus</li>
<li>Nam iaculis commodo</li>
<li>Current crumb</li>
</ul>
So, just make sure your class="current" is on the last item in your list.
If this is dynamic, then it can be done with server sided code or probably some JavaScript

Changing text color of active menu item with CSS

I need your help with changing the text color of the active menu item on my website, using CSS.
(It's a Joomla website and I'm modifying the standard Gantry framework template to our needs).
Here is the CSS for the active menu item...
.gf-menu.l1 > li.active {
background: none;
color: #19D57E;
border: none;
border-bottom: 3px solid #19D57E;
border-radius: 0px;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
margin: 0 auto;
padding-top: 1px;
}
And here is the CSS for the passive menu items...
.gf-menu .item {
font-family: Arial, Helvetica, sans-serif;
font-size: 13px;
line-height: 21px;
color: #555555;
padding: 4px 8px;
text-align: left;
text-shadow: 0px 0px 0 transparent;
text-decoration: none;
display: block;
outline: 0;
cursor: pointer;
font-weight: bold;
}
What I want is for the color of the text in active menu item to be green (#19D57E).
The active menu item is already displaying a green line at the bottom, but the text color of the menu item remains black like in the passive menu items. As you can see, I have specified the text of the color, but for some reason it is not doing it.
What am I doing wrong?
If you want to have a look at the website, please go to http://www.barrylong.tv/index.php/home
Thanks a lot!
Hector
This is the CSS needed:
.gf-menu.l1 > li.active a {
color: #19D57E;
}
Note the a after .active
Hope this helps
add this in your style sheet .gf-menu > .active > a {
color: #19D57E;
}.
I think you have to change the color of the .item element in the .active li-element. At the moment you are trying to change the color of the li-element and not of the link.
.gf-menu.l1 > li.active .item {
/* ... */
color: #19D57E;
/* ... */
}
Find the CSS block: for item101 active last
Notice in your source for "home":
<li class="item101 active last">
<a class="item" href="/index.php/home">Home </a> </li>
You will see the text color property to change. The reason what you are doing isn't working is that you are changing the wrong CSS block properties.

CSS, menu:active not working

I have a simple menu:
<ul id="menu2">
<li> Home</li>
<li> About us</li>
<li> Contacts</li>
</ul>
And in css file I have:
#menu2 {
background: black;
float: left;
list-style: none;
margin: 0;
padding: 0;
width: 220px;
}
#menu2 li {
margin: 0;
padding: 0;
}
#menu2 a {
background: black;
border-bottom: 1px solid #393939;
color: #ccc;
display: block;
margin: 0;
padding: 9px 16px;
text-decoration: none;
}
#menu2 a:hover {
background: black url("../images/select.png") left center no-repeat;
color: #fff;
padding: 9px 16px;
}
#menu2 a:active {
background: black url("../images/select.png") left center no-repeat;
color: #fff;
padding: 9px 16px;
}
Everything works well except for #menu2 a:active not working at all while #menu2 a:hover (with same rules) works well. What is the problem? Did I miss something?
It is working as expected. I colored the active state red.
Try clicking on en element and hold the button down. The background will be red.
You don't see a change, because you CSS for hover and active are identical!
Sample
http://jsfiddle.net/dqH3F/1/
Sample contains
#menu2 a:active {
background: red url("../images/select.png") left center no-repeat;
color: #fff;
padding: 9px 16px;
}
Can you provide more details of what exactly is not working and/or a demo. Looking at the code it appears to be fine.
The :active state refers to when a link is pressed, so if you press and hold your mouse button down on your menu item it should be working as expected since hover works active has the same properties.
A link with :active will not remain that way when your on the page it links too, it reverts back to a normal link.
Your background for :hover and :active in the code above is the exact same.
Are you trying to set a background x and y position on active?
Without image background and different colors (for testing) your code works fine: see here http://jsfiddle.net/stursby/9Pccb/

Resources