当前位置: 首页 > news >正文

解决 居中 问题

 

<div class="parent">
  <div class="child">DEMO</div>
</div>


水平居中
(1)inline-block + text-align 优点是兼容性好。 inline-block 内容有多宽,就是多宽。 text-align 对inline元素有用。但是需要一些代码修复,text-align:center造成的问题。 <style type="text/css"> .parent {width: 200px; height: 30px; text-align: center;background-color: #ccc;} .child {display: inline-block;background-color: #369;} </style> (2)table + margin table 即像block元素,宽度又跟着内容走。 优点是指设定了child元素,不关系parent的样式。在ie8支持。 <style type="text/css"> .parent {width: 200px; height: 30px; background-color: #ccc; } .child {display: table; background-color: #369;margin: 0 auto;} </style> (3)absolute + transform transform:translateX(-50%) 移动自身的-50%的像素。 问题:主要是兼容性的问题,transform是css3. <style type="text/css"> .parent {position: relative; width: 200px; height: 20px; background-color: #ccc;} .child {position: absolute; left: 50%; transform: translateX(-50%); background-color: #369;} </style> (4) flex + justify-content 优点:只需要设定父元素。 或者对child 设定 margin: 0 auto; 缺点:低版本的不支持flex <style type="text/css"> .parent {display: flex; justify-content:center; width: 200px; height: 20px; background-color: #ccc;} </style> 垂直居中 (1)table-cell + vertical-align 变成单元格,加上vertical-align:middle; 优点:兼容性比较好。 <style type="text/css"> .parent {display: table-cell; vertical-align: middle; height: 100px;background-color: #ccc;} .child {background-color: #369;} </style> (2) absolute + transform <style type="text/css"> .parent {position: relative; height: 100px;background-color: #ddd;} .child {position: absolute; top: 50%; transform: translateY(-50%);} </style> (3)flex + align-items 优点只需要设定parent。 <style type="text/css"> .parent {display: flex; align-items:center; height: 100px;background-color: #ddd;} .child {background-color: #369;} </style> 水平和垂直都居中 (1) inline-block + text-align + table-cell + vertical-align <style type="text/css"> .parent { width: 100px; height: 100px; background-color: #444; text-align: center; display: table-cell; vertical-align: middle; } .child { background-color: #369; display: inline-block; } </style> (2)absolute + transform <style type="text/css"> .parent { width: 100px; height: 100px; background-color: #444; position: relative; } .child { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } </style> (3)flex + justify-content + align-items <style type="text/css"> .parent { width: 100px; height: 100px; background-color: #444; display: flex; justify-content: center; align-items:center; } .child { background-color: #369; } </style>

 

转载于:https://www.cnblogs.com/hgonlywj/p/4899705.html

相关文章:

  • 提交(post)xml文件给指定url的2种方法
  • Why NO to: MySQL, Symfony, CakePHP, Smarty, etc.
  • grub引导程序
  • 不使用(a+b)/2这种方式,求两个数的平均值
  • 可以动态添加图片的轮播插件
  • nginx rewrite模块探究与实验
  • SpringMVC(六):Spring 整合quartz作业调度框架
  • ecshop中那些有意思的代码
  • 查看死锁原因 /data/anr/traces.txt
  • 如何删除 SQL Server 表中的重复行
  • Android 不通过USB数据线调试的方法
  • Android网络开发之OkHttp--基本用法GET
  • 查看Android应用包名package和入口activity名称
  • ueditor 最新版本【1.4.3.1】单独文件/图片上传处理方法
  • 【原创】sizeof运算符总结
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • android图片蒙层
  • create-react-app做的留言板
  • PhantomJS 安装
  • 阿里云Kubernetes容器服务上体验Knative
  • 初识MongoDB分片
  • ------- 计算机网络基础
  • 讲清楚之javascript作用域
  • 聊聊springcloud的EurekaClientAutoConfiguration
  • 微服务框架lagom
  • 我有几个粽子,和一个故事
  • 协程
  • 从如何停掉 Promise 链说起
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​香农与信息论三大定律
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • ${factoryList }后面有空格不影响
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (+4)2.2UML建模图
  • (1)(1.13) SiK无线电高级配置(五)
  • (二)Eureka服务搭建,服务注册,服务发现
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (原創) 未来三学期想要修的课 (日記)
  • (转)visual stdio 书签功能介绍
  • ./configure,make,make install的作用
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .dwp和.webpart的区别
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET 指南:抽象化实现的基类
  • ?.的用法
  • @在php中起什么作用?
  • [20160902]rm -rf的惨案.txt
  • [20190416]完善shared latch测试脚本2.txt
  • [Android Studio 权威教程]断点调试和高级调试
  • [C/C++]数据结构 深入挖掘环形链表问题
  • [C++]:for循环for(int num : nums)
  • [Dxperience.8.*]报表预览控件PrintControl设置