鱼C论坛

 找回密码
 立即注册
查看: 2007|回复: 6

[已解决]不二如是老师的html5+css3 第五节课程垂直居中失败

[复制链接]
发表于 2018-7-14 08:48:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <!doctype html>
  2. <html>
  3.         <head>
  4.                 <meta charset="utf-8">
  5.                 <title>欢迎来到HTML世界</title>
  6.                 <style type="text/css">
  7.                         html {
  8.                                 height: 100%;
  9.                                 color: #FF0088;
  10.                                 /*设置颜色*/
  11.                         }
  12.                         body {
  13.                                 background: url("https://picsum.photos/1600/1200/?random") center center;/*导入居中得图片*/
  14.                                 background-size: cover;
  15.                         margin: 0; /*初始化*/
  16.                         padding:0; /*初始化*/
  17.                         position: relative;
  18.                         }
  19.                         #container {
  20.                                 width: 100%;
  21.                                 text-align: center;
  22.                                 /*上面两行是水平居中*/
  23.                                 position: absolute;
  24.                                 top: 50%;
  25.                                 transform: translateY(-50%);
  26.                                 /*上面的是垂直居中*/
  27.                         }
  28.                 </style>
  29.         </head>
  30.         <body>
  31.                 <div id="container">
  32.                         <h1>我爱鱼C</h1>
  33.                         <p>www.fishc.com-让编程改变世界</p>
  34.                         <a >咻!</a>

  35.                 </div>
  36.         </body>
  37. </html>
复制代码

请问垂直水平的问题在哪?
多谢
最佳答案
2018-7-14 14:08:07
1、get用实用Tips - 32 - 超好用的#贴图网站 |【Lorem Picsum】中的在线贴图技巧,很棒!

2、请在body样式中添加:
  1. height: 100%;
复制代码


因为父级元素是相对定位,所以要利用height撑开,这样子元素才知道相对一个宽度进行top50%的移动
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-14 08:50:18 | 显示全部楼层
无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-14 08:55:57 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-14 10:38:47 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-14 14:03:00 | 显示全部楼层
已解决:
  1. <!doctype html>
  2. <html>
  3.         <head>
  4.                 <meta charset="utf-8">
  5.                 <title>欢迎来到HTML世界</title>
  6.                 <style type="text/css">
  7.                         html {
  8.                                 height: 100%;
  9.                                 color: #FF0088;
  10.                                 /*设置颜色*/
  11.                         }
  12.                         body {
  13.                                 background: url("https://picsum.photos/1600/1200/?random") center center;/*导入居中得图片*/
  14.                                 background-size: cover;
  15.                         }
  16.                         #container {
  17.                                 width: 100%;
  18.                                 text-align: center;
  19.                                 /*上面两行是水平居中*/
  20.                                 position: absolute;
  21.                                 top: 50%;
  22.                                 transform: translateY(-50%);
  23.                                 /*上面的是垂直居中*/
  24.                         }
  25.                 </style>
  26.                 <body
  27.                         margin: 0; /*初始化*/
  28.                         padding:0; /*初始化*/
  29.                         position: relative;>
  30.         </head>
  31.         <body>
  32.                 <div id="container">
  33.                         <h1>我爱鱼C</h1>
  34.                         <p>www.fishc.com-让编程改变世界</p>
  35.                         <a >咻!</a>

  36.                 </div>
  37.         </body>
  38. </html>
复制代码

初始化我写错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-14 14:08:07 | 显示全部楼层    本楼为最佳答案   
1、get用实用Tips - 32 - 超好用的#贴图网站 |【Lorem Picsum】中的在线贴图技巧,很棒!

2、请在body样式中添加:
  1. height: 100%;
复制代码


因为父级元素是相对定位,所以要利用height撑开,这样子元素才知道相对一个宽度进行top50%的移动
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-16 20:52:52 | 显示全部楼层
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 06:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表