Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent HTML text orphans?

Tags:

html

css

layout

I often have an image that I wrap text around, and sometimes the texts wraps awkwardly, like so:

enter image description here

In the HTML, the image is floated to the left and the text simply follows:

<p><img style="float:left;" src="/images/[image]" /></p>
<p>This is my David Copperfield, <em>I was born</em> kind of bio.  For a more concise one, please see the <a href="/jenny/press-kit#bio">press kit</a>.</p>
...

This mostly works, except when the text length just happens to run past the bottom of the image and flow back to the left margin, and when the amount of text isn't long enough to fill more than one line (in this case, it's only one word). When that happens, it looks really bad.

So, is there a way to control the text flow so that this doesn't happen?

like image 352
Joshua Frank Avatar asked Sep 03 '13 12:09

Joshua Frank


People also ask

How do you stop orphans in HTML?

The most simple way to get rid of widows is to manually add a non-breaking space inside your HTML code. A non-breaking space acts as a glue between two words, and force them to be on the same line. A non-breaking space looks like this: &nbsp; .

What is an orphan in HTML?

The orphans property sets or returns the minimum number of lines for an element that must be visible at the bottom of a page (for printing or print preview). The orphans property only affects block-level elements. Tip: orphans:5 means that at least 5 lines must be visible above the page break.

What is widows and orphans in HTML?

widows = minimum number of lines in a paragraph split on the new page. orphans = minimum number of lines in a paragraph split on the old page.

What is orphan CSS?

The orphans CSS property sets the minimum number of lines in a block container that must be shown at the bottom of a page, region, or column.


1 Answers

What you could do is add overflow: hidden to the p tags where there is text. This will make it so any text that wraps after the image will be in line with the larger part. Now when you have large paragraphs this may look funny, however if your paragraphs are all fairly short this should help.

Example: http://jsfiddle.net/8ZsKy/2/

alternately you could just add a class rule and apply it to potential "problem" paragraphs.

p.wrap-inline {
    overflow:hidden;
}

EDIT: updated jsfiddle (oops)

like image 175
Don Avatar answered Oct 16 '22 18:10

Don