Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<ul> height when containing floating <li>

I have a list:

<ul>  <li>Number 1</li>  <li>Number 2</li>  <li>Number 3</li>  ... </ul> 

All the <li> are floating. I need the height of the <ul> box. If I remember correctly this is not valid:

<ul>  <li>Number 1</li>  <li>Number 2</li>  <li>Number 3</li>  ...  <hr class="clear" /> </ul>  .clear {    clear: both; } 

How can I do this? The number of items in the list can be different so I can't use fixed height.

like image 782
tsusanka Avatar asked Jul 19 '11 17:07

tsusanka


People also ask

How do you find the height of UL?

How to I get the full height of the list as $(ul). height() just gives the height of the scroll viewport not the height of the full list. Things like Get The Full Height Of HTML Element That's Partially Hidden By Overflow:Hidden With JQuery / Javascript wont work as there is no inner wrapper, there is just the list.

When to use float property in CSS?

The CSS float property controls the positioning and formatting of content on the page. Its most common use is to wrap text around images. However, you can use the float property to wrap any inline elements around a defined HTML element, including lists, paragraphs, divs, spans, tables, iframes, and blockquotes.

What is float CSS property?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).

What UL and Li mean?

Unordered lists ( UL ), ordered lists ( OL ), and list items ( LI )


2 Answers

Good options to contain the floats:

  • Add overflow: hidden to the ul.
  • Use clearfix.
like image 161
thirtydot Avatar answered Sep 21 '22 04:09

thirtydot


This isn't a direct answer to your question, but as an alternative could you consider using display:inline-block? These days I just use that instead of float where possible, as essentially most of the time it can achieve the same sort of objective without the total hassle of making containers properly contain inner floating elements and having to clear them all the time.

like image 29
Trevor Avatar answered Sep 21 '22 04:09

Trevor