html - 为什么这个 div 会低于页面的 50% 而不是直接到中心?
问题描述
.try {
background-color: aqua;
width: 100px;
height: 100px;
margin: 0 auto;
margin-top: 50%;
position:relative;
}
<div class="try"></div>
您好,很抱歉提出一个愚蠢的问题,但是是否可以在不使用 flexbox 的情况下让这个 div 进入页面的中心?
解决方案
您需要在 css insted of margin-top 中设置 top 和 transform 属性。
top: 50%;
transform: translateY(-50%);
.try {
background-color: aqua;
width: 100px;
height: 100px;
margin: 0 auto;
position: relative;
top: 50%;
transform: translateY(-50%);
}
body,
html {
height: 100%;
}
<div class="try"></div>
推荐阅读
- c - 蓝牙 C 程序找不到库 -lbluetooth
- python-2.7 - 为什么 apscheduler 使用 get_jobs 为空?
- java - jni.h - 没有这样的文件或目录 - 正确设置 JAVA_HOME 环境变量
- python - 如何修改绘图轴起始位置?
- javascript - 我如何通过reducer.js文件中redux中的对象数组映射来删除用户
- node.js - 列出 s3 中大于某个 lastModified 日期的文件
- dialogflow-es - 如何问“这有帮助吗?” 在呈现来自 Intent 的响应后,在对话结束时在 DialogFlow 中
- ios - 如何使用 Swift 在 Facebook 上分享视频?
- python-3.x - 扁平化字典列表
- c++ - C++ 函数模板完全特化错误