Aligning an Image and text within a Div CSS - html

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;
}

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>

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.

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/

Related

Why do floats whose width is percentage behave differently inside a container with a sibling vs if their width is in pixels?

.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
width:;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
In this example, the width of the float is the size of its content, the text, and the container has expanded just enough to contain both siblings.
Now, if i increase the width of the float in pixels, the float will start pushing the sibling, which will expand the container.
.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
width:100px;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
You might have to use the full screen on the snippet to see it.
Now, if i use percentage instead of pixels on the width of the float, the container only expands as much as to contain the text of the float, while the extra width does not push the sibling. Instead the float is pushed down on a new line, and the extra width on the container remains to be seen on the right side. Why is this happening?
Or to be more specific, the width of the float does push the sibling but only until there's no more space, then the float is pushed down, and it keeps expanding on a new line, while the container maintains the width space of the original float size.
.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
width:10%;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
Here's a screenshot to illustrate my question.
Screenshot
I sort of understand that the float is based on percentage, so the container has to maintain that ratio, but even if both siblings are based on percentage, and then you increase the percentage of one of them, the container still maintains the width of when both siblings had perfect 100% width.
Here's what i mean:
.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
width:90%;
}
.overlay {
background-color: skyblue;
width:10%;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
Now, you see how both sibling take exactly 100%, but now if you make the width of the float 20%, it will be pushed down, as there is no more space, but the container will remain the same width as it was before based on the float being 10% only. This is what i mean. Why does it maintain that width?
Percentage need a reference so in order to find the percentage width of the childs we first need to know the one of the parent element which is also based on its content. We have a loop.
In such case, we first consider the width of the childs to be auto in order to identify the width of the parent then we use that width to calculate the one of the child elements and the size of the parent element will no more change (otherwise we will have an infinite loop).
Here is an illustration to better understand
.container {
display: inline-block;
position: relative;
border: 10px solid yellow;
margin:5px;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
float: left;
}
<p>Initial state</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200">
<div class="overlay">
Text
</div>
</div>
<p>let use some percentage</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:20%">
<div class="overlay" style="width:80%">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:80%">
<div class="overlay" style="width:80%">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:40%">
<div class="overlay">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:100%">
<div class="overlay" style="width:100%">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:200%">
<div class="overlay" style="width:150%">
Text
</div>
</div>
You can clearly notice that in all the cases, the width of the parent remain unchanged and the child will use that width as a reference for their calculation. That width was initially calculated based on the default width of the text and the image.
Using pixel value is a different story as we don't have any complex calcuation and the parent will adjust its width based on the childs:
.container {
display: inline-block;
position: relative;
border: 10px solid yellow;
margin:5px;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
float: left;
}
<p>Initial state</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200">
<div class="overlay">
Text
</div>
</div>
<p>let use some percentage</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:20px">
<div class="overlay" style="width:80px">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:80px">
<div class="overlay" style="width:80px">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:40px">
<div class="overlay">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:100px">
<div class="overlay" style="width:100px">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:200px">
<div class="overlay" style="width:150px">
Text
</div>
</div>
For more accurate details you can refer to the specification: https://www.w3.org/TR/css-sizing-3/#intrinsic-contribution

Writing text after inserting an image in html

I am trying to write a description below an image on a html page, but the text is automatically aligned to right side of the image.
I am a beginne, please help!
Thamks in advance!
.images {
margin-left: auto;
margin-right: auto;
width: 40%;
}
#my_freaking_id {
color: red;
font-size: 50px;
}
<h1>Pictures</h1>
<div class="images">
<img src="https://picsum.photos/200/200?random">
<p>Image 1</p>
</div>
<div class="images">
<img src="https://picsum.photos/200/200?random">
<p>Image 2</p>
</div>
<div id="my_freaking_id">
It has been a long day, without you my friend and I will tell you all about it when I see you again!<br>I hope our friendship lasts forever!
</div>
Maybe just add to css?
.images img, .images p {
display:block;
}

Centering an Image

So this is probably an easy answer, but my image which is supposed to be sitting in the middle of the page is slightly off and it's irritating me a lot.
HTML:
</section>
<section class="img_section">
<div>
<img src="images/menu.jpg" alt="menu" align="center">
</div>
</section>
CSS:
img section{
float:center;
text-align:center;
}
I've tried removing float, and taking it out of a section entirely, but it won't budge.
Any help would be amazing
there's no such thing as float: center;
also img section doesn't style image within section.
you could try either:
1:
img {
display: block;
margin: 0 auto;
}
2:
section div {
text-align: center;
}
section div img {
display: inline-block;
}
depending on your other styles
This is simple to answer.
#div1 {
text-align: center;
}
</section>
<section class="img_section">
<div id="div1">
<img src="images/menu.jpg" alt="menu" align="center">
</div>
</section>
The text-align: center; property is going to make all the elements inside the div centered. Also, it is better if you select the div with a specific class or ID. This is going to go on all div elements on the page.
this is much more simple. just include center tag as
<center>
<section>
<div>
<img src="images/menu.jpg" alt="menu" align="center">
</div>
</section>
</center>
try in css:
.img_section img{
text-align:center;
}
html:
<section class="img_section">
<img src="images/menu.jpg" alt="menu" align="center">
</section>
I like to use
margin: 0 auto;
for centering

How to get word wrap to wrap at smaller widths

I am trying to get a line of text to word wrap while it displays next to the image. When the text is too long it puts the text under the image, and then it will do word wrap. How can I get the text to wrap so it fits next to the image at all widths?
In my example I included what I mean by next to the image, and what I mean by not wrapping until under the image. All I have come up with to try is changing the width of #ItemText and that does not produce the desired results.
#Items{
text-align:left;
display:block;
vertical-align: top;
max-width:300px;
background-color:gray;
}
#imgItem{
display:inline-block;
}
#ItemText{
display:inline-block;
vertical-align: top;
}
.ItemName{
display:block;
vertical-align: top;
font-weight: bold;
}
.ItemNum{
display:inline-block;
}
<div id="Items">
<img id='imgItem' height="100" width="50" src='s.jpg' />
<div id="ItemText">
<div class="ItemName">
This text is short enough
</div>
<div class="ItemNum">
AB503
</div>
</div>
</div>
<br/>
<div id="Items">
<img id='imgItem' height="100" width="50" src='s.jpg' />
<div id="ItemText">
<div class="ItemName">
This text doesn't wrap until it goes under the image
</div>
<div class="ItemNum">
AB503
</div>
</div>
</div>
Without adding any css to a <p>, it defaults to wrapping around floating elements. We can achieve what you want by removing a majority of your css and simply floating the image to the left and changing your <div>s to <p>s.
HTML
<div id="items">
<div class="item">
<img class="item-image" height="100" width="50" src='s.jpg' />
<p class="item-name">This text doesn't wrap until it goes under the image</p>
<p class="item-num">AB503</p>
</div>
</div>
#items{
max-width: 300px;
text-align: left;
background-color: gray;
}
CSS
.item {
min-height: 100px;
padding: 10px;
}
.item-image {
float: left;
padding-right: 10px
}
.item-name {
font-weight: bold;
}
.item-num {
}
That will wrap the text nicely within the .item div. Note that because the image is floating, you'll need to set a min-height on the .item div to prevent your image from extending past the bounds of the .item div. I also added some padding to to keep the content away from the edges.
You could apply new margins based on the image dimensions like so: https://jsfiddle.net/L6v60u03/
HTML
<div class="Items">
<img class='imgItem' height="100" width="50" src='http://theartmad.com/wp-content/uploads/2015/04/Smiley-Face-5.jpg' />
<div class="ItemText">
<div class="ItemName">This text is short enough</div>
<div class="ItemNum">AB503</div>
</div>
</div>
<br/>
<div class="Items">
<img class='imgItem' height="100" width="50" src='http://theartmad.com/wp-content/uploads/2015/04/Smiley-Face-5.jpg' />
<div class="ItemText">
<div class="ItemName">This text doesn't wrap until it goes under the image</div>
<div class="ItemNum">AB503</div>
</div>
</div>
CSS
.Items{
text-align:left;
display:block;
vertical-align: top;
max-width:300px;
height: 100px;
background-color:gray;
}
.imgItem{
display:inline-block;
}
.ItemText{
display:block;
vertical-align: top;
}
.ItemName{
display:block;
vertical-align: top;
font-weight: bold;
}
.ItemNum{
display:inline-block;
}
JS
var width = $(".imgItem").css("width");
var height = $(".imgItem").css("height");
var trueWidth = parseInt(width) + 10; // Adjust padding by adding more
var trueHeight = parseInt(height) * -1 + 10; // Adjust padding by adding more
$(".ItemText").css("margin-left", trueWidth);
$(".ItemText").css("margin-top", trueHeight);

Aligning elements inside stacked DIVs

Description of Problem:
I'm attempting to arrange the kittens in a star-like pattern with 3 DIV "rows." I would like for the first top row's kitten to be centered on the page (easy enough); the second (or '#middle') row to have their cats left-aligned and right-aligned, respectively; and the third ('#bottom') row to have its cats aligned similar to the second row, but slightly indented on both sides. Again, like a star.
I know the float property essentially makes the element(s) absolutely positioned, which collapses the bottom two rows' height, so that's probably not the right answer. But I've also tried text-align and futzing with margins. My brain is fried. What am I doing wrong?
Fiddle:
http://jsfiddle.net/k97CG/1/
HTML Structure:
<div id="top">
<div id="container1" class="containers">
<div id="cat1">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
<div id="middle">
<div id="container2" class="containers">
<div id="cat2">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
<div id="container3" class="containers">
<div id="cat3">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
<div id="bottom">
<div id="container4" class="containers">
<div id="cat4">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
<div id="container5" class="containers">
<div id="cat5">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
CSS Structure:
.containers {
position: relative;
width: 125px;
height: 125px;
}
#top, #middle, #bottom {
position: relative;
width: 100%;
border: 1px solid red;
}
#container1 {
margin: 0 auto;
}
#container2 {
float: left;
}
#container3 {
float: right;
}
#container4 {
float: left;
}
#container5 {
float: right;
}
Is there a reason you can't just place them all in one div, then position them with CSS?
<div>
<img id="img01" src="img1">
<img id="img02" src="img1">
<img id="img03" src="img1">
<img id="img04" src="img1">
<img id="img05" src="img1">
</div>
then
div {
position:relative;
width:100%;
height:100%;
}
div img {
position:absolute;
}
#img01 {
top:x;
left:y;
} etc
As a rule, you shouldn't rely on HTML for visually styling content unless you have no other option. That's what CSS is for.
Is this the one you are looking for:
#top, #middle, #bottom {
position: relative;
width: 100%;
border: 1px solid red;
clear:both;
}
DEMO