Tizag Webmaster Forums  

Go Back   Tizag Webmaster Forums > User Submissions > Tutorial Forum

Reply
 
Thread Tools Display Modes
  #1  
Old 04-08-2007, 12:21 PM
MFSP's Avatar
MFSP MFSP is offline
Newbie
 
Join Date: Apr 2007
Posts: 28
MFSP will become famous soon enough
[CSS3] :target Tutorial

There's an interesting new method I discovered to highlight a portion of text when a user clicks on a link. It's based entirely off CSS and the :target selector, which, unfortunately, was first introduced in a CSS3 specification and thus very few browsers support it. However, the way it works degrades somewhat well in older browsers, because it will jump them to the portion of text you want them to read, even if it won't highlight it. As far as I know, right now this only works in Firefox and Safari.

First, you have an anchor to somewhere in your webpage, like this:

HTML Code:
<p>Click on <a href="#linktostuff">this link</a>.</p>
.
.
.
.
<ul>
<li id="linktostuff">Very Important Stuff<li>
</ul>
Then, merely add the following to the head of your page or to your external stylesheet:

HTML Code:
<style type="text/css">
:target {
background: #ffff7e;
}
</style>
And that's it! In older browsers, the link will just move their view to where you want them to look, but in newer standards-compliant browsers users will see the text highlighted. The :target selector should soon become more widespread as CSS3 is fully introduced.

You can see an example of this right here.
Reply With Quote
Sponsored Links
  #2  
Old 04-08-2007, 12:25 PM
Nile's Avatar
Nile Nile is offline
Teacher
 
Join Date: Feb 2007
Posts: 1,617
Nile has a spectacular aura aboutNile has a spectacular aura about
Send a message via MSN to Nile Send a message via Yahoo to Nile
lol!
Thats pretty cool!
Always wanted to know how to do that!
+rep!
__________________
Currently hiding Brian's key.
-Nile, I try to highlight my code.
([x]HTML, CSS) PHP, My[SQL], Javascript, C#, AS 3 [learning]
Reply With Quote
  #3  
Old 04-08-2007, 12:28 PM
BBarash's Avatar
BBarash BBarash is offline
Teacher
 
Join Date: Nov 2006
Location: London, England
Posts: 2,024
BBarash has a spectacular aura about
Send a message via MSN to BBarash Send a message via Skype™ to BBarash
Hey, Ive never seen this before. Pretty sweet.
__________________
Benji Barash - 16
Currently dabbling with Objective-C & Cocoa Touch
@BBarash | BBarash.co.uk
Reply With Quote
  #4  
Old 04-08-2007, 12:34 PM
techietim's Avatar
techietim techietim is offline
Moderator
 
Join Date: Jul 2006
Location: Canada -- Home of Beavers and Maple Syrup
Posts: 4,664
techietim is just really nicetechietim is just really nicetechietim is just really nicetechietim is just really nice
Of course, I have to come up with my own method
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Hello!</title>
  <style type="text/css">
  body{
    width: 500px;
  }
  </style>
  <script language="JavaScript" type="text/javascript">
  function highl(a){
    document.getElementById(a).style.background = "#FFFFBF";
    document.getElementById(a).style.padding = "5px";
  }
  </script>
</head>
<body>
<p><a href="#hi" onclick="highl('hi');">go</a></p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque sed arcu. Duis at erat sed tellus convallis molestie. Vestibulum aliquam. Nulla quis erat. Sed et purus id purus cursus viverra. Integer tincidunt, pede a pharetra varius, dui purus imperdiet nulla, feugiat condimentum nisi nulla at nulla. Mauris vitae orci. Sed pede erat, consectetuer quis, porta vel, porta eu, nibh. Integer non lorem nec nunc ultricies euismod. Cras consectetuer dolor eu diam. Morbi semper scelerisque metus. Nullam ultricies dui eu enim. In hac habitasse platea dictumst. Morbi lorem erat, posuere non, vehicula ut, iaculis dignissim, leo. Nunc fringilla pede vel dolor. Nunc lacinia, eros eget adipiscing condimentum, justo orci fermentum arcu, euismod tincidunt nunc nisi ut sem.
</p>
<p>
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi leo dolor, cursus a, pharetra vel, consequat in, ipsum. Aenean neque risus, facilisis ut, molestie a, viverra ac, lacus. Cras rutrum tempor tellus. Sed eget nunc. Aliquam pulvinar. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus mollis semper quam. Aliquam volutpat urna non dolor. Proin turpis erat, bibendum sed, vulputate in, placerat ac, augue. In in urna. Nam neque arcu, elementum non, tempus ac, luctus non, nibh. Quisque sem sem, commodo sit amet, iaculis non, elementum vel, quam. Pellentesque vel dolor non felis tincidunt sollicitudin. Phasellus a elit eu mi accumsan tristique. Donec pretium tortor vel urna. Integer lectus velit, commodo sit amet, semper vestibulum, feugiat at, arcu. Phasellus vel magna.
</p>
<p>
Sed consequat quam sit amet ante. Aenean feugiat, sem nec scelerisque luctus, orci enim cursus neque, a luctus tellus tellus euismod velit. Aliquam sed erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nunc ultricies nisl quis pede. Suspendisse sem eros, viverra nonummy, pretium id, varius vitae, ipsum. Duis nibh leo, vulputate quis, egestas ac, porta sit amet, lacus. Nulla facilisi. Sed turpis ligula, pulvinar tincidunt, feugiat eget, iaculis nec, felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nam metus risus, faucibus aliquam, congue ut, consectetuer sit amet, odio. Donec pretium ante sit amet sapien. Praesent eu turpis. Maecenas ipsum. Suspendisse scelerisque nisi ut ipsum. Cras lacus. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In hac habitasse platea dictumst.
</p>
<p>
Donec dignissim, tortor sit amet elementum varius, justo elit sodales est, eget bibendum justo erat nec elit. Praesent porttitor, risus non dapibus mattis, ligula sem congue pede, pretium blandit leo pede sit amet justo. Fusce eu turpis. <span id="hi">Maecenas ullamcorper. Suspendisse potenti.</span> Donec quis pede vel odio eleifend adipiscing. Donec felis. Praesent massa ante, feugiat eu, faucibus sed, porta volutpat, sem. Duis vel felis. Suspendisse sit amet lacus vel mauris ornare mollis. Integer lacinia felis vitae quam. Maecenas volutpat placerat mi. Quisque lorem leo, tempor ut, hendrerit a, tristique dictum, ligula.
</p>
</body>
</html>
DEMO
__________________
techietim - jcite
<><
Reply With Quote
  #5  
Old 04-08-2007, 12:35 PM
BBarash's Avatar
BBarash BBarash is offline
Teacher
 
Join Date: Nov 2006
Location: London, England
Posts: 2,024
BBarash has a spectacular aura about
Send a message via MSN to BBarash Send a message via Skype™ to BBarash
Even coolier! Nice one.
__________________
Benji Barash - 16
Currently dabbling with Objective-C & Cocoa Touch
@BBarash | BBarash.co.uk
Reply With Quote
  #6  
Old 04-08-2007, 12:36 PM
MFSP's Avatar
MFSP MFSP is offline
Newbie
 
Join Date: Apr 2007
Posts: 28
MFSP will become famous soon enough
Quote:
Originally Posted by techietim View Post
Of course, I have to come up with my own method
The advantage of my method is that it will work even with javascript disabled, since it's pure CSS.
Reply With Quote
  #7  
Old 04-08-2007, 12:37 PM
techietim's Avatar
techietim techietim is offline
Moderator
 
Join Date: Jul 2006
Location: Canada -- Home of Beavers and Maple Syrup
Posts: 4,664
techietim is just really nicetechietim is just really nicetechietim is just really nicetechietim is just really nice
True, but having JavaScript disabled is less likely then having a non CSS3 compliant browser.
__________________
techietim - jcite
<><
Reply With Quote
  #8  
Old 04-08-2007, 12:42 PM
MFSP's Avatar
MFSP MFSP is offline
Newbie
 
Join Date: Apr 2007
Posts: 28
MFSP will become famous soon enough
Quote:
Originally Posted by techietim View Post
True, but having JavaScript disabled is less likely then having a non CSS3 compliant browser.

Well, that will change in a few months. In the meantime, perhaps

HTML Code:
<style type="text/css"> :target { background: #ffff7e; } </style>
<!--[if IE]>
  <script language="JavaScript" type="text/javascript">
  function highl(a){
    document.getElementById(a).style.background = "#ffff7e";
    document.getElementById(a).style.padding = "5px";
  }
  </script>
<![endif]-->
IE will ignore the CSS that it dosen't know, but see the javascript and use that. Firefox and Safari will ignore the javascript, and use the CSS.
Reply With Quote
  #9  
Old 04-08-2007, 12:45 PM
Nile's Avatar
Nile Nile is offline
Teacher
 
Join Date: Feb 2007
Posts: 1,617
Nile has a spectacular aura aboutNile has a spectacular aura about
Send a message via MSN to Nile Send a message via Yahoo to Nile
Hey!
I thought that when you highlighted a word you got tp choose the color of the highlightation, but how did you get it so when you highlighted the word it would become your color that you wanted it to become?
__________________
Currently hiding Brian's key.
-Nile, I try to highlight my code.
([x]HTML, CSS) PHP, My[SQL], Javascript, C#, AS 3 [learning]
Reply With Quote
  #10  
Old 04-08-2007, 12:47 PM
MFSP's Avatar
MFSP MFSP is offline
Newbie
 
Join Date: Apr 2007
Posts: 28
MFSP will become famous soon enough
Quote:
Originally Posted by Nile55 View Post
Hey!
I thought that when you highlighted a word you got tp choose the color of the highlightation, but how did you get it so when you highlighted the word it would become your color that you wanted it to become?

You just change the color value in the :target CSS or javascript to whatever color you want. What you're really doing is changing the background color on the linked object, not actually 'highlighting' the text.

Last edited by MFSP; 04-08-2007 at 12:50 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 01:09 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
© 2008, Tizag