css实现居中的方法

news/2024/9/21 16:49:01 标签: css, 前端, html
htmledit_views">

水平居中

1. 行内设置text-align

给父元素设置text-align为center,一般用于实现文字水平居中

2. 给当前元素设置margin:0 auto

原理:块级独占一行,表现为在水平方向上占满整个父容器,当水平方向padding,border,width成定值时,margin左右为auto,默认margin左右平分剩余空间

html"><head>
    <style>
        .content{
            width: 600px;
            height: 300px;
            border: 1px solid #000;
        }
        .box{
            width: 200px;
            height: 200px;
            background-color: darkcyan;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="box"></div>
    </div>
</body>

但是当该元素设置了position为absolute时,需要加上left为0,right为0才能使margin:0 auto生效,让元素左右边界都紧贴其包含块的边缘。

html">.box{
    width: 200px;
    height: 200px;
    background-color: darkcyan;
    margin: 0 auto;
    left: 0;
    right: 0;
    position: absolute;
}

3. justify-content

父元素设置display为flex布局,justify-content为center,设置盒子在主轴的对齐方式为center

html">.content{
   width: 600px;
   height: 300px;
   border: 1px solid #000;
   display: flex;
   justify-content: center;
}

4. transform + position

父元素设置relative,子元素设置absolute,left为50%,距离左边偏移父元素50%,transform为translateX(-50%),向左平移自身宽度的50%

5. margin-left + position

与transform类似,只是margin-left为负的自身宽度的一半

当未知宽高时用transform,已知宽高可以用margin-left

垂直居中

1. 单行文本可以设置line-height

设置line-height与height相等

一般用于文字垂直居中

2. align-items

父元素设置flex,align-items为center,定义元素在侧轴对齐方式为center

3. transform + position

与水平居中一样,设置top:50%距离上边偏移父元素50%,transform为translateY(-50%)

4. margin-top + position

与水平居中一样,设置top:50%,margin-top:负的自身高度的一半

5. margin:0 auto

子元素设置absolute定位,top为0,bottom为0,margin设置auto

html">.box{
    width: 200px;
    height: 100px;
    background-color: darkcyan;
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
}

6. table-cell + vertical-align

给父元素设置display为table-cell,vertical-align为middle

整体居中

行内:text-align:center; line-height:height

块级:

1. flex

html">.content{
    width: 400px;
    height: 300px;
    border: 1px solid #000;
    display: flex;
    justify-content: center; /*水平居中*/
    align-items: center;  /*垂直居中*/
}

2. transform

html">.content{
    width: 400px;
    height: 300px;
    border: 1px solid #000;
    position: relative;
}
.box{
    width: 200px;
    height: 100px;
    background-color: darkcyan;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%)
}

3. margin

html">.content{
    width: 400px;
    height: 300px;
    border: 1px solid #000;
    position: relative;
}
.box{
    width: 200px;
    height: 100px;
    background-color: darkcyan;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -100px;
}


http://www.niftyadmin.cn/n/5669091.html

相关文章

大数据处理从零开始————3.Hadoop伪分布式和分布式搭建

1.伪分布式搭建&#xff08;不会用&#xff0c;了解就好不需要搭建&#xff09; 这里接上一节。 1.1 伪分布式集群概述 伪分布式集群就是只有⼀个服务器节点的分布式集群。在这种模式中&#xff0c;我们也是只需要⼀台机器。 但与本地模式不同&#xff0c;伪分布式采⽤了分布式…

从零开始学习TinyWebServer

写在前面 项目参考&#xff1a;https://github.com/qinguoyi/TinyWebServer 写作框架/图参考&#xff1a;https://blog.csdn.net/qq_52313711/article/details/136356042?spm1001.2014.3001.5502 原本计划是&#xff0c;先将项目代码大概看一遍&#xff0c;然后再着手实现一下…

Android Perfetto 学习

1、如何抓取性能日志 方式1、通过手机里的System Tracing抓取 1、点击Settings->System->Developer options->System Tracing->Record trace 打开 2、操作完成后&#xff0c;点击Settings->System->Developer options->System Tracing->Record trace…

Pytorch复习笔记--pytorch常见交叉熵函数的实现

1. nn.CrossEntropyLoss() 计算公式如下&#xff1a; L o s s ( x , c l a s s ) − l n ( e x [ c l a s s ] ∑ i e x [ i ] ) − x [ c l a s s ] l n ( ∑ i e x [ i ] ) Loss(x, class) -ln(\frac{e^{x[class]}}{\sum_{i}e^{x[i]}}) -x[class] ln(\sum_{i}e^{x[i]…

【C++】透析string类

个人主页&#xff1a;CSDN_小八哥向前冲~ 所属专栏&#xff1a;C入门 目录 string类介绍 auto和范围for auto关键字 范围for string类常用接口说明 string类常见构造 string类容量操作 string类的访问及遍历操作 string类修改操作 string的结构说明 vs下的结构 G下的…

数字英文验证码识别 API 对接说明

数字英文验证码识别 API 对接说明 本文将介绍一种 数字英文验证码识别 API 对接说明&#xff0c;它是基于深度学习技术&#xff0c;可用于识别变长英文数字验证码。输入验证码图像的内容&#xff0c;输出验证码结果。 接下来介绍下 数字英文验证码识别 API 的对接说明。 注册…

Android Manifest权限清单

Android权限部分可分为安装权限、运行时权限、特殊权限。 其中安装权限分普通权限和签名权限&#xff1a;普通权限安装后就有&#xff0c;无需重新授权&#xff1b;签名权限则需要系统签名才有的权限&#xff1b; 特殊权限则需要打开指定的系统页面进行授权&#xff0c;当然使用…

使用Assimp加载glb/gltf文件,然后使用Qt3D来渲染

文章目录 1.代码2.说明2.1.调用2.2.关于贴图 1.代码 ModelLoader.h #ifndef MODELLOADER_H #define MODELLOADER_H#include <QObject> #include <Qt3DRender> #include <QVector3D> #include <QGeometry>#include <assimp/Importer.hpp> #incl…