I have the following markup:
<li id="CN2787">
<img class="fav_star" src="images/fav.png">
<span>Text, text and more text</span>
</li>
I want it so that if the text wraps, it doesn't go into the 'column' for the image. I know I can do it with a table
(which I was doing) but this is not workable for this reason.
I've tried the following without success:
li span {width: 100px; margin-left: 20px}
.fav_star {width: 20px}
I also tried float: right
.
Thanks.
EDIT: I want it to look like this:
IMG Text starts here and keeps going... and
wrap starts here.
Not like this:
IMG Text starts here and keeps going... and
wrap starts in the space left for the image.
If you want the
margin-left
to work on aspan
element you'll need to make itdisplay: inline-block
ordisplay:block
as well.Wrap a div around the image and the span and add the following to CSS like so:
HTML
CSS
LESS
Very simple answer for this problem that seems to catch a lot of people:
See example: http://jsfiddle.net/vandigroup/upKGe/132/
The
span
element is an inline element, you can't change its width in CSS.You can add the following CSS to your span so you will be able to change its width.
Another way, which usually makes more sense, is to use a
<p>
element as a parent for your<span>
.Since
<p>
is ablock
element, you can set its width using CSS, without having to change anything.But in both cases, since you have a block element now, you will need to float the image so that your text doesn't all go below your image.
P.S. Instead of
float:left
on the image, you can also putfloat:right
onli p
but in that case, you will also needtext-align:left
to realign the text correctly.P.S.S. If you went ahead with the first solution of not adding a
<p>
element, your CSS should look like so:setting
display:flex
for the text worked for me.For those who want some background info, here's a short article explaining why
overflow: hidden
works. It has to do with the so-called block formatting context. This is part of W3C's spec (ie is not a hack) and is basically the region occupied by an element with a block-type flow.Every time it is applied,
overflow: hidden
creates a new block formatting context. But it's not the only property capable of triggering that behaviour. Quoting a presentation by Fiona Chan from Sydney Web Apps Group: