21

I stumbled upon the following snippet in a source code of a web site.

<link href="#" id="colour-scheme" rel="stylesheet">

What does this do?

DarthVader
  • 52,984
  • 76
  • 209
  • 300

9 Answers9

3

This does actually absolutely nothing except staying on the same page.

This comes from the Anchors that allow to jump on a part of a page (More specifically, on an id).

This is usually written to say that some link should be introduced here, because of its no-effectness. When you're coding a website, it's often useful to show links, even if the page the link refers to isn't yet existing. This is very often meant to be a temporary solution.

As specified in Ryan's and Tom's answers, it could also be to be used to load dynamically the CSS files.

Community
  • 1
  • 1
Jerska
  • 11,722
  • 4
  • 35
  • 54
3

Without a base element, it does not do anything, except consume the browser’s resources a little. By URL specifications, # as a URL is a reference to the start of the document at the current base URL. Since the tag would have to be in an HTML document, it would fail (either because the document is served with an HTML media type or after the browser has in vain tried to parse HTML with a CSS parser).

If you use a base tag that sets the base URL to one that refers to a CSS document, it would technically work, e.g.

<!doctype html>
<title>Demo</title>
<base href="http://www.cs.tut.fi/~jkorpela/basic.css">
<link href="#" id="colour-scheme" rel="stylesheet">
<h1>Hello world</h1>

This would be rather abnormal, really, and it would effectively prevent you from using relative URLs otherwise in the document. So this is just a theoretical possibility, rather than what’s really going on.

Probably href="#" is just a placeholder here, to be overwritten by JavaScript code, or something. It’s bad coding style for several reasons. It would be better to omit the href attribute (even though this is technically invalid in HTML5) and have one inserted dynamically.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
2

Using a # in a link tag is commonly used to allow you to use javascript with later on if the URL is unknown or doesn't need to be set by default.

Example:

HTML

<link href="#" id="colour-scheme" rel="stylesheet">

JS

document.getElementById("colour-scheme").href="red.css"; 

This allows you to set the URL of the stylesheet in JS rather than statically set the location.

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
1

Probably some stylesheet that is to be loaded later on.

Gustav
  • 1,361
  • 1
  • 12
  • 24
1
href = uri

This attribute specifies the location of a Web resource, thus defining a link between the current element (the source anchor) and the destination anchor defined by this attribute.

Rubyist
  • 6,486
  • 10
  • 51
  • 86
1

Using a hash (#) as the reference is often done by developers yet to include the actual reference when it's not known, however if this is on a live website it may be that JavaScript is being used to load a stylesheet based on the users colour-scheme choice. Before they've made that choice no colour scheme is required so no reference is given, hence the #.

0

Generally we use to call our css file for example below. Suppose I have a html file and I want to call my external css file, at that time I need to use . For more information please check this link http://www.w3schools.com/tags/att_link_href.asp

Swarnamayee Mallia
  • 2,004
  • 14
  • 11
0

My Guess, as per the html link tags its mainly used to link the external files like

href="theme.css"

Since you are using href="#" it would not do anything / serve any purpose.

Vinit Kadkol
  • 1,221
  • 13
  • 12
0
<link href="#" id="colour-scheme" rel="stylesheet">

href : This is to specify the location of the CSS file you want to import in your web page

when using href="#" it won't import any CSS file.

Aravind30790
  • 974
  • 9
  • 22