How to get word wrap to wrap at smaller widths - html

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

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

Text on Img - without using height/width

i want to place Text on an img (centered vertically and horizontally).
I have tried a lot of stuff. The problem is the img and the text can change. Also the img have to be floated next to each other.
Here is an example:
http://jsfiddle.net/h5az77hd/1/
<div id="div0">
<div class="div1">
<div class="div2">
<div class="div3">Start</div>
<img src="http://fs2.directupload.net/images/150222/w4d322gc.png" style="display:block;" width="auto" height="150">
</div>
</div>
<div class="div1">
<div class="div2">
<div class="div3">Testtext</div>
<img src="http://fs2.directupload.net/images/150222/w4d322gc.png" style="display:block;" width="auto" height="150">
</div>
</div>
</div>
.div1 {
display:inline-block;
vertical-align:middle;
}
.div2 {
float: left;
background:#2E2EFE;
}
.div3{
display: inline-block;
position:absolute;
}
I cant use margin/width/height/... settings (bcs the text and img can change)
Are you guys having an idea?
Whenever I need to center things without knowing it's width / height I use a combination of display: table display: table-cell; vertical-align: middle; and text-align: center.
When you have a parent with display: table; and a children with display: table-cell; vertical-align: middle;, the children will be at the middle (vertically) of table, then, just use text-align: center; to center things horizontally.
In this case, you will need to adjust some things in your markup to make it works.
Here is a demo (using your image): http://jsfiddle.net/499nhjb0/1/
In your case, you can try to change .div3 to behaviour as .container-text-wrapper.
Hope it helps!

Center text over fluid images

I have several fluid images that I want to float and stack up next to each other. For example I have the img-div width set to 50% which stacks two images in each row. The images also increase in size depending on browser size. At the same time I want to be able to put a text over the middle of each image that floats around and stays in the center of the images. My problem is the text not centering. When I set img as absolute it centers but the stacking gets messed up.
Any idea how I can do this via CSS only?
Here's my HTML code:
<div id="container">
<!-- image1 -->
<div class="img-div">
<a href="#">
<img src="images/image1.jpg" />
<div class="txt-div">
<p>Some text goes here!</p>
</div>
</a>
</div>
<!-- image2 -->
<div class="img-div">
<a href="#">
<img src="images/image2.jpg" />
<div class="txt-div">
<p>Another text.</p>
</div>
</a>
</div>
<!-- image3 -->
<div class="img-div">
<a href="#">
<img src="images/image3.jpg" />
<div class="txt-div">
<p>Some more text.</p>
</div>
</a>
</div>
<!-- image4 -->
<div class="img-div">
<a href="#">
<img src="images/image4.jpg" />
<div class="txt-div">
<p>Last text.</p>
</div>
</a>
</div>
</div>
Here's my CSS:
.img-div {
width: 50%;
float:left;
a {
position: relative;
display: block;
height: 100%;
font-size: 0;
text-align: center;
}
a:before {
vertical-align: middle;
content: '';
display: inline-block;
height: 100%;
width: 0;
}
.txt-div {
vertical-align: middle;
position: relative;
z-index: 10;
display: inline-block;
font-size: medium;
p {
padding: 0;
margin:0;
}
}
img {
position: absolute;
width: 100%;
z-index: 9;
top: 0;
left: 0;
}
}
You almost had it :)
use position:relative/ absolute to draw your text-container over the image, and within use the pseudo :before and vertical-align technique to center your <p>.
.img-div {
width: 50%;
display:inline-block;
position: relative;
}
.img-div img {
width:100%;
}
a {
text-align: center;
}
.txt-div {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
z-index:1;
}
.txt-div:before {
content:'';
padding-top:100%;
vertical-align:middle;
display:inline-block;
}
.txt-div p {
max-width:95%;
display:inline-block;
vertical-align:middle;
}
DEMO: http://codepen.io/gc-nomade/full/Cxkqf
remove the div around the p and set position:absolute; to the p set the img to position:relative; the p will now go over the img, if I'm correct. set text-align:center; to p to get it centered. (sometimes it might be good to width:100%; the p too)
You could use CSS background images to place each image in a dynamically sized div, then simply float the divs and they will fill in whatever space without altering position of the block level elements. In this case each div would get a unique id according to the image it would contain and the urls for your images would be a background image for the div. The only drawback to this that I can see is that if images were disabled you would not get alt text, but your divs would remain the specified size. and in their correct position.

Aligning an Image and text within a Div CSS

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/

HTML:Adjust height between br element

In the given fiddle, I want first word align to the top of the image and second aligned at the bottom of the image.
http://jsfiddle.net/himanshuy/W6ATN/15/
I have tried line-height for the span and margin for the br element but nothing seem to work.
I re-arranged some of your html. First, I put the img element and the two span elements in their own divs. So there are two inline divs. I also added a style to one of the spans. You can see the end result here: http://jsfiddle.net/W6ATN/17/
In case that fiddle fails, here is the markup:
html
<div class="box">
<div class="logo">
<div class="inlineDiv">
<img src="c:\work\img\logo3.jpg" width="80" height="80" />
</div>
<div class="inlineDiv">
<span class="spanTop">YAD</span>
<span>HIM</span>
</div>
</div>
</div>​
css
div.box {
background-color: #000000;
color: #FFFFFF;
width: 300px;
height: 400px;
text-align: center;
}
img {
margin-top:20px;
}
.inlineDiv
{
display:inline;
}
.spanTop
{
margin-top:10px;
position:absolute;
}
span {
font-size: 30px;
color: red;
clear:both;
line-height:45px;
}