John M. Astell ♦ Portfolio
This website uses CSS sprites for the logos that appear on the top left of each page. There are several different logos, each with a different picture. Rather than having each logo in a separate file, all seven are in a single file. Each page's HTML has a container for the logo, and CSS is used to display the correct logo (the "sprite") for the page. The benefits for this approach are:
Here's the image in the logo sprite file:
![]()
(The red border around the image is added with CSS to show the bounds of the image. It is not part of the image.)
The dimensions of each logo are 305 pixels by 110 pixels. A one pixel line of whitespace separates the logos, so the overall image is 305x776. This separator isn't necessary for CSS sprites but it makes the master image more human readable and thus more maintainable over time.
The logo container in the page's HTML is the same height as a logo, and CSS is used to display the overall image as a background image in the container. By correctly positioning the overall image in the container, the desired logo appears in the container while the rest of the image is outside the container and is not seen. The upper left corner of the overall image is the origin (x=0,y=0). So, displaying a desired logo just requires specifying its origin: the top logo is x=0,y=0 while the bottom logo is x=0,y=-666.
Finally, the HTML container for the image is actually wider than the width of a logo (in case I create an extra-wide logo someday). CSS will by default repeat the background image to fill the available space. I don't want this, so I specify no-repeat. Here's the CSS resulting CSS:
div#logo {
background-image: url('media/logos/jma_logos_sprite.png');
background-position: 0px -555px;
background-repeat: no-repeat;
}
And this is the logo that is displayed:
In the above box, the background color initially is set to gold to match the logo's base color, and the background repeat initially is set to be non-repeating. (The background color is set in the CSS but not in div#logo, in case you're wondering why it's not in the code listing above.) Press the Toggle Background Color button to toggle it between gold and white. Press the Toggle Repeat On/Off to see how repeat works.
There are ways to further reduce the file size of the image for this demo. The text part of each logo is always the same and in the same position. It is possible to construct each logo from two images: an icon image and a text image. That way, the text would only have to be present once in the master image. Further, since each logo is on the same gold color, the white separator and some gold edging could be deleted, so that the gold edge area on the bottom of one logo could also be used as the gold edge area on the top of the next logo. That is, rather than the top logo starting at x=0,y=0 and the second logo at x=0,y=-111, the second logo could start at perhaps x=0,y=-100. Note, however, that this will make the master image harder to maintain over time, as each logo would no longer be evident at a glance.
These demos show examples of the utility of CSS3. The CSS3 for the demos and the JavaScript/jQuery code that manipulates the CSS is in the page's HTML file (rather than in separately loaded files), so that you can just look at the page's source to see everything of importance.
:hover pseudo-class to a link has its style invoked when the user hovers the mouse over the link.:nth-of-type(even) to style every even-numbered row. Let's see this (and more)!| Elf | Human | Dwarf | Hobbit | Wizards | Ents |
|---|---|---|---|---|---|
| Galadriel | Aragorn | Thorin | Bilbo | Saruman | Treebeard |
| Celeborn | Faramir | Balin | Frodo | Gandalf | Skinbark |
| Elrond | Theoden | Dwalin | Sam | Radagast | Beechbone |
| Cirdan | Eomer | Gloin | Meriadoc | Alatar | Quickbeam |
| Arwen | Eowyn | Gimli | Peregrin | Pallando | Leaflock |
| Add your own | Add your own | Add your own | Add your own | Add your own | Add your own |
:before, :after, and content Pseudo-Elements:before and :after pseudo-elements can insert content before and after elements they are applied to. The actual content to be inserted can be specified with the content property, which a static string or other values like a counter, the contents of an attribute of the selector, an url to an image, and so on.:before and :after for every hyperlink (anchor tag), with :before's having content: "Hyperlink: " and :after's having content: " (" attr(href) ")". This would result in "Hyperlink: " being displayed before the hyperlink and the value of the hyperlink's href attribute, enclosed in parentheses, after the link.
This link doesn't use :before and :after:
Some random link.
This link does use :before and :after:
Yet another random link.
(JavaScript prevents both links from actually navigating anywhere.)
:before and :after are used contextually, such as only when the web page is printed. Many hyperlink names don't actually indicate where they navigate to, which can make them a bit cryptic when the page is printed. Also, many hyperlinks are styled in ways that don't make them apparent that they are links when printed. In these cases, noting that the link is indeed a link and showing it actual navigation is useful in the printed output.:before and :after is to add GUI icons to certain HTML elements. Want every link to have a link symbol next to it? Just apply one :after specifying the icon you want for all hyperlinks. See Nicolas Gallagher's GUI icons blog for more details.Hover the mouse over the following link to see its hover-help text:
Again another random link.
data-help attribute in the link. (The demo does not use a title attribute for this text, as any browser that natively displays the contents of a title attribute on mouse hover could clash with the :after implementation of hover-help text.)Port harmonic system femtosecond infrared, bus or, encapsulated encapsulated procedural hyperlinked potentiometer. Record read-only encapsulated array patch reflective solution recognition infrared transistorized dithering femtosecond, metafile services read-only. Services array frequency, encapsulated encapsulated cache, logarithmic hyperlinked silicon in patch encapsulated scan.
Broadband echo element log phaselock in coordinated transmission. Transmission generator bypass video logistically metafile generator video coordinated record procedural. Element floating-point ethernet transistorized prototype device procedural hyperlinked. Distributed, metafile high development led distributed, metafile echo dithering.
Use the buttons to change the roundness of the corners for this box.
Use the buttons to change the box shadow.
opacity setting that determines how opaque/transparent an element is. The opacity of an element, however, is inherited by all its children elements. Sometimes this is what you want, but sometimes it's like an incurable genetic disease. CSS3 also allows RGBA to be used, where the "A" stands for "alpha" or "alpha channel" and determines how transparent the color specified by the "RGB" part is. CSS3 RGBA can be used instead of opacity to avoid inheritance issues.Opacity Demo 1
The inner box has the opacity setting. The outer box is the parent of the inner box and thus is not affected by the change in opacity.
Opacity Demo 2
The outer box has the opacity setting. The inner box is the child of the outer box and thus is affected by the change in its parent's opacity.
Alpha Demo 1
The inner box has the alpha setting, which is applied to the background color of the inner box. The parent outer box is not affected. (See Alpha Demo 3 if you want the inner box's border to also use the alpha setting.)
Alpha Demo 2
The outer box has the alpha setting, which is applied to the background color of the outer box. The child inner box is not affected, unlike how opacity works. (See Alpha Demo 4 if you want the outer box's border to also use the alpha setting.)
Alpha Demo 3
The inner box has the alpha setting, which is applied to the background color and border of the inner box. The parent outer box is not affected.
Alpha Demo 4
The outer box has the alpha setting, which is applied to the background color and border of the outer box. The child inner box is not affected, unlike how opacity works.
opacity is fairly good: IE7 and IE8 are non-standard but a proprietary filter can be easily added to make them behave exactly as if they could use opacity. Browser support for CSS RGBA is not quite as good, as IE7 and IE8 don't support it, although IE9+ does. (IE7, IE8, and IE9 all have opacity issues and bugs; see http://www.jacklmoore.com/notes/ie-opacity-inheritance/ for a succinct summary.):hover pseudo-class), the change between CSS properties occurs immediately. CSS transitions allow time-based transitions to occur (although, if your browser doesn't support CSS transitions, the style changes still occur instantly). It's demo time::before and :after pseudo-elements).@font-face: Downloadable Fonts@font-face allows a web page to specify fonts that can be downloaded from a server for use on the page. This gives websites the ability to use the precise fonts that it needs for its desired look. Without @font-face or a service like Google Web Fonts, a website needs to restrict its font use to "web safe" fonts: fonts that are likely available on all operating systems the website will be viewed on. The following demo uses the Alex Brush font, downloaded from the server:If your browser fully supports @font-face, this text uses the Alex Brush font.
@font-face, here's a picture of what you should be seeing above:
@font-face is not without issues, alas:
@font-face. This requires that the fonts be hosted on the same domain as the website that uses them, or else they won't be downloaded.@font-face, which is impractical for many websites. Some fonts allow free @font-face use (the font used here does), but the burden is on the website developer to examine a font's licensing scheme for its @font-face permissions.@supports: Feature Queries@supports allows CSS feature detection in the CSS code itself rather than having to use JavaScript. When a page loads, @supports statements in the CSS are executed, check whether the browser supports specified CSS features (the "support conditions"), and cause conditional CSS styles to be applied based on the outcome of the check. Example: @supports (display: inline-flex) {
div.example { display: inline-flex; }
}example class. Logical operators not, and, and or can be used with @supports support conditions to create versatile checks.@supports itself, it just ignore all the CSS associated with the @supports statement. So, the basic CSS could define the styles that are used by default, while @supports could refine the styles for browsers that understand @supports.@supports unfortunately is not yet widely supported by the major browsers, although Chrome, Firefox, and Opera have or will soon have support. Internet Explorer and Safari do not support @supports, so JavaScript CSS feature detection will still be necessary for some time to come.Press a button to clip the background.
This table is used to test if your browser supports certain CSS3 pseudo-classes and pseudo-elements. It should be hidden. |