|
发表于 2019-2-20 15:19:06
|
显示全部楼层
交作业,container的父元素body定位不能为relative,否则container的top属性不起作用。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>第四、五节:页面美化,优化</title>
- <style>
- *{
- margin: 0;
- padding: 0;
- }
- html{
- height: 100%;
- color: #d81d1d;
- }
- /* body元素的heigt不能设置为100%,设置后,背景图会有重复
- * 原因:浏览器似乎会给body加了一个margin:8,body元素的height实际会比html的高度值小
- * 处理:方式1:清除margin值;使用*{margin:0}初始化
- * 方式2:让body继承html的高度
- */
- body{
- background: url("../images/bg.jpg") center center;
- /* 设置网页的背景图片,并水平垂直居中 */
- background-size: cover;
- /* 设置背景图模式为cover */
- /* position: relative; */
- /* body元素position不能设置relative,否则container的top属性不起作用 */
- }
- #container{
- width: 100%;
- text-align: center;
- position: absolute;
- /* 设置垂直居中 */
- top: 50%;
- transform: translateY(-50%);
- /* 垂直上移50%的高度 */
- }
- </style>
- </head>
- <body>
- <div id="container">
- <h1>我爱鱼C</h1>
- <p>fishc.com.cn-让编程改变世界</p>
- <a href="http://fishc.com.cn">传送门</a>
- </div>
- </body>
- </html>
复制代码 |
|