tags:

views:

307

answers:

3

I have a paragraph followed by an unordered list, with several list items. I also have an image floated to the left of that. The problem I am having is that the list item margin/padding is being overlapped by that image.

I want the bullets that are next to the image to indent like it should.

Here is a test I wrote up for debugging, where you can see my issue in action.

All of this is inside of a CMS, so the image dimensions are variable, as well as the paragraphs and possible lists in the text.

Any solutions?

(See my first comment for pictures.)

+1  A: 

Add this:

ul{ list-style-position: inside}

That's it!

cpharmston
I don't want to do that because then the other lines don't left align with the first letter of the first line.
caitlin
It appears to work fine here:http://jquery.nodnod.net/cases/580/runHave you used a CSS reset?
cpharmston
He is using your solution. If you go into the code and make one of those li's text a lot longer, you can see. (Or just look at the CSS).
caitlin
Oops, I missed your HTML demo, I just saw the screenshots. I'm guessing that the problem is due to the fact that you don't have margin and padding declared. Back in the day, before CSS resets were common, I remember an idiom where you always defined left margin and padding for lists to ensure consistent bullet placement. I thought it was from an ALA article, but a quick search yielded nothing. Anyway, I suggest using some form of CSS reset, whether it's Eric Meyer's fantastic one (http://meyerweb.com/eric/tools/css/reset) or a simple *{margin:0;padding:0}
cpharmston
This is an extracted piece from a full dev website where I am using resets. But, I did the *{margin:0;padding:0} and it did not help.
caitlin
A: 

You can give your list items an overflow property:

li {
    overflow: hidden;
}

That will cause the list item to sort of behave correctly: They will display as a square block, continuing where the image ends as well, they don´t flow nicely to the left. The next list item will.

jeroen
This does the same thing as "display: block" on the list item does. My bullets disappear. What is this supposed to do?
caitlin
I´ll have to check, I used it somewhere to solve the same problem...
jeroen
Can´t find it and can´t get it to work again. Sorry...
jeroen
A: 

If you don't bother about adding javascript, here is a jQuery script that will add a margin to the ul that overlaps the image so all the list items remain aligned, and then assigns a negative margin to the li's that doesn't overlap.

$(function(){
    //Define a context so we only move lists inside a specified parent
    var context = $("#test_div");    

    //Build a list of images position a size
    var imgRects = [];
    var imgs = $("img.left", context);
    imgs.each(function(i){
        var pos = $(this).position();
        pos.right = pos.left + $(this).outerWidth(true);
        pos.bottom = pos.top + $(this).outerHeight(true);
        imgRects.push(pos);        
    });

    //Process each li to see if it is at the same height of an image        
    var lis = $("li", context);
    lis.each(function(i){
        var li = $(this);
        if(li.parent().css('marginLeft') != "0px"){
            return; //Already moved
        }
        var top = li.position().top;
        for(var j in imgRects){
            var rect = imgRects[j];
            if(top > rect.top && top < rect.bottom){
                li.parent().css('marginLeft', rect.right);
                return;
            } else if(li.parent().css('marginLeft') != "0px"){
                li.css('marginLeft', -1 * rect.right);
            }
        }
    });
});

I've tested with your demo page and jQuery 1.3.2 and it works on FF3.5 and IE8 because the image is on top of the document. If the image appears in the middle of a ul, the firsts li's will remain padded. If you need to correct this issue leave a comment and will try to update the script.

Serhii