搜索
您的当前位置:首页正文

相对于某个div定位:position: relative &a

来源:知库网

编写中文网页,记得把头部语言改为 <html lang="zh-cn"> 而不是<html lang="en">

<!DOCTYPE html>
<html lang="zh-cn">

<head>
<meta charset="utf-8">
<style type="text/css">
* {
    padding: 0;
    margin: 0;
}

img {
    width: 100%;
    display: block;
}

.imgs {
    width: 100%;
    height: auto;
    position: relative;
}

.imgs p {
    width: 300px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    position: absolute;
    left: 50%;
    top: 50%;
    background: #E46262;
    opacity: .3;
    margin-left: -150px;
    margin-top: -50px;
}
</style>
</head>

<body>
    <div class="imgs">
        <img src="images/banner.jpg" alt="" title="这是logo">
        <p>这里是相对于div定位</p>
    </div>
</body>

</html>
相对于上层div定位,效果图.png

总结:

外层div添加:

position: relative;

内层div添加:

position: absolute; left: 50%; top: 50%;
Top