zoukankan      html  css  js  c++  java
  • 水平居中和transform: translateY(-50%) 实现元素垂直居中效果

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>垂直居中</title>
        <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .center {
             960px;
            height: 500px;
            margin: 0 auto;
            background: #1879D9;
        }
        
        .center p {
            background: #fff;
            position: relative;
            top: 50%;
            transform: translateY(-50%);
            left: 0;
            right: 0;
            margin: 0 auto;
             100px;
        }
        </style>
    </head>
    <body>
        <div class="center">
            <p>垂直居中内容</p>
        </div>
    </body>
    </html>
    

      效果如图:

     注意的是,当左右居中内容可能超过一半,iPhone5就会出现掉下来

    如下代码:

    html:

    <div class="oi_price_title"><span>付费项目:一生运势详批</span></div>

    样式:

    .oi_price_title{ position: relative; text-align: center;font-size: 0.15rem;margin-bottom: 0.05rem;min-height: 0.24rem; text-align: center;}
    .oi_price_title:before{ content: ""; position: absolute; left: 0; top:50%; transform: translateY(-50%);-webkit-transform: translateY(-50%); height: 1px; width: 100%; background-color: #fae5ca }
    .oi_price_title span{ position: absolute; left: 50%; top:50%; transform: translate(-50%,-50%); -webkit-transform: translate(-50%,-50%); z-index: 1; background-color: #fcefdd; height: 0.22rem; line-height: 0.22rem; padding: 0 0.05rem; border:1px solid #fae5ca; border-radius: 3px; font-size: 0.14rem; }

    如图:

     最终样式改为:

    .oi_price_title{ position: relative; text-align: center;font-size: 0.15rem;margin-bottom: 0.05rem;min-height: 0.24rem; text-align: center;}
    .oi_price_title:before{ content: ""; position: absolute; left: 0; top:50%; transform: translateY(-50%);-webkit-transform: translateY(-50%); height: 1px; width: 100%; background-color: #fae5ca }
    .oi_price_title span{ position: relative; z-index: 2; background-color: #fcefdd; height: 0.22rem; line-height: 0.22rem; padding: 0 0.05rem; border:1px solid #fae5ca; border-radius: 3px; font-size: 0.14rem; }

  • 相关阅读:
    转发与重定向的区别
    Servlet开发详讲
    Servlet的常见错误
    HTTP请求方式之POST和GET的区别
    Spring各种类型数据的注入
    Spring容器的基本使用
    Python接口自动化-测试用例编写
    Python接口自动化-设计测试用例
    python简明教程之数据结构(列表、元组、字典、集合)
    python简明教程之函数
  • 原文地址:https://www.cnblogs.com/huanghuali/p/6339374.html
Copyright © 2011-2022 走看看