-2

So i was wondering if there was a way to use html and css to change a piece of text based on what page you are on, i.e if you are on: home page, the text would display "home page". i don't want to have to change what it says on each page, i was hoping i could get it changing on it's own.

I dont want it to be like this;

<div>
<ul>
<li>Current Page:</li>
<li>Home Page</li>
</ul>
</div>  

I want it to be automatic, each age has almost the same lines of code.

Is this possible? or am i just and idiot?

Nash
  • 69
  • 1
  • 1
  • 6
  • How would the HTML/CSS know which page the user is on? You'd have to add something like a class dynamically, and if you're doing that, you might as well change the text directly instead. – JJJ May 29 '16 at 09:44
  • Rather use JavaScript to do something like this, not html or css. – Vernon May 29 '16 at 09:45
  • So it's possible, but just not worth doing? – Nash May 29 '16 at 09:46

1 Answers1

0

In short: It is not possible to do that in HTML or CSS.

HTML isn't a programming language. It's a markup language, defining how browsers should render your page. The same goes for CSS. It defines how elements with a certain class or id should be rendered, but they can't do if ... else ... tests like a programming language would.

What can you do? You can use javascript. With javascript you can check which page you're on, and based on this result, you can either change the text contents, or change the classes. By changing the classes of an element, you could activate or deactivate certain css rules as you wish. It's fairly easy to implement this in javascript, as you'll find lot's of examples on the internet, and for example jQuery can make it easier for you to alter the page contents. To get the current page in javascript, see this answer: Get current URL in web browser

Another alternative would be using a server-side programming language like PHP. You could change the header depending on which page was requested. However, this might be a little overkill if you only need to change the header.

Community
  • 1
  • 1
Bertware
  • 297
  • 8
  • 16
  • I see. Thanks for a really thorough answer. I guess i'll just have to be satisfied with what i had before xD – Nash May 29 '16 at 10:36