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

Clipping content

From: CSS: Page Layouts

Video: Clipping content

One of the things that absolutely positioned elements allow us to do that we can't do with other element types is to use the clip property. Now the clip property allows us to essentially crop an element and its contents. Let's take a look at that by opening up clipping.htm, which you can find in the 04_05 directory. And the page structure of this one has changed a little bit. Instead of our normal three div tags that we have been using, essentially we have a section with a class of container, and now inside of that we have a div that has a paragraph inside of it as well. So we want a little bit more content inside of this so that you can actually see the clipping taking place.

Clipping content

One of the things that absolutely positioned elements allow us to do that we can't do with other element types is to use the clip property. Now the clip property allows us to essentially crop an element and its contents. Let's take a look at that by opening up clipping.htm, which you can find in the 04_05 directory. And the page structure of this one has changed a little bit. Instead of our normal three div tags that we have been using, essentially we have a section with a class of container, and now inside of that we have a div that has a paragraph inside of it as well. So we want a little bit more content inside of this so that you can actually see the clipping taking place.

So essentially we are just going to crop that content down a little bit. I am going to preview that in the browser for you, so you can see what's kind of going on here, and here is the content that we are going to be clipping. You can see it's being positioned using absolute positioning. So what values can we use for the clip property? Well, there are not too many. The clipping property has three values that we can use. We have Shape, Auto, and Inherit. Now Inherit and Auto are pretty much the same as doing nothing. Inherit is asking the parent what's your clipping property, whereas Auto is just nothing.

Now Shape, on the other hand, that is something. Shape allows us to pass a shape and some offsets into it that describe the shape of the clipping. Now, currently the only shape that browsers will accept is a rectangle. You can see the syntax right here. So the only shape that it allow us to pass into it right now is rectangle. There's hope that in the future we will be able use different shapes, like ovals, or polygons, but for right now rectangle is it. Now that is followed by a series of offsets. Now these offsets basically start at the top, so the first offset is top, then we have right, bottom, and left.

So if you're looking for a way to remember that, you can think of trouble, top, right, bottom, left. Or you can think of starting with top and going in a clockwise fashion. So what we want to do is we want to take this element and clip it using offset values such as these. So I am going to go back into my code and I want to go up and find element1, so this is who we are going to clip. Remember, this is only available for absolutely positioned elements. So I am going to go down on the next line here and I am going to type in clip, and then I am going to do the rectangular shape, and then inside the parentheses, I am going to give it an offset of 50 pixels for the top, comma, 200 pixels for the right-hand side, comma, 150 pixels for the bottom, and then after another comma, finally 50 pixels for the left-hand side.

Then I am going to remember my semicolon. I will go ahead and save that. I am going to go back out to the browser and refresh the page, and now you can see our content is clipped. Given the values that I just gave you, 50, 200, 150 and 50, is that what you were expecting? I'm betting not. And most of the time people get really, really confused about clipping properties. Basically, let's just practice using these values right here. Most people would assume that a top offset of 10 pixels means that it would come down and start clipping here, and then a 10-pixel offset from the right, they assume it comes to the right- hand side, it comes 10 pixels in.

25 pixels, 25 pixels up and then 10 pixels, 10 pixels over. But actually nothing could be further from the truth. Really what happens is top and bottom offsets start from the top. Right and left offsets start from the left-hand side. So a top offset of 10 would start here and go down 10, just like you would expect, but then a bottom offset of 25 would go down 25 pixels and start clipping here. So it creates a sort of inner frame from those edges, and although once you think about it, it makes sense, it's still kind of hard to calculate exactly what you're going to be clipping, to be quite honest with you.

So in order to really control the clip property, you need to know what the width and height of the element that you are working with is; otherwise, you've got no real way of controlling what content gets clipped. So I am going to go back into my code, and I don't see any height or width on element1, but it's a div tag, so if I go up, I can see that in the div tag, I've got 400 pixels' worth of width. In most cases that's pretty much what your elements are going to be like, and the height is generally created by the content and the padding and that sort of thing. But in this case if you are going to do clipping, one thing is that it only happens on absolutely positioned elements.

So for those, you generally are going to be setting element width and height probably more frequently than you would on elements that are on normal flow. But here we don't have a height, so I am going to go ahead and set one. So right after our width, I am going to go ahead and set a height value of 150 pixels. So let's think about this for a moment. We have 400 pixels' worth of width, 150 pixels' worth of height. We have 25 pixels' worth of padding all the way around, so the true height is 200 pixels and the true width is 450 pixels.

Then we have this 2-pixel border. So let's give ourselves a little exercise here. Let's say we want to take the clip property and just clip the 2-pixel border off. Why would you ever do that? Well, you could do something really cool. You could take the border off and then based on user interactivity or something like that, you could undo the clip property and make the border come back up. You could make elements sort of expand and contract. I mean there's all sorts of really kind of cool stuff you can do with clip if you're using it with absolutely positioned elements. So I am going to go down to element1 and I am going to change our clip here.

I am going to change the top clip to 2 pixels. Remember, that's 2 pixels down from the top. And then I'm going to show you different way to write this syntax. Instead of using commas, I am just going to use white space between them, so no comma, but I do need a white space there. The next thing I am going to do is I am going to go ahead and give this offset at 452 pixels. Now, why 452? Remember, this is the right-hand offset. That means it's going go all the way over from the left edge to the right, 452 pixels. Think about the width of our element. 450 pixels of width, with 4 pixels of total border, so two on the left-hand side, 2 on the right-hand side, so this is going to clip off the two that's on the right-hand side.

Now our bottom offset is going to be 200 pixels. Again, remember that the bottom offset starts at the top of the element and then comes down. I am going to get rid of the comma, make sure I have my white space, and then the very last one, which is the left- hand side, is also going to be just 2 pixels, because it's going to start at left- hand side and go over 2 pixels and clip it. Again, I've got to remember the white space there. Okay, so what's the difference in the two syntaxes? The first syntax we used had commas; the second syntax does not.

Well, the first syntax, the one with commas in it, that is the one that the W3C specification says is the proper syntax. The syntax you are seeing here is the one that older versions of Internet Explorer actually support. Now because all the modern browsers support this as well, just to be backwards compatible, because back in the day, Internet Explorer 8 and above now support the standard syntax, but older versions of IE, IE7 and 6, for example, in order to get it to work, designers would use this syntax. So all the other browsers, like Chrome and Firefox and browsers like that, said, okay, we will support that too, whatever. So if you want to support older versions of IE use this syntax.

If you want to use it in the standards- compliant way, use the previous syntax. So if I save this, go back out to the browser, and refresh, you can see that it is doing exactly what we wanted it to do, it's clipping off just the border and nothing else. Remember, this clip property works for any element that is absolutely positioned, and that means that any element that's considered to be in the absolute positioning model. So if we go back to element1 and change its position from absolute to fixed, remember, fixed elements are also considered to be part of the absolute positioning positioning model. That sounds weird to say.

But the absolute positioning model, if I save that, and preview it again, yes, it changes the position of that, but again, the clip property is still working. If I change the position relative, the clip property wouldn't work. Now I doubt that you are going to need to use clipping very often, but it is nice to know that you have yet another way to control absolutely positioned elements within your layouts.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40856 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.