css实现的在图片上面添加文字说明

css实现的在图片上面添加文字说明:
在一些图片效果中,图片上面会有文字说明,下面就通过代码实例介绍一下如何实现此效果。
代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<style type="text/css">
.thediv{
  width:400px;
  height:200px;
  position:relative;
}
img{
  width:400px;
  height:200px;
}
.thediv span{
  width:400px;
  height:50px;
  display:block;
  text-align:center;
  line-height:50px;
  background-color:#333;
  filter:alpha(opacity=50);
  -moz-opacity:0.5;
  opacity:0.5;
  position:absolute;
  top:100px;
  color:red;
}
</style>
</head>
<body>
<div class="thediv">
  <img src="mytest/demo/tree.jpg"/>
  <span>蚂蚁部落欢迎您</span>
</div>
</body>
</html>

以上代码实现了我们的要求,图片上面有文字说明,并且具有半透明效果。实现原理非常的简单,也就是设置最外层div使用相对定位,然后设置span元素绝对定位,这样就可以以外层div为定位参考对象,然后设置top属性值就可以了。但是上面的并不完美,因为不但背景透明了,而且文字也透明了,这往往并不是我们希望看到的,代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<style type="text/css">
.thediv{
  width:400px;
  height:200px;
  position:relative;
}
img{
  width:400px;
  height:200px;
}
.thediv span{
  width:400px;
  height:50px;
  display:block;
  text-align:center;
  line-height:50px;
  position:absolute;
  top:100px;
}
.content{
  background-color:#333;
  filter:alpha(opacity=50);
  -moz-opacity:0.5;
  opacity:0.5;
}
.text{
  color:red;
  z-index:100;
}
</style>
</head>
<body>
<div class="thediv">
  <img src="mytest/demo/tree.jpg"/>
  <span class="content"></span>
  <span class="text">蚂蚁部落欢迎您</span>
</div>
</body>
</html>

以上代码实现了我们的要求,背景透明但是文本不透明。在上面的代码中,之所以文字透明,是因为透明属性具有继承性,子元素也会出现透明效果,这样就可以利用定位的方式将存放文本的span元素覆盖与半透明的span元素之上。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=12679

更多内容可以参阅:http://www.softwhy.com/divcss/

时间: 2024-08-09 14:46:45

css实现的在图片上面添加文字说明的相关文章

python 图片上添加文字

1 import PIL 2 from PIL import ImageFont 3 from PIL import Image 4 from PIL import ImageDraw 5 6 #设置字体,如果没有,也可以不设置 7 font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",13) 8 9 #打开底版图片 10 imageFile = "base.png&qu

opencv在图片上添加文字

/****************************************** func:cvText desc:put text on an image @param img The image pointer which we want to put text on @param text the text pointer @param x the x coordinate @param y the y coordinate @return null ****************

【Android】在图片上添加文字

[Android]在图片上添加文字 在Edittext中插入图片 下载地址:http://www.devstore.cn/code/info/604.html

图片上添加文字--&lt;div&gt;

1.常用的方式是:将一张图片设置为背景,然后在里面加文字,你加入的图片代码是: <img src="img.jpg" width='100px" height="50px"> 改为 <div style="background:url('img.jpg') no-repeat;width:100px;height:50px">添加文字</div> ===========================

python PIL(pillow)图像处理-图片上添加文字

from PIL import Image, ImageDraw, ImageFont def gen_img(size=None): if size is None: size = 400 #生成大小为400x400RGBA是四通道图像,RGB表示R,G,B三通道,A表示Alpha的色彩空間 image = Image.new(mode='RGBA', size=(400, 400), color=(255, 55, 55)) # ImageDraw.Draw 简单平面绘图 draw_tabl

html+css 在图片上添加文字

html <view class="container"> <image class="" src="{{book.image}}"></image> <view class="description"> <text class="title">{{book.title}}</text> <text class="aut

在图片上添加文字水印

1 <?php 2 /** 3 打开任何一种格式的图片 在图片的中间加上一个文字水印 保存 4 只是保存下来 并不会输出到浏览器 5 */ 6 function imagewater($filename,$string){ 7 //获得图片的属性 8 list($width,$height,$type) = getimagesize($filename); 9 //可以处理的照片的类型 10 $types = array(1=>"gif",2=>"jpeg&

在图片上添加文字

<ul class="departmentheadpic"> <li style=" background:#68aae1 url(skin/images2/department.png) no-repeat center top;"> <div style="width:1000px;height:230px;margin:0 auto;"> <div class="pictext"

DIV CSS鼠标经过悬停在图片上时图片上方显示文字(转)

DIV CSS鼠标悬停在没有文字内容图片上时图片上方显示文字,完全是纯css div实现.CSS实现鼠标悬停放图片上方时显示美化内容. 原始图片显示没有文字在上方,当鼠标经过悬停时显示文字并且文字背景为半透明.可以使用纯DIV+CSS实现鼠标悬停图片上显示文字内容. 纯div+css实现第一张图片没有文字内容,鼠标悬停时出现第二张图那样效果 使用div css实现鼠标悬停图片上方时显示文字内容原理: 首先我们设置一个盒子对象,并且将图片使用style标签内设置为CSS背景图片,同时设置该对象ht