面试官:你能实现多少种水平垂直居中的布局(定宽高和不定宽高)

简介: 我们在日常的开发中,经常会遇到这样一个问题,就是如何实现居中水平垂直居中对齐。并且在面试中也会出现这样的问题,但是我们往往回答的不是很全部,而导致没有得到面试加分。接下来我们通过不同的方式来实现,让我们成功破解这道面试。


我们在日常的开发中,经常会遇到这样一个问题,就是如何实现居中水平垂直居中对齐。并且在面试中也会出现这样的问题,但是我们往往回答的不是很全部,而导致没有得到面试加分。接下来我们通过不同的方式来实现,让我们成功破解这道面试。


1、定宽高


一、绝对定位和负magin值


<template>
    <div id="app">
        <div class="box">
            <div class="children-box"></div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    position: relative;
}
.children-box {
    position: absolute;
    width: 100px;
    height: 100px;
    background: yellow;
    left: 50%;
    top: 50%;
    margin-left: -50px;
    margin-top: -50px; 
}
</style>


二、绝对定位 + transform


<template>
    <div id="app">
        <div class="box">
            <div class="children-box"></div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    position: relative;
}
.children-box {
    position: absolute;
    width: 100px;
    height: 100px;
    background: yellow;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%); 
}
</style>


三、绝对定位 + left/right/bottom/top + margin


<template>
    <div id="app">
        <div class="box">
            <div class="children-box"></div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    position: relative;
}
.children-box {
    position: absolute;
    display: inline;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0px;
    background: yellow;
    margin: auto;
    height: 100px;
    width: 100px;
}
</style>


四、flex布局


<template>
    <div id="app">
        <div class="box">
            <div class="children-box"></div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: flex;
    justify-content: center;
    align-items: center;
}
.children-box {
    background: yellow;
    height: 100px;
    width: 100px;
}
</style>


五、grid布局


<template>
    <div id="app">
        <div class="box">
            <div class="children-box"></div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: grid;
}
.children-box {
    width: 100px;
    height: 100px;
    background: yellow;
    margin: auto;
}
</style>


六、table-cell + vertical-align + inline-block/margin: auto


<template>
    <div id="app">
        <div class="box">
            <div class="children-box"></div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.children-box {
    width: 100px;
    height: 100px;
    background: yellow;
    display: inline-block;// 可以换成margin: auto;
}
</style>


2、不定宽高


一、绝对定位 + transform


<template>
    <div id="app">
        <div class="box">
            <div class="children-box">111111</div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    position: relative;
}
.children-box {
   position: absolute;
   background: yellow;
   left: 50%;
   top: 50%;
   transform: translate(-50%, -50%);
}
</style>


二、table-cell


<template>
    <div id="app">
        <div class="box">
            <div class="children-box">111111</div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.children-box {
   background: yellow;
   display: inline-block;
}
</style>


三、flex布局


<template>
    <div id="app">
        <div class="box">
            <div class="children-box">11111111</div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: flex;
    justify-content: center;
    align-items: center;
}
.children-box {
    background: yellow;
}
</style>


四、flex变异布局


<template>
    <div id="app">
        <div class="box">
            <div class="children-box">11111111</div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: flex;
}
.children-box {
    background: yellow;
    margin: auto;
}
</style>



五、grid + flex布局


<template>
    <div id="app">
        <div class="box">
            <div class="children-box">11111111</div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: grid;
}
.children-box {
    background: yellow;
    align-self: center;
    justify-self: center;
}
</style>


六、gird + margin布局


<template>
    <div id="app">
        <div class="box">
            <div class="children-box">11111111</div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    display: grid;
}
.children-box {
    background: yellow;
    margin: auto;
}
</style>


七、writing-mode属性布局


<template>
    <div id="app">
        <div class="box">
            <div class="children-box">
                <p>11111</p>
            </div>
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    writing-mode: vertical-lr;
    text-align: center;
}
.box>.children-box {
    writing-mode: horizontal-tb;
    display: inline-block;
    text-align: center;
    width: 100%;
}
.box>.children-box>p {
    display: inline-block;
    margin: auto;
    text-align: left;
    background: yellow;
}
</style>

writing-mode 属性定义了文本在水平或垂直方向上如何排布。 兼容性上还有些小瑕疵,但大部分浏览器已经支持。


3、番外(图片定高|不定高水平垂直居中)


一、table-cell


<template>
    <div id="app">
        <div class="box">
            <img src="https://ss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/242dd42a2834349b406751a3ceea15ce36d3beb6.jpg">
        </div>
        </div>
</template>
<style type="text/css">
.box {
     height: 200px;
    width: 200px;
    display: table-cell;
    text-align: center;
    border: 1px solid #ccc;
    vertical-align: middle;
}
</style>


二、 ::after


<template>
    <div id="app">
        <div class="box">
            <img src="https://ss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/242dd42a2834349b406751a3ceea15ce36d3beb6.jpg">
        </div>
    </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    text-align: center;
}
.box::after {
    content: '';
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}
img {
    vertical-align: middle;
}
</style>


三、 ::before


<template>
    <div id="app">
        <div class="box">
            <img src="https://ss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/242dd42a2834349b406751a3ceea15ce36d3beb6.jpg">
        </div>
        </div>
</template>
<style type="text/css">
.box {
    width: 200px;
    height: 200px;
    border: 1px solid #ccc;
    text-align: center;
    font-size: 0;
}
.box::before {
    display: inline-block;
    vertical-align: middle;
    content: '';
    height: 100%;
}
img {
    vertical-align: middle;
}
</style>


四、总结


这里总结了很多种垂直水平居中的方法,但是肯定不是最完全的,还有很多其他中方式。但是总体的思路可以总结为以下几点。


一、内联元素居中布局


  1. 水平居中


  • 行内元素可设置:text-align: center;
  • flex布局设置父元素:display: flex; justify-content: center;


  1. 垂直居中


  • 单行文本父元素确认高度:height === line-height
  • 多行文本父元素确认高度:disaply: table-cell; vertical-align: middle;


二、块级元素居中布局


  1. 水平居中


  • 定宽: margin: 0 auto;
  • 不定宽: 参考上诉例子中不定宽高例子。


  1. 垂直居中


  • position: absolute设置left、top、margin-left、margin-to(定高);
  • position: fixed设置margin: auto(定高);
  • display: table-cell;
  • transform: translate(x, y);
  • flex(不定高,不定宽);
  • grid(不定高,不定宽),兼容性相对比较差;
目录
相关文章
|
2月前
|
前端开发 JavaScript Serverless
揭秘CSS布局神器:vw/vh、rem、%与px大PK,掌握它们,让你的网页设计秒变高大上,面试难题迎刃而解!
【8月更文挑战第4天】在Web开发中,合理选择CSS单位对响应式布局至关重要。本文探索viewport单位(vw/vh)、rem、百分比(%)及像素(px)的基础知识与应用场景。通过代码示例,展示如何运用这些单位实现全屏布局、尺寸比例调整、灵活的元素大小及固定尺寸。最后,模拟面试题,介绍如何仅用CSS实现一个元素的高度为其宽度两倍且响应视口变化的方法。
166 8
|
5月前
|
前端开发 容器
CSS面试考点:隐藏元素、BFC、垂直居中、CSS3新特性
【4月更文挑战第2天】 CSS面试考点:隐藏元素、BFC、垂直居中、CSS3新特性
49 10
|
5月前
|
JSON 小程序 JavaScript
面试官说,布局小程序页面记得用TDesign组件库
面试官说,布局小程序页面记得用TDesign组件库
|
5月前
|
前端开发 Java C++
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
|
5月前
|
前端开发 开发者 容器
【前端|CSS系列第4篇】面试官:你了解居中布局吗?
【前端|CSS系列第4篇】面试官:你了解居中布局吗?
面试官必问——水平垂直居中的11种解决方案
盒子水平垂直居中的N种方案 1、定位+margin拉回
70 0
|
存储 NoSQL Java
面试必问:说一下 Java 虚拟机的内存布局?
面试必问:说一下 Java 虚拟机的内存布局?
116 0
|
前端开发 容器
🍊Flex布局最佳实践之骰子实战篇(面试高频考点,速来围观呀~)
🍊Flex布局最佳实践之骰子实战篇(面试高频考点,速来围观呀~)
581 6
🍊Flex布局最佳实践之骰子实战篇(面试高频考点,速来围观呀~)
|
缓存 算法
面试官:RecyclerView布局动画原理了解吗?
面试官:RecyclerView布局动画原理了解吗?
面试官:RecyclerView布局动画原理了解吗?
|
前端开发 容器
面试题:水平垂直居中的17种方法
CSS 方面问的最多的问题之一,我想分三种情况,水平居中、垂直居中和水平垂直居中来分析
148 0