欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
答:使用CSSposition属性
创建图库或类似的照片时,您可能需要在图像上放置一些标题文字或说明。您可以将CSS定位方法与margin属性结合使用,以将文本定位或放置在图像(即<img>元素)上。
 
让我们看下面的示例,以了解其基本工作原理:
 
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Placing Text Over an Image in CSS</title>
<style>
    .box{
        position: relative;
        display: inline-block; /* Make the width of box same as image */
    }
    .box .text{
        position: absolute;
        z-index: 999;
        margin: 0 auto;
        left: 0;
        right: 0;
        top: 40%; /* Adjust this value to move the positioned div up and down */
        text-align: center;
        width: 60%; /* Set the width of the positioned div */
    }
</style>
</head> 
<body>
    <div class="box">
        <img src="images/kites.jpg" alt="Flying Kites">
        <div class="text">
            <h1>Flying Kites</h1>
        </div>
    </div>
</body>
</html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h61127.shtml