博客
关于我
前端笔记(总结几种水平垂直居中的方式)
阅读量:462 次
发布时间:2019-03-06

本文共 1503 字,大约阅读时间需要 5 分钟。

水平居中是网页开发中常见的布局需求,本文将介绍三种常用的实现方法,供开发者参考。

1. 使用 transform 属性

这种方法属于 CSS 变换的一种,通过将元素沿 X、Y 轴进行位移来实现居中效果。具体实现方式如下:

.d1_1 {    width: 200px;    height: 200px;    background-color: red;}.d2_1 {    width: 100px;    height: 100px;    background: blue;    transform: translate(50%, 50%); /* 位移 */}

这种方法的优点是实现简单,且不会影响其他元素的布局位置。通常建议在使用时,先设置为相对定位或绝对定位,以确保不会干扰页面的正常布局。

2. 使用弹性布局 (Flexbox)

弹性布局是现代 CSS 中最为常用的居中方法之一。这种方法通过设置容器的 display: flex 属性,并结合 align-itemsjustify-content 来实现垂直和水平居中。以下是一个典型的示例:

.d3 {    width: 200px;    height: 200px;    background-color: red;    display: flex;    align-items: center; /* 垂直居中 */    justify-content: center; /* 水平居中 */}.d4 {    width: 100px;    height: 100px;    background: blue;}

这种方法的优势在于代码简洁,跨浏览器支持率高(IE10+)。不过,需要注意的是在某些旧浏览器版本中可能存在兼容性问题。

3. 使用 vertical-align 属性

这种方法通常用于行内元素的居中,适用于较少的场景。通过将元素设置为行内元素,并使用 vertical-align: middle 来实现居中效果。以下是一个示例:

.d5 {    width: 200px;    height: 200px;    background-color: red;    text-align: center;}.d5::after {    content: '';    display: inline-block;    height: 100%;    vertical-align: middle;}.d6 {    width: 100px;    height: 100px;    background-color: blue;    display: inline-block;    vertical-align: middle;}

这种方法的实现相对复杂,通常用于特定场景,且需要对元素的布局有较好的理解。

注意事项

  • 在使用 transform 属性时,建议先设置为 position: relativeposition: absolute,以避免影响其他布局。
  • 对于弹性布局,建议在容器中设置 flex: 1display: flex,并根据具体需求设置 align-itemsjustify-content
  • 在使用 vertical-align 时,建议将父容器设置为行内布局(white-space: nowrap),并确保元素的 display 属性为 inline-block
  • 通过以上三种方法,可以根据具体需求选择最合适的实现方式。

    转载地址:http://rmbbz.baihongyu.com/

    你可能感兴趣的文章
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>