Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Working with opacity

From: CSS: Page Layouts

Video: Working with opacity

Needing to control an element's opacity is a common occurrence in page layout. In CSS we have two primary means of controlling transparency: the opacity property and using the RGBA or HSLA color declarations. So I am going to be working with the opacity.htm file, which is found in the 08_05 directory. Just to give you a heads-up on the structure of the page, if we scroll down, after some descriptive text, we have a section that has three different div tags in it, and they have a class of opacity, alpha, and background.

Working with opacity

Needing to control an element's opacity is a common occurrence in page layout. In CSS we have two primary means of controlling transparency: the opacity property and using the RGBA or HSLA color declarations. So I am going to be working with the opacity.htm file, which is found in the 08_05 directory. Just to give you a heads-up on the structure of the page, if we scroll down, after some descriptive text, we have a section that has three different div tags in it, and they have a class of opacity, alpha, and background.

If I just preview that in the browser, you can see what's happening. The div with the background has the image in beside it, and we have a div tag on the left-hand side that we are going to use the opacity property for. Then we have a dig tag on the right- hand side that we are going to use alpha transparency for. Okay. So if I go up into my code, I can see I already have some selectors for those two elements, so we are just going to start adding properties directly to them. The first one, and probably the easiest one to do, is the opacity property. So if I go into the opacity selector, I can just use opacity, and I can give it whatever value I want.

Now the values for opacity range from 0 to 1. 1 being totally opaque, and 0 being totally transparent. If I do .5, for example, that's going to be 50% opaque. So if I save the file, go into my browser, and refresh that, you can see that element is now about 50% transparent. Now almost all browsers support the opacity property. Now earlier versions of Internet Explorer prior to version 9 didn't support opacity, but there is a quick and easy way to achieve support for IE. If I go right down below the opacity property and I type in filter, and then I say alpha and then inside alpha filter I do opacity and I can say =50.

Now this is a proprietary Internet Explorer property. So if there is some type of a validation test that your CSS has to pass, this is not going to validate it. It's not a valid CSS code. But it will work in IE, and it's not going to harm any other browser, so I really don't see the harm in having it in your code. So, if I save this, and now I want to go down into alpha and talk about different alpha transparencies that we can use. You see right now that the background color of our file is an rgb value, and one of the nice things that has happened recently with CSS is we've begun to have support in browsers for rgba.

So I am going to change both the border and the background to rgba values. Now rgba adds a fourth value in the declaration, and that fourth value is going to be alpha transparency. Now what I like about this, more than the opacity property, is you saw that with the opacity property the entire element was transparent, so it really affects everything. With alpha declarations, anywhere that you can apply a color value you can choose a separate alpha value.

So you could see after the commas I've added here to the rgba, I can just pass in different values. So, for example, border, I could pass in .8. So in terms of the color range, it's still the same. It's 0 to 1. Then for the background, for example, after that particular comma, I could say .5. So if I save that, go back into my browser, and test it, now you can see that the border has a different level of transparency than the fill, and even more importantly, you will notice that the content is left unaffected.

So the content remains at 100% and the background and the border in this case remain semitransparent. If I wanted to, I can certainly give a color declaration and use rgba as well. Now in addition to rgb, we also have support now for hsl, and that's hue, saturation, and lightness, if you will. So the hsla is supported too. Now their values are hue, saturation, and a lightness value. The hue is just from a degree of 0 to 360. So I am going choose something in a blue range, let's say around 280.

I will do that for both of these. And then your brightness and your saturation is passed along as percentage values. So for saturation, I am going to take the background and really desaturate it. Maybe about 20%. And then for the border, maybe I will make that more saturated, say 50%. Brightness value is the same way. Maybe I'll keep the background kind of dark, maybe about 30%, and for the border maybe I will make that a little bit lighter. Let's say 50%. And I will do the same thing for the alpha, so 50% for the background, 80% for the border.

So if I save this and go into the browser and test it, you can see the color has changed slightly and we get, again, transparency for the border and background that are different. So you can use either hsla or rgba; it doesn't matter which one you use. Now I mentioned before that getting the opacity property supported in earlier versions of Internet Explorer wasn't that difficult. rgba is a little bit trickier. It is supported in IE9, but prior to IE9, getting Internet Explorer really to work with transparency, especially using alpha values like that, it was actually pretty difficult.

So I want to show you an article written by the just absolute brilliant Lea Verou, who, if I scroll down a little bit, this is--if you just search for bulletproof, cross-browser RGBA backgrounds today, on her blog, Lea.verou.me, then you will get this article. What is awesome about this--it's a little old it was written in 2009--but that's okay, because it's for earlier versions of Internet Explorer. She goes through all the different syntax that you need in order to get it supported for that. So if you want to use rgba and you really want solid cross-browser-compliant code, this is a great article for you to read.

So being able to control transparency natively through CSS is extremely helpful for creating complex and creative page layouts. There is a lot really cool stuff you can do with it. Now keep in mind that you may need to provide fallback support for earlier versions of IE, but for the most part, the syntax is easy to remember and easy to use.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40625 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

Join now "Already a member? Log in

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 CSS: Page Layouts.

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


OK
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
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.