css怎么在图片上添加文字

html-css013

css怎么在图片上添加文字,第1张

文字在图片上显示

<!DOCTYPE html>

<html>

<head>

<title></title>

<meta charset="UTF-8">

<style>

#abox{

position:relative/*容器相对定位*/

width:300px/*宽度300px*/

margin:0 auto/*横向居中*/

}

#abox img{

width:300px/*宽度300px*/

}

span{

position:absolute/*文字容器绝对定位*/

display:block/*span转为块元素*/

width:100%/*宽度相对于父容器100%*/

text-align:center/*文字居中*/

top:0/*距离父容器顶部0*/

left:0/*距离父容器左侧0*/

color:red/*文字颜色红色*/

font-size:18px/*文字大小既文字高度18px*/

font-weight:bold/*文字加粗*/

}

</style>

</head>

<body>

<div id="abox"><!--容器,相对定位-->

  <img src="123.jpg"><!--图片-->

  <span>我要显示文字</span><!--文字,绝对定位-->

</div>

</body>

</html>

需要准备的材料分别有:电脑、浏览器、html编辑器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<body>标签中,输入html代码:。

<div style="margin-top: 30pxwidth: 200pxheight:80pxborder: 1px solid blue">

<div style="background-color: whiteposition: absoluteleft: 30pxtop: 15px">文字</div>

</div>

3、浏览器运行index.html页面,此时实现了边框角落开口显示文字。

第一步:我们需要准备一个图像

作为一个例子,我想在深色背景的背景上放置白色文字。

第二步:将图像和字母放在一个div元素中

将图像和字母放在一个div标签中。在示例中,将文字“万里长城”放在p标记中。当然您可以使用标题标签而不是p标签,也可以使用span标签。

1

2

3

4

<div class = "example" >

<img src="image/greatwall.jpg">

<p>万里长城</p>

</div>

第三步:指定position属性

为每个元素设置css的position属性。

对作为父元素的div指定为position:relative,以及对包含该字符串的p标签设置为absolute。img标签不动。

把p标签的位置设置为top:0left:0。

为了把图像放在横向上,请指定为img标签的宽度为width : 100%。

1

2

3

4

5

6

7

8

9

10

11

12

.example{/*父元素div*/

position: relative/*相対定位*/

}

.example p {

position: absolute/*绝対定位*/

color: white/*文字设为白色*/

top: 0

left: 0

}

.example img {

width: 100%

}