Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tricks for floating elements inside list elements

I'm trying to create a instruction list using ordered lists (ol). Inside I want the text for the instruction to be floated left and the image showing the step floated right.

I've tried wrapping the content inside the list item (li) like so:

    <ol>
      <li><p style="float:left">Follow these instructions</p><img style="float:right" src="[***]" alt="" /></li>
    </ol>

That just sends the image outside the li element. I've tried adding a clearing div after the two floated elements inside the li, I've tried adding a clear hack to the li element itself. I've also tried nesting the p and img elements inside floated divs.

Is this even possible? Would like to use this manner, but will generate some other solution if not.

like image 284
tigre Avatar asked Jun 23 '10 19:06

tigre


2 Answers

You could also consider using the overflow: hidden technique; adding overflow: hidden to your li will enable it to expand to contain the floated content.

jsFiddle here.

like image 105
Matt Gibson Avatar answered Oct 23 '22 14:10

Matt Gibson


A non-floated container will not recognise its contents as having and width or height, causing the container to collapse to its minimum allowed height given any other CSS rules in place.

For the paragraph and image to be constrained by the containing list element, you will need to float both the list element and the parent ordered list.

Both the ordered list and the list elements will then reduce to the minimum width required to presentationally encapsulate the content. This may result in an overly-thin list. Apply suitable widths to the ordered list and the list items to handle this.

Consider clearing any elements immediately below the ordered list in the DOM to ensure they don't slip in to the left or right of the list.

like image 5
Jon Cram Avatar answered Oct 23 '22 12:10

Jon Cram