To display text on an image using CSS, you need to: Create an element containing the image and assign it an ID or class in HTML. Use position and top and left properties in CSS to position text elements on images. Add text to CSS elements using the content property. Style text using properties such as font-family, font-size, and color. Fine-tune text position and style as needed to get the desired effect.
How to use CSS to display text on an image
To use CSS to display text on an image, you can use the following Steps:
1. Create element in HTML
In HTML, create a div or span element containing the image and assign it an ID or class.
<code class="html"><div id="image-container"> <img src="image.jpg" alt="Image"> </div></code>
2. Positioning elements in CSS
Use CSS to position text elements on images. You can use position
and top
, left
attributes:
<code class="css">#image-container { position: relative; } #text { position: absolute; top: 20px; left: 10px; }</code>
position: relative
to set the container element as a reference point. position: absolute
Removes the text element from the normal document flow and positions it relative to the container. The top
and left
properties determine the offset position of the text element within the container. 3. Add text content to CSS
Use the content
attribute to add text to a CSS element:
<code class="css">#text { content: "This is my text"; }</code>
4. Styling text
Use CSS to style text, such as font, size, and color:
<code class="css">#text { font-family: Arial, sans-serif; font-size: 20px; color: white; }</code>
5. Fine-tuning
Fine-tune text position and style as needed to get the desired effect. You can also use other CSS properties, such as background-color
and padding
, to beautify text elements.
Sample code
<code class="html"><div id="image-container"> <img src="image.jpg" alt="Image"> </div></code>
<code class="css">#image-container { position: relative; } #text { position: absolute; top: 20px; left: 10px; font-family: Arial, sans-serif; font-size: 20px; color: white; }</code>
The above is the detailed content of How to make text appear on the picture with css. For more information, please follow other related articles on the PHP Chinese website!