css 标签上 title 和 alt 的区别

html-css017

css 标签上 title 和 alt 的区别,第1张

1. alt 是给搜索引擎识别, 在图像无法显示时的替代文本title 是关于元素的注释信息, 主要是给用户解读 。

2. 当鼠标放到文字或是图片上时有 title 文字显示 。

3. (因为IE不标准)在 IE 浏览器中 alt 起到了 title 的作用, 变成文字提示 。

    1. 在定义 img 对象时, 将 alt 和 title 属性写全, 可以保证在各种浏览器中都能正常使用 。

4. alt 属性和 title 属性的区别是:

    1. 前者 alt 是在图片无法加载的时候才会显示的其值

    2. 而 title 是在图片正常加载鼠标划上去显示的值

    3. 虽然 alt 也有后者的功能, 但是只是在低版本的 ie 浏览器才支持, 高版本及标准浏览器不支持这个功能了 。

1、首先先在页面里加载一张图片,代码和效果如下图所示:

2、然后设置给图片起一个class名,方便一会儿的操作。

3、然后给图片设置css样式,因为方便的原因就直接在html页面写css样式了。

4、经常使用“margin: 0 auto”来实现水平居中,而一直认为“margin: auto”是不能实现垂直居中,但是实际上,仅需要添加一些限制便能实现效果,就是通过定位:

position: absolute

top: 0

left: 0

bottom: 0

right: 0

设置定位让上下左右都为0,然后通过margin:0 auto,来让元素实现上下左右都居中。

5、设置完CSS样式之后,通过浏览查看代码的效果就可以,可以看到图片已经实现了。

6、最后给大家附上全部的代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>使用CSS让图片水平垂直居中</title>

</head>

<body>

<img class="pic" src="img/timg.jpg" alt="" />

</body>

<style type="text/css">

.pic{

margin: auto

position: absolute

top: 0

left: 0

bottom: 0

right: 0

}

</style>

</html>

写个简单的例子给你吧

htlm如下:

<h4>图片水平居中</h4>

<div class="demo1">

<img src="你的图片" alt="">

</div>

<h4>图片垂直居中</h4>

<div class="demo2">

<div class="imgbox">

<img src="你的图片" alt="">

</div>

</div>

<h4>图片水平垂直居中</h4>

<div class="demo3">

<div class="imgbox">

<img src="你的图片" alt="">

</div>

</div>

css如下:

<style type="text/css">

.demo1{width: 200pxheight: 200pxborder: 1px solid #cccdisplay: inline-blocktext-align: center}

.demo1 img{width: 100pxheight: auto}

.demo2{width: 200pxheight: 200pxborder: 1px solid #cccdisplay: table}

.demo2 .imgbox{display: table-cellvertical-align: middle}

.demo2 .imgbox img{width: 100pxheight: auto}

.demo3{width: 200pxheight: 200pxborder: 1px solid #cccdisplay: table}

.demo3 .imgbox{display: table-cellvertical-align: middletext-align: center}

.demo3 .imgbox img{width: 100pxheight: auto}

</style>