I am still pretty new to coding and feel proud of my progress. I have searched and searched to no avail for a solution to my problem. My navigation tabs work correctly, but I can't seem to figure out how to make my active page tab be the same color as the hover color. I used the code from an article at http://blixt.org/articles/tabbed-navigation-using-css#section=introduction. I contacted the author, but have not received a response from him. The only solutions I have found entail completely changing my code to one without using the tabs. I have tried working within the "inspect element" feature, but have made no progress. My webiste is http://actonrecovery.com/. Please help if you can.
Here is my html code:
<!--my ordered list for a table of contents TOC-->
<ol id="toc">
<li><a href="recovery.html" id=“recovery”><span>Coach</span></a></li>
<li><a href="coaching.html" id=“coaching”><span>What Is Coaching?</span></a></li>
</ol>
Here is my css:
/*style the default state for each list item (tab) inside the TOC*/
ol#toc { height: 2em; line-style: none; margin: 0; padding: 0; }
/*padding the left part so it won't be covered by the background image of the <a> element*/
ol#toc a { background: #bdf url(tabs.gif); color: #008; display: block; float: left; height: 2em; padding-left: 10px; text-decoration: none; }
ol#toc a:hover { background-color: #3af; background-position: 0 -120px; }
ol#toc a:hover span { background-position: 100% -120px; }
ol#toc li { float: left; margin: 0 1px 0 0; }
/*offset the tab image when a tab is selected*/
ol#toc li.current a { background-color: #48f; background-position: 0 -60px; color: #fff; font-weight: bold; }
ol#toc li.current span { background-position: 100% -60px; }
ol#toc span { background: url(tabs.gif) 100% 0; display: block; line-height: 2em; padding-right: 10px; }
According to your css, you should add class current to li
<ol id="toc">
<li><span>Coach</span></li>
<li class="current"><span>What Is Coaching?</span></li>
</ol>
Also, your id has some other type of quote(” ”), change it to normal quotes(" ")
You can easily add your 'current' class with the same background color as your hover pseudocode. I created a jsfiddle to show you what I mean.
HTML:
<ol id="toc">
<li><a href="recovery.html" id=“recovery”><span>Coach</span></a></li>
<li><a href="recovery.html" id=“recovery”><span>Coach</span></a></li>
<li><a href="recovery.html" id=“recovery”><span>Coach</span></a></li>
<li class="current"><a href="coaching.html" id=“coaching”><span>What Is Coaching?</span> </a></li>
</ol>
CSS:
li {
background: #bdf;
color: #008
}
li:hover {
background: #48f;
}
.current {
background: #48f;
}
http://jsfiddle.net/rynslmns/A29cC/1/
You would need to add your current class to your html for the current page you would like to show 'current'
On the tab that is active, just it's class to "active" or whatever else you want to call it.
Like this:
HTML:
<!--my ordered list for a table of contents TOC-->
<ol id="toc">
<li><a href="recovery.html" id=“recovery”><span>Coach</span></a></li>
<li class="active"><a href="coaching.html" id=“coaching”><span>What Is Coaching?</span></a></li>
</ol>
CSS:
li
{
background: #bdf;
color: #008
}
li:hover
{
background: #48f;
}
.active
{
background: #48f;
}
That is what I did for my website.
If you just have a static HTML and CSS page you could add the .current class to the current pages tab. For example on the home page add the current class to the home page tab, etc. etc.
Related
I'm running into issues on my first ever website. I have successfully created a navigation bar at the top which looks and acts somewhat how I want it to (other than the color scheme but that can come later). The issue is that whenever I click a different link on the bar I want that box to change color, but it is currently stuck highlighted on the homepage. I think this is something super simple but i cannot find it. Thank you for the help.
body
{
font-family:sans-serif;
width: 100%;
margin: 0px;
}
/* upper strip holding the tabs*/
ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
position:fixed;
top: 0px;
width: 100%;
background-color: #328CC1
}
li
{
float:left;
border-right:3px solid #30FFE3;
}
li a
{
display: block;
color: whitesmoke;
text-decoration: none;
padding: 14px 16px;
text-align: center;
}
li a:hover:not(.active)
{
background-color: #111;
}
a.active
{
background-color: #EAB126
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>Academics</li>
<li>Athletics</li>
<li>Contact</li>
</ul>
Change this:
a.active
background-color: #EAB126
}
to this:
li a:focus {
background-color: #EAB126
}
Here is the JSFiddle demo
This does what you ask BUT if this is a navigation bar then bear in mind that the control will lose focus as soon as you change page. It would be better if you use Javascript/JQuery to handle that much more easily.
A typical way of handling this is to have each page have a class that includes the page name: <div class="academics"> for example.
Now modify your header (within the page div) as follows:
<ul>
<li><a class="for_home" href="#home">Home</a></li>
<li><a class="for_academics" href="#academics">Academics</a></li>
<li><a class="for_athletics" href="#athletics">Athletics</a></li>
<li><a class="forcontact" href="#contact">Contact</a></li>
</ul>
this would be followed by the css as follows:
.home .for_home, .academics .for_academics, .athletics .for_athletics, .contact .for_contact {
background-color: #EAB126;
}
Then the menu item for the current page will be highlighted.
Congratulations on your first website!
I have built this Vertical Menu with hidden submenus however I cannot get the submenu to display when the user hovers. How could I go about doing this? Also how can I get the text to be formatted all the way left, since they are lists I can get rid of the bullets, however I cannot get the text to go where the bullets used to be. Also, I am wondering what the best way would be to set the width of the "main-nav". I don't want anything to be over the text except the logo. The body of the site would be next to the navigation. I want the side of the logo to also line up with the left side of the text, and I cannot figure out how to do this. The red border is just for testing purposes (obviously).
Here is the link to my codepen.
[BONUS] I am trying to create my own site from scratch with wordpress and a custom theme. How does one create it so that the logo image is taken from the site identity tab in the customize sidebar? And also just display text if no logo is chosen in the identity bar. Would it be some wordpress php function? Also, I would want the logo to be apart of the main-navigation by default. I have the register_nav_menu() function in my functions.php file and it assigns a menu to Main Navigation, also giving it a class main-navigation. How could I get the logo to by default appear above this menu? Any tips on this would be greatly appreciated. (Wordpress/coding noob here)
HTML:
<div id="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/2000px-Google_2015_logo.svg.png" class="logo-branding" />
<nav id="site-navigation" class="main-navigation">
<ul>
<li class="active">Overview</li>
<li>About</li>
<li>Submenu</li>
<ul class="sub-menu">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<li>Contact</li>
</ul>
</nav>
CSS:
.main-navigation {
bottom: 2%;
margin-left: 4%;
display: block;
float: left;
border: 1px solid red;
position: fixed;
overflow: hidden;
width: 15%;
}
.main-navigation li, .main-navigation a {
list-style-type: none;
text-align: left;
text-decoration: none;
color: black;
text-transform: lowercase;
font: 16pt helvetica, sans serif;
padding: 1%;
}
.main-navigation a:hover, .main-navigation .active {
color: tan !important;
font-weight: bold !important;
}
.main-navigation .sub-menu {
display: none;
}
.main-navigation .sub-menu:hover {
display: block;
}
#container {
height: 10000px;
}
.logo-branding {
display: block;
position: fixed;
margin-top: 8%;
transform: rotate(90deg);
width: 15%;
}
JS:
/* No JS */
I believe that this is your desired behaviour?
To do this, you need to place your ul submenu inside the li for the menu item that is displayed. This is the only change I made to the HTML.
You can then add a CSS rule so that when you hover over the li, its ul child becomes visible. i.e: .main-navigation li:hover {display: block; }.
The reason it didn't work when you did .main-navigation .sub-menu:hover is because when it is not being displayed, you cannot hover over it, so the hover state is never triggered. In the rule which I added, it is triggered when you hover over the containing li.
.main-navigation {
bottom: 2%;
margin-left: 4%;
display: block;
float: left;
border: 1px solid red;
position: fixed;
overflow: hidden;
width: 15%;
}
.main-navigation li,
.main-navigation a {
list-style-type: none;
text-align: left;
text-decoration: none;
color: black;
text-transform: lowercase;
font: 16pt helvetica, sans serif;
padding: 1%;
}
.main-navigation a:hover,
.main-navigation .active {
color: tan !important;
font-weight: bold !important;
}
.main-navigation .sub-menu {
display: none;
}
.main-navigation li:hover ul {
display: block;
}
#container {
height: 10000px;
}
.logo-branding {
display: block;
position: fixed;
margin-top: 8%;
transform: rotate(90deg);
width: 15%;
}
<div id="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/2000px-Google_2015_logo.svg.png" class="logo-branding" />
<nav id="site-navigation" class="main-navigation">
<ul>
<li class="active">Overview
</li>
<li>About
</li>
<li>Submenu v
<ul class="sub-menu">
<li>Item 1
</li>
<li>Item 2
</li>
</ul>
</li>
<li>Contact
</li>
</ul>
</nav>
EDIT: I may have made a mistake regarding WordPress, so I deleted that part of the answer so that I do not mislead anyone. E. Shio, however, found a link which explains it almost step by step. I'll summarise what this link says, just in case it someday gets deleted or the page url gets moved.
First, you check if there is a custom logo, for which you use has_custom_logo (). You then output that custom logo with the_custom_logo(). This is a relatively new feature to Wordpress though, so to maintain backwards compatibility, you should check if the function exists with function_exists( 'the_custom_logo' ). If there was no custom logo, you can output the text to display inside an else statement. Here's an example:
if( function_exists('the_custom_logo') ) {
if( has_custom_logo() ) {
the_custom_logo();
} else {
$blogname = get_bloginfo('name');
echo "<h1>$blogname</h1>";
}
}
If you have any questions about the CSS for the menu, I'm more than happy to help! (I'm no expert in Wordpress though, so I probably can't help with any Wordpress specific things, but I can try! XP)
In my navigation I want to be able to and click on a navigation link and have the li item change color when you go the page. Eg. I click on the about us tab in the navigation, it goes to the about us page and the about us page tab in the navigation bar has changed color. I tried nav ul li:active but it is not working.
Here is the css:
#nav {
display: block;
position:relative;
border: 1px solid #002799;
background: linear-gradient(to bottom, #4970E3 0%, #121B3E 100%) repeat scroll 0% 0% transparent;
background: -moz-linear-gradient(to bottom, #4970E3 0%, #121B3E 100%) repeat scroll 0% 0% transparent;
background: -webkit-linear-gradient(to bottom, #4970E3 0%, #121B3E 100%) repeat scroll 0% 0% transparent;
padding: 0px 0px 0px 0px;
border-radius: 15px;
height: 40px;
width: 470px;
margin: 0px auto;
font: Bold 16px Verdana;
}
#nav ul {
margin: 0px;
padding: 0px;
min-width:250%;
}
#nav li {
list-style: none;
float: left;
position: relative;
display:inline;
width:auto;
}
#nav ul li {
list-style: none;
float: left;
position: relative;
}
#nav ul li:last-child a {
border:none;
}
#nav ul li:hover {
background: #060652;
}
#nav ul li:active {
background: #060652;
}
#nav ul li:hover ul {
display:block;
width:100%;
}
#nav ul ul {
display: none;
position:absolute;
left:0px;
min-width:250%;
z-index: 999;
background-color: #4970E3;
}
#nav ul ul li {
border: 1px solid #FFFFFF;
display:block;
float: none;
z-index: 999;
width: auto;
}
#nav ul ul li a {
border-right: none;
font: Bold 16px Verdana;
width: auto;
}
#nav ul li a {
text-decoration: none;
display: block;
border-right: 1px solid #121B3E;
padding: 10px 15px;
color: #fbfbfb !important;
}
Here is the html:
<div id="nav">
<ul>
<li>Home
</li>
<li>About Us
<ul>
<li>Why OSTech</li>
<li>Testimonials</li>
<li>Case Study 1</li>
<li>Case Study 2</li>
<li>Green IT</li>
<li>OSdesk Intel vPro</li>
<li>Workstation Innovation</li>
<li>Consolidation and Centralisation</li>
<li>The Green Grid</li>
<li>Clean Technologies</li>
<li>OSdesk Remote Management</li>
</ul>
</li>
<li><a>What We Do</a>
<ul>
<li>OSdesk</li>
<li>OSguard</li>
<li>OSmon</li>
<li>OSvault & OSclass</li>
<li>OSmail & OShost & OSshare</li>
<li>OStrack & OSdms</li>
<li>OSarchive & OSgroup</li>
<li>OSfaq & OShelp</li>
<li>OSbill & OScal</li>
</ul>
</li>
<li>Contact Us<ul>
</li>
</ul>
</div>
You've misunderstood what :active means.
Your web browser has no concept of which li on the page represent the current page, unless you tell it somehow.
The active selector selects the active link. That is, a link you've selected using keyboard navigation (but haven't followed yet) or a link you've just clicked on (before the new page loads). Once the new page loads, none of the links are active again.
So, you can only use :active on a elements, and not for what you're trying to do here.
Instead, you want to add a class to the li for whichever tab represent the current page, and use CSS to style that li appropriately:
<li>Why OSTech</li>
<li class="current">Testimonials</li>
<li>Case Study 1</li>
with:
li.current {
background: #060652;
}
:active is only the action when the link is actively pressed, so you can see it clearly if you click on the link and hold down on it, instead of letting go.
In order to do this you will need to assign a class to the a for the page that is open. There are ways to accomplish this with a server-side language so you don't have to do it manually, but if you are making a static .html page, you will need to manually add a class (like class="active") to the a for each page that is "active".
So for example:
menu.html will have Menu and About
Whereas about.html will have Menu and About
Just as everyone said, you'll need to use a separate class for the a, :active won't work like what you want.
However, if you use the $_GET['page_id'] value you can set the class automatically. Like this:
<li><a <?php if ($_GET['page_id'] == 56) { echo 'class="active" '; } ?>href="http://osweb01.ostech.com.au/?page_id=56">OSbill & OScal</a></li>
You can even go an extra mile, and have the page_id stored in an array at the top of the php file. And do a foreach loop and echo out the links as well as class.
<li><a>What We Do</a>
<ul><?php
$menu1 = array ( 41,43,45,47,49,52,54,62,56 );
foreach ($menu1 as $menu_item) {
echo "<li><a ";
if ($_GET['page_id'] == $menu_item) { echo 'class="active" '; }
echo 'href="http://osweb01.ostech.com.au/?page_id='.$menu_item.'">OSbill & OScal</a></li>';
} ?>
</ul>
</li>
If the above doesn't work (which it didn't for me) just apply a different colour to your navigation bar depending on which page you are on.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li b {
background-color: green;
}
li a:hover {
background-color: red;
color: white;
}
Use code as above? Simple navigation bar with 'active' faked out.
<ul>
<li><b href = "homepage.html">Homepage</a></li>
</ul>
To extend on what #Aister posted I would not use echo on the html but
<li>
<a>What We Do</a>
<ul>
<?php
$menu1 = array ( 41,43,45,47,49,52,54,62,56 );
foreach ($menu1 as $menu_item) {
?>
<li>
<a <?= ($_GET['page_id'] == $menu_item)? 'class="active" ' : '';?> href="http://osweb01.ostech.com.au/?page_id=<?=$menu_item?>">OSbill & OScal</a>
</li>
<?php
}
?>
</ul>
</li>
The main advantage in this approach is that your templates will be a lot easier to read in your editor because the html syntax highlighting will recognize the HTML and not just see a string.
One caveat to my approach is that you need to have the php ini configured to allow short tags or it will break. If it doesn't work and you can't change it replace
<?=
with
<?php echo
I've created my navigation menu using CSS and an image sprite for the rollover states (i.e hover, active). However, I'm trying to create a 'selected/current' state (which in my case is the same as the active state) so that dependent on the page you've selected, the corresponding navigation button is highlighted.
Here's what I have:
CSS:
#menu li {
margin: 0;
padding: 0;
height: 50px;
list-style: none;
display: inline;
float: left;
line-height: 40px;
}
#menu a {
display: block;
height: 50px;
}
#menu a:hover {
background-image:url(../Images/about_rollover.gif)
}
#about {
width: 90px;
}
#about a:hover {
background-position: 0 -50px;
}
#about a:active {
background-position: 0 -100px;
}
#about a:selected {
background-position: 0 -100px;
}
#portfolio {
width: 90px;
}
#portfolio a:hover {
background-position: 90px -50px;
}
#portfolio a:active {
background-position: 90px -100px;
HTML:
<ul id="menu">
<li id="about"><a href"#"></a></li>
<li id="portfolio"></li>
</ul>
Image sprite: view here
One way to solve this would be to put a class on the body tag that identifies the page you are currently browsing, and applying the selected state to the nav item when it falls under that particular class. Like:
HTML:
<body class="about">
<ul id="menu">
<li id="about"><a href=...
CSS:
body.about ul li.about { [selected background position] }
First:
You use some language at the server level to implement the site?
If so, it would be interesting to check the page that is selected and add a specific class to mark it, for example (via php):
<ul id="menu">
<li id="about"<?php echo $accessedPage == 'about' ? ' class="selected"' : ''; ?>><a href"#"></a></li>
<li id="portfolio"></li>
</ul>
Second:
You can group a css selector to hover and selected:
#about a:hover,
#about .selected a {
background-position: 0 -50px;
}
#portfolio a:hover,
#portfolio .selected {
background-position: 90px -50px;
}
You will need to look at the current path some type of application logic, not strictly CSS.
For example you can use javascript:
$('document').ready(function () {
//Gets the current window location
var currentPath = window.location.pathname;
/*Logic to identify where the user is relative
to your webpage and update the style*/
if (currentPath = '/portfolio')
{
$('portfolio').addclass('menuactive');
}
});
You will have to add classes define the states for your different menu items. But this should get you in the right direction.
Ok hey guys.
So what I try to acheive is to have a menu in the topnav of my site and when hovring the mouse over to show some stuff in a list under it.
so far I'm working on local on a test html file until I get it working.
so what i got so far is this menu:
<ul id="menu">
<li>Notifications
<ul>
<li id="foot-notify-954>
Xtesting left a comment for your blog 22 hours ago
</li>
<li id="foot-notify-953>
X
<p>testing left a comment for your blog <span>22 hours ago</span></p>
</li>
</ul>
</li>
and my css code:
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul { display: none }
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #2C5463;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover { background: #617F8A }
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #617F8A }
li:hover li a:hover { background: #95A9B1 }
I think the problem is that I'm having more than 1 <a> hyperlink inside the notifications <li>
id like each li notification to show in 1 line, as in the format, the X button at the start to remove it then the notification itself.
First, you have to check the html syntax:
list should looks like this:
<ul>
<li>Coffee</li>
<li>Tea</li>
<li><a href='#'>Milk</a></li>
</ul>
This tool helps you find the errors (red highlighted):
http://jsbin.com/emowir/1/edit
Here is your example:
<ul id="menu">
<!-- type 1: NOT drop down-->
<li>Home</li>
<!--type 2: drop down-->
<li>About Us
<ul>
<li>The Team</li>
<li>History</li>
<li>Vision</li>
</ul>
</li>
</ul>
What would you like to insert and where?
Your problem does seem to be bad code formatting. This is a clean and edited version of your code. The code "breaking" is an issue of CSS formatting. Using inline-blocks instead of blocks helps get things lined up properly, and shifting the background style to the <li> rather than the <a> makes it look better.
Your problem is the following:
ul li a {
display: block;
This makes every link you insert into the list a block. Try start to float things like in this example I made from your code, http://jsfiddle.net/xN8sc/1/