How to have the class=“selected” depending on what the current page/url is


Tags: php,javascript,html,css,html5

Problem :

This is my first post so forgive as I am just new in the world of web development.

Normally, when I try to make a website, I create a file called header.html and footer.html so that I only change data once in all of the pages rather than having multiple same headers on many html files. And include them all in a php file together with the content and the php codes that comes per page.

Now my problem is because I only have 1 header, the css is designed in a way that whatever the current menu/tab is, it will be marked as "selected" so that its obvious to the user what page they are currently in.

My question is how do I solve this problem:

1.) To have the class="selected" depending on what the current page/url is.

<!--Menu Starts-->
        <div class="menu">
            <div id="smoothmenu" class="ddsmoothmenu">
                <ul>
                    <li><a href="index.php" class="selected">Home</a></li>
                    <li><a href="about.php">About</a> </li>
                    <li><a href="services.php">Services</a> </li>
                    <li><a href="features.php">Features</a></li>
                    <li><a href="#">Support</a>
                        <ul>
                            <li><a href="support1.php">Support 1</a></li>
                            <li><a href="support2.php">Support 2</a></li>
                         </ul>
                    </li>
                </ul>
             </div>
        </div>
<!-- Menu Ends--!>

Thank You :)



Solution :

If you're looking for a non-javascript / php approach...

First you need to determine which nav-link should be set as active and then add the selected class. The code would look something like this

HTML within php file

Call a php function inline within the hyperlink <a> markup passing in the links destination request uri

<ul>
    <li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
    <li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a> </li>
    <li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a> </li>
    <li><a href="features.php" <?=echoSelectedClassIfRequestMatches("features")?>>Features</a></li>
    <li><a href="#">Support</a>
        <ul>
            <li><a href="support1.php" <?=echoSelectedClassIfRequestMatches("support1")?>>Support 1</a></li>
            <li><a href="support2.php" <?=echoSelectedClassIfRequestMatches("support2")?>>Support 2</a></li>
         </ul>
    </li>
</ul>

PHP function

The php function simply needs to compare the passed in request uri and if it matches the current page being rendered output the selected class

<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
    $current_file_name = basename($_SERVER['REQUEST_URI'], ".php");

    if ($current_file_name == $requestUri)
        echo 'class="selected"';
}
?>

    CSS Howto..

    How to change background while changing visiblity?

    How to make comment shape using css

    How to make layout with 3 elements where 1 is resizable

    How to control child div height of a flex item

    How do I gather a class' css attributes given nothing but the className?

    Website elements go haywire when the user zooms in or out. How do I keep them from moving?

    Flexbox: how to get divs to fill up 100% of the container width without wrapping?

    How can I add a custom HTML map marker to a Nokia HERE map?

    How to get CSS transform rotation value in degrees with JavaScript

    How to make a scrolling website mobile friendly?

    How to rotate a chart 90 degrees including the text values?

    How to Hide a Textbox with only CSS

    How to style select crossbrowser [duplicate]

    How to revert back to Desktop website from Mobile Website when using CSS Media Queries?

    jquery: how to determine if a style is set via css class or hardcoded by style attribute? [duplicate]

    How to arrange this php if-statement which outputs ratings (good, great, exxelent, etc.)?

    how to change the CSS Class of the asp label dynamically based on its value

    How to get bean property on css file?

    How to remove the default active tab color when another tab is active using jquery

    How do I keep a grid-type-arrangement of floating div elements centered in their container at various screen widths? [closed]

    How to keep links and hover effects on my icon-fonts isolated with CSS

    How to fix unable to load css file in maven build on maven springmvc project [closed]

    How to animate through stacked images Javascript

    How to use icon images?

    How can I display links in 2 rows?

    Slideshow Glitch - Debugging Assistance

    How to make controlls for epub using HTML 5,JavaScript and CSS?

    How to mark up a tiled layout with CSS?

    how to indent html output using CSS

    How to feather the edges of an image using CSS