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

Vertically centering block-level elements

From: Dreamweaver CS4 with CSS Essential Training

Video: Vertically centering block-level elements

One of the most challenging things to accomplish in CSS is to vertically center block level elements inside parent containers. If you don't know the dimensions of the containing element, things get a lot easier as you can just use margins and/or padding to do the job for you. But if you don't know the dimensions of the parent element though? In those cases, while there is no bulletproof method, you can turn to a pair of techniques that will get the job done for you. The first technique we are going to look at involves using the vertical align property we learned about previously. Instead of using the vertical align on adjust inline elements, we can also use it on block level elements, if the block level elements are inside a table cell.

Vertically centering block-level elements

One of the most challenging things to accomplish in CSS is to vertically center block level elements inside parent containers. If you don't know the dimensions of the containing element, things get a lot easier as you can just use margins and/or padding to do the job for you. But if you don't know the dimensions of the parent element though? In those cases, while there is no bulletproof method, you can turn to a pair of techniques that will get the job done for you. The first technique we are going to look at involves using the vertical align property we learned about previously. Instead of using the vertical align on adjust inline elements, we can also use it on block level elements, if the block level elements are inside a table cell.

You would think that would require a lot of unnecessary table elements clogging up a code structure but you'd also be wrong. We can use the display property to tell standard compliant browsers to display the container elements as if they were table cells. Doing this allows us to employ the vertical align property to anything contained within. There are, as we'll see, some drawbacks to using this method. So open up the vertical_centering1.htm. So there are two vertical centering files. You want to make sure you have the first one, vertical_centering1.htm, opened up.

Now I'm going to turn my attention to the sidebar because this is what we are going to want to center. There has been a little structural change to the sidebar. I'm going to show what I mean. So we still have our parent sidebar tag but inside that we have a brand new div tag with an id of verticalAlign and inside that is our sidebar content. In this case, the block level element of a paragraph. So we want to take the paragraph and vertically center it within the sidebar and it's the div tag that's going to be the key to getting this done. So that's a little extra structural markup but for the results we are going to get, I think you will agree that it is worth it.

Now the first thing I want to do is horizontally center the paragraph within the sidebar and that's pretty easy to achieve. What we are going to do is I'm going to go to my CSS Styles panel, and I'm going to find my #sidebar p selector. We are just going to go ahead and center this using auto margins. So I'm going to add a property and I'm going to add the margin property and we want to give it a top and bottom margin of 0. So we are going to use a little shorthand notation here, then I'm going to do a space and type in auto. All right, so let's talk about what this is doing.

So whenever you use shorthand notation for margins, and you pass two values, the first value is used for top and bottom, the second value is used for right and left. So by saying 0 top and bottom, we are not adding any additional margin vertically to our paragraph but by using auto margin left and right, what we are basically saying is based on the width of the paragraph, the remaining width inside the parent element should be split equally between the left and the right side. This results in the centering of the element. It's the same technique we use to center our pages so there is a lot more about this technique in the chapter on layout.

Okay, so let's turn our attention back to vertically centering this element now. So the next thing we need to do is that in our parent element rule, in this case that's the #sidebar rule. Now normally we have been editing our rules using the CSS Styles panel but this time I'm going to double-click this to open up our CSS Rule Definition dialog box. The reason is I want you guys to know where this property is found in case you are creating this rule from scratch. So in the Category, I'm going to click on the Block category because that's where we can find the Display property. Now normally when people use the Display property, they are either changing something from an inline to a block level element or back to an inline element, or just saying none so that something doesn't display all together.

But as you can see from this pull-down menu, there are a lot of other choices. Now since the sidebar is the parent element, we are going to make it display like a table. So choose table, and click OK. Now you will note that there is no visual change to our sidebar but the structural change to it is now within the browser, we can make things inside of it behave as if they were inside of a table, and that's going to lead us to our vertical centering. So I'm going to go ahead and create a brand new CSS rule and this can be an ID selector, and we are going to do #verticalAlign.

So we got to spell that exactly the same way that it is spelled and capitalized in the code itself. We are going to define that in main.css, and I'm going to click OK. Now there is not going to be a lot to this rule. We are concentrating mainly on that vertical alignment. So I'm going to click on the Block category. Now this time, when I grab the pull-down menu for Display, instead of going down to table, what I'm going to choose is table-cell. So we are making the parent element behave as a table, we are making this interior block level element to behave as a table cell.

Now because this is a table cell, certain properties now apply to it. One of those properties would be vertical align. So I'm going to grab the Vertical-align pull-down menu and I'm going to choose middle, I click OK and I'm going to do Save All. Now just because it doesn't look right in Dreamweaver, it doesn't mean that it isn't going to work in our browsers. So what I'm going to do is I'm going to go pull-down menu and choose to preview that in Firefox. Sure enough, I now have a vertically aligned element, perfect. Now in the perfect world, I wouldn't really need to use any other technique but unfortunately, while this technique works in the standard compliant browsers, Internet Explorer can be decidedly none-standards compliant.

The Display table-cell property is not supported in Internet Explorer meaning this technique won't work for that browser. So I'm going to go ahead and go back in the Dreamweaver and close vertical_centering1. Over in the 05_09 folder, I'm going to open up vertical_centering2 because for Internet Explorer we are going to use another technique. This one works fine in IE but unfortunately it doesn't work in other standard compliant browsers. And for those of you who are new to CSS, I'm sorry to say that this is not an isolated occurrence. Welcome to the wonderful world of browser support.

In this technique, we'll use positioning to achieve our centering. Now if you don't know a lot about positioning, you might want to check out the chapter on layout where we talk about positioning in greater detail. For the time being, we are going to focus on structure and how this positioning attribute allows us to vertically center without knowing the exact height of the container element. So if I look over my sidebar, I can see and in this example, the structure is exactly the same. So at least the good news about these two techniques is that it can use exactly the same structure. So we have a paragraph, we have a verticalAlign div tag that's going to help us with our centering and then we have the parent sidebar.

So the first thing I'm going to do is go in and use auto margins to horizontally center our sidebar paragraphing. So go to #sidebar p, we'll add this property and again we are just going to add the same shorthand notation, margin 0 for top and bottom, space and then the word auto to center it horizontally, okay. Go ahead and do a Save All. And now let's get into the meat of this particular technique. What we are going to do is go up to your sidebar selector and you can just go ahead and double-click that if you would like to bring that up in the CSS Rule Definition dialog box.

And again, I just want you to see where the positioning attributes are set within this rule, in case you are creating a new one. In fact, we have a category for Positioning. So I'm going to click on the Positioning category and we'll go ahead and set a couple of attributes here. The first thing I'm going to do is set the Position to relative, and I'm going to set Overflow to hidden now. Let's talk about what that does for us. By setting Position to relative, we cause any elements nested inside of it to be positioned relative to it, and not the view or other parent tags.

So really we are just setting the sidebar as the boundaries for where the paragraph inside of it is going to be positioned. The reason that we did hidden is just in case the paragraph has more content in it than the sidebar can hold, the paragraph will be clipped off and will spill over and ruin the layout. So go ahead and click OK, and the next thing we need to do is add our rule for vertical align. So I'm going to choose New CSS Rule, once again this is an ID selector, and we are going to type in #verticalAlign.

Make sure you are defining this one in the main2.css, and go ahead and click OK. Now here is where our techniques really differ. I'm going to go to the Positioning category again, and this time I'm going to position it using absolute positioning. Now we cover positioning in the chapter on layout, so to learn a lot more about how these positioning attributes work, please be sure to check out the layout chapter. So we are setting this to position absolute, and we are setting our Top position to 50%. All right, so what is that going to do for us? Well, what that's going to do is it's going to move the entire verticalAlign div down, so that the top of it is halfway down the sidebar.

So if I click Apply, you can literally see that happen. Remember it uses this point as a starting point because we set our sidebars positioning attribute to relative. So it moves it down to halfway, 50%. Now that gets us close but it doesn't get us there. So I'm going to click OK and there is still one more rule we have to modify. I am going to go up and find the #sidebar p, double-click that and we are going to do a little positioning on this element as well. So I'm going to click on Positioning and I'm going to change the Position from absolute this time to relative.

Relative positioning allows it to stay within this normal document flow and allows us to offset it and that is exactly what we are going to do here. For Placement, for Top, I'm going to type in -50%. Now let's talk about what that's going to do. It's going to move it halfway between the top of its parent container element, that's a sidebar, and its current position. So that's going to move the top of the paragraph to here, which is going to give us our vertical centering. I'm going to go ahead and click OK, do a Save All. Now sort of kind of looks centered here but it's not rendering correctly, and if I tested this in any other browser than Internet Explorer, it wouldn't look right to begin with.

But if I preview this in Internet Explorer, we can see that our paragraph is vertically centered within the sidebar. So there you have it. One technique that works in standard compliant browser and another one works in Internet Explorer. Now as you'll learn in our chapter on deploying CSS, we could pass the Internet Explorer specific markup in using a conditional comment. In a perfect world, we could just use one technique and stick with it but in the very real world of CSS, you'll often have to find multiple ways of dealing with different browsers and devices.

It's a frustrating but necessary skill when creating your styles. In truth, there is even a simpler solution to the problem: design around it. One of the things that learning CSS will teach you is the beauty of simple and elegant code. Designing towards that will often avoid problems such as this.

Show transcript

This video is part of

Image for Dreamweaver CS4 with CSS Essential Training
Dreamweaver CS4 with CSS Essential Training

120 video lessons · 41514 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 7m 50s
    1. Welcome
      1m 18s
    2. Do I need to know CSS to use Dreamweaver?
      2m 15s
    3. Using the exercise files
      1m 30s
    4. Setting up a custom workspace
      2m 47s
  2. 1h 2m
    1. Separating structure from presentation
      4m 14s
    2. Adding meaning with ID and class attributes
      7m 50s
    3. Understanding basic selectors
      11m 10s
    4. Understanding complex selectors
      11m 21s
    5. Examining the Cascade
      10m 16s
    6. Understanding order of inheritance
      5m 2s
    7. Understanding specificity
      5m 43s
    8. Using Dreamweaver to resolve conflicts
      7m 4s
  3. 52m 48s
    1. Working with starter pages
      2m 1s
    2. Defining fixed, elastic, liquid, and hybrid
      6m 3s
    3. Understanding starter page structures
      6m 25s
    4. Modifying CSS globally
      8m 58s
    5. Moving CSS between files
      12m 31s
    6. Preparing custom starter pages
      10m 9s
    7. Creating custom starter pages
      6m 41s
  4. 1h 25m
    1. Designing with CSS in mind
      3m 13s
    2. Using Fireworks to create site prototypes
      2m 41s
    3. Defining page structure
      8m 52s
    4. Creating the initial layout
      10m 24s
    5. Page creation and asset sharing
      11m 20s
    6. Using common libraries to create site prototypes
      5m 11s
    7. Building interactive prototypes
      17m 6s
    8. Optimizing images in Fireworks
      11m 47s
    9. Exporting web graphics from Fireworks CS4
      2m 43s
    10. Exporting interactive prototypes
      3m 11s
    11. When to export XHTML and CSS from Fireworks CS4
      8m 34s
  5. 48m 28s
    1. CSS workflows in Dreamweaver
      1m 17s
    2. Using the CSS Styles panel
      5m 12s
    3. Setting CSS preferences
      9m 50s
    4. Understanding Dreamweaver's CSS visual aides
      3m 50s
    5. The Code Navigator
      6m 5s
    6. Controlling CSS with the Properties Inspector
      8m 52s
    7. Using Related Files
      4m 35s
    8. Working with Live view
      4m 12s
    9. Working with the Reference panel
      4m 35s
  6. 1h 13m
    1. Declaring font families
      4m 57s
    2. Creating custom font family declarations in Dreamweaver
      6m 0s
    3. Understanding units of measurement
      6m 14s
    4. Controlling font sizing
      8m 41s
    5. Controlling line spacing
      7m 20s
    6. Controlling vertical margins
      7m 52s
    7. Horizontally aligning text
      3m 16s
    8. Vertically aligning text
      5m 30s
    9. Vertically centering block-level elements
      10m 31s
    10. Setting column width
      3m 33s
    11. Using font shorthand notation
      9m 15s
  7. 1h 10m
    1. Background properties
      4m 33s
    2. Using background images
      5m 16s
    3. Controlling background image tiling
      5m 33s
    4. Positioning background images
      4m 42s
    5. Using percentage values for positioning
      5m 10s
    6. Creating custom list bullets
      5m 23s
    7. CSS drop shadows
      7m 40s
    8. Image replacement techniques
      7m 24s
    9. Adding screen-only content
      7m 51s
    10. Complex background graphics
      9m 10s
    11. Using CSS Sprites
      8m 0s
  8. 38m 48s
    1. Reviewing table tag structure
      4m 48s
    2. Using thead and tbody for styling
      5m 45s
    3. Styling table captions
      4m 30s
    4. Styling headers
      8m 29s
    5. Styling table content
      4m 18s
    6. Creating alternating row colors
      3m 29s
    7. Using pseudo-class selectors for tables
      2m 27s
    8. Creating custom table borders
      5m 2s
  9. 43m 37s
    1. Examining form structure
      3m 26s
    2. Styling fieldset and legend elements
      7m 42s
    3. Styling form elements globally
      6m 31s
    4. Using classes to identify form elements
      5m 55s
    5. Styling user feedback
      6m 10s
    6. Adding user interaction
      2m 52s
    7. Styling Spry form validation fields
      11m 1s
  10. 44m 48s
    1. Using lists for navigation
      1m 40s
    2. Creating horizontal menus pt. 1: Stripping list styling
      3m 28s
    3. Creating horizontal menus pt. 2: Displaying links horizontally
      3m 54s
    4. Creating horizontal menus pt. 3: Styling links
      5m 25s
    5. Creating horizontal menus pt. 4: Rollovers
      3m 48s
    6. Creating horizontal menus pt. 5: Indicating current page
      3m 48s
    7. Creating horizontal menus pt. 6: Modifying cursor usage
      2m 11s
    8. Creating horizontal menus pt. 7: Positioning menus
      3m 8s
    9. Styling vertical menus pt. 1: Vertical menu considerations
      6m 42s
    10. Styling vertical menus pt. 2: Defining width for link elements
      4m 46s
    11. Styling vertical menus pt. 3: Using background graphics with navigation
      5m 58s
  11. 1h 40m
    1. Box model review
      7m 4s
    2. Understanding margin collapse
      7m 15s
    3. Reviewing normal document flow
      11m 0s
    4. Understanding floating
      8m 56s
    5. Containing and clearing floats
      9m 26s
    6. Understanding relative positioning
      5m 30s
    7. Understanding absolute positioning
      5m 29s
    8. Understanding the AP Elements panel
      11m 57s
    9. Understanding fixed positioning
      2m 24s
    10. Using Dreamweaver to define document structure
      10m 11s
    11. Creating a two-column layout
      17m 1s
    12. Using Dreamweaver's Design-Time style sheets
      3m 49s
  12. 28m 59s
    1. Introducing Spry widgets
      2m 18s
    2. Updating the Spry framework
      45s
    3. Examining the default Spry styles
      6m 36s
    4. Modifying tabbed panels through CSS
      5m 18s
    5. Styling Spry widgets
      8m 49s
    6. Organizing Spry style sheets
      5m 13s
  13. 34m 49s
    1. Creating print style sheets
      2m 57s
    2. Assigning media types
      3m 29s
    3. Styling type for print
      9m 21s
    4. Suppressing element printing
      3m 29s
    5. Controlling page breaks
      8m 39s
    6. Using @media blocks
      3m 5s
    7. Creating alternative style sheets
      3m 49s
  14. 35m 22s
    1. Using Dreamweaver's Browser Compatibility Check
      3m 58s
    2. Fixing code errors with Adobe's CSS Advisor
      4m 45s
    3. Strategies for browser compatibility
      5m 8s
    4. Implementing browser compatibility
      8m 18s
    5. Formatting code for deployment
      3m 15s
    6. Creating modular style sheets
      3m 38s
    7. Assembling modular style sheets
      6m 20s
  15. 38s
    1. Goodbye
      38s

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 Dreamweaver CS4 with CSS Essential Training.

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.