Why do my list item bullets overlap floating elements

HtmlCss

Html Problem Overview


I have an (XHTML Strict) page where I float an image alongside regular paragraphs of text. All goes well, except when a list is used instead of paragraphs. The bullets of the list overlap the floated image.

Changing the margin of the list or the list items does not help. The margin is calculated from the left of the page, but the float pushes the list items to the right inside the li itself. So the margin only helps if I make it wider than the image.

Floating the list next to the image also works, but I don't know when the list is next to a float. I don't want to float every list in my content just to fix this. Also, floating left messes up the layout when an image is floated to the right instead of left of the list.

Setting li { list-style-position: inside } does move the bullets along with the content, but it also causes lines that wrap to start aligned with the bullet, instead of aligned with the line above.

The problem is obviously caused by the bullet being rendered outside the box, the float pushing the contents of the box to the right (not the box itself). This is how IE and FF handle the situation, and as far as I know, not wrong according to the spec. The question is, how can I prevent it?

Html Solutions


Solution 1 - Html

I have found a solution to this problem. Applying an ul { overflow: hidden; } to the ul ensures that the box itself is pushed aside by the float, instead of the contents of the box.

Only IE6 needs an ul { zoom: 1; } in our conditional comments to make sure the ul has layout.

Solution 2 - Html

Adding an improvement to Glen E. Ivey's solution:

ul {
    list-style: outside disc;
    margin-left: 1em;
}
ul li {
    position: relative;
    left: 1em;
    padding-right: 1em;    
}​

http://jsfiddle.net/mblase75/TJELt/

I prefer this technique, since it works when the list needs to flow around the floating image, while the overflow: hidden technique will not. However, it's also necessary to add padding-right: 1em to the li to keep them from overflowing their container.

Solution 3 - Html

This is where the "display" property comes into its own. Set the CSS below to make the list work alongside the floated content.

display: table; works alongside floated content (filling the gap) but without hiding content behind it. Much like a table does :-)

.img {
  float: left;
}

.table {
  display: table;
}

<img class="img" src="https://via.placeholder.com/350x350" alt="">
<ul>
  <li>Test content</li>
  <li>Test content</li>
  <li>Test content</li>
</ul>
<ul class="table">
  <li>Test content</li>
  <li>Test content</li>
  <li>Test content</li>
</ul>

EDIT: Remember to add a class to isolate which lists you wish to do this for. E.g. "ul.in-content" or more generally ".content ul"

Solution 4 - Html

Try list-style-position: inside to change the layout of the bullets.

Solution 5 - Html

Why overflow: hidden works

The solution is as easy as:

ul {overflow: hidden;}

A block box with overflow: other than visible establishes a new block formatting context for its contents. W3C recommendation: <http://www.w3.org/TR/CSS2/visuren.html#block-formatting>

Example

The buttons on my website, which are <li> in disguise, are made like this. Make the viewport (window) of your browser smaller to see the indenting in action.

My website as an example

Article with examples

Solution 6 - Html

At http://archivist.incutio.com/viewlist/css-discuss/106382 I found a suggestion that worked for me: style the 'li' elements with:

position: relative;
left: 1em;

Where you replace "1em" with the width of the left padding/margin that your list items would have if the float weren't present. This works great in my application, even handling the case where the bottom of the float occurs in the middle of the lists--the bullets shift back over to the (local) left margin just right.

Solution 7 - Html

By adding overflow: auto; to your ul works for me at least.

Update

I've updated my jsfiddle to visualize what's going on. When having the ul beside the floating img, the content of the ul will be pushed by the float, but not the actual container. By adding overflow: auto the whole ul-box will be pushed by the float instead of only the content.

Solution 8 - Html

You could assign position: relative; left: 10px; to the li. (You may additionally want to give it a margin-right: 10px;, otherwise it might become too wide on the right side.)

Or, if you want to use float for the ul -- as suggested by others -- you can probably stop the rest from floating right of the ul by using clear: left on the element that follows the ul.

Solution 9 - Html

Disclaimer

Lists next to floated elements cause issues. In my opinion, the best way to prevent these sorts of floating issues is to avoid floating images that intersect with content. It'll also help when you have to support responsive design.

A simple design of having centered images between paragraphs will look very attractive and be much easier to support than trying to get too fancy. It's also one step away from a <figure>.

But I really want floated images!

Ok, so if you're crazy persistent enough to continue down this path, there are a couple techniques that can be used.

The simplest is to make the list use overflow: hidden or overflow: scroll so that the list is essentially shrink wrapped which pulls the padding back to where it's useful:

img {
  float: left;
}
.wrapping-list {
  overflow: hidden;
  padding-left: 40px;
}

<img src="http://placehold.it/100x100"/>
<ul class="wrapping-list">
  <li>lorem</li>
  <li>ipsum</li>
  <li>dolor</li>
  <li>sit</li>
  <li>amet</li>
</ul>

This technique has a few problems though. If the list gets long, it doesn't actually wrap around the image, which pretty much defeats the entire purpose of using float on the image.

img {
  float: left;
}
.wrapping-list {
  overflow: hidden;
  padding-left: 40px;
}

<img src="http://placehold.it/100x100"/>
<ul class="wrapping-list">
  <li>lorem</li>
  <li>ipsum</li>
  <li>dolor</li>
  <li>sit</li>
  <li>amet</li>
  <li>lorem</li>
  <li>ipsum</li>
  <li>dolor</li>
  <li>sit</li>
  <li>amet</li>
  <li>lorem</li>
  <li>ipsum</li>
  <li>dolor</li>
  <li>sit</li>
  <li>amet</li>
</ul>

But I really want wrapping lists!

Ok, so if you're even crazier more persistent and you absolutely must continue down this path, there's another technique that can be used to wrap the list items and maintain bullets.

Instead of padding the <ul> and trying to get it to behave nicely with bullets (which it never seems to want to do), take those bullets away from the <ul> and give them to the <li>s. Bullets are dangerous, and the <ul> just isn't responsible enough to handle them properly.

img {
  float: left;
}
.wrapping-list {
  padding: 0;
  list-style-position: inside;
}
.wrapping-list li {
  overflow: hidden;
  padding-left: 25px;
}

<img src="http://placehold.it/100x100"/>
<ul class="wrapping-list">
  <li>lorem</li>
  <li>ipsum</li>
  <li>dolor</li>
  <li>sit</li>
  <li>amet</li>
  <li>lorem</li>
  <li>ipsum</li>
  <li>dolor</li>
  <li>sit</li>
  <li>amet</li>
  <li>lorem</li>
  <li>ipsum</li>
  <li>dolor</li>
  <li>sit</li>
  <li>amet</li>
</ul>

This wrapping behavior can do weird things to complex content, so I don't recommend adding it by default. It's much easier to set it up as something that can be opted into rather than something that has to be overridden.

Solution 10 - Html

I am using this to solve this problem:

ul {
   display: table;
}

Solution 11 - Html

Try the following on your UL tag. This should take care of the bullets overlaying your image and you don't have to mess up your left allignment caused by list-position: inside.

overflow: hidden; 
padding-left: 2em; 

Solution 12 - Html

Struggled with this myself. Best I've managed is the following:

ul {
    list-style-position: inside;
    padding-left: 1em;
    text-indent: -1em;
}

The text is not actually indented but the bullet shows.

Solution 13 - Html

After fighting with this interesting issue in several projects, and investigating why it happens, I finally believe I found both: a working and 'responsive' solution.

Here is the magic trick, live example: http://jsfiddle.net/superKalo/phabbtnx/

ul {
    list-style: none;
    position: relative;
    padding-left: 0; /* remove any left padding */
    margin-left: 0; /* remove any left margin */
    left: 35px;
}
li {
    padding-left: 0; /* remove any left padding */
    margin-left: 0; /* remove any left margin */
    text-indent: -19px; /* adjust as much as needed */
}
li:before {
    content: '•\00a0\00a0\00a0';
    color: #000; /* bonus: you can customize the bullet color */
}

Solution 14 - Html

Edited to update based on OP's comment

ok, then just break it up into 2 divs nested together

ul  {background: blue; position:static;}
.therest {position:relative; width:100%}
.indent {float:left; }

<div class="therest">
<p>
Est tincidunt doming iis nobis nibh. Ullamcorper eorum elit lius me delenit. 
</p>
<hr />
<h3>Lorem</h3>
<div class="indent">
<ul>
<li>list element</li>
<li>list element</li>
<li>list element</li>
</ul> 
<div>
the rest now under the UL
</div>

try changing the ul li css to

ul {float:left; background: blue; }

Solution 15 - Html

Working inside an LMS without access to head of doc, found it easier to go with margin-right: 20px as an inline style for the image. Which I owe to this site.

Solution 16 - Html

try this:

li{
    margin-left:5px;
}

If you want them to go left, just put in a -##px value.

Solution 17 - Html

or you could do this:

 #content ul {
        background:none repeat scroll 0 0 #AACCDD;
        float:left;
        margin-right:10px;
        padding:10px;

and then remove all the styling from the li

Solution 18 - Html

width: 300px;
height: 30px;
margin-left: auto;
right: 10px;

margin-left: auto will cause the element itself to be right aligned. Set height and width of the element you want - in my it's a background image in a div inside

Solution 19 - Html

How about this?

ul{float:left; clear:right}

Solution 20 - Html

You could try also floating the ul to the left, and define an appropriate width for it, so that it floats next to the image.

Something a little like this jsfiddle?

Solution 21 - Html

I fixed it with

div.class-name ul {
  clear: both;
}

Solution 22 - Html

width: auto; overflow: hidden;

Solution 23 - Html

Add display:table; to ul:

ul{display:table;}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKamiel WanrooijView Question on Stackoverflow
Solution 1 - HtmlKamiel WanrooijView Answer on Stackoverflow
Solution 2 - HtmlBlazemongerView Answer on Stackoverflow
Solution 3 - HtmlmartinedwardsView Answer on Stackoverflow
Solution 4 - HtmlannakataView Answer on Stackoverflow
Solution 5 - HtmlSerge StroobandtView Answer on Stackoverflow
Solution 6 - HtmlGlen E. IveyView Answer on Stackoverflow
Solution 7 - HtmlMaehlerView Answer on Stackoverflow
Solution 8 - HtmlChris LercherView Answer on Stackoverflow
Solution 9 - HtmlzzzzBovView Answer on Stackoverflow
Solution 10 - HtmlLifeIsShortView Answer on Stackoverflow
Solution 11 - HtmlJoeView Answer on Stackoverflow
Solution 12 - HtmlStephenView Answer on Stackoverflow
Solution 13 - HtmlKaloyan KosevView Answer on Stackoverflow
Solution 14 - HtmlANC_MichaelView Answer on Stackoverflow
Solution 15 - HtmlLynda WilliamsView Answer on Stackoverflow
Solution 16 - HtmlDouglasView Answer on Stackoverflow
Solution 17 - HtmlReeceView Answer on Stackoverflow
Solution 18 - HtmlNikolay IvanovView Answer on Stackoverflow
Solution 19 - HtmlAdam LynchView Answer on Stackoverflow
Solution 20 - HtmlHoudmontView Answer on Stackoverflow
Solution 21 - HtmlNeal GarrettView Answer on Stackoverflow
Solution 22 - Htmlevan toderView Answer on Stackoverflow
Solution 23 - Htmlevan toderView Answer on Stackoverflow