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--元素居中各种处理方法的更多相关文章

  1. css使子元素在父元素居中的各种方法

    html结构: <div class="parent"> <div class="child"></div> </di ...

  2. 使用CSS完成元素居中的七种方法

    在网页布局中元素水平居中比元素垂直居中要简单不少,同时实现水平居中和垂直居中往往是最难的.现在是响应式设计的时代,我们很难确切的知道元素的准确高度和宽度,所以一些方案不大适用.据我所知, 在CSS中至 ...

  3. <转载>使CSS文字图片div元素居中方法之水平居中的几个方法

    文字居中,文字垂直居中水平居中,图片居中,图片水平居中垂直居中,块元素垂直居中?当我们在做前端开发是时候关于css居中的问题是很常见的.情 况有很多种,不同的情况又有不同的解决方式.水平居中的方式解决 ...

  4. cssy元素居中的方法有哪些?

    css的元素居中 各位小伙伴们在努力写网页的时候有没有遇到过这样的一个问题呢? 在写的时候发现他不居中,可是要分分钟逼死强迫症的啊! 别急,我来啦 哈哈哈 今天就带来三种css的元素居中的方法 第一种 ...

  5. 网页元素居中的n种方法

    导语:元素居中对齐在很多场景看上去很和谐很漂亮.除此之外,对于前端开发面试者的基础也是很好的一个考察点.下面跟着作者的思路,一起来看下吧. 场景分析 一个元素,它有可能有背景,那我要它的背景居中对齐 ...

  6. HTML 元素居中的方法

    网址:http://www.cnblogs.com/asqq/archive/2012/04/09/2438745.html 1. 元素的定位的方法选择 :absolute . 2. 给定元素的宽和高 ...

  7. 大前端学习笔记整理【一】CSS盒模型与基于盒模型的6种元素居中方案

    概览 CSS盒模型,规定了元素框来处理元素的 内容.内边距.边框和外边距的方式 元素部分是指内容部分,也是最实际的内容,包围内容的称之为内边距,内边距外围是边框,边框外围就是外边距:且外边距是透明的, ...

  8. css中元素居中总结

    很多时候,我们需要让元素居中显示:1. 一段文本的水平居中,2. 一张图片的水平居中,3. 一个块级元素的水平居中:4. 单行文本的竖直居中,5. 不确定高度的一段文本竖直居中,6. 确定高度的块级元 ...

  9. div(固定宽度和不固定宽度)居中显示的方法总结

    今天我总结一下css实现div居中的方法,有的是固定宽度的,还有的是不固定宽度的. 1.使用自动外边距实现DIV CSS居中 CSS中首选的让元素水平居中的方法就是使用margin属性—将元素的mar ...

  10. Css元素居中设置

    你对DIV CSS居中的方法是否了解,这里和大家分享一下,用CSS让元素居中显示并不是件很简单的事情,让我们先来看一下CSS中常见的几种让元素水平居中显示的方法. DIV CSS居中 用CSS让元素居 ...

随机推荐

  1. fgets实现

    char *fgets(char *s, int n, FILE *stream) { register int c; register char *cs; cs = s; while(--n > ...

  2. day 34 作业

    作业 mysql> create table tea( -> id int unsigned auto_increment primary key, -> name varchar( ...

  3. 【分布式搜索引擎】Elasticsearch之安装Elasticsearch可视化平台Kibana

    一.Kibana简单介绍 Kibana 是为 Elasticsearch设计的开源分析和可视化平台.你可以使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互.你 ...

  4. JAVAWEB复习笔记-day02

    1.CSS样式优先级 优先级:由上到下,由外到内.优先级越来越高 2.css选择器 html标签选择器 class选择器(.) id选择器(#) 3.优先级 style属性>id选择器>c ...

  5. pytest文档30-功能用例与自动化用例完美对接(allure)

    前言 做自动化做久了,经常会思考一个问题,到底别人是怎么做的自动化,跟自己的有啥不一样,看过不少书和资料,都是停留在demo的层面. 真正把自动化做的好的大牛又不屑于分享自己的劳动成果,所以大部分情况 ...

  6. 201871020225-牟星源《面向对象程序设计(java)》第十二周学习总结

    201871020225-牟星源<面向对象程序设计(java)>第十二周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  7. python中判断两个对象是否相等

    #coding=utf-8#比较两个对象是否相等#python 2中使用cmp(),==,is#is 主要是判断 2 个变量是否引用的是同一个对象,如果是的话,则返回 true,否则返回 false. ...

  8. Django项目中出现的错误及解决办法(ValueError: Dependency on app with no migrations: customuser)

    写项目的时候遇到了类似的问题,其实就是没有生成迁移文件,执行一下数据库迁移命令就好了 ValueError: Dependency on app with no migrations: customu ...

  9. pointnet++之scannet/train.py

    1.作者可能把scannet数据集分成了训练集和测试集并处理成了.pickle文件. 2.在代码运行过程中,作者从.pickle文件中读出训练集1201个场景的x.y.z坐标和测试集312个场景的x. ...

  10. b站滑动验证码图片的获取-python

    本文仅是获取验证码图片,python+selenium实现 图片的处理,算出偏移位置网上都有现成的:而由于b站的更新,图片的获取则与之前完全不同,不能直接从html中拿到 过程比较曲折所以记录一下,可 ...