Reputation: 537
I am trying to align an image and text within a div
so it looks like the following
IMAGE TEXT
However when I get them aligned as such, the background colour of the div
no longer appears.
Can anyone suggest what I am doing wrong?
HTML:
<div class="introduction">
<div class="image">
<img src="">
</div>
<div class="text">
<p>Text</p>
<p>Good luck!!</p>
</div>
</div>
css:
.introduction {
margin: 0 50px;
padding: 20px;
background-color: #FFFFFF;
color: #000000;
-moz-border-radius: 10px;
border-radius: 10px;
}
.image {
width: 30%;
float: left;
}
.text {
width: 70%;
float: left;
}
Upvotes: 0
Views: 91
Reputation: 1219
I would suggest you 2 solutions:
1) If you want to your output look like this:
IMAGE TEXT
IMAGE TEXT
TEXT
HTML:
<div class="whole">
<img class="ib" src="myimg.png" alt="my img" />
<p class="ib">my text my text my text my text my text my text my text is so short I will die alone</p>
</div>
CSS:
.ib{ display:inline-block;}
.whole{ vertical-align:top}
.ib img{width:30%; border:0;}
.ib p{width:70%;margin:0;padding:0;}
2) or like this:
IMAGE TEXT TEXT
TEXT TEXT
HTML:
<img src="myimg.png" alt="my img" class="leftimg" />
<p>my text my text my text is so short I will die alone</p>
CSS:
.leftimg {
float:left;
margin-right:5px;
}
DEMO: http://jsfiddle.net/goodfriend/b66J8/37/
Upvotes: 0
Reputation: 1958
Something like this may achieve what you want:
<style>
.introduction{
margin:0px;
padding:5px;
background-color:orange;
}
.introduction img{
float:left;
padding:10px;
}
.introduction p{
padding-left:50px;
background-color:blue;
}
</style>
<div class="introduction">
<img src="img/can_25.png" />
<p>Text</p>
<p>Good Luck</p>
</div>
Since your p's aren't floated, they will hold your div open .. depending on the size of your image.
Upvotes: 0
Reputation: 966
Putting the two floats in there side by side makes the parent container's height effectively 0. You can put a div with a style="clear:both;" before the parent's closing tag and you will get your background back.
<div class="introduction">
<div class="image">
<img src="" />
</div>
<div class="text">
<p>
Text
</p>
<p>Good luck!!</p>
</div><div style="clear:both;"></div></div>
Upvotes: 1