Boldewyn
Boldewyn

Reputation: 82734

Lists next to floated elements: How to get list items in the right place?

This fiddle shows the basic problem: http://jsfiddle.net/boldewyn/Qvfgv/

There is a floated element and next to it a list in the main flow. The list items extend into the space of the floated element.

A workaround is to use overflow: hidden on the <ul>. But then the list doesn't 'flow' around the div anymore but stays at the right all the time, which is undesired for all the white space below the floated element.

What I'm looking for:

What doesn't work:

It may well be, that there is no CSS-only solution for this. This question is literally my last resort before telling the customer that it's not possible.

Upvotes: 4

Views: 3586

Answers (4)

Skin
Skin

Reputation: 182

I agree with TomWardrop in that the best solution i have found to ensure that nested list indents are consistent when next to a floated element or not is to reset the padding and using relative positioning with a left setting.

ul {
  position: relative;
  padding-left:0;
  margin-left:0;
  left: 1em;
}

See fiddle.

Update.

If you want the inset only on nested lists and to keep the first level inline with paragraph. Set left to 0 for ul css declaration and then:

ul ul {
  left:1em;
}

see fiddle

Upvotes: 0

TomWardrop
TomWardrop

Reputation: 567

The position: relative; is the best solution I've found, just be sure to add padding or a margin to the right to compensate for the offset. This works with list-style-position set to inside or outside. You just need to adjust the offset accordingly.

ul {
  position: relative;
  left: 3em;
  margin-right: 3em;
}

Upvotes: 1

Gareth
Gareth

Reputation: 5719

Is this what you're after?

I've just increased the margin-right of the block from 10px to 30px.

Edit

I think I've got the effect you want, but it may be a bit too hacky for your needs - jsFiddle

Upvotes: 2

SpliFF
SpliFF

Reputation: 38956

list item bullets often seem to sit further left than they should. The answer is simple: add some left margin to your <li>'s or left padding to your <ul>. That will shift everything to the right.

I typically see this behaviour in sites that use CSS resets because ul,li {margin:0;padding:0} will give the behaviour you're seeing. In fact your fiddle does this:

ul {
  margin: 0;
  padding: 0 0 0 20px;
}

li {
  margin: 0;
  padding: 0;
}

So yeah, you're actually aligning the text with the left edge but not the bullet which goes past the edge (that's some kind of CSS quirk I guess). In your case you have a 20px left padding on ul but that doesn't seem to be enough, try putting it on the li or using margins.

Upvotes: 0

Related Questions