分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

css垂直居中

发布时间:2023-09-06 01:43责任编辑:彭小芳关键词:暂无标签

css水平居中很容易实现,如果是行内元素,直接使用text-align:center,如果是块元素,则可以marin-left:auto;margin-right:auto;来实现。

垂直居中是前端开发中极其常见的需求,理论上很简单,实践起来却有难度。常用的有以下几种方法:

html格式:

<div class="out-box"> ???????<div class="inner-box"> ???????????<p>inner text</p> ???????????<h5>inner title</h5> ???????</div> ???</div>

1.基于绝对定位的方案

.out-box{ ???????????width: 200px; ???????????height: 200px; ???????????background: yellow; ???????????margin: 50px auto; ???????????position: relative; ???????} ???????.inner-box{ ???????????width: 80px; ???????????height: 100px; ???????????background: blue; ???????????position: absolute; ???????????left: 50%; ???????????top: 50%; ???????????transform: translate(-50%,-50%);
       color:#fff;
       text-align:center;
}

效果如图:

2.基于flexbox的解决方案

.out-box{ ???????????width: 200px; ???????????height: 200px; ???????????background: yellow; ???????????margin: 50px auto; ???????????display: flex; ???????} ???????.inner-box{ ???????????background: blue; ???????????margin: auto; ???????????color: #fff; ???????????text-align: center; ???????}

效果如图所示:

css垂直居中

原文地址:https://www.cnblogs.com/cherryshuang/p/8457715.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved