How do I style a span to look like a link without using javascript?

HtmlCss

Html Problem Overview


For my website I will need to use <span> instead of <a>, because I am using mostly ajax and thus instead of links I have onclick ajax events as attributes in my spans.

As a result, I had to manually style the spans to look like links. I have used hover and visited pseudo classes to change background and text colour, but to change the mouse default to a pointer finger on hover, will I need to use javascript? Or can I do that using css?

Also, I have just realized: I could not just use the <a> tag anyways instead of <span>, but just instead of an href, I would include an onclick? It should work just the same, no?

Html Solutions


Solution 1 - Html

span {
     cursor:pointer;
     color:blue;
     text-decoration:underline;
}

<a href="#">Hyperlink</a><br />
<span>Span</span>

Additionally, you can use :hover pseudo-class to style the element when hovered (you can use any styles not just the ones originally used). For example:

span:hover {
     text-decoration:none;
     text-shadow: 1px 1px 1px #555;
}

Solution 2 - Html

Note that if your website is public and you are counting on search engines to crawl your site, you lose a lot by leaving out links without href since spiders have nothing to grab on while crawling your page.

You should use a complete link - in case your javascript breaks down the user is still able to navigate through pages:

<a href="http://www.example.com">Link</a>

than you can disable the link with jquery by using [preventDefault()][1] - and you totally separated base html and the javascript part, meaning your site is still usable without javascript on.

Than you don't need to bother with span hover and anything - but just for the sake of it

span:hover {
cursor:pointer;
}

will enable hover hand cursor on hovered span. [1]: http://api.jquery.com/event.preventDefault/

Solution 3 - Html

Just add cursor:pointer; in your span css.

Solution 4 - Html

#Option1

Just use an anchor link as follows:

<a href="#" onclick="someFunction();"> Link </a>

#Option2

I don't know why you would wanna use span , but if you do you can do the following styles to make it look similar to an anchor link.

span {
    color: #000000; /* Change this with links color*/
    cursor: pointer;
    text-decoration: underline;
}

span:hover {
    color: #444444; /* Change the value to with anchors hover color*/
}

Solution 5 - Html

Use CSS to display the cursor as a pointer:

<span style="cursor: pointer;">Pseudolink</span>

http://jsfiddle.net/kkepg/

Solution 6 - Html

You could use an anchor. But within javascript you'd have to use event.preventDefault() But there is a CSS method thats smaller and easier. Keep your span and use this:

span:hover{
    cursor:pointer;
}

Solution 7 - Html

You can change the cursor to a pointer by specifying the cursor: pointer CSS rule.

You can also use <a> tags instead of <span>, in fact they can behave nicer with screen readers and other similar devices. You don't need to leave out the href attribute if you use the preventDefault() and stopPropagation() JavaScript functions in the onClick handler. This way you can retain some level of backward compatibility with non-JS enabled browsers.

Solution 8 - Html

You could use a button instead of span and use bootstrap css classes to style it like a link:

<button class="btn btn-link">Link</button>

It will react on mouseOver as normal links do. Run the code snippet to preview the result:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<button class="btn btn-link">this is my link styled with bootstrap</button>

Solution 9 - Html

You can use an onClick event but if I remember correctly, you must return false in order to prevent your page from jumping around; something like:

<a href="#" onClick="myfunction();return false;">

or: <a href="#" onClick="return myfunction();"> provided that myfunction will return false.

You can also directly call a javascript from href but you must cast the result to void in order to block to the browser to try to follow the result as a valid link:

<a href="javascript:void(myFunction())">

Even if you still want to use the onClick property; it would still be a good idea to replace the href="#" with href="javascript:void(0)" ...>.

Other people have mentionned using the event.preventDefault() and stopPropagation(). I don't remember ever using one of these but I must admit that it has been many years since the last time that I have coding some javascript in a HTML link; so you should definitely investigate the use of these two functions.

EDIT: maybe that using a href="javascript:void(0)" could be a bad idea sometimes; see http://drupal.org/node/1193068 .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1787489View Question on Stackoverflow
Solution 1 - HtmlDharmanView Answer on Stackoverflow
Solution 2 - HtmleasweeView Answer on Stackoverflow
Solution 3 - HtmlMuhammad BilalView Answer on Stackoverflow
Solution 4 - HtmlAmythView Answer on Stackoverflow
Solution 5 - HtmlStefan NeubertView Answer on Stackoverflow
Solution 6 - HtmlbashleighView Answer on Stackoverflow
Solution 7 - HtmlKaivosukeltajaView Answer on Stackoverflow
Solution 8 - HtmlFerdinandView Answer on Stackoverflow
Solution 9 - HtmlSylvainLView Answer on Stackoverflow