CSS pseudo class to display background color - html

I have three html pages that can be visited through tags that are on the navbar
So, there is not a main layout that shares the same navbar. Each page has its own HTML5 but they share the same CSS file
When a certain page is getting visited, its page on the navbar has a class "active " (See below)
navbar at the index.html
<nav>
Home
Teams
History
USA Ultimate
</nav>
navbar at the team.html
<nav>
Home
Teams
History
USA Ultimate
</nav>
and so on for the history page
Each tag has been given CSS rules to be displayed like a button. (no bootstrap used)
What I tried to do is whenever I am on a certain page I want the tag to have a white background color. In order for the visitor to know which page is currently open.
Finally I have configured it with the following CSS rule
Solution
.active{
background-color: #FFFFFF;
}
Initially i tried to do it by using pseudo-class
1)
nav a:active{
background-color: #FFFFFF;
}
2)
nav a:active:before{
background-color: #FFFFFF;
}
3)
nav a:active:after{
background-color: #FFFFFF;
}
Also used :target pseudo class but did not work.
For those that marked 1, 2, 3 and did not work, i tried it as I read the documentation and having seen a proposed solution on fiddle.
Can someone tell me what is the difference between the solution that worked and the others that I tried allong with :target and did not work?

As long as you have three pages that each one has its own HTML code, the way you solved it is one that you could do it, elsewhere in order to use :target you had to move as follow
Specify on each <a> an id and that id to be lace also at the end of the href.
See bellow
<div class="side_col">
<a href="#sl1">
<div id="sl1" class="side_link">Accounts
</div>
</a>
<a href="#sl2">
<div id="sl2" class="side_link">Newsletter
</div>
</a>
<a href="#sl3">
<div id="sl3" class="side_link">Operator
</div>
</a>
<a href="#l4">
<a href="#sl4">
<div id="sl4" class="side_link">Invoice
</div>
</a>
</a>
And the CSS
#sl1:target:before, #sl2:target:before, #sl3:target:before, #sl4:target:before { background-color: #24BDE9; }

The :active pseudo-class is only applied to an element while it is considered to be active: for an anchor, that is typically for the time from when you click your mouse button on it until you release your mouse button. That is not a particularly long time!
Taking a look at your example, it would work like this:
User begins clicking the Home link. Their mouse button depresses, and that link becomes :active. Your CSS using the :active pseudo-class in a rule applies to it.
The user releases the mouse button. That link is no longer :active and your CSS using the pseudo-class stops applying.
Page navigation happens. That link that was :active certainly isn't any more! This is when your class would take over and you achieve that effect that you want.
:active has a very short duration tied literally to the element being interacted with by the user: they are making it :active by clicking on it and not releasing their mouse button. In your case, that interaction is brief and will not carry over during page navigation.
This is why using a class works: that class is there when the page loads, will always stay there (unless you change it programmatically), and gives you a way to apply your CSS like you want.
EDIT to answer a question on :target below.
:target matches an element by ID to the URL fragment. In your example, you aren't using IDs, nor are you using URL fragments, so there is nothing that would be considered a :target.
:target could work with a structure like this below, but it's going a long way to solve a problem best solved by classNames:
<nav>
Home
Teams
<a href="history.html#history" id="history>History</a>
</nav>
In the example above, each link has a fragment and an element with the ID matching the fragment (#home and the first nav item's ID, for example). In that case, you would have an element that would be considered a :target.

You used nav a:active{..., but active is the class, so your selector has to be nav a.active{... instead. (dot, not colon)

nav a.active { ... }
The only difference is saying a.active instead of a:active due to the fact that .active is a class. You can check out this w3schools link: (scroll down a bit to where it says "Active/Current Navigation Link")
Hopefully this helped!

Related

Footer link not working properly when being hovered

Hi there I have a rather strange problem going on here with one social media link in the footer section of a website. I have applied a simple :hover rule with a specified colour slap, when the element is being hovered. Well so far so good, but the link doesn't change its colour when being hovered. I even put !important rule (which I very well know that isn't a good practise at all) and still no improvement. When opened in DevTools and applied the hover checkbox somehow the rule applies and the icon gets changed. My question is why is this happening, and is this problem coming from the nested tags, because I clearly see that the new CSS rule is there and should be working? The theme is quite outdated and old-fashioned, and is working on Wordpress.
Add the following code
.vc_icon_element.vc_icon_element-outer .vc_icon_element-inner.vc_icon_element-size-sm .vc_icon_element-icon:hover, html .vc_icon_element.vc_icon_element-outer .vc_icon_element-inner:hover > span {
color: #07918A !important;
}
Problem: you hovered the < a > and gave it a hover class but the icon < span > was not affected by it. My code should fix this.

How to change the link color in specific area using css

I've taken a look at many questions that I thought were a problem similar to mine but none of them worked for me which is why I'm posting this. I'm still new to programming (just know the basics) so please bear with me:
I own a website All Gaming Everything
please open any post on the website
The problem I was facing was that all of the <a> elements on the website were grey in color so using the custom CSS option in my wordpress theme, I just did
a:link { color:white; }
Now while this worked like a charm and all of my elements have the same color (white), I want the color of a:link as red on ONLY the body of the article since white prevents the visitor from seeing if there is any link on the post. I tried changing the aspects using inspect element and custom CSS but nothing seems to be working for me. I have access to both custom CSS and custom HTML editors on my wordpress theme so I'd really appreciate any help with this and once again sorry if I talked noobish (I am a noob).
You can use a css selector inherited. That means if you have a container with a <a> element as child for example
<div id="container">
Link
</div>
do in your css
#container a {
color: #ff0000;
}
or
div a {
color: #ff0000;
}
now all <a> in #container got red color, if no other declarations from wp stylesheets exists.
Take a look to some great websites like w3schools.com and get the basics of css selectors, childs, parents and much more.
Since you are using wordpress and the wygiswys wrap text inside <p></p> you can try:
p a:link{color:red};
and it will change the color of each link inside a paragraphe.
I visited your site and noticed all articles are wrapped in a div with a class "td-post-content". This way, all you have to do is create a CSS rule that targets anchor tags only within those divs.
Here:
.td-post-content a {
color: red;
}
It workd like a charm. I tested on the post below which has a link within the article body:
By the way, you don't need to use a:link on your CSS statements. Only a will do it. ;)
Cheers!

setting the focus on the nav bar

Does anyone know what css and html code I can use that will set the focus on the nav bar. In other words I want the page tab such as home page to be highlighted when the user is on the home page. If the user go's to the contact page then the contact page nav bar tab should be highlighted.
You can do this in HTML/CCS providing your pages have classes or ID's. Using the home example:
HTML
<body class="home">...
<li class="home>...</li><li class="other>...</li>
CSS
.home li.home { ... }
.other li.other { ... }
This won't set the :focus but it will give you the visual effect you desire.
You can use some sort of class for your links. An example is to have an active class which has some outline effect on it or has a different background-color. But you cannot achieve that by just using HTML/CSS, you will need some javascript to add and remove this class, based on where you are on your page.
Hope this helps!

How do I style a span to look like a link without using javascript?

For my website I will need to use <span> instead of <a>, because I am using mostly ajax and thus instead of links I have onclick ajax events as attributes in my spans.
As a result, I had to manually style the spans to look like links. I have used hover and visited pseudo classes to change background and text colour, but to change the mouse default to a pointer finger on hover, will I need to use javascript? Or can I do that using css?
Also, I have just realized: I could not just use the <a> tag anyways instead of <span>, but just instead of an href, I would include an onclick? It should work just the same, no?
span {
cursor:pointer;
color:blue;
text-decoration:underline;
}
Hyperlink<br />
<span>Span</span>
Additionally, you can use :hover pseudo-class to style the element when hovered (you can use any styles not just the ones originally used). For example:
span:hover {
text-decoration:none;
text-shadow: 1px 1px 1px #555;
}
Note that if your website is public and you are counting on search engines to crawl your site, you lose a lot by leaving out links without href since spiders have nothing to grab on while crawling your page.
You should use a complete link - in case your javascript breaks down the user is still able to navigate through pages:
Link
than you can disable the link with jquery by using preventDefault() - and you totally separated base html and the javascript part, meaning your site is still usable without javascript on.
Than you don't need to bother with span hover and anything - but just for the sake of it
span:hover {
cursor:pointer;
}
will enable hover hand cursor on hovered span.
Option1
Just use an anchor link as follows:
Link
Option2
I don't know why you would wanna use span , but if you do you can do the following styles to make it look similar to an anchor link.
span {
color: #000000; /* Change this with links color*/
cursor: pointer;
text-decoration: underline;
}
span:hover {
color: #444444; /* Change the value to with anchors hover color*/
}
Just add cursor:pointer; in your span css.
Use CSS to display the cursor as a pointer:
<span style="cursor: pointer;">Pseudolink</span>
http://jsfiddle.net/kkepg/
You could use an anchor. But within javascript you'd have to use event.preventDefault() But there is a CSS method thats smaller and easier. Keep your span and use this:
span:hover{
cursor:pointer;
}
You can change the cursor to a pointer by specifying the cursor: pointer CSS rule.
You can also use <a> tags instead of <span>, in fact they can behave nicer with screen readers and other similar devices. You don't need to leave out the href attribute if you use the preventDefault() and stopPropagation() JavaScript functions in the onClick handler. This way you can retain some level of backward compatibility with non-JS enabled browsers.
You could use a button instead of span and use bootstrap css classes to style it like a link:
<button class="btn btn-link">Link</button>
It will react on mouseOver as normal links do.
Run the code snippet to preview the result:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<button class="btn btn-link">this is my link styled with bootstrap</button>
You can use an onClick event but if I remember correctly, you must return false in order to prevent your page from jumping around; something like:
<a href="#" onClick="myfunction();return false;">
or: <a href="#" onClick="return myfunction();"> provided that myfunction will return false.
You can also directly call a javascript from href but you must cast the result to void in order to block to the browser to try to follow the result as a valid link:
<a href="javascript:void(myFunction())">
Even if you still want to use the onClick property; it would still be a good idea to replace the href="#" with href="javascript:void(0)" ...>.
Other people have mentionned using the event.preventDefault() and stopPropagation(). I don't remember ever using one of these but I must admit that it has been many years since the last time that I have coding some javascript in a HTML link; so you should definitely investigate the use of these two functions.
EDIT: maybe that using a href="javascript:void(0)" could be a bad idea sometimes; see http://drupal.org/node/1193068 .

Can I use CSS to change a bookmark's properties when its link is being hovered over?

If I have a simple HTML and CSS document using bookmarks (named links) and ordinary links, is it possible to alter a bookmark (eg. change its color) when its link is being hovered over.
For example, if I have the following HTML
...
<a name="bookmark1">Bookmark One</a>
<a name="bookmark2">Bookmark Two</a>
<a name="bookmark3">Bookmark Three</a>
...
Link to BM#1
...
can I write CSS along the lines of:
a:hover
{
"the bookmark's color": red
}
which would have the effect of changing the bookmark's font (and not its link's) color to red? That is, the text "Bookmark One" changes color, not "Link to BM#1".
UPDATE: thanks to everyone who answered. In summary, it seems you can't affect the target of a link while hovering over it using just CSS. You need to resort to javascript. For my simple purposes I didn't want to go the trouble, so I selected the answer that was CSS only but required clicking on the link.
This can be done using the general sibling combinator but the source order will have to be different, the links that reference the named links must come before the named links since your selector is targeting the named links.
Link to BM#1
Link to BM#2
<a name="bookmark1">Bookmark One</a>
<a name="bookmark2">Bookmark Two</a>
<a name="bookmark3">Bookmark Three</a>
Another problem with this is that since there aren't variables or back-references in CSS you must explicitly make a CSS selector for each of the links you want to do this with.
a:hover[href="#bookmark1"] ~ a[name="bookmark1"],
a:hover[href="#bookmark2"] ~ a[name="bookmark2"] {
color: red;
}
Example 1
You'll notice in Example 1 "bookmark3" doesn't highlight since there is no rule referencing it.
A much more general and easier to maintain approach would be to highlight the named link after the link to it was clicked instead of on hover. You could do this with a simple :target selector
a:target {
color: red;
}
Example 2
If you are absolutely married to the idea that it must be on hover and don't mind using JavaScript you could do it with a little bit of jQuery:
$('a[href^="#bookmark"]').hover(function() {
// grab target of this link and remove the leading hash
var name = $(this).attr('href').replace(/^.*#/, '');
$('a[name="' + name + '"]').addClass('highlightedbookmark');
}, function() {
// grab target of this link and remove the leading hash
var name = $(this).attr('href').replace(/^.*#/, '');
$('a[name="' + name + '"]').removeClass('highlightedbookmark');
});
Example 3
Better yet this solution doesn't have the HTML source order restrictions that the pure CSS method does: Example 4
Assuming the ellipses in your question imply the location of your bookmarks, and they are the immediately following sibling of the links, you can use the adjacent-sibling selector:
div:hover + div {
color: blue;
}
JS Fiddle demo
Reference:
adjacent-sibling selector (CSS2).
You can assign a class to the bookmark link. See the below example:
HTML
<a name="bookmark1" class="bookmark">Bookmark One</a>
Link to BM#1
CSS
a:hover{
color: red;
}
a.bookmark:hover{
color: blue;
}
That way your normal links will hover RED (in this example) and your bookmark links will hover BLUE.
Use attribute selector:
a[name]:hover {
color: red;
}
You have to use the color tag to alter the color of text. It'd be also better if you use an id or class.
a:hover{
color:red;
}
#bookmark a:hover{
color:red;
}
EDIT
In case the text to be changes is different from the link text then you can use jQuery to do it, ex:
$("#boomkark-link").hover( function(){
$("#bookmark").css('color', 'RED');
}
#dave; may be you can define it with simple css declaration.
.wrap1 a, .wrap2 a{
background:green;
}
.wrap1 a:hover, .wrap2 a:hover{
background:red;
}
a{background:yellow;}
a:hover{background:pink;}
If the links are in different divs & you want to target an specific a tag.
check this for more http://jsfiddle.net/sandeep/RsmAg/
EDIT:
may be nth-child is also an option. Check THIS