Inline-block pushing down over two lines

Hi all

Been working on a small snippet of CSS to allow a background image to sit next to the text.
I’ve created a small example to highlight the issue I’m having.

If the text dosen’t reach the end the container in the first div the div with the background color sits nicely next to the text. Though, if the text exceeds enough to push down onto two lines the div with the background color pushes down onto the third line. How can I keep them on the same line even when the text is spread over a number of lines?

<div style="width:30%;oveflow:hidden">
<div style="display:inline-block">Some text goes here, add more text to see the problem</div>
<div style="display:inline-block;width:30px;height:30px;background-color:#555;"></div>
</div>

Thanks, Barry

A picture of the problem in hand.

You haven’t given your inline block explicit width. You would experience the same problem even if you used floats, btw.

Without some sort of width, the element will expand to contain it’s contents, so if there is more than one window width of content then the width of your element will be 100%, thus bumping down it’s adjacent elements.

If teh gray square is purely decorative, you could clean up your code and achieve the effect you want like this:


			 .w:before{
			 	content:'';
			 	width:80px;
			 	height:80px;
			 	background-color:#555;
			 	float:right;
			 }
			 .w {width:30%; overflow: hidden}
 
<div class='w'>
This would be your content. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

if the square represents actual content … then you will have to put that element first in the source order.

if that i snot semantically possible, , the best one can do is ‘make’ space. this mean a column of empty space where the box will reside. of course because you cant mix measurements it will end up beign something like this:



			 .box{
 			 	width:20%;
			 	height:55px;
			 	background-color:#555;
  			 	display: inline-block;
  			 	margin-left: -.2em /* quick whitespace bug correction*/
			 }
			 .w {width:30%; outline:1px solid}
			 .w .c { display: inline-block;max-width: 80%;  }
			 .c, .box, .w{vertical-align: top;}
 
 
<div class='w'> 
 <div class='c'>This would be your content. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<div class="box"></div>
</div>


again floats may be a better solution, as they allow you to use margins…


			 .box{
 			 	width:50px;
			 	height:50px;
			 	background-color:#555;
			 	margin-left: -50px;
			 }
			 .w {width:30%; outline:1px solid; overflow: hidden}
			 .w .c { display: inline-block;max-width: 100%;  padding-right: 50px; }
			 .c, .box   {float:left}


I hope that helps.

Then again maybe THIS is what you want:


<!DOCTYPE HTML>

<html>
	<head>
		<title></title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="viewport" content="width=device-width">
		<style type="text/css">
 			 .count {padding:0 .3em; background: brown;vertical-align:  top; color:#fff; min-width: 1.5em; display: inline-block; text-align: center;  position: relative;   font-weight:  bold; }
 			 .count:after{ content:''; position: absolute; right:-.3em; bottom: -10px; width:0; height:0;  border: 10px solid transparent; border-left-color:brown; z-index: -1; }
 			 .count:before{ content:''; position: absolute; right:0; left:0; top:0; bottom:0; box-shadow: 1px 1px 3px #555; z-index: -2 }
 			 .w { padding:1em 0 1em 6em; list-style: none;font-family: Helvetica; font-size: 90%; background:  tan; position:relative; z-index: 1; overflow:hidden}
 			 .w li {padding-bottom: 1.5em; line-height: 1.55; width: 33%; float:left; margin-left: -5em  }
 			 .w li +li {margin-left:2em}
 		
  		</style>
	</head>
	<body>
<ul class='w'> 
 <li ><span class="l">This would be your content. Lorem ipsum dolor.</span> <span class="count">5</span></li>
 <li ><span class="l">This would be your content. Lorem ipsum dolor. Lorem ipsum dolor sit amet, consectetur adipisicing elit,  longer line  </span> <span class="count">15</span></li>
 <li ><span class="l">This would be your content. Lorem ipsum dolor.loerm loneger line</span> <span class="count">10</span></li>
 </ul>
	</body>
</html>


Yes this is what I’m after, thank you.
Loving the box shadow effect. I currently have a background image taking care of the effect myself.

I see what you have done here, but having trouble adding this into my code. I’m dealing with markup that I can’t really alter but only style.
If you have a look at http://www.playday.no and scroll down a bit you’ll see the problem I’m having as previously shown in the screen shot.

Suggestions on how to fix it using the current code I have?

Cheers, Barry