zoukankan      html  css  js  c++  java
  • pc端常见布局---水平居中布局 单元素不定宽度

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>常见元素布局</title>
            <style type="text/css">
                /* 一、水平居中布局 */
                /* 1.单个元素水平居中 宽度不固定 缺点:需要涉及到父类的样式*/
                .content {
                    text-align: center;
                }
    
                .box {
                    display: inline-block;
                    color: #fff;
                    background: #0000FF; 
                }
            </style>
        </head>
        <body>
            <div class="content">
                <div class="box">
                宽度不固定
                </div>
            </div>
        </body>
    </html>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>常见元素布局</title>
            <style type="text/css">
                /* 一、水平居中布局 */
                /* 2.单个元素水平居中 宽度不固定 缺点:设置为表格元素,内部元素的布局有可能受到影响*/
                .box {
                    display: table;
                    margin: 0 auto;
                    background: #ff9933;
                    color: #fff; /* background和color测试更好的观看效果 */
                }
            </style>
        </head>
        <body>
            <div class="content">
                <div class="box">
                    宽度不固定
                </div>
            </div>
        </body>
    </html>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>常见元素布局</title>
            <style type="text/css">
                /* 一、水平居中布局 */
                /* 3.单个元素水平居中 宽度不固定 缺点:transform,兼容性较差*/
                .content {
                    position: relative;
                }
    
                .box {
                    position: absolute;
                    left: 50%;
                    transform: translateX(-50%);
                    background: #ff9933;
                    color: #fff; /* background和color测试更好的观看效果 */
                }
            </style>
        </head>
        <body>
            <div class="content">
                <div class="box">
                    宽度不固定
                </div>
            </div>
        </body>
    </html>

    效果:

  • 相关阅读:
    php 数组
    条件语句if else ,switch ,while ,do.while
    if..else 判断中的 Boolean()转换
    wampserver 集成环境
    sublime text 安装及使用
    vue tab切换
    SVG 基础
    gitosis管理员的密钥丢失解决办法
    源码安装MySQL
    Xshell远程登录
  • 原文地址:https://www.cnblogs.com/lhl66/p/10374403.html
Copyright © 2011-2022 走看看