List item color for anchored section

Hi,

I have a left side fixed menu made of images that link to sepcific image on a specific section of the page, and I would like to have the image that is being viewd to be highlighted (either background, border, or opacity, doesn’t matter).
Is it possible with css only?
How would I do it?

e.g. menu image

<a href="#" class="foto1" data-id="foto1">
<img src="" />
</a>
<a href="#" class="foto2" data-id="foto2">
<img src="" />
</a>

image to be viewed

<div class="col">
<p class="caption">
<a id="foto1">
<img src="path/to/image/1" />
</a>
<a id="foto2">
<img src="path/to/image/2" />
</a>
</p>
</div>

Thank you

CSS can do so many things - this being one of them. This was fun to make.

<!DOCTYPE html>
<html lang="en">
<head>
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<title>Clickable Gallery (CSS ONLY)</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="UTF-8">
<style type="text/css">
.container
{
  max-width:800px;
  margin:0 auto;
  border:1px solid #333;
  padding:1rem;
  position:relative;
  overflow:hidden;
}
.img-hold
{
  float:right;
  margin:100px 10px 10px 0;
  width:300px;
  height:300px;
  background:#fffccc;
  border:1px solid #000;
  padding:3px;
  position:relative;
  z-index:1;
}
ul.links
{
  list-style:none;
  margin:0 20px;
  padding:0;
  border:1px solid #000;
  border-bottom:none;
  width:8rem;
}
.links li
{
  background:blue;
  color:#fff;
  border-bottom:1px solid #000;	
}
.links li a
{
  display:block;
  padding:5px;
  background:green;
  color:#fff;
  text-decoration:none;
}
.links li span
{
  position:absolute;
  right:30px;
  top:-9999rem;
  width:300px;
  height:300px;
}
.links li span:target + a
{
  background:teal;
  color:#000;
}
.links li span:target
{
  top:120px;
}

#img1{z-index:2;}
#img2{z-index:3;}
#img3{z-index:4;}
</style>
</head>
<body>
<div class="container">
	<p class="img-hold"><img src="http://www.pmob.co.uk/temp/images/zimg1.jpg" width="300" height="300" alt="Alternative text" /></p>
	<ul class="links">
		<li><span id="img1"><img src="http://www.pmob.co.uk/temp/images/zimg2.jpg" width="300" height="300" alt="Alternative Text" /></span><a href="#img1">Link 1</a></li>
		<li><span id="img2"><img src="http://www.pmob.co.uk/temp/images/zimg3.jpg" width="300" height="300" alt="Alternative Text" /></span><a href="#img2">Link 2</a></li>
		<li><span id="img3"><img src="http://www.pmob.co.uk/temp/images/zimg4.jpg" width="300" height="300" alt="Alternative Text" /></span><a href="#img3">Link 3</a></li>
	</ul>
</div>
</body>
</html>

Stole images off from @PaulOB ; hope you don’t mind. Also modified a demo he had which used hover, and changed some HTML/CSS around to make it clickable.

Feel free :smile:

Thank you very much.
Really amazing to an illiterate in css like me.

However I am not being able to make it work in my page, if you go over the code I’ll really appreciate it.
I tried playing with it, but no matter what I did I couldn’t get it to work.
If I put your code as it is in the left column it works just fine, but if I move the "img-hold£ to the right, it just breaks, that like other combinations, and play with the code you kindly supplied.
Like said above my illiteracy wins.

Following you can see the actual code of my page, I stripped it of the markup and css that I think isn’t necessary to keep it short, so no worry about it looking awkward.
I’d like the ul (class=“links-sx”) to be on the left column (class=“left”), and the images on the right column (class=“right”)

I don’t mind the solution to have all images swapping in one single box, which is more elegant than my “inline” solution.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>gallery</title>
<link rel="stylesheet" href="css/stylesheet-orange-2.css" />
<!-- Bootstrap core CSS -->
<link href="css/bootstrap-orange-2.css" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy this line! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<style type="text/css">

.logo {
    float: left;
    margin: 0 -100% 0 0;
    padding: 0.45em;
    width: 27%;
}
.top-nav {
    float: left;
    margin: 0 -100% 0 27%;
    width: 73%;
    padding: 0.45em 1.71em;
}

.wrapper {
    color: #fff;
    float: left;
    margin: 115px 0 0 0;
    min-height: 200px;
    width: 100%;
}
.left {
    background: #999;
    color: #fff;
    float: left;
    padding: 9px;
    width: 25%;
    text-align: right;
    height: 100vh;
    position:fixed;
}
.left img {
    width: 27%;
}
a.foto1 {
    border: 1px solid #f00;
}
.right {
    background: #f00;
    color: #fff;
    float: left;
    margin: 0 0 0 27%;
    width: 65%;
    padding: 19px;
    height: 100vh;
}
</style>

</head>
<body>
<!-- Fixed navbar -->
<div class="navbar navbar-default navbar-fixed-top header" role="navigation">
    <div class="row">
        <div class="logo">
            <div id="logo">
            <img src="path-to-logo-image" alt="logo" title="logo" />
            </div>
        </div>
        <div class="top-nav">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                    <a class="navbar-brand" href="#">ABCD</a>
            </div><!--/.navbar-header -->
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#" class="scroll-link" data-id="gallery">Gallery</a></li>
                    <li><a href="#" class="scroll-link" data-id="about">about</a></li>
                    <li><a href="#" class="scroll-link" data-id="general">general</a></li>
                    <li><a href="#" class="scroll-link" data-id="contact">Contact</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </div><!--/.row -->
</div><!--/.navbar navbar-default navbar-fixed-top header -->

<div class="wrapper">
    <div class="left">
        <ul class="links-sx">
            <li>
                <a href="#img-1" class="scroll-link foto1" data-id="img-1">
                <img src="path-to-small-image-one" alt="image one" />
                </a>
            </li>
            <li>
                <a href="#img-2" class="scroll-link foto1" data-id="img-2">
                <img src="path-to-small-image-two" alt="image two" />
                </a>
            </li>
            <li>
                <a href="#img-3" class="scroll-link foto1" data-id="img-3">
                <img src="path-to-small-image-three" alt="image three" />
                </a>
            </li>
            <li>
                <a href="#img-4" class="scroll-link foto1" data-id="img-4">
                <img src="path-to-small-image-four" alt="image four" />
                </a>
            </li>
            <li>
                <a href="#img-5" class="scroll-link foto1" data-id="img-5">
                <img src="path-to-small-image-five" alt="image five" />
                </a>
            </li>
        </ul>
    </div><!--/ .left -->
    <div class="right">

    <section>
        <div id="about" class="page-section">
            <div class="container">
                <h1>about</h1>
                <p class="caption">
                <b>about us infos.</b>
                </p>
            </div><!--/.container-->
        </div><!--/#about-->
    </section>

    <section>
        <div id="general" class="page-section">
            <div class="container">
                <h1>general</h1>
                <p class="caption">
                <b>general infos.</b>
                </p>
            </div><!--/.container-->
        </div><!--/#general-->
    </section>

    <section>
        <div id="gallery" class="page-section">
            <div class="container">
            <h1 class="h1">Gallery<small class="required"> samples</small></h1>

            <div class="row">
                <div class="col-md-9">
                    <p class="caption">
                        <a id="img-1">
                        <img src="path-to-LARGE-image-one" alt="image one" />
                        </a>
                    </p>
                    <p class="caption">
                        <a id="img-2">
                        <img src="path-to-LARGE-image-two" alt="image two" />
                        </a>
                    </p>
                    <p class="caption">
                        <a id="img-3">
                        <img src="path-to-LARGE-image-three" alt="image three" />
                        </a>
                    </p>
                    <p class="caption">
                        <a id="img-4">
                        <img src="path-to-LARGE-image-four" alt="image four" />
                        </a>
                    </p>
                    <p class="caption">
                        <a id="img-5">
                        <img src="path-to-LARGE-image-five" alt="image five" />
                        </a>
                    </p>
                </div>
            </div>

            </div><!--/#container-->
        </div><!--/#gallery-->
    </section>

    <aside>
        <div class="page-section">
            <div class="container">
                <hr />
                <div class="row">
                    <div class="col-md-6">
                        <ul class="list-unstyled">
                            <li><a class="credits" href="#" class="scroll-link" data-id="gallery">Gallery</a></li>
                            <li><a class="credits" href="#" class="scroll-link" data-id="about">about</a></li>
                            <li><a class="credits" href="#" class="scroll-link" data-id="general">general</a></li>
                            <li><a class="credits" href="#" class="scroll-link" data-id="contact">Contact</a></li>
                            </ul>
                        </div>
                    <div class="col-md-6">
                        <ul class="list-unstyled">
                            <li>Extra</li>              
                        </ul>
                    </div>
                </div>
                <hr />
                <div class="row">
                    <div class="col-md-12">
                    &copy; 2015, All rights reserved
                    </div>
                </div>
            </div><!--/.container-->
        </div><!--/.page-section-->
    </aside>

    </div>
</div>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    // navigation click actions    
    $('.scroll-link').on('click', function(event){
        event.preventDefault();
        var sectionID = $(this).attr("data-id");
        scrollToID('#' + sectionID, 750);
    });
    // scroll to top action
    $('.scroll-top').on('click', function(event) {
        event.preventDefault();
        $('html, body').animate({scrollTop:0}, 'slow');         
    });
    // mobile nav toggle
    $('#nav-toggle').on('click', function (event) {
        event.preventDefault();
        $('#main-nav').toggleClass("open");
    });
});
// scroll function
function scrollToID(id, speed){
    var offSet = 50;
    var targetOffset = $(id).offset().top - offSet;
    var mainNav = $('#main-nav');
    $('html,body').animate({scrollTop:targetOffset}, speed);
    if (mainNav.hasClass("open")) {
        mainNav.css("height", "1px").removeClass("in").addClass("collapse");
        mainNav.removeClass("open");
    }
}
if (typeof console === "undefined") {
    console = {
        log: function() { }
    };
}
</script>

  </body>
</html>

Thank you

Would you happen to have this on a live website? I only have access to a chromebook for the weekend so making local copies for testing is out of the picture for a couple days.

Thank you.

I uploaded here the files, I also zipped all the files for your convenience.
holytablets dot org/gallery_test
holytablets dot org/gallery_test/gallery_test.zip

You don’t have any id=“img-1/2/3” in your code for the anchors to target your selection. The anchors have the href at “img-1” /2 /3 etc. There is no id matching these hrefs.

Thank you for your patience.

I had tried them, and had removed them for you to see my actual markup, indeed in the live example there is no span either.

I just readded them, but still no joy, most probably I am adding them in the wrong place, and/or order.

In your code you have the the image “box” (class=“img-hold”), and then you have the ul, and it works as you planned, but in my markup where I have ul in the fixed left column, and put the images on the scrolling right column it breaks, and I am sure it’s because I am doing it wrong.
I have tried different combinations, but still no success.

I google some more, would you say it has to do with bootstrap js (which I am using), I read this, but it’s even more uncomprehensible:

I also found this which comes close to what I need, though not exactly a fixed column:
http://callmenick.com/lab-demos/7-single-page-smooth-scroll/

Unless I’m misunderstanding the problem you just need the ids on the images as Ryan pointed out.

e.g. Here;

<div class="row">
            <div class="col-md-9">
                <p class="caption">
                <img id="img-1" src="http://holytablets.org/gallery_test/images_test/slider_test_01.jpg" alt="test 1" />
                </p>
            </div>
            <div class="col-md-3">
            <p class="caption">
            test 01
            </p>
        </div>
        </div>
        <div class="row">
            <div class="col-md-9">
                <p class="caption">
                <img id="img-2" src="http://holytablets.org/gallery_test/images_test/slider_test_02.jpg" alt="test 2" />
                </p>
            </div>
        <div class="col-md-3">
            <p class="caption">
            test 02
            </p>
        </div>
        </div>
        <div class="row">
            <div class="col-md-9">
                <p class="caption">
                <img id="img-3" src="http://holytablets.org/gallery_test/images_test/slider_test_03.jpg" alt="test 3" />
                </p>
            </div>

The id is in each image here: <img id=“img-1” etc… (and then img-2 and img-3)

On another matter you have errors here where you have put 2 class attributes on elements.

<ul class="list-unstyled">
                            <li><a class="credits" href="#" class="scroll-link" data-id="gallery">Gallery</a></li>
                            <li><a class="credits" href="#" class="scroll-link" data-id="about">about</a></li>
                            <li><a class="credits" href="#" class="scroll-link" data-id="general">general</a></li>
                            <li><a class="credits" href="#" class="scroll-link" data-id="contact">Contact</a></li>
                            </ul>

If you have two classes to apply to an element then use a space separated list.

class=“credits scroll-link”

 <li><a class="credits scroll-link" href="#"  data-id="contact">Contact</a></li>
1 Like

Thank you.

I had tried your suggestion, among other combinations, and it doesn’t work, you can check it at the link, as I updated the file as per your suggestions; as you can see it works, but it displays the images on the left fixed column, not on the scrolling side (page), as intended.

Thank you for pointing out the error, about two class attributes.

I tried the suggestion in the getbootstrap.com to use this

<body data-spy="scroll" data-target=".ab-navbar">

But I can’t get it, for sure it’s me who is missing something.
If you’d like to take a look at that test it’s here (with the double class corrected)
holytablets dot org/gallery_test_2/

You’ve added the ids to other elements as well and ids must be unique. Remove the ids from here:

e.g. <span id="img-1">, <span id="img-2">, <span id="img-3">

<span id="img-1">
                    <img src="images_test/slider_test_01.jpg" alt="test 1" /></span><a href="#img-1">link 1
                    </a>
                    </li>
                    <li>
                    <span id="img-2">
                    <img src="images_test/slider_test_02.jpg" alt="test 2" /></span><a href="#img-2">link 2
                    </a>
                    </li>
                    <li>
                    <span id="img-3">
                    <img src="images_test/slider_test_03.jpg" alt="test 3" /></span><a href="#img-3">link 3
                    </a>
                    </li>
                    </ul>

The id is the target of the href and now the browser doesn’t know where its going because it has 2 ids the same :slight_smile:

Thank you for your patience.

With all the combinations I tried, no wonder I forgot something.
However, I did remove the span with the IDs, and it does take me to the intended image, but the link on the left is not highlighted, which is what I am trying to achieve.

I got it working in my demo because of how I did the source order. You wouldn’t be able to do this in CSS with the way things are laid out.

Gonna need Javascript for this (unless Paul sees something I don’t.)

I tried throwing in an element you could use to position over the list item as sort of a layover but the code won’t allow for it.

I’d wait for Paul to chime in but I honestly don’t see a CSS only way with your current structure.

That’s not what you said in post #1 :smile:

You said you wanted the image highlighted not the link. You would need js to highlight the link itself.

Something like this:

$(document).ready(function() {
    // add class to link when clicked   
    $('body').on('click','.ab-links-sx li a', function(event){
            $('.navon').removeClass('navon');
            $(this).addClass('navon');
    });
});

Then in the CSS:

.ab-links-sx li a.navon{outline:1px solid red}

When I first tested the page (locally after adding my original fixes) selecting the link on the left sent you to the image with a nice scroll effect but you seem to have removed that now.

Also not quite sure what you are doing with the tiny image next to the links on the left.

Thank you both, I really appreciate.

I’ll try your script, and report back.

That’s not what you said in post #1

I apologize, I just reread it, and you are absolutely right, although Ryan intended it the way I needed, in my defense I should say English is not my first language, and I di state it in a confusing way, what I meant was:

and I would like to have the link image, of the image menu, that is being viewd to be highlighted

Sorry again.

You are right, the scrolling was smooth, and seems gone with the changes made trying to follow this thread - NOT saying because of your suggestions, I am sure I messed something, or there is some incompatibility; I’ll try to start over from the original state, and go step by step to find out (hopefully) the culprit.

Also not quite sure what you are doing with the tiny image next to the links on the left.

I am trying to have images instead of text for the links, basically thumbnails to be clicked to go to the larger image down the page.

Ok, great, with Paul’s js now the links are highlighted, and I just need to tweak them to display nicely.

For someone looking for similar solution:
I first put it in the <head>, then before, and after the <ul>, and it worked when I put it at the end just before <body>.

There is one small issue for which I will need your help, maybe I’ll open a new thread, and hope you guys will be there.

As per the smooth scroll it was due to this:

No scrolling class

<li>
<img src="images_test/slider_test_03.jpg" alt="test 3" /><a href="#img-3">link 3</a>
</li>

With scrolling class

<li>
<a href="#img-3" class="scroll-link foto1" data-id="img-3">
<img src="images_test/slider_test_03.jpg" alt="test 3" />link 3
</a>
</li>

Thank you very much for your help, and patience.

You can add your question here if it’s basically the same topic rather than start a new thread (unless the question is completely unrelated).

Thank you.

The issue is that when moving away from the last clicked image the highlight stays on, I mean you click on the image, and then decide to go to another section of the page although you are not viewing the image the link image is still highlighted.

Yeah, you’d need Javascript for that to detect what part of the screen you are viewing.