I'm new to HTML and CSS and I just finished implementing my Navigation Bar which works great so far. It is black and on hovering over the fields they turn grey.
Now, I'm trying to implement that the field of the active page stays in that grey but it isn't working for me so far and I believe it's because of the HTML. On index.html nothing should be activated as I wont be using that page later. It's just the standard side.
For Example then I have Home.html, Contact.html and Forum.hmtl.
If I open Index.html in my browser and in my Navbar click on Home I will be navigated to the at the moment exact same page like Index.html, but on this page i want the home navfield to be highlighted. Do I have to (in the Home.html) state that this field is active? How do I do that.
NAV: (in home.html currently the same as in index.html)
<ul id="Navigation">
<li>IC Home</li>
<li>IC Forum</li>
<li>IC Contact</li>
</ul>
Do I have to put something which states that <li>IC HOME</li> is active?
This is how I did the hover effect in my css file:
ul#Navigation a:hover, ul#Navigation span
{
border-color: white;
border-left-color: black; border-top-color: black;
color: white; background-color: #adadad;
}
and I thought i could just do the same for active:
ul#Navigation a:active, ul#Navigation span
{
border-color: white;
border-left-color: black; border-top-color: black;
color: white; background-color: #adadad;
}
I'd really appreciate some help as I'm still new to this.
You must add a class to the active navigation element and styled it.
HTML:
<ul id="Navigation">
<li>IC Home</li>
<li>IC Forum</li>
<li>IC Contact</li>
</ul>
CSS:
ul#Navigation .active{
border-color: white;
border-left-color: black;
border-top-color: black;
color: white;
background-color: #adadad;
}
So on forum.html the active class will be on the IC Forum link.
For the contact.html will be on the IC Contact and so on...
yes you need to change the nav on every html page. or you need some script to detect what page it is on. active only applied the style on your mouseDown on the link.
<ul id="Navigation">
<li>IC Home</li>
<li>IC Forum</li>
<li>IC Contact</li>
</ul>
.activePage {
// different style here.
}
According to W3Schools
Definition and Usage The :active selector is used to select and style
the active link.
A link becomes active when you click on it.
To highlight current page in the navigation you need to add extra class to mark the element as the active page (current page). for example you will have
#navigation li a.current{
color: #ffffff;
background:#f1d74c;
}
and the html
<div id="navigation">
<li>
<a ...> other page</a>
</li>
<li>
<a class="current" ...>current page</a>
</li>
</div>
It's a server-side thing -- when rendering the page, add a class like "current" to the link. Then you can style it separately from the other links.
For example
StackOverflow renders the links with class="youarehere" when it points to the page you're already on.
Rather than just placing a class on a direct parent element, it can be beneficial to provide a base parent class on the body tag, allowing control over all elements within that page (if possible).
When doing that, you can use attribute selectors in CSS, like this and style as required:
.page-contact [href^="contact"] {
//do something
}
I have provided a low-level example, here:
http://codepen.io/seemly/pen/HIKlb
Also as you are new to HTML and CSS, take the following advice - there is no real need to use ID's in HTML for styling purposes. Ever. Use CSS classes instead.
Related
I'm trying to format some links within an unordered list using an external stylesheet, when the links are moused over they should increase in size and be displayed blue. My syntax is below:
HTML
<ul>
<li>
<a href="#dejaview"><img src="imgs/dejaview_lft.gif" width="95px" height="75px" alt="Dejaview"
title="Click for more info"/>Dejaview</a>
</li>
There are three more links below this before the UL ends
Stylesheet
ul.a:hover
{
font-size: 200%;
color: blue;
}
What am I doing wrong here, I can't work it out.... I have tried both li.a:hover and what is currently above, and I was under the impression that if I wanted to change the format for all links within a list I didn't need to create a class for them. I could be wrong though, I'm a n00b to CSS
Thanks
Rick
ul li a:hover
{
font-size: 200%;
color: blue;
}
Should work
because the dot '.' in before 'a' stands for a class called 'a'
Your CSS is wrong.
You need to edit like below.
ul a:hover
{
font-size: 200%;
color: blue;
}
ul.a:hover will work when your code like below; '.' means 'class'.
<ul class="a">
<li>hi</li>
</ul>
I want to create a nav bar that uses anchor links (the nav bar is fixed and the user stays on one page). By default, I'd like to have the first link in the nav bar styled with a background highlight to indicate it has been selected. If the user clicks on a different link on the nav bar, I'd like that link to be given the selection styling instead.
Is there a pure HTML/CSS method to do this?
Edit: I am currently tinkering with turning the nav links into secret radio buttons. I'll report back if I get it to work.
You can use the :active Selector.
a:active {
background-color: yellow;
}
This style will be applied to the last element you clicked on... once you lose focus though, it will not retain the style.
It would be much better to just change the class via javascript if you can, in my opinion anyway.
CSS
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + a {
background: blue !important;
color: white !important;
}
HTML
<input type="radio" id="x" name="selectedLink" checked />
<a href="#associatedAnchor1" onclick="document.getElementById('x').checked = true">
This is a link that will apply 'selected' style to itself and
strip the 'selected style from all other links in its group
</a>
<input type="radio" id="y" name="selectedLink" />
<a href="#associatedAnchor2" onclick="document.getElementById('y').checked = true">
This is a link that will apply 'selected' style to itself and
strip the 'selected style from all other links in its group
</a> <!-- and so on -->
It uses a tiny amount of JavaScript, but it's the closest thing to an answer that probably exists. Hope it's useful to somebody! :)
You can use :target and style with that. It would look something like:
li {
display: inline-block;
float: left;
border: 1px solid white;
}
a {
color: white;
text-decoration: none;
padding: 15px;
background-color: #bada55;
}
#targetDiv {
width: 50px;
height: 50px;
background-color: #bada55;
float: right;
border: 1px solid white;
}
:target {
background-color: purple !important;
}
<ul>
<li>First
</li>
<li>Second
</li>
<li>Third
</li>
<li>Target
</li>
<li>Target Div<li>
</ul>
The fiddle.
Note
This will interfere with the browser history, so you may want to watch out for that. It could also create a "jump", but if it's a fixed navigation you may be fine. The fiddle has a e.preventDefault() on the links to prevent the jump, but I think you could be fine without it.
UPDATED
Added a fiddle and included targeting other divs as per the comment.
So right now I have the following HTML and CSS
<div id = "navi">
<ul>
<li><a id = "topLinks" class = "currentPage" href = "index.html"> Home </a></li>
<li><a id = "topLinks" href = "blockOne.html"> Read </a></li>
<li> Write </li>
<li> Vote </li>
<li> Donate </li>
</ul>
</div>
CSS:
#topLinks:hover, #topLinks:active{
color: black;
}
.currentPage{
color:#ce5438;
font-size: 40px;
}
The problem is that if I current have my website on Home, the color for Home will be black (what I wanted), but if i then click on Read, the link for home will become a blue-ish color instead of return back to white. I tried to setting a:visited to white, but then it'll just change all links that I've visited to white regardless whether I'm on a page or not (no black font color on current page).
Help please?
You'll just want to target the links within your navigation:
#navi a.currentPage {
color: #000;
}
#navi a:visited {
color: #fff;
}
This question already has answers here:
How to make css a:active work after the click?
(8 answers)
Closed 9 years ago.
Edited to include HTML as requested - I have removed the full urls as there is no point posting them anyway as they are on a protected staging server
I am trying to amend a friend's website which has been built so that there is a sub menu which appears on every page but is coded only once.
I want to be able to add a highlight to the link for the page you are currently viewing, but I have to do this all in one html snippet - so the code below preceeds the html list which is used for navigation.
The list renders fine, except that I can't get the current page to highlight (the 'active' tag only highlighting as you click the page).
I have read some other posts about adding 'current link' formatting in a separate file but, unfortunately, I have to include all the code in this snippet.
Given that, is what I am trying to achieve possible?
Thanks
CSS:
<style>
#navigation {
color: #ffffff;
font-family: Sans-Serif;
height: 34px;
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
#navigation li a {
color: #000000;
display: block;
font-size: 12px;
font-weight: bold;
padding: 10px 18px;
text-decoration: none;
}
#navigation li a:hover {
background-color: #b36521;
color: #ffffff;
}
#navigation li a:active {
background: #f1d74c;
color: #ffffff;
}
</style>
HTML:
<div id="submenu">
<h3>Menu</h3>
<ul id="navigation">
<li>Soap bars</li>
<li>Liquid soap</li>
<li>Bath and body</li>
<li>Skincare</li>
<li>Candles</li>
<li>Gifts</li>
<li>Favours</li>
<li>Browse by scent</li>
</ul>
</div>
http://www.w3schools.com/cssref/sel_active.asp
Definition and Usage
The :active selector is used to select and style the active link.
A link becomes active when you click on it.
To highlight current page in the navigation you need to add extra class to mark the element as the active page (current page).
for example you will have
#navigation li a.current{
color: #ffffff;
background:#f1d74c;
}
and the html
<div id="navigation">
<li>
<a ...> other page</a>
</li>
<li>
<a class="current" ...>current page</a>
</li>
</div>
Tim have a great way to add current to the current page link, you only need to add one javascript line:
$("a[href*='" + location.pathname + "']").addClass("current");
For some reason, I made it so my text (a) that is active is bolded, but it is only active when it is clicked with my mouse, when it is released from the click, it turns off and the text goes back to it's normal state.
Why is this?
If you got to MSN, look at the text above their search bar. When you click on it, it bolds and turns orange. Without leaving the page. That's what I am trying to do.
HTML:
<div id="searchtopics">
<ul>
<li>Web </li>
<li>MSN </li>
<li>Images </li>
<li>Video </li>
<li>News </li>
<li>Maps </li>
<li>Shopping </li>
</ul>
</div>
CSS:
#searchtopics {
position:absolute;
margin-left:208px;
margin-top:38px;
}
#searchtopics a {
text-decoration:none;
float:left;
padding: 2px 6px 4px 6px;
color:rgb(100,100,100);
}
#searchtopics a:hover{
text-decoration:underline;
}
#searchtopics a:active{
color:rgb(100,100,100);
font-weight:bold;
}
#searchtopics ul {
display:inline;
list-style:none;
}
#searchtopics ul li {
display:inline;
color:rgb(100,100,100);
font-family:"arial", times, sans-serif;
font-size:12px;
}
That's because that link is only active hen you click it with the mouse. If you want the effect to last for the entire length of the mouse being over it use :hover. If you want it to last after the page has been visited use :visited.
edit
If you want the link to stay active when a new page is loaded you'll need to give that link a class that applies that style to it:
<li>Images </li>
#searchtopics a:active, a.active {
If you want to display link which contains to actually displayed content, there is no other solution than little javascript. Write some function to display content which will disable all other bolds, enable bold for current link and display the content.
There are four main states of a link:
a
a:hover
a:active
a:visited
a - is just when it is sitting there and it has never been clicked or in the processing of being clicked.
a:hover - this is kind of like the equivalent of a mouseover event in javascript (if you are familiar with that). when the pointer "hovers" over the link the style can be changed then.
a:active - this styling only shows up the split second that something is clicking on the link. This pseudo class is probably the least used by the general public for this reason.
a:visited - this styling shows up after a person clicks on a link.
So if you are trying to change the style of a link that is noticeable, I would recommend using a different pseudo class rather than a:active.
Hope this helps!
I know it's old topic but simple solution for it is to replace a.active instead of a:active , it did the trick for me so it should do for you aswell