![]() |
|
#1
|
||||
|
||||
|
[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> HTML Code:
<style type="text/css"> :target { background: #ffff7e; } </style> You can see an example of this right here. |
| Sponsored Links |
|
#3
|
||||
|
||||
|
Hey, Ive never seen this before. Pretty sweet.
__________________
Benji Barash - 16 Currently dabbling with Objective-C & Cocoa Touch @BBarash | BBarash.co.uk |
|
#4
|
||||
|
||||
|
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> |
|
#5
|
||||
|
||||
|
Even coolier! Nice one.
__________________
Benji Barash - 16 Currently dabbling with Objective-C & Cocoa Touch @BBarash | BBarash.co.uk |
|
#6
|
||||
|
||||
|
The advantage of my method is that it will work even with javascript disabled, since it's pure CSS.
|
|
#8
|
||||
|
||||
|
Quote:
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]--> |
|
#9
|
||||
|
||||
|
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] |
|
#10
|
||||
|
||||
|
Quote:
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|