HTML--元素居中各种处理方法
1、水平居中
对于行内元素可以使用:
- .center-children {
- text-align: center;
- }
对于块元素,你可以设置其左右外边距为:auto;同时你还应该设置该元素的宽度,不然的话,元素的宽度会撑满整个浏览器或者一种是没反应(不过你设置背景就会看到了)。
- .center-me {
- margin: 0 auto;
- }
如果你想让多个块元素在一行当中显示,首先你得设置display的属性,inline-block;在使用上面说的方法。还有一种方式是设置display:flex;justify-content: center;
CSS代码:
- body {
- background: #f06d06;
- font-size: 80%;
- }
- main {
- background: white;
- margin: 20px 0;
- padding: 10px;
- }
- main div {
- background: black;
- color: white;
- padding: 15px;
- max-width: 125px;
- margin: 5px;
- }
- .inline-block-center {
- text-align: center;
- }
- .inline-block-center div {
- display: inline-block;
- text-align: left;
- }
- .flex-center {
- display: flex;
- justify-content: center;
- }
HTML代码:
- <main class="inline-block-center">
- <div>
- I'm an element that is block-like with my siblings and we're centered in a row.
- </div>
- <div>
- I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
- </div>
- <div>
- I'm an element that is block-like with my siblings and we're centered in a row.
- </div>
- </main>
- <main class="flex-center">
- <div>
- I'm an element that is block-like with my siblings and we're centered in a row.
- </div>
- <div>
- I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
- </div>
- <div>
- I'm an element that is block-like with my siblings and we're centered in a row.
- </div>
- </main>
2、垂直居中
1)如果只是单行的情况:让行高等于元素的高度来欺骗别人达到居中的目的。
- <main>
- <div>
- I'm a centered line.
- </div>
- </main>
- body {
- background: #f06d06;
- font-size: 80%;
- }
- main {
- background: white;
- margin: 20px 0;
- padding: 40px;
- }
- main div {
- background: black;
- color: white;
- height: 100px;
- line-height: 100px;
- padding: 20px;
- width: 50%;
- white-space: nowrap;
- }
white-space: nowrap;表示段落中的文本不换行;超出宽度的将不会在显示。
2)如果要多行居中,一般设置上下的内边距来实现,不行的话还有两种方法:一种是将文本放置在表格单中。另一种则是模仿表格的形式。首先为其设置一个容器,再将装有文本的容器放在里面。设置边框,对齐方式,显示方式等就可以了。
- <table>
- <tr>
- <td>
- I'm vertically centered multiple lines of text in a real table cell.
- </td>
- </tr>
- </table>
- <div class="center-table">
- <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
- </div>
- body {
- background: #f06d06;
- font-size: 80%;
- }
- table {
- background: white;
- width: 240px;
- border-collapse: separate;
- margin: 20px;
- height: 250px;
- }
- table td {
- background: black;
- color: white;
- padding: 20px;
- border: 10px solid white;
- /* default is vertical-align: middle; */
- }
- .center-table {
- display: table;
- height: 250px;
- background: white;
- width: 240px;
- margin: 20px;
- }
- .center-table p {
- display: table-cell;
- margin:;
- background: black;
- color: white;
- padding: 20px;
- border: 10px solid white;
- vertical-align: middle;
- }
border-collapse 属性设置表格的边框是否被合并为一个单一的边框,还是象在标准的 HTML 中那样分开显示;
display:table- cell属性指让标签元素以表格单元格的形式呈现,类似于td标签。目前IE8+以及其他现代浏览器都是支持此属性的,但是IE6/7只能对你说 sorry了。
如果上述方法都不行,恐怕就得使用flex了
- <div class="flex-center">
- <p>I'm vertically centered multiple lines of text in a flexbox container.</p>
- </div>
- body {
- background: #f06d06;
- font-size: 80%;
- }
- div {
- background: white;
- width: 240px;
- margin: 20px;
- }
- .flex-center {
- background: black;
- color: white;
- border: 10px solid white;
- display: flex;
- flex-direction: column;
- justify-content: center;
- height: 200px;
- resize: vertical;
- overflow: auto;
- }
- .flex-center p {
- margin:;
- padding: 20px;
- }
如果这个也行不通的话,使用下面的ghost-center.
- <div class="ghost-center">
- <p>I'm vertically centered multiple lines of text in a container. Centered with a ghost pseudo element</p>
- </div>
- body {
- background: #f06d06;
- font-size: 80%;
- }
- div {
- background: white;
- width: 240px;
- height: 200px;
- margin: 20px;
- color: white;
- resize: vertical;
- overflow: auto;
- padding: 20px;
- }
- .ghost-center {
- position: relative;
- }
- .ghost-center::before {
- content: " ";
- display: inline-block;
- height: 100%;
- width: 1%;
- vertical-align: middle;
- }
- .ghost-center p {
- display: inline-block;
- vertical-align: middle;
- width: 190px;
- margin:;
- padding: 20px;
- background: black;
- }
HTML--元素居中各种处理方法的更多相关文章
- css使子元素在父元素居中的各种方法
html结构: <div class="parent"> <div class="child"></div> </di ...
- 使用CSS完成元素居中的七种方法
在网页布局中元素水平居中比元素垂直居中要简单不少,同时实现水平居中和垂直居中往往是最难的.现在是响应式设计的时代,我们很难确切的知道元素的准确高度和宽度,所以一些方案不大适用.据我所知, 在CSS中至 ...
- <转载>使CSS文字图片div元素居中方法之水平居中的几个方法
文字居中,文字垂直居中水平居中,图片居中,图片水平居中垂直居中,块元素垂直居中?当我们在做前端开发是时候关于css居中的问题是很常见的.情 况有很多种,不同的情况又有不同的解决方式.水平居中的方式解决 ...
- cssy元素居中的方法有哪些?
css的元素居中 各位小伙伴们在努力写网页的时候有没有遇到过这样的一个问题呢? 在写的时候发现他不居中,可是要分分钟逼死强迫症的啊! 别急,我来啦 哈哈哈 今天就带来三种css的元素居中的方法 第一种 ...
- 网页元素居中的n种方法
导语:元素居中对齐在很多场景看上去很和谐很漂亮.除此之外,对于前端开发面试者的基础也是很好的一个考察点.下面跟着作者的思路,一起来看下吧. 场景分析 一个元素,它有可能有背景,那我要它的背景居中对齐 ...
- HTML 元素居中的方法
网址:http://www.cnblogs.com/asqq/archive/2012/04/09/2438745.html 1. 元素的定位的方法选择 :absolute . 2. 给定元素的宽和高 ...
- 大前端学习笔记整理【一】CSS盒模型与基于盒模型的6种元素居中方案
概览 CSS盒模型,规定了元素框来处理元素的 内容.内边距.边框和外边距的方式 元素部分是指内容部分,也是最实际的内容,包围内容的称之为内边距,内边距外围是边框,边框外围就是外边距:且外边距是透明的, ...
- css中元素居中总结
很多时候,我们需要让元素居中显示:1. 一段文本的水平居中,2. 一张图片的水平居中,3. 一个块级元素的水平居中:4. 单行文本的竖直居中,5. 不确定高度的一段文本竖直居中,6. 确定高度的块级元 ...
- div(固定宽度和不固定宽度)居中显示的方法总结
今天我总结一下css实现div居中的方法,有的是固定宽度的,还有的是不固定宽度的. 1.使用自动外边距实现DIV CSS居中 CSS中首选的让元素水平居中的方法就是使用margin属性—将元素的mar ...
- Css元素居中设置
你对DIV CSS居中的方法是否了解,这里和大家分享一下,用CSS让元素居中显示并不是件很简单的事情,让我们先来看一下CSS中常见的几种让元素水平居中显示的方法. DIV CSS居中 用CSS让元素居 ...
随机推荐
- jQuery遍历 - 过滤first(),last()和eq()使用
jQuery遍历 - 过滤最基本的过滤方法是first(),last()和eq(),它们允许您根据元素在一组元素中的位置选择特定元素. 其他过滤方法(如filter()和not())允许您选择与特定条 ...
- tomcat特殊字符处理问题解决方案
tomcat特殊字符处理问题解决方案 直接加上如下代码,本质是通过反射加上过滤字符 @Configuration public class TomcatConfig { @Bean public Co ...
- 理解Java方法增强
在实际开发中,我们往往需要对某些方法进行增强,常用的方法增强的方式有三种. 类继承 .方法覆盖 必须控制对象创建,才能使用该方式 装饰者模式方法加强 必须和目标对象实现相同接口或继续相同父类,特殊构造 ...
- 原生 JavaScript 代替 jQuery【转】
目录 用原生JavaScript代替jQuery Query Selector CSS & Style DOM Manipulation Ajax Events Utilities Promi ...
- linux date 设置系统时间
设置 系统时间 注意时间格式 date -s "date" [root@localhost c]# date -s "2019-05-29 10:58:00" ...
- Redis数据库详解
NoSQL 若杀死进程应使用pkill 数据设计模式:分布式.非关系型.不提供ACID 特性:简单数据模型.源数据和应用数据分离.弱一致性 优势: 避免不必要的复杂性 高吞吐量, 高 水平扩展能力和低 ...
- Portainer
docker search portainer docker pull portainer/portainer docker run -it \ --name prtainer \ -p 9000:9 ...
- JVM 启动参数,共分为3类
JVM 启动参数,共分为3类: 类别 说明 标准参数(-) 所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 非标准参数(-X) 这些参数不是虚拟机规范规定的.因此,不是所有VM的实现(如:H ...
- pip安装指定版本的程序的命令
pip安装指定版本的程序的命令 pip install -i https://pypi.douban.com/simple/ django==1.10.3 或者 pip install django= ...
- 百度PaddlePaddle:
百度正式发布PaddlePaddle深度强化学习框架PARL 近日,百度PaddlePaddle正式发布了深度强化学习框架 PARL,同时开源了基于该框架的.在 NeurIPS 2018 强化学习赛事 ...