Step 1 : Create css file named "
image.css" which specifies the dimension of the image and image url in class named "
div" and dimensions for the text to be placed on the image. Use padding and
margin on the text to get it
vertically centered and also set "text-align" to "center" in class named "
p".
Ex :- image.css
.div { width:300px; height:100px; background-image:url('image.jpg'); }
.p {margin:0; padding:25px 0 0 0; text-align:center; }
Step 2 : Create html file named "
image.html" and create "<div>" for the image and "<p>" for the tex.
Ex :- <div>
<p>Text Over an image</p>
</div>
Full Code :
image.css
.div { width:300px; height:100px; background-image:url('image.jpg'); }
.p {margin:0; padding:25px 0 0 0; text-align:center; }
Difficult Method :
We can also use "position" and "z-index" place text over an image. There are 2 different methods in this :
Method 1 :
We can use Absolute positioning and z-index= -1 :
Step 1 : Create css file named "image.css" which specifies the dimension of the image, image url and z-index as -1 in class named "image" and dimensions for the text to be placed on the image. Use positon as absolute and set top and left margins as you wish on the text to get it
vertically centered and also set "text-align" to "center" in class named "imageText".
Ex :- image.css
.image {z-index: -1;width:300px; height:100px; background-image:url('bg.jpg'); }
.imageText {position: relative; top:20px; left:20px; }
Step 2 : Create html file named "
image.html" and create "<div>" for the image and "<p>" for the tex.
Ex :- <div class="image">
<p class="imgageText">Text Over an image</p>
</div>
Full Code :
image.css
.image { z-index: +1;width:300px; height:100px; background-image:url('bg.jpg'); }
.imageText {position: relative; top:20px; left:20px; }
image.html
<html>
<head>
<title>Text over an image </title>
<link rel="stylesheet" type="text/css" href="image.css">
</head>
<body>
<div class="image">
<p class="imageText">Text Over an image</p>
</div>
</body>
<html>
Method 2:
We can use Relative positioning and z-index= +1 :
Step 1 : Create css file named "
image.css" which specifies the dimension of the image, image url and z-index as -1 in class named "
image"
and dimensions for the text to be placed on the image. Use positon as
absolute and set top and left margins as you wish on the text to get it
vertically centered and also set "text-align" to "center" in class named
"
imageText".
Ex :- image.css
.image { z-index: +1;width:300px; height:100px; background-image:url('bg.jpg'); }
.imageText {position: relative; top:20px; left:20px; }
Step 2 : Create html file named "image.html" and create "<div>" for the image and "<p>" for the tex.
Ex :- <div class="image">
<p class="imgageText">Text Over an image</p>
</div>
Full Code :
image.css
.image { z-index: +1;width:300px; height:100px; background-image:url('bg.jpg'); }
.imageText {position: relative; top:20px; left:20px; }
image.html
<html>
<head>
<title>Text over an image </title>
<link rel="stylesheet" type="text/css" href="image.css">
</head>
<body>
<div class="image">
<p class="imageText">Text Over an image</p>
</div>
</body>
<html>