How to Add Text in An Image
Sometimes we want to add text in the image, not below or above it. We can set the position, top, right, bottom, or left property or the text element.
top:
top-right:
left:
bottom-right:
bottom:
bottom-left:
right:
One important thing to keep in mind is that the we nee to add a some element to wrap around the target image and the text element we want to show in the image.
Example and Code
top-left:top:
top-right:
left:
bottom-right:
bottom:
bottom-left:
right:
<div style = "posotion:relative"> <img src = "something.jpg"> <div style = "position:absolute;bottom:0;left:45%">some text</div> </div>
How to Use
- Make sure the img tag and text div tag are wrap in a div tag.
- The wraper tag needs to have position: relative property, so that the text div can sit in it.
- Set the text div to position: absolute; and start defining the postion, left, top, right, or bottom until the text is at where you want it to be.
- One trick is to remember the wraper div is what the text being relative to, so its position should be relative. The text should have absolute position relative to the div.
A Big Problem
One big problem is that the wrapper div, in default, has a width of 100%, then the image text may show up in some expected place. It may show up in the fartherest right or left of the div when the image's width is bellow 100% of the browser.
<div style = "text-align:center;background-color:lightgrey;position:relative"> <img src = "someapple.jpg"> <div style = "right:0;top:0;position:absolute">Apple</div> </div>
This problem can be solved by setting the wrapper div display to inline-block:
<div style = "text-align:center;background-color:lightgrey;position:relative;display:inline-block"> <img src = "someapple.jpg"> <div style = "right:0;top:0;position:absolute">Apple</div> </div>
One more problem about how to center text in the image
In the above examples, when text div is set to left or right of 50%, the text will actually not aligned canter. This is the result that the text has some width and the left or right property pushes the text toward center respevtively with no consideration of the text width.
<div style = "text-align:center;background-color:lightgrey;position:relative;display:inline-block"> <img src = "someapple.jpg"> <div style = " right: 50%; bottom: 0px;position:absolute">Big Apple</div> </div>
This problem can be solved by setting the text div width to 100% and remove the left or right property. And you can controll the left or right by setting the text div's text-align property, such as center.
<div style = "text-align:center;background-color:lightgrey;position:relative;display:inline-block"> <img src = "someapple.jpg"> <div style = "width:100%;text-align:center; bottom: 0px;position:absolute">Big Apple</div> </div>
One use of this is we can add button on the image
<div style = "text-align:center;background-color:lightgrey;position:relative;display:inline-block">
<img src = "someapple.jpg">
<div style = "width:100%;text-align:center; bottom: 0px;position:absolute">
<button style = "display:inline">Big Apple</button>Big Apple
</div>
</div>
How to align text block vertically or horizontally in an image.
We can use the transform translate to move the text block half of its width to the left and half of its height up to align the text. This is needed because of the left, right, top or bottom CSS property applies to the left or top side of the text block, which will place the text block too much to the right if we use right:50%, too much to the bottom, if we use top:50%. More so especially when the text is long.
This problem can be addressed by assigning transform:translate(-50%, -50%) to center the text. If the text is right next to the image borders, we can use transform:translate(-50%,0) to move the text to the bottom or transform:translate(0,-50%) just to center vertically.
By the way in the translate(0,-50%), the first parameter 0 is the x position and the second parameter -50% is the y position. We can adjust each of these parameters to move the text horizontally or vertically respectively.
Examples
TEXT
<div style = "position:relative"> <img style = "width:100%" src="someimg.png"/> <divstyle = "position:absolute;left:50%;bottom:0; transform: translate(-50%, 0%);">TEXT</div> </div>
TEXT
<div style = "position:relative"> <img style = "width:100%" src="someimg.png"/>
<div style = "position:absolute;left:50%;bottom:0; transform: translate(-50%, -50%);">TEXT</div> </div>
TEXT
<div style = "position:relative"> <img style = "width:100%" src="someimg.png"/>
<div style = "position:absolute;top: 50%; left: 0px; transform: translate(0, -50%);">TEXT</div> </div>
Comments
Post a Comment