translate(-50%,-50%) 属性:
向上和左,移动自身长 宽 的 50%,使其居于中心位置。
与使 用M arg in实现居中不同的是,mar gin 必须 知道 自身的宽高,而translate可以在不知道宽高的情况下进行居中,tranlate函数中的百分比是相对于自身宽高的百分比
(使用top和left为50%时,以窗口左上角为原点)。
示例:
<!DOCTY PE ht ML > <html lang="en"> <head> < ;m eta charset="UTF-8"> <meta n am e="viewport" content="width=device-width, in IT ial -s cale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie= Edge "> <title>Document</title> <style media="screen"> .cont ai ner { position: relative; width: 50%; } .container img { width: 100%; dis play : block; h ei ght: auto; } .overlay { width: 100%; height: 100%; position: absolute; left: 0; top: 0; right: 0; bottom: 0; opacity: 0; transition: 0.5s ease; background: rgb(0, 0, 0); } .container:hover .overlay { opacity: 0.5; } .text { color: white; font- Size: 20px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -ms -t ransform: translate(-50%, -50%); } </style> </head> <body> <h2>淡入效果</h2> <div class="container"> <img src="./img/photo2. jpg " alt="Avatar" class="image"> <div class="overlay"> <div class="text">Hello World</div> </div> </div> </body> </html>
效果:
到此这篇关于CSS中的translate(-50%,-50%)实现水平垂直居中效果的 文章 就介绍到这了,更多相关css translate水平垂直居中内容请搜索以前的文章或继续浏览下面的相关文章,希望大家以后多多支持!
总结
以上是 为你收集整理的 CSS中的translate(-50%,-50%)实现水平垂直居中效果 全部内容,希望文章能够帮你解决 CSS中的translate(-50%,-50%)实现水平垂直居中效果 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
查看更多关于CSS中的translate(-50%,-50%)实现水平垂直居中效果的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did201439