旧时代css领域三大经典问题

我们知道,在css中,有很多属性都可以达到居中的效果,而作为一名前端开发者,垂直居中要比水平居中麻烦的多,这是历史遗留的问题。
最初的css针对的主要是web文档的布局,他对垂直方向的需求并没有那么的强烈,以至于并没有专门的设计来实现这一需求。
事实上在css领域,存在着三大经典问题,垂直居中,多列等高和宽度自适应。
我们逐一来看一下这这三大经典问题。

垂直居中

在讲垂直居中之前,我们顺带来说一下水平居中,如果是块级元素,可以在子元素中添加margin:0 auto,如果是行内元素,可以直接添加一个text-align:center这是最为经典的实现方式,当然还有其他的方式,也可以参考下面垂直居中的一些方式,我们重点来讲垂直居中,假设有这样两个元素:

<style>
        .parent{
            width: 100px;height: 100px;
            background-color: aquamarine;
        }
        .child{
            width: 100px;height: 10px;
            background-color: red;
        }
    </style>
    <div class="parent">
        <div class="child"></div>
    </div>
  1. 绝对定位和负边距
    .parent{
        position: relative;
    }
    .child{
        position: absolute;
        top: 50%;
        margin: -5px 0 0 0; /* 值得一提的是如果child的高度为10%,这里的负边距可以改为-5% */
     }
  1. 绝对定位和transform
    .parent{
        position: relative;
    }
    .child{
        position: absolute;
        top: 50%;
        transform: translate(0, -50%);
     }
  1. 绝对定位和margin:auto
    .parent{
        position: relative;
    }
    .child{
        position: absolute;
        top: 0;bottom: 0;
        margin: auto;
     }
  1. 万能的伪元素
.child{
    margin-top: -5px;
}
.parent::before{
    content: '';
    height: 50%;
    display: block;
}
  1. 表格布局table
    .parent{
        display:table;
    }
    .child{
        display:table-cell;
        vertical-align: middle;
     }
  1. 单行文本下的line-height
  2. 弹性布局(flex)弹性布局下的实现实际不止这么一种方式这里提一下,后面会单独开一章节讲弹性布局
.parent{
    display: flex;
    align-items: center;
}

多列等高

我们先来看一下这到底是一个什么问题:

<style>
        .child{
            width: 100px;
            background-color: #2196F3;
            display: inline-block;
        }
    </style>
    <div class="parent">
        <div class="child">内容内容内容内容内容内容内容内容内容内容</div>
        <div class="child">内容</div>
        <div class="child">内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</div>
    </div>
多列等高问题

因为内容的不同这三列拥有各自的高度,怎么让他们一样高呢?

  1. 真等高:flex布局
    这是最简单的一种方式,只需要给parent加一个属性display:flex,因为弹性布局默认自带等高布局的特点。
  2. 真等高:利用table
    table布局同样也具有等高的特性,这个也非常简单,给parent加一个属性display:table,然后改变child的display为display:table-cell
    3、假等高:利用子组件的内外边距(padding和margin)
.parent{
    overflow: hidden;
}
.child{
    display: inline-block;
    padding-top: 9999px;
    margin-top: -9999px;
}
/* 或者 */
.child{
    padding-bottom: 9999px;
    margin-bottom: -9999px;
    float: left;
}
  1. 假等高:父容器渐变背景色

宽度自适应

最后一个问题就是在我们的多列布局中,最后一个div宽度自适应的问题,我们来看解决方法:
<style>
.parent{
width: 200px;height: 100px;
}
.child1{
width: 100px;height: 100%;
background-color: #2196F3;
display: inline-block;
}
.child2{
height: 100%;
background-color: rgb(44, 228, 27);
display: inline-block;
}
</style>
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>

  1. flex-group:1
.parent{
     display:flex;
}
.child2{
    flex-group:1; /* 或者直接flex:1也是一样的效果 */
}
  1. 依旧是table,将parent设置display:table,然后将child1设置为display:table-cell
  2. float配合overflow:hidden
.child1{
    float: left;
}
.child2{
    background-color: rgb(44, 228, 27);
    overflow:hidden;
}

child2会默认填满整个parent,然后因为overflow的关系隐藏在child1下的部分。

  1. 这里有一种特殊情况,如果子元素只有一个,需要定死一边的宽度,往另外一遍填满,还可以使用绝对定位加margin-left的方法
.parent{
     position: absolute;
}
.child{
    margin-left:100px;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 10,156评论 0 26
  • 单列布局 水平居中 水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种...
    阿七笔记阅读 1,463评论 0 1
  • 单列布局水平居中水平居中 水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介...
    Osmond_wang阅读 2,732评论 0 1
  • 移动开发基本知识点 一.使用rem作为单位 html { font-size: 100px; } @media(m...
    横冲直撞666阅读 8,941评论 0 6
  • display:设置元素的显示方式 display:block(块级元素) 默认为父元素宽高,可设置宽高相对前序换...
    bluishwhiteC阅读 3,885评论 0 0

友情链接更多精彩内容