Start learning with our library of video tutorials taught by experts. Get started

CSS3 First Look

Using attribute selectors


From:

CSS3 First Look

with James Williamson

Video: Using attribute selectors

To find out just how useful attribute selectors can be, we'll add some styling to the Explore California contact page, focusing on how attribute selectors allow us to add visual styling cues without adding unnecessary classes or markup. Now I have the contact.htm file opened here in Chrome and you can find this in the 02_04 folder in your Exercise Files. Now we are going to target one thing at a time. So the first thing I want to target here is the tour brochure link.
Expand all | Collapse all
  1. 3m 5s
    1. Welcome
      1m 20s
    2. Using the exercise files
      1m 45s
  2. 31m 30s
    1. What is CSS3?
      5m 26s
    2. The current status of CSS3
      3m 35s
    3. An overview of CSS3 capabilities
      2m 24s
    4. Can you use CSS3 now?
      5m 31s
    5. Detecting support for CSS3
      9m 0s
    6. Understanding vendor prefixes
      5m 34s
  3. 1h 9m
    1. An overview of child and sibling selectors
      3m 11s
    2. Using child and sibling selectors
      7m 17s
    3. An overview of attribute selectors
      3m 19s
    4. Using attribute selectors
      8m 32s
    5. Pseudo-class UI selectors
      5m 56s
    6. Negation pseudo-class selectors
      6m 48s
    7. Target pseudo-class selectors
      5m 39s
    8. Structural selectors
      3m 58s
    9. Nth-child selector syntax
      10m 0s
    10. First, last, and only structural selectors
      5m 39s
    11. Using structural selectors to write more efficient code
      8m 52s
  4. 45m 28s
    1. Color formats in CSS3
      7m 9s
    2. Transparency in CSS3
      9m 10s
    3. CSS3 gradients
      4m 11s
    4. Creating linear gradients
      13m 57s
    5. Creating radial gradients
      11m 1s
  5. 49m 8s
    1. Working with web fonts
      6m 38s
    2. @font-face syntax
      4m 52s
    3. Downloading sample fonts
      6m 5s
    4. Writing @font-face declarations
      7m 57s
    5. Using web fonts
      6m 42s
    6. Using text shadows
      7m 14s
    7. Creating multi-column text
      9m 40s
  6. 50m 55s
    1. An overview of the flexible box model
      4m 42s
    2. Controlling box orientation
      5m 2s
    3. Setting element flexibility
      12m 59s
    4. Distributing boxes
      7m 54s
    5. Controlling box alignment
      12m 38s
    6. Working with box-sizing
      7m 40s
  7. 1h 5m
    1. Using border-radius
      6m 20s
    2. Creating custom rounded corners
      10m 21s
    3. Understanding border images
      5m 15s
    4. Using border images
      8m 52s
    5. Creating box shadows
      8m 58s
    6. CSS3 backgrounds
      4m 55s
    7. Controlling background size
      8m 46s
    8. Creating multiple background images
      6m 4s
    9. Using background-origin
      3m 18s
    10. Clipping background content
      3m 2s
  8. 40m 8s
    1. An overview of CSS3 2D transforms
      4m 26s
    2. Using 2D transforms
      8m 16s
    3. Setting transform origins
      5m 24s
    4. An overview of CSS3 transitions
      5m 0s
    5. Animating CSS properties
      6m 12s
    6. Using easing in animations
      5m 41s
    7. An overview of 3D transforms
      5m 9s
  9. 37m 56s
    1. Understanding media queries
      6m 18s
    2. Strategies for targeting multiple devices
      6m 4s
    3. Writing styles for target screen sizes
      12m 11s
    4. Deploying styles through media queries
      3m 55s
    5. Basing media queries on page orientation
      2m 24s
    6. Targeting media queries for iOS devices
      7m 4s
  10. 1m 6s
    1. Goodbye
      1m 6s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS3 First Look
6h 34m Appropriate for all Nov 29, 2010

Viewers: in countries Watching now:

In CSS3 First Look, staff author James Williamson provides an in-depth introduction to the newest CSS standard, detailing its modular format, history, and current level of browser support, while also demonstrating its capabilities and applications. The course includes tutorials on using new selectors, modifying typography and color, working with the box model, and understanding media queries. Exercise files accompany the course.

Topics include:
  • Understanding the history of CSS3
  • Working with the new selectors
  • Adding transparency and gradients
  • Specifying web fonts with @font-face
  • Understanding the advances to page layout
  • Looking at CSS3 box model capabilities
  • Using 2D and 3D transforms
  • Understanding media queries
Subject:
Web
Software:
CSS
Author:
James Williamson

Using attribute selectors

To find out just how useful attribute selectors can be, we'll add some styling to the Explore California contact page, focusing on how attribute selectors allow us to add visual styling cues without adding unnecessary classes or markup. Now I have the contact.htm file opened here in Chrome and you can find this in the 02_04 folder in your Exercise Files. Now we are going to target one thing at a time. So the first thing I want to target here is the tour brochure link.

That actually is going to allow our users to download a PDF file and I'd really like them to know that this link is going to open a PDF for them. That way they could choose whether they wanted to click on it or not, they at least know what they're going to be getting. So I'm going to switch back to my code editor and again, I have the same file opened here, contact.htm from the 02_04 folder. I am also going up to open up the main.css, which can be found in the _css directory of the same folder. Since I'm using Dreamweaver, I can switch back and forth between the two of them without having to open up separately, which is one of the things I really like about Dreamweaver actually.

Okay, the first thing I am going to do is scroll down to about line 88 in my code and this is where I have some just basic link styling in my styles and I'm going to add that to that selector. It's going to target that one specific link and really any link that's going to be linking to a PDF file. I am going to add it right there. So I am just going to type in a, and then do an attribute selector which is going to require the bracket notation and I am going to type href. So I am looking for the href attribute. Now I wanted to do $=, because I only want to target a suffix, so ending in is essentially what I'm doing here, and in quotation marks I'm going to type in .pdf.

So again, I am looking for any href attribute that ends in .pdf. Now I'm going to close that. Open up my brackets and then close as little curly braces. And inside that I am just going to add a little of padding, so I am going to do padding-right of about 50 pixels and I am going to add a background image. So that's why I am adding a little bit of padding to right-hand side of this, so that I can have this PDF icon that I have done show up to the right side of that. So here we'll just do a background and this background is going to have a URL of ../_images/pdf_icon.gif.

So that's an image that's already in the _images directory, and we're going to give it no-repeat so that it only shows up once. And I'm going to align it to the right and to the vertical center. So that way it's going to show up on the right-hand side where our padding is, and it should be vertically centered with the inline box. So I am going to go ahead and save that and then preview this again in Chrome and you can see now right beside tour brochures we get that nice little PDF icon to let people know that hey, when you are going to be downloading this, you're going to be downloading a PDF file.

And of course, that didn't affect any of the other links. It only affects that one because that one had a match to the attribute. Cool. Now I would also like people to know when they're going to click on a link that's going to take them away from our site. You can see these links are linking us further into our site. However, these links down here are actually taking us out to Wikipedia, where you can learn more about this area. So I would like to let people know that they are going to be navigating away from my site. And of course, we're going to do that by using an attribute selector. So here I am back in my code and again just below the selector that I just wrote I am going to write another one.

So here I am going to say a href^="http://" and then my bracket. So again, what that is doing is it's saying, hey, any attribute selector that starts with this, so here we did ends with and here we did starts with. All right, and once again, we are going to do something very similar here. Oh and by the way, I really only want this to show up when somebody hovers over it, because if I haven't show up in my default styling then that little icon is going to show up all the time.

It could break some of my text flowing, so maybe just when somebody hovers over it. So notice that with an attribute selector you can attach pseudo class selectors to them, which is cool. So we'll just say hover and we're going to do a very similar selector. So padding-right: 20 pixels and then we are going to do a background again here, we'll do url and again, this is going to be ../_images/external_link.gif.

And again here it's going be no-repeat right center. So it's just like the last one. All right, I am going to save this and again I am going to back and preview that in my browser. So now when I hover over these, notice that I'm getting a little icon that I don't get when I hover over my other links. Again, it's letting me know hey, this is going to go outside of your site, so it sort of uses that universal external link icon. Okay, the next thing I want to do is affect this styling. You can see all of these icons are floating to the left of the name of the tour, the package itself, and the price of it.

That's fine, but I want to show you kind of how this being done currently and how attribute selectors could maybe make that a little easier. I'm going to switch back to my code. So here I am back in the CSS and I am on about line 942 in my CSS code. You can see that we are targeting images inside the specials div tag, which is inside the sidebar. Now that's fine, but what happens if I have other images in a specials sidebar div tag? That could happen in my site. Well, they are all going to get same styling and maybe I don't want those floated.

Maybe I only want them floated when they are going to be these little icons. Well if I switch to my source code I can see that I've done something that can really help us out here. In my alt tags for these images, notice that the word icon is used in each one of these. So California Calm icon, From desert to sea icon, Backpack Cali icon. So the word icon is added to the alt tag of each one of those. It's descriptive but it also gives me a hook for use for my attribute styling. So I am going to go back to my CSS and I am going to modify this selector. So what I am going to do is I am just going to change this.

I am going to add the attribute to it, alt*="icon". So again, what this is going to do is it's going to basically say hey, if you see an image tag that has an alt attribute that contains the word icon anywhere in it, go ahead and target it. So that ways it's going to limit it to just those. Now if I save this and again preview this in Chrome, I don't see any visual change here, but if I place another image inside that specials div tag it's not going to get the same styling, so I have limited it to just those icons, so that's really cool.

One more thing. Notice the forms that I have got here, the input tags really aren't the width that I want them to be. I'd like them to be a little bit wider. Now in the past we've done that by applying classes and then targeting the width via the class, but I don't have to do that. Remember these input tags have a specific attribute called type and for text fields the type is equal to text. So now we know what we know about attribute selectors, we can target them specifically. Again, I'm going to go back in the code. So back in my CSS at about line 835 you'll find the styles for the contact form.

That's where we want to add this. So I am just going to place my cursor at the very top of the contact form styles, and I am going ahead and add a new selector. So #mainContent input, and then I am going to use an attribute selector here. I am going to say type="text". So here I'm being very explicit. I am saying only if the input form field has a type attribute that is equal to text and here we're just going to go ahead and give those guys a width of 250 pixels.

I'll go ahead and save that and if I test this in my browser, I can see that my text fields are all now the exact width that I wanted them to be. Really cool. Now aside from the versatility that we seen, that attribute selectors give us here, the best thing about them is their widespread support. And in fact unless you're having a target IE6 specifically, there's really nothing to prevent you from adding attribute selectors to your own styles. Have fun with them. Keep that in mind when you're planning your site design to see where they might fit in your workflow and as with anything you do, you want to test them thoroughly as you begin to use them.

Find answers to the most frequently asked questions about CSS3 First Look.


Expand all | Collapse all
Please wait...
Q: I'm following along with the video "Transparency in CSS3".  James shows us how to achieve transparency in Internet Explorer by going to Kimili.com and entering a HSLA value to generate code for transparency.

Here that code:

background: transparent;-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#BF0E0C0B,endColorstr=#BF0E0C0B)"; /* IE8 */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#BF0E0C0B,endColorstr=#BF0E0C0B); /* IE6 & 7 */ zoom: 1;

When this code is added to my HTML file it removes transparency on browsers that do in fact support it. So I'm left with NO transparency. Why?
A: The problem is in the filter code. If the IE background is called last, the first "transparent" declaration will remove all previous colors, regardless of browser. To resolve this, place the rule inside a conditional comment for IE or remove the "transparent" declaration at the front of the rule.
Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS3 First Look.

Return to your organization's learning portal to continue training, or close this page.


OK

Course retiring soon

CSS3 First Look will be retired from the lynda.com library on April 24, 2014. Training videos and exercise files will no longer be available, but the course will still appear in your course history and certificates of completion.


Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked