Highlight current navigation tab based on url or subdirectory - html

I've created a vertical navigation on the left of our site. We'd like the background color for a .item to change based on the subdirectory where a user is viewing content. So if someone clicks on a nav .item, the href will redirect them to a page and we want that .item to be highlighted a unique hex color that we can customize for each nav .item. All 6 nav items would have a different color.
One point of clarification is that sometimes folks may visit our site without having ever clicked a navigation item. I want the navigation items to still be highlighted based on the current subdirectory where a person is viewing content. This helps them easily identify where they are and how to get back if they navigate to other parts of the community. Also if a person does a global search and stumbles upon content in one of our 6 main areas, we want the nav menu to instantly identify their current location (based on url) and highlight that nav .item in our vertical nav bar.
Is Javascript or Jquery the way to go? Any help would be appreciated!!
Heres a FIDDLE with all the code.
sample CSS:
.navback {
position: fixed;
top: 0;
left: 0px;
width: 100px;
height: 100%;
background: #283237;
z-index: 4;
}
.navbar {
position: fixed;
top: 44px;
left: 0px;
width: 100px;
height: 60vh;
background: #283237;
display: flex;
z-index: 5;
flex-direction: column;
}
.topbar {
border-top: 1px solid #000;
top: 44px;
}
.navbar .item {
flex: 1;
text-align: center;
display: flex;
justify-content: center;
flex-direction: column;
padding-top: 40px;
padding-bottom: 40px;
max-height: 100px;
z-index: 5;
}
.navbar .item div.label {
color: #fff;
position: relative;
top: 5px;
font-size: 13px;
font-family: -apple-system, BlinkMacSystemFont, Helvetica, Arial, "Segoe UI", sans-serif;
transition: all 300ms cubic-bezier(0.68, -0.55, 0.27, 1.55);
left: -100px;
}
Sample HTML:
<div class="topbar"></div>
<div class="navback leftnav">
<div class="navbar">
<div class="item hvr-shrink">
<a href="https://community.canopytax.com/">
<div>
<img src="https://png.icons8.com/ios/35/ffffff/home.png"/>
<div class="label">Home</div>
</div>
</a>
</div>
<div class="item hvr-shrink">
<a href="https://community.canopytax.com/community-central/">
<div>
<img src="https://png.icons8.com/ios/40/ffffff/conference-call.png">
<div class="label">Central</div>
</div>
</a>
</div>

JS/jQuery
// get the first directory by splitting "/dir/path/name" into an array on '/'
// get [1] instead of [0] b/c the first should be blank. wrap in /s.
hereDir = "/" + window.location.pathname.split("/")[1] + "/";
// rebuild the URL since you're using absolute URLs (otherwise just use hereDir)
hereUrl = window.location.protocol + "//" + window.location.host + hereDir;
$(".item")
.find("[href^='" + hereUrl + "']")
.closest(".item").addClass("here");
Note .find("[href^=...]") selects things that start with what you're looking for.
CSS
/* now use .here to style */
.item.here {
background-color: purple;
}
.item.here .label {
font-weight: bold;
}

To answer your question directly, yes this could be done also via JavaScript/jQuery but there is a far simpler way using the css :active selector.
For example, if the user clicks the .item
then the code would be:
.item:active {
background-color: #cecece; // or whatever styling you want
}
Sidenote: As a webdesigner myself, in general i'd advise using the :hover selector when it comes to navbar highlightng instead of the :active one.

Use jquery in your html (https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js)
Add the following script
$('.item').click(function(){
$('.item.active').removeClass("active");
$(this).addClass('active');
})
CSS
.item.active {
background-color: red;
}
Please see updated fiddle

If you are using jQuery you can loop through each anchor and test it against the current URL of the page like this:
$(function highlightCurrentUrl() {
var currentUrl = window.location.href;
var items = $(".item").each(function() {
var anchor = $(this).find('a');
$(this).removeClass('active');
//comparison logic
if (anchor.prop('href') == currentUrl) {
$(this).addClass("active");
}
});
});
What this does is add a class to the matching .item in the menu. (This won't work in JSFiddle due to Content Security policy so you will have to test it your own environment.)
Next, you will need to define the styles that will be applied to an .item.active DIV tag. And, if you want different colors for different items, you should probably give them ID's in you markup, so you can reference them individually:
<div class="item hvr-shrink" id="home-link">
<a href="https://community.canopytax.com/">
<div>
<img src="https://png.icons8.com/ios/35/ffffff/home.png"/>
<div class="label">Home</div>
</div>
</a>
</div>
<div class="item hvr-shrink" id="central-link">
<a href="https://community.canopytax.com/community-central/">
<div>
<img src="https://png.icons8.com/ios/40/ffffff/conference-call.png">
<div class="label">Central</div>
</div>
</a>
</div>
These rules are saying that when the active class is added to the div with the ID home-link or central-link it should have the following properties
#home-link.active {
background-color: blue;
}
#central-link.active {
background-color: green;
}

Related

Darkmode Button

i have implemented a button which should change the background color, into blue from black.
But i didnt find a easy way, how I can do that.
This is my HTML file
<div style="background-color: #161624; width: 100%; height: 20%; "></div <div style="background-color: #efece7; width: 100%; height: 60% "> </div> <div style="background-color: #161624; width: 100%; height: 20%; vertical-align: bottom ; "</div>
<button id="changecolor">Change Color</button>
</div>
I want that the button changecolor, change the background color, where the height is 20% at both.
enter image description here
The blueblack color should change
I would recommend using a seperate css file for this with classes of colors and designs depending on dark/light mode.
simply add a css class of example down below and add some javascript to be able to change the class on the button or element you want to use as the switch.
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
body {
padding: 25px;
background-color: white;
color: black;
font-size: 25px;
}
.dark-mode {
background-color: black;
color: white;
}
<body>
currently this is just pointing to the body but can easily be adjusted to point at any element you would like it to
Just let me know if you have any questions

How can I make my navigation bar links(About, Services, Projects) "stop running off the screen"-make it stop?

When I try to size down my desktop screen navigation size of 1440px(90em) to any lower width screen, my navigation bar links start dropping off the screen. I have tried using some media query combinations, but nothing to show for it.I haven't got much experience with frontend, so I am a little bit thin on this side. Any long-term fixes to this one?Any hint on this one will be highly appreciated.
HTML header code:
<!--header-->
<header>
<nav class="nav__bar">
<a href="#" class="logo">
<img src="./images/logo.svg" alt="Sunnyside logo">
</a>
<ul class="nav__links">
<li class="nav__item">
About
</li>
<li class="nav__item">
Services
</li>
<li class="nav__item">
Project
</li>
Contact
</ul>
<img src="./images/icon-hamburger.svg" alt="toggle menu icon" class="toggle__menu">
</nav>
</header>
CSS header styles:
header {
height: 5em;
position: absolute;
left: 0;
right: 0;
}
.nav__bar {
height: 100%;
width: 90em;
margin: auto;
display: flex;
justify-content: space-between;
align-items: center;
flex: 1 1 auto;
padding: 0 2em;
}
.nav__links {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
list-style: none;
}
.nav__item {
margin: 1em;
}
.nav__link {
text-decoration: none;
font-size: 1.125em;
color: hsl(0, 0%, 100%);
font-family: 'Barlow', sans-serif;
transition: all 350ms ease-in-out;
}
.nav__link:hover {
color: hsl(232, 10%, 55%);
}
.toggle__menu {
cursor: pointer;
display: none;
}
In your example code, you set the color of the link text to white... it's white on white. But that's not fully the problem... you should also remove width:90em from the .nav_bar... it's unnecessary. see this codepen https://codepen.io/aequalsb/pen/jOmyJNp
Just simply allow the <nav> to "be itself"... which is a block level element and naturally attempts to stretch out to fit available width.
padding in CSS Sizes the margin inside a button or element. Try using margin: (how many 'px' it's going off the screen); and I've had this problem before:
SOLUTION 1:
use margin-*left or top*: *px is going off screen*
<style>
#button {
width: 100px; /* the width of the button */
position: absolute;
left: 50%; /* always 50% when centering */
margin-left: -50px; /* minus half the size of the element */
}
</style>
<button id="button">Center of page</button>
SOLUTION 2
i've had this problem before, and in best situations, use position: absolute instead of relative if you are positioning the element.
<head>
<style>
.background {
position: relative;
}
.overlap {
position: absolute;
left: 30px;
}
</style>
</head>
</style>
</head>
<body>
<!-- background-element -->
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/Circle_Rufous_Solid.svg/1200px-Circle_Rufous_Solid.svg.png" class="background" width="10.5%" />
<!-- Overlap element -->
<img src="https://cdn.onlinewebfonts.com/svg/img_24930.png" class="overlap" width="10%" />
</body>
SOLUTION 3
if none of the above works, consider using javascript: device tester command and redirect to an error page with unsupported devices.
This example will detect a handful of mobile-devices, and if so, it'll redirect to 𝘩𝘵𝘵𝘱://𝘨𝘰𝘰𝘨𝘭𝘦.𝘤𝘰𝘮
<script>
if( /Android|webOS|iPhone|iPad|Mahc|Macintosh|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
window.location.href = "http://google.com";
} else {
}
</script>
NOTE: if there is big problem you cannot solve, and none of these work, its best to do research or find some articles to find your answer. Then consider using stackoverflow.

Relative positioning to an absolutely positioned element

I am experimenting with single-page design, and have built a page out of a number of different pages, by essentially creating a column of <div> elements each at 100% of the window height, and positioned absolutely one under another.
Each page, therefore, requires it's elements to be set absolutely, since there is (as far as I can tell) no normal document flow to follow. However, when it comes to text, I am finding it difficult to design. I'd like to try and keep the current design, rather than start over.
On a particular page, I have a text container. In that container, there are two titles. The first title heads two small paragraphs, the second heads one:
HTML
<div id="introTextContainer">
<div id="introTextHeader" class="blurbheader">First title</div>
<div id="introText" class="blurb">
Generic text about the roots of the company
</div>
<div id="introTextParg2" class="blurb">
further information about the roots
</div>
<div id="introStatement1" class="blurb StrongStatement">
Second title
</div>
<div id="introTextParg3" class="blurb">
Eye-catching mission statement
</div>
</div>
CSS
.blurb {
font-family: Lato;
font-size: 16px;
padding-right:10px;
font-size: 1.4vh
}
.blurbheader {
font-family: Lato;
font-size: 2vh;
padding-right:10px;
}
.StrongStatement {
font-family: Lato;
font-size: 20px;
}
#introTextContainer {
height: 20%;
width: 45%;
background: rgba(255,255,255,0.8);
position: absolute;
left: 200px;
top: 10%;
border-radius: 5px;
}
#introTextHeader {
position: absolute;
width: 280px;
left: 30px;
top: 5px;
height: 10%;
}
#introText {
position:absolute;
left: 10px;
font-family: Lato;
}
#introTextParg2 {
position:absolute;
left: 10px;
}
#introStatement1 {
position:absolute;
left: 30px;
}
#introTextParg3 {
position:absolute;
left: 10px;
}
At a standard resolution of 1080p, all of this looks perfectly acceptable. However, when the resolution is changed, the design is not responsive to the same. As you can see with the CSS, I have been experimenting with viewport-height for the text, however the spacing and height of the <div> elements is a different matter.
How can I create a positioning/size context within the scope of the text containers, in order that I can set the height and padding of the text paragraphs therein, within the scope of those containers? The current approach uses javascript, but I don't like the idea of attempting to use javascript to target every screen size possible as this would result in a lot of spaghetti code. Ideally, I would like to use javascript only to set the heights of the "page" <div> elements and the containing boxes.
edit: CSS, JS, HTML of page design
The first page is given a width and height of 100%. The subsequent pages are altered with JQuery. The value data-section-name is used in the seamless scrolling plugin. The canvas is used only to house the background image:
HTML
<section id="pageOne" class="panel pageone" data-section-name="sectionpageOne"> <!-- About -->
<canvas id="pageOneCanvas"></canvas>
<div id="introTextContainer">
<div id="introTextHeader" class="blurbheader">First title</div>
<div id="introText" class="blurb">
Generic text about the roots of the company
</div>
<div id="introTextParg2" class="blurb">
further information about the roots
</div>
<div id="introStatement1" class="blurb StrongStatement">
Second title
</div>
<div id="introTextParg3" class="blurb">
Eye-catching mission statement
</div>
</div>
</div>
</section>
CSS
#pageOne {
position: absolute;
/*top: 0;*/
left: 0;
width: 100%;
}
#pageOneCanvas {
height: 100%;
width: 100%;
z-index: 1;
background-image: url("/Resources/images/aboutcanvas.jpg");
background-size: cover;
opacity: 0.6
}
JS
// Canvas height and positioning
var posheight = $(window).height();
$("#home").height(posheight);
$("#pageOne").height(posheight);
$("#pageTwo").height(posheight);
$("#pageThree").height(posheight);
$("#pageFour").height(posheight);
$("#pageFive").height(posheight);
$("#pageOne").css("top", posheight);
$("#pageTwo").css("top", (posheight * 2));
$("#pageThree").css("top", (posheight * 3));
$("#pageFour").css("top", (posheight * 4));
$("#pageFive").css("top", (posheight * 5));
Additionally, the current solution for fixing the text container dimensions is also in JS. You can start to see why I want to achieve this in CSS, as it's becoming messy here:
var introTextTitleHeight = $("#introTextHeader").height();
$("#introText").css("top", (introTextTitleHeight + 10));
$("#introTextParg2").css("top", ($("#introText").position().top + $("#introText").height() + 10));
$("#introStatement1").css("top", ($("#introTextParg2").position().top + $("#introTextParg2").height() + 10));
$("#introTextParg3").css("top", ($("#introStatement1").position().top + $("#introStatement1").height() + 10));
Would removing position: absolute; on the child divs not do what you want?
And use margin-left instead of left.
So the rules would look like this:
#introTextHeader {
margin-left: 30px;
margin-top: 5px;
height: 10%;
}
#introText {
margin-left: 10px;
font-family: Lato;
}
#introTextParg2 {
margin-left: 10px;
}
#introStatement1 {
margin-left: 30px;
}
#introTextParg3 {
margin-left: 10px;
}

CSS tabs using href

In school we had about 5 minutes left, so I decided to go on W3Schools on random tutorials. And found CSS tab tutorial. It was something like:
span {display: none;}
span:active {display: block;}
Basically, when: <a href="#menu1"> was clicked, it would display: block the element that it has in href meaning, menu 1 would be displayed. The way it was made was somehow that you could add new ones easily, by adding new #id then putting div with that id, nothing had to be changed in CSS. It was about 10-20 lines in CSS too. Anyone know? I seriously need it. There was no JavaScript involved, or JQuery or anything like that.
I'm not sure if I understand your question right, but that sounds for the target technic for me. With CSS3 there is the :target Parameter available. With that you can add a css state, when they're selected via an anchor.
Here is a really simple example, what's the trick behind that.
#contents div {
border: 1px black dotted;
display: none; //hides all div elements
}
#contents div:target {
display: block; //shows the selected div (target)
}
<div id="tabs">
<div id="menu">
Menu 1
Menu 2
Menu 3
Menu 4
</div>
<div id="contents">
<div id="content1">Suspendisse potenti. Mauris in lacinia.</div>
<div id="content2">Pellentesque pulvinar venenatis ante in.</div>
<div id="content3">Vestibulum a nisi viverra, hendrerit.</div>
<div id="content4">Nullam leo ipsum, euismod sed.</div>
</div>
</div>
There's no way of doing this without javascript. Active is a pseudo class meaning it represents the state of an element. That state would need to be set initially or you'd have to add a class of active which again would require javascript to add and remove. Maybe, your tacher didn't discuss the javascript part with you but here is a link that will show you how to do what you are looking for.
http://www.w3schools.com/w3css/w3css_tabulators.asp
EDIT:
Well i did find this js fiddle https://jsfiddle.net/eu81273/812ehkyf/ that is apparently css only. He is actually using input fields and using the 'checked' state which can be set initially in html. So check that out
here you got href:#img1 active view
html,body {
font-family: 'Raleway', sans-serif;
padding: 0 2em;
font-size: 18px;
background: #222;
color: #aaa;
text-align:center;
}
h1 {
font-size: 3em;
font-weight: 200;
margin: 0.5em 0 0.2em 0;
}
p {
margin: 1.5em 0;
color: #888;
}
.italic { font-style: italic; }
.small { font-size: 0.8em; }
/** LIGHTBOX MARKUP **/
.lightbox {
/** Default lightbox to hidden */
display: none;
/** Position and style */
position: fixed;
z-index: 999;
width: 100%;
height: 100%;
text-align: center;
top: 0;
left: 0;
background: rgba(0,0,0,0.8);
}
.lightbox img {
/** Pad the lightbox image */
max-width: 90%;
max-height: 80%;
margin-top: 2%;
}
.lightbox:target {
/** Remove default browser outline */
outline: none;
/** Unhide lightbox **/
display: block;
}
<!-- Lightbox usage markup -->
<link href="http://fonts.googleapis.com/css?family=Raleway:200,100,400" rel="stylesheet" type="text/css" />
<h1>Pure CSS Lightbox</h1>
<p>Click the thumbnail below to activate the lightbox</p>
<!-- thumbnail image wrapped in a link -->
<a href="#img1">
<img src="https://c3.staticflickr.com/8/7315/28008413826_696528d68c.jpg " height="100" width="100" >
</a>
<!-- lightbox container hidden with CSS -->
<a href="#_" class="lightbox" id="img1">
<img src="https://c3.staticflickr.com/8/7315/28008413826_696528d68c.jpg">
</a>
<p class="italic small">Image credit: Me</p>
live demo

Change img src on hover

How do I create an url link prefixed with a small thumbnail-image, such that when I hover on them, BOTH the link color and the thumbnail-image change
Example:
Im now using an image tag that goes with an anchor tag, Im able to change the anchor tag text color on hover, however I dont know how to change the img src accordingly
CSS:
.hoverable-link {
color: gray;
}
.hoverable-link:hover {
color: blue;
}
HTML:
<div>
<img src="thumbnail-1"> //Change to thumbnail-2
Cool Link
</div>
JsFiddle: https://jsfiddle.net/rbb5ow1v/9/
In conclusion:
[1] How can I change img src when it's on hover
[2] How can I trigger hover-event for both element at the same time
I would give fontello.com a go
Once you have chosen the desired icons set up your tag as follows
<span class="icon-twitter"></span>example
When you do the CSS you just have to apply a hover state to the anchor and because of fontello it will change that colour too by just using the CSS color attribute.
EDIT:
If you are using a specific twitter icon that you made. Try changing it to an SVG, and change its fill. Same can be applied to the fontello where you can display none and reveal on hovers.
[1] How can I change img src when it's on hover
You can't do this with CSS alone, as src is a DOM attribute not a CSS attribute, to accomplish this some javascript is required with HTML DOM Event system
<body>
<div>
<img onmouseenter="highlight(this)" onmouseleave="unhighlight(this)"
src="thumbnail1">
Some Link
</div>
<script>
function highlight(image) {
image.src = "thumbnail2"; //Blue Image
}
function unhighlight(image) {
image.src = "thumbnail1"; //Gray Image
}
</script>
</body>
JsFiddle: https://jsfiddle.net/f0c7p3tL/2/
List of DOM Events: http://www.w3schools.com/jsref/dom_obj_event.asp
Another approach is to not using the src DOM attribute at all. Instead you can use the background CSS attribute, that way you can utilize the CSS:hover selector
CSS:
#my-thumbnail {
background: url("/thumbnail1") no-repeat;
width: 32px;
height: 32px;
}
#my-thumbnail:hover {
background: url("/thumbnail2") no-repeat;
}
HTML:
<div>
<img id="my-thumbnail">
Some Link
</div>
JsFiddle: https://jsfiddle.net/7xoprwky/
[2] How can I trigger hover-event for both element at the same time
Again, two approaches are available here.
First is using javascript and the HTML DOM Events. In this approach, instead of triggering events on either of the child elements, we want them to be triggered on the surrounding <div> parent element. Then, in the event handler, we select the child elements and change their DOM Attribute accordingly
<body>
<div onmouseenter="highlight(this)" onmouseleave="unhighlight(this)">
<img id="my-thumbnail" src="thumbnail1">
<a id="my-anchor" href="#potato">Some Link</a>
</div>
<script>
var myThumbnail = document.getElementById('my-thumbnail'),
myAnchor = document.getElementById('my-anchor');
function highlight() {
myThumbnail.src = "/thumbnail2";
myAnchor.style.color = "blue";
myAnchor.style.fontWeight = "bold";
}
function unhighlight() {
myThumbnail.src = "/thumbnail1";
myAnchor.style.color = "gray";
myAnchor.style.fontWeight = "normal";
}
</script>
</body>
JsFiddle: https://jsfiddle.net/2uthconL/
In the second approach we utilize the CSS selector syntax to highlight our internal element from our surrounding div
CSS:
#my-thumbnail-link {
}
#my-thumbnail-link img { /* Select all img tag inside div */
background: url("/thumbnail1") no-repeat;
width: 32px;
height: 32px;
}
#my-thumbnail-link:hover img { /* Select all img tag inside div when it is hovered */
background: url("/thumbnail2") no-repeat;
}
#my-thumbnail-link a { /* Select all a tag inside div */
color: gray;
}
#my-thumbnail-link:hover a { /* Select all a tag inside div when it is hovered */
color: blue;
font-weight: bold;
}
HTML:
<div id="my-thumbnail-link" class="vcenter-parent">
<img class="vcenter-child">
Some Link
</div>
JsFiddle: https://jsfiddle.net/x61dy0mk/2/
More on CSS Selector: http://www.w3schools.com/cssref/css_selectors.asp
If your thumbnail is just a static asset, I recommend the CSS approach over the Javascript HTML DOM one for its readability and maintainability (imagine keeping thousands of event handlers)
maybe you can try this one:
html
css for styling
.twitterbird {
margin-bottom: 10px;
width: 160px;
height:160px;
display:block;
background:transparent url('twitterbird.png') center top no-repeat;
}
.twitterbird:hover {
background-image: url('twitterbird_hover.png');
}
this answer is based on this question CSS: image link, change on hover
Update - Just try this one:
html
<ul>
<li><a id="hoverable" href="#"><i class="home-icon"></i><span class="text">Link 1</span></a></li>
<li><a id="hoverable" href="#"><i class="tshirt-icon"></i><span class="text">Link 2</span></a></li>
</ul>
css
a {
color: black;
text-decoration: none;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.home-icon {
background: url("http://s1.postimg.org/gk5fbl6vv/home_black.png") no-repeat;
width: 32px;
height: 32px;
display: inline-block;
margin-right: 20px;
}
a:hover .home-icon {
background: url("http://s2.postimg.org/43870q29h/home_green.png") no-repeat;
}
.tshirt-icon {
background: url("http://s30.postimg.org/61bqc12fh/tshirt_black.png") no-repeat;
width: 32px;
height: 32px;
display: inline-block;
margin-right: 20px;
}
a:hover .tshirt-icon {
background: url("http://s17.postimg.org/3x9qzn8sb/tshirt_green.png") no-repeat;
}
a#hoverable:hover {
color: green;
font-weight: bold;
}
demo is on this link https://jsfiddle.net/nv4dw8vr/