1、水平居中

对于行内元素可以使用:

  1. .center-children {
  2. text-align: center;
  3. }

对于块元素,你可以设置其左右外边距为:auto;同时你还应该设置该元素的宽度,不然的话,元素的宽度会撑满整个浏览器或者一种是没反应(不过你设置背景就会看到了)。

  1. .center-me {
  2. margin: 0 auto;
  3. }

如果你想让多个块元素在一行当中显示,首先你得设置display的属性,inline-block;在使用上面说的方法。还有一种方式是设置display:flex;justify-content: center;

CSS代码:

  1. body {
  2. background: #f06d06;
  3. font-size: 80%;
  4. }
  5.  
  6. main {
  7. background: white;
  8. margin: 20px 0;
  9. padding: 10px;
  10. }
  11.  
  12. main div {
  13. background: black;
  14. color: white;
  15. padding: 15px;
  16. max-width: 125px;
  17. margin: 5px;
  18. }
  19.  
  20. .inline-block-center {
  21. text-align: center;
  22. }
  23. .inline-block-center div {
  24. display: inline-block;
  25. text-align: left;
  26. }
  27.  
  28. .flex-center {
  29. display: flex;
  30. justify-content: center;
  31. }

HTML代码:

  1. <main class="inline-block-center">
  2. <div>
  3. I'm an element that is block-like with my siblings and we're centered in a row.
  4. </div>
  5. <div>
  6. 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.
  7. </div>
  8. <div>
  9. I'm an element that is block-like with my siblings and we're centered in a row.
  10. </div>
  11. </main>
  12.  
  13. <main class="flex-center">
  14. <div>
  15. I'm an element that is block-like with my siblings and we're centered in a row.
  16. </div>
  17. <div>
  18. 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.
  19. </div>
  20. <div>
  21. I'm an element that is block-like with my siblings and we're centered in a row.
  22. </div>
  23. </main>

2、垂直居中

1)如果只是单行的情况:让行高等于元素的高度来欺骗别人达到居中的目的。

  1. <main>
  2. <div>
  3. I'm a centered line.
  4. </div>
  5. </main>
  1. body {
  2. background: #f06d06;
  3. font-size: 80%;
  4. }
  5.  
  6. main {
  7. background: white;
  8. margin: 20px 0;
  9. padding: 40px;
  10. }
  11.  
  12. main div {
  13. background: black;
  14. color: white;
  15. height: 100px;
  16. line-height: 100px;
  17. padding: 20px;
  18. width: 50%;
  19. white-space: nowrap;
  20. }

white-space: nowrap;表示段落中的文本不换行;超出宽度的将不会在显示。

2)如果要多行居中,一般设置上下的内边距来实现,不行的话还有两种方法:一种是将文本放置在表格单中。另一种则是模仿表格的形式。首先为其设置一个容器,再将装有文本的容器放在里面。设置边框,对齐方式,显示方式等就可以了。

  1. <table>
  2. <tr>
  3. <td>
  4. I'm vertically centered multiple lines of text in a real table cell.
  5. </td>
  6. </tr>
  7. </table>
  8.  
  9. <div class="center-table">
  10. <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
  11. </div>
  12.  
  13. body {
  14. background: #f06d06;
  15. font-size: 80%;
  16. }
  17.  
  18. table {
  19. background: white;
  20. width: 240px;
  21. border-collapse: separate;
  22. margin: 20px;
  23. height: 250px;
  24. }
  25.  
  26. table td {
  27. background: black;
  28. color: white;
  29. padding: 20px;
  30. border: 10px solid white;
  31. /* default is vertical-align: middle; */
  32. }
  33.  
  34. .center-table {
  35. display: table;
  36. height: 250px;
  37. background: white;
  38. width: 240px;
  39. margin: 20px;
  40. }
  41. .center-table p {
  42. display: table-cell;
  43. margin:;
  44. background: black;
  45. color: white;
  46. padding: 20px;
  47. border: 10px solid white;
  48. vertical-align: middle;
  49. }

border-collapse 属性设置表格的边框是否被合并为一个单一的边框,还是象在标准的 HTML 中那样分开显示;

display:table- cell属性指让标签元素以表格单元格的形式呈现,类似于td标签。目前IE8+以及其他现代浏览器都是支持此属性的,但是IE6/7只能对你说 sorry了。

如果上述方法都不行,恐怕就得使用flex了

  1. <div class="flex-center">
  2. <p>I'm vertically centered multiple lines of text in a flexbox container.</p>
  3. </div>
  4.  
  5. body {
  6. background: #f06d06;
  7. font-size: 80%;
  8. }
  9.  
  10. div {
  11. background: white;
  12. width: 240px;
  13. margin: 20px;
  14. }
  15.  
  16. .flex-center {
  17. background: black;
  18. color: white;
  19. border: 10px solid white;
  20. display: flex;
  21. flex-direction: column;
  22. justify-content: center;
  23. height: 200px;
  24. resize: vertical;
  25. overflow: auto;
  26. }
  27. .flex-center p {
  28. margin:;
  29. padding: 20px;
  30. }

如果这个也行不通的话,使用下面的ghost-center.

  1. <div class="ghost-center">
  2. <p>I'm vertically centered multiple lines of text in a container. Centered with a ghost pseudo element</p>
  3. </div>
  4.  
  5. body {
  6. background: #f06d06;
  7. font-size: 80%;
  8. }
  9.  
  10. div {
  11. background: white;
  12. width: 240px;
  13. height: 200px;
  14. margin: 20px;
  15. color: white;
  16. resize: vertical;
  17. overflow: auto;
  18. padding: 20px;
  19. }
  20.  
  21. .ghost-center {
  22. position: relative;
  23. }
  24. .ghost-center::before {
  25. content: " ";
  26. display: inline-block;
  27. height: 100%;
  28. width: 1%;
  29. vertical-align: middle;
  30. }
  31. .ghost-center p {
  32. display: inline-block;
  33. vertical-align: middle;
  34. width: 190px;
  35. margin:;
  36. padding: 20px;
  37. background: black;
  38. }

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. jQuery遍历 - 过滤first(),last()和eq()使用

    jQuery遍历 - 过滤最基本的过滤方法是first(),last()和eq(),它们允许您根据元素在一组元素中的位置选择特定元素. 其他过滤方法(如filter()和not())允许您选择与特定条 ...

  2. tomcat特殊字符处理问题解决方案

    tomcat特殊字符处理问题解决方案 直接加上如下代码,本质是通过反射加上过滤字符 @Configuration public class TomcatConfig { @Bean public Co ...

  3. 理解Java方法增强

    在实际开发中,我们往往需要对某些方法进行增强,常用的方法增强的方式有三种. 类继承 .方法覆盖 必须控制对象创建,才能使用该方式 装饰者模式方法加强 必须和目标对象实现相同接口或继续相同父类,特殊构造 ...

  4. 原生 JavaScript 代替 jQuery【转】

    目录 用原生JavaScript代替jQuery Query Selector CSS & Style DOM Manipulation Ajax Events Utilities Promi ...

  5. linux date 设置系统时间

    设置 系统时间 注意时间格式 date  -s "date" [root@localhost c]# date -s "2019-05-29 10:58:00" ...

  6. Redis数据库详解

    NoSQL 若杀死进程应使用pkill 数据设计模式:分布式.非关系型.不提供ACID 特性:简单数据模型.源数据和应用数据分离.弱一致性 优势: 避免不必要的复杂性 高吞吐量, 高 水平扩展能力和低 ...

  7. Portainer

    docker search portainer docker pull portainer/portainer docker run -it \ --name prtainer \ -p 9000:9 ...

  8. JVM 启动参数,共分为3类

    JVM 启动参数,共分为3类: 类别 说明 标准参数(-) 所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 非标准参数(-X) 这些参数不是虚拟机规范规定的.因此,不是所有VM的实现(如:H ...

  9. pip安装指定版本的程序的命令

    pip安装指定版本的程序的命令 pip install -i https://pypi.douban.com/simple/ django==1.10.3 或者 pip install django= ...

  10. 百度PaddlePaddle:

    百度正式发布PaddlePaddle深度强化学习框架PARL 近日,百度PaddlePaddle正式发布了深度强化学习框架 PARL,同时开源了基于该框架的.在 NeurIPS 2018 强化学习赛事 ...