资讯专栏INFORMATION COLUMN

内部元素横向&&垂直居中的15种常见写法

时飞 / 1056人阅读

摘要:非常简洁易懂,排名不分先后,开整第一种常见的第二种第三种通过的第四种标配标配标配三个标配一起使用才能发挥作用第五种,不能大量使用,会影响性能第六种第七种第八种第九种第十种第十一

非常简洁易懂,排名不分先后,开整!
HTML:

 
第一种:常见的margin
.main {
            position: relative;
            width: 800px;
            height: 500px;
            background-color: lightblue;
        }
        
        .content {
            position: absolute;
            width: 300px;
            height: 200px;
            top: 50%;
            left: 50%;
            margin-left: -150px;
            margin-top: -100px;
            background: lightpink;
        }
第二种:position+transform
.main {
            position: relative;
            width: 800px;
            height: 500px;
            background-color: lightblue;
        }
        
        .content {
            position: absolute;
            width: 300px;
            height: 200px;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: lightpink;
        }
第三种:通过position的top、right、bottom、left
.main {
            position: relative;
            width: 800px;
            height: 500px;
            background-color: lightblue;
        }
        
        .content {
            position: absolute;
            width: 300px;
            height: 200px;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: lightpink;
            margin: auto;
        }
第四种:display:table-cell
.main {
            width: 800px;
            height: 500px;
            background: lightblue;
            display: table-cell;
            /*标配*/
            vertical-align: middle;
            /*标配*/
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
            margin: auto;
            /*标配*/
        }
        /*三个标配一起使用才能发挥作用*/
第五种:calc,不能大量使用,会影响性能
 .main {
            width: 800px;
            height: 500px;
            background-color: lightblue;
        }
        
        .content {
            width: 300px;
            height: 70px;
            background: lightpink;
            margin: auto;
            position: relative;
            top: calc((100% - 70px)/2);
        }
第六种:flex+align-items
 .main {
            display: flex;
            width: 800px;
            height: 500px;
            background-color: lightblue;
            justify-content: center;
            align-items: center;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
        }
第七种:flex+align-self
.main {
            display: flex;
            width: 800px;
            height: 500px;
            background-color: lightblue;
            justify-content: center;
            text-align: center;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
            align-self: center;
        }
第八种:flex+margin
.main {
            display: flex;
            width: 800px;
            height: 500px;
            background-color: lightblue;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
            margin: auto;
        }
第九种:grid+align-content
.main {
            display: grid;
            width: 800px;
            height: 500px;
            background-color: lightblue;
            justify-content: center;
            align-content: center;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
        }
第十种:grid+align-item
.main {
            display: grid;
            width: 800px;
            height: 500px;
            background-color: lightblue;
            justify-content: center;
            align-items: center;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
        }
第十一种:grid+align-self
.main {
            display: grid;
            width: 800px;
            height: 500px;
            background-color: lightblue;
            justify-content: center;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
            align-self: center;
        }
第十二种:grid+margin
.main {
            width: 800px;
            height: 500px;
            display: grid;
            background: lightblue;
        }
        
        .content {
            width: 300px;
            height: 200px;
            margin: auto;
            background: lightpink;
        }
第十三种:grid+palce-content
  .main {
            width: 800px;
            height: 500px;
            display: grid;
            place-content: center;
            /*这是justy-content与align-items的结合写法*/
            background-color: lightblue;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
        }
第十四种:grid+place-items
 .main {
            display: grid;
            width: 800px;
            height: 500px;
            background-color: lightblue;
            place-items: center;
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
        }
第十五种:grid+template
.main {
            margin: auto;
            width: 800px;
            height: 500px;
            background-color: lightblue;
            display: grid;
            grid-template-columns: 1fr auto 1fr;
            grid-template-rows: 1fr auto 1fr;
            grid-template-areas: ". . ." ". amos ." ". . .";
        }
        
        .content {
            width: 300px;
            height: 200px;
            background: lightpink;
            grid-area: amos;
        }

好啦,十五种方法Get!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/113784.html

相关文章

  • 内部元素横向&&垂直居中15常见写法

    摘要:非常简洁易懂,排名不分先后,开整第一种常见的第二种第三种通过的第四种标配标配标配三个标配一起使用才能发挥作用第五种,不能大量使用,会影响性能第六种第七种第八种第九种第十种第十一 非常简洁易懂,排名不分先后,开整!HTML: 第一种:常见的margin .main { position: relative; ...

    crossoverJie 评论0 收藏0
  • 内部元素横向&&垂直居中15常见写法

    摘要:非常简洁易懂,排名不分先后,开整第一种常见的第二种第三种通过的第四种标配标配标配三个标配一起使用才能发挥作用第五种,不能大量使用,会影响性能第六种第七种第八种第九种第十种第十一 非常简洁易懂,排名不分先后,开整!HTML: 第一种:常见的margin .main { position: relative; ...

    tomlingtm 评论0 收藏0
  • [译]HTML&CSS Lesson4: 盒子模型

    摘要:而内联元素会并排显示,宽度紧随内容变化而变化。但元素又按照内联元素显示,不会另起一行。这种情况下,只能指定非内联元素的属性值。如下是为非内联元素设置宽度的例子高度元素的默认高度取决于它的内容。 现在我们已经熟悉了HTML和CSS。了解了它的基础。现在我们来更深入的了解元素在页面中的呈现和大小。 在这节课中,我们将会讨论什么是盒子模型,它的工作模式是怎样的。我们也会在课程中学习一些新的C...

    yzd 评论0 收藏0
  • [译]HTML&CSS Lesson4: 盒子模型

    摘要:而内联元素会并排显示,宽度紧随内容变化而变化。但元素又按照内联元素显示,不会另起一行。这种情况下,只能指定非内联元素的属性值。如下是为非内联元素设置宽度的例子高度元素的默认高度取决于它的内容。 现在我们已经熟悉了HTML和CSS。了解了它的基础。现在我们来更深入的了解元素在页面中的呈现和大小。 在这节课中,我们将会讨论什么是盒子模型,它的工作模式是怎样的。我们也会在课程中学习一些新的C...

    developerworks 评论0 收藏0
  • 前端-CSS3&H5

    摘要:高度模型浅识为的简写,简称为块级格式化上下文,为浏览器渲染某一区域的机制,中只有和中还增加了和。并非所有的布局都会在开发中使用,但是其中也会涉及一些知识点。然而在不同的纯制作各种图形纯制作各种图形多图预警 一劳永逸的搞定 flex 布局 寻根溯源话布局 一切都始于这样一个问题:怎样通过 CSS 简单而优雅的实现水平、垂直同时居中。记得刚开始学习 CSS 的时候,看到 float 属性不...

    Hydrogen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<