Tutorials

Latest Word » Tutorials » Correcting Orphans w/ Overflow
Untitled-4

Correcting Orphans w/ Overflow

Tags: ,

The overflow property in CSS can be used in various ways and comes in handy when correcting bugs. Below are some tutorials that demonstrate how to clear up some common issues using the overflow property. After checking them out, I have one more to add to the list.

Got Orphans?

Below is an example of an Orphan in a paragraph which has an image floated to the left. This orphan is commonly seen when aligning a paragraph next to an image that exceeds its height.

Correct orphans with CSS

This is where we can use overflow: hidden; to fix the problem. Add overflow: hidden; to your targeted <p> tag with CSS Specificity, or wrap the paragraph with a container that has overflow:hidden; applied to it.

Correct orphans with CSS

As you can see, the paragraphs do not wrap around the floated image. Take a look at the HTML and CSS below for a demonstration.

HTML
<img src="your_image.jpg" class="thumb" />
<div class="description">
    <h3>Heading 1</h3>
    <p>Paragraph goes here...</p>
    <p>Paragraph goes here...</p>
</div>
CSS
.thumb {
    float: left;
    margin: 5px 20px 20px 0;
    padding: 5px;
    border: 1px solid #ccc;
    background: #f0f0f0;
}
.description {overflow: hidden;}

By adding overflow: hidden; to the .description container, you can now add a combination of elements without it ever wrapping around the floated image. Simple and clean, just the way we want it.

Correct orphans with CSS

Have Any Tricks to Share?

If you know of any other tricks using the overflow property, I am interested in hearing about it. If you have any questions or concerns regarding this alignment trick, please let me know anytime.

Updates:

Related Posts

Author Bio

My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!

You can learn more about me or Twitter  Follow me on twitter for more updates and resources!

Did You Enjoy This Post?

subscribe  Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.

+ Add Comment53 Peeps Have Spoken Their Minds...

  1. Markus

    nice tip ;) cheers

  2. sortea2

    A very important bug to fix with overflow:hidden is when you have a layout with a sidebar with float:right and the main content with float:left, if you don’t set overflow:hidden to a a container div, you can’t set a background or a border property to them because they don’t fill it.

    To footers and more that has helped me so much.

    Sorry for my poor english and thanks for your post.

  3. Beben

    Sometimes somebody likes better for sticky style on looks writing..
    btw its a usefully tutor…thank you Master SOH

  4. webangel78

    Unfortunately it does’t work in IE6.

  5. Cara

    I was trying to do something exaclty like this with my blog comments. Eventually I floated the , this however would have been neater. Really cool tip… thanks

  6. Ingoool

    wow! this is a cool trick in useing the overflow: hidden
    Thank you for that

  7. JMLeon

    Nice trick! I use overflow:auto with a container which has every block in it floated, so the conainer doesn’t get “flat”.

  8. Sealth Reinhold

    Thanks Soh. Your tutorials are clear, concise and enjoyable to read.
    Also, nice job on the new web site. Looks great and functions amazingly.

  9. Evan Mullins

    Nicely done, I hadn’t realized that overflow:hidden could solve the orphan issue here. Will def come in handy. Thanks!

  10. Soh

    @WEBANGEL78 You can make this work in IE6 with a lil hack. I usually don’t recommend using hacks but IE6 is our red headed step child…

    IE6 Workaround: Just add a float.

    *html .description {float: left;}
  11. Pippin Williamson

    Very nice! I’ve run into problems with orphans, but have never come up with a good solution. This is it.

  12. Terry

    Been using overflow:hidden to fix wrapping float issues for some time now. Didn’t know it had this effect as well. Good to know. Thanks for posting!

  13. Louis

    Good tip, Soh. So simple that I can’t believe I’ve never seen this written about before.

    Just one question though: You mentioned that you need to float the paragraph to get it to work in IE6, but wouldn’t that fix it in all browser anyhow? So it would be less code to just use “float: left” once without hacks and without overflow.

  14. Soh

    @Louis The problem is, if you add a float: left; to a paragraph tag (in modern browsers), this will make the paragraph expand as far as the text reaches, which can cause the layout to break.

    When floating two elements side by side, the widths of the combined elements cannot exceed the width of its container, this is where the problem comes into play. It just seems IE6 does not have this problem, one of those scenarios where we take advantage of the browsers flaws.

    I used to use a float: left; to create this type of layout, but I always had to specify the exact width. I just found that overflow: hidden; was much more simple and flexible to implement for this type of scenario :-) Let me know your thoughts!

  15. Louis

    @Soh

    Ah, yes, that’s right, I forgot about that. I was actually going to do some testing to see if there was another way to accomplish this, but I got lazy. :(

  16. sahojie

    also used a padding-left:xxxpx to tag,the padding-left width equal the image’s width+padding+margin+border

  17. Sam Benson

    This is great. One of those little gems that are so hard to come by these days.

    Thanks

  18. Rachel

    Great tut..nicely done..thanks

  19. Roman Janko

    Great trick! :)

  20. Yigit Ozdamar

    Nice nice! I’m really glad that you came back with amazing articles.

  21. Steve

    Thank you for your great tutorials and willingness to share your talent.

    Unfortunately, the dark theme of the new site (cursing, undead with their brains hanging out, and the general color of the site) don’t sit well with me and I am deleting your RSS feed from my list.

    Maybe I missed the cursing and such in the articles you used to post before the design change, but I was all set to continue reading using my noColor bookmarklet to make things more readable for me, but the addition of cursing is unnecessary, and in my opinion, unprofessional.

    I realize most people use (or hear) foul language day in and day out, and I’m not expecting anyone to change because of me, but I have chosen not to talk, act, or think like that so I must say goodbye!

    I know my “leaving” will not affect your readership or the quality ideas and examples in your posts, I just wanted to “speak my mind”. :0)

    I hope life “brightens” up in every way for you and yours!

    :)

  22. Soh

    Thank you for your honesty Steve :-) I apologize for the rough material on the site~ Best of luck with your journey!

  23. Fred

    Really good trick, I didn’t know it :)

  24. Rich S.

    This is a pretty good trick. Thanks for the heads up. Love the site by the way.

  25. Luigi

    very Good!!!! ;-)

  26. Steven

    Thanks for the tip but shame about IE6. What i normally is just set width for the divs. Solves any unexpected IE6 errors.

  27. Surge Consulting Group

    Nicely done! One for the CSS Notebook!
    - Dave

  28. webangel78

    @Soh:
    To validate IE6 fix it must be “* html .description {float: left;}” with interval between * and html.

  29. Project Center

    No more ugly orphans with this trick! Thanks for the the visuals as well as the “how to”.

  30. Jason G.

    This is a cool trick and will come in handy. *html vs * html ? They do the same thing, they both work, and they both don’t validate. Tomato Tomäto its how ever you want to write it. Thanks for the tip!

  31. Alan

    Thanks very much for this, came in handy on a project im doing just now.

  32. Gary

    you really is amazing.. keep it up!

  33. Adam Hermsdorfer

    Nice! This definitely alleviates the need for additional classes – thus bloating the code.

  34. ivan

    Interesting post, worth playing around with the editor and see how it is rendered

  35. Aery

    Hi,

    Good to Know This

    Thanks for Sharing

    Regards.

  36. Rilwis

    Very nice tip. Thanks for sharing.

  37. rai

    i like it. thanks for the tip

  38. Paradox

    http://reddit.com actually uses this exact technique

  39. Gaurav Mishra

    this is so cool!
    never tried it.. what a share

  40. Sapan Shah

    Thanks for the post, I was having trouble with exactly same orphan text lines and now I’m happy I stumbled on your post.

    Also, your website rocks.. the top navigation, left share floating div, comments area and the footer… its super sexy! Keep up the good work!

    Sapan

  41. Sagar Ranpise

    Absolutely Awesome Trick! Thanks for sharing!

  42. Ken

    Very good. Steve, you need to lighten up. I can see ONE instance of the word “damn” in this article. That’s not cursing, unless you’re five or stuck with your hands wrapped around a book of fairy tales (AKA The Holy Bible) at all times. Great article.

  43. Richard Vanbergen

    @Steave Damn is not a swear, but never mind. I think we should be allowed to curse I just don’t like it when people do it too much, it makes you sound like you are not able to convey your thoughts properly. I also don’t like it when people complain about it, they’re just words, and some of them are very useful. They shouldn’t be banned from use, just overuse.

    Unfortunately, this wont stop orphans, this only stops text from warping around other elements. An orphan is a small chunk of text forced onto the next line by text wrapping at the end of the paragraph, like the last line on the comment above me.

    I think the only way to stop orphans properly (using CSS and no manual intervention) is to use text-align:justify; but even that doesn’t work properly as browsers cannot place dashes in content to ensure an even text contrast, and justified text is harder to read.

    I did see a jQuery plugin for this a while ago. But it’s long gone now, I can’t find it on Google.

  44. Krishan

    @soh: I disagree. This is no solution to fixing orphans when you don’t know what type of content or how long they are going to be. This would be good in a limited number of case.

    Its a better idea to have a margin-left and the float the image. Thereby you specify that content can be as long as the client wants them to be and not to limit them.

  45. Ave Navalta

    This is soooo coool ^^ Thanks!

  46. Calvin Tennant

    Very nice. I will use this.

  47. ChrisB

    Brilliant – so simple and it works!

  48. Shefik

    Add “overflow: hidden” to the outer container too, in order to clear the float of the image + description

  49. Ronald L. Harris, Jr.

    This is easy to do and so useful. I’m not an expert at all so I’m happen this is so fun and quick to do. Thank you.

  50. Michelle

    So simple and yet I’ve never seen mention of it before. This reduces a lot of code for a project I’m currently working on. Wish I had ran across it a month ago but better late than never! :)

    On another note, I love the design of your site. Nice clean interface that makes it easy to find and read content while still looking great.

  51. Giselle

    Thanks so much for this! Very useful!

  52. Ezequiel

    Great! Thanks

  53. ramya

    very nice!

Speak Your Mind...

Post HTMLNeed to Post HTML Code? Use Postable to post code friendly html. *Wrap all html code in <pre></pre> tags.
Post HTMLNeed to Keep Updated with Comments? Subscribe to comments now.

CSS Gallery