如何实现CSS居中?–CSS居中常用方法

来源:http://www.ido321.com/824.html

一、水平居中

1、内联元素居中:相对父级块级元素居中对齐

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

2、块级元素居中:设置margin-left和margin-right为auto让它居中(同时还要设置width,否则它就会承满整个容器,无法看出居中效果)

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

如果有很多块级元素需要水平居中成一行,最好使用一个不同的display类型。这是一个使用inline-block和flex的例子。

演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/embedded/result/

   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: <main class="flex-center">
  13:   <div>
  14:     I‘m an element that is block-like with my siblings and we‘re centered in a row.
  15:   </div>
  16:   <div>
  17:     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.
  18:   </div>
  19:   <div>
  20:     I‘m an element that is block-like with my siblings and we‘re centered in a row.
  21:   </div>
  22: </main>

css:

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

二、垂直居中

1、内联元素:设置相等的上下padding,或者利用line-height

   1: .link {
   2:   padding-top: 30px;
   3:   padding-bottom: 30px;
   4: }

文本不会换行的情况下,可以使用line-height,让其与height相等去对齐文本。

   1: .center-text-trick {
   2:   height: 100px;
   3:   line-height: 100px;
   4:   white-space: nowrap;
   5: }

多行的文本也可以利用上下等padding的方式也可以让多行居中,但是如果这方法没用,你可以让这些文字的容器按table cell模式显示,然后设置文字的vertical-align属性对齐,

演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/1/embedded/result/

html:

   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: <div class="center-table">
   9:   <p>I‘m vertically centered multiple lines of text in a CSS-created table layout.</p>
  10: </div>

css

   1: body {
   2:   background: #f06d06;
   3:   font-size: 80%;
   4: }
   5: table {
   6:   background: white;
   7:   width: 240px;
   8:   border-collapse: separate;
   9:   margin: 20px;
  10:   height: 250px;
  11: }
  12: table td {
  13:   background: black;
  14:   color: white;
  15:   padding: 20px;
  16:   border: 10px solid white;
  17:   /* default is vertical-align: middle; */
  18: }
  19: .center-table {
  20:   display: table;
  21:   height: 250px;
  22:   background: white;
  23:   width: 240px;
  24:   margin: 20px;
  25: }
  26: .center-table p {
  27:   display: table-cell;
  28:   margin: 0;
  29:   background: black;
  30:   color: white;
  31:   padding: 20px;
  32:   border: 10px solid white;
  33:   vertical-align: middle;
  34: }

2、块级元素

若元素有固定高度,可以这样垂直居中

   1: .parent {
   2:   position: relative;
   3: }
   4: .child {
   5:   position: absolute;
   6:   top: 50%;
   7:   height: 100px;
   8:   margin-top: -50px; /* 如果没有使用: border-box; 的盒子模型则需要设置这个 */
   9: }

如果不知道元素高度,则这样

   1: .parent {
   2:   position: relative;
   3: }
   4: .child {
   5:   position: absolute;
   6:   top: 50%;
   7:   transform: translateY(-50%);
   8: }

也可以使用flexbox

   1: <main>  
   2:   <div>
   3:      I‘m a block-level element with an unknown height, centered vertically within my parent.
   4:   </div>  
   5: </main>
   1: body {
   2:   background: #f06d06;
   3:   font-size: 80%;
   4: }
   5: main {
   6:   background: white;
   7:   height: 300px;
   8:   width: 200px;
   9:   padding: 20px;
  10:   margin: 20px;
  11:   display: flex;
  12:   flex-direction: column;
  13:   justify-content: center;
  14:   resize: vertical;
  15:   overflow: auto;
  16: }
  17: main div {
  18:   background: black;
  19:   color: white;
  20:   padding: 20px;
  21:   resize: vertical;
  22:   overflow: auto;
  23: }

三、同时水平和垂直居中

1、元素有固定高度和宽度:先绝对居中,然后上移和左移50%的宽度即可

   1: //这种方案有极好的跨浏览器支持。
   2: .parent {
   3:   position: relative;
   4: }
   5: .child {
   6:   width: 300px;
   7:   height: 100px;
   8:   padding: 20px;
   9:   position: absolute;
  10:   top: 50%;
  11:   left: 50%;
  12:   margin: -70px 0 0 -170px;
  13: }

2、元素的高度和宽度未知或可变的:使用transofrm属性在两个方向都平移负50%

   1: .parent {
   2:   position: relative;
   3: }
   4: .child {
   5:   position: absolute;
   6:   top: 50%;
   7:   left: 50%;
   8:   transform: translate(-50%, -50%);
   9: }
时间: 2024-08-02 22:57:34

如何实现CSS居中?–CSS居中常用方法的相关文章

HTML和CSS中的居中效果(1)

HTML和CSS中的居中效果 单行上下左右居中 Html: <div class="container"></div> CSS: .container{ width: 600px; height: 300px; text-align: center; line-height: 300px; border: 1px solid green; } 固定高宽上下左右居中 Html: <div class="container"><

css实现永远居中的背景图片

<title>css实现永远居中的背景图片丨</title> <STYLE TYPE="text/css"> <!-- BODY {background-image: URL(/images/m02.jpg); background-position: center; background-repeat: no-repeat; background-attachment: fixed;} --> </STYLE> <br

css中各种居中的奇技淫巧总结

css中各种居中的奇技淫巧总结   第一种,在固定布局中比较常用的技巧设置container的margin:0 auto:   第二种(从布局中入手)   css .outer{ height:200px; width:200px; background:gray; position:relative; } .inner{ width:100px; height:100px; top:50%; left:50%; background:black; position:absolute; marg

【转】如何只用CSS做到完全居中

英文原版链接:http://codepen.io/shshaw/full/gEiDt 我们都知道 margin:0 auto; 的样式能让元素水平居中,而 margin: auto; 却不能做到垂直居中……直到现在.但是,请注意!想让元素绝对居中,只需要声明元素高度,并且附加以下样式,就可以做到: 1 2 3 4 5 .Absolute-Center {   margin: auto;   position: absolute;   top: 0; left: 0; bottom: 0; rig

元素居中css

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>垂直</title> <style type="text/css"> #content { position:absolute; top:0; bottom:0; left:0; right:0; margin:auto; height:240px; width:7

CSS 背景-CSS background

这里有个很好的样式学习网站:http://www.divcss5.com/rumen/r125.shtml 一.Css background背景语法   -   TOP CSS背景基础知识 CSS 背景这里指通过CSS对对象设置背景属性,如通过CSS设置背景各种样式. 背景语法: background: background-color || background-image || background-repeat || background-attachment || background-

#CSS# 初识CSS

CSS:Cascading Style Sheet (层叠样式表) CSS的内容可以分开写入CSS文件中,PHP文件中加入以下语句即可: <link herf="css/style.css" type="text/css" rel="stylesheet"/> 其中,style.css写入定义的样式,如: ul.menu{                       list-style-type:none;    backgroun

CSS:CSS+DIV布局网页

现代网页布局:CSS+DIV: 一般的网页都是顺序布局的,很难达到我们需要的网页布局格式,此时使用DIV进行分层布局,类似于盒子,对每一部分内容进行设计.这是现在主流的网页布局方式,使用DIV+CSS. CSS盒模型: 网页设计中常听的属性名:内容(content).填充(padding).边框(border).边界(margin), CSS盒子模式都具备这些属性. CSS中, Box Model叫盒子模型(或框模型),Box Model规定了元素框处理元素内容(element content)

Firefox与IE的CSS兼容CSS HACK技巧

1. Div居中问题 div设置 margin-left, margin-right 为 auto 时已经居中,IE 不行,IE需要设定body居中,首先在父级元素定义text-algin: center;这个的意思就是在父级元素内的内容居中. 2.CSS 链接(a标签)的边框与背景 a链接加边框和背景色,需设置 display: block, 同时设置 float: left 保证不换行.参照 menubar, 给 a 和 menubar 设置高度是为了避免底边显示错位, 若不设 height

DIV CSS优化 CSS压缩技巧教程

DIV CSS优化 DIVCSS5为大家介绍简单的CSS优化.涉及优化内容:CSS代码优化.CSS重用优化.缩短字符.删除换行等处优化 目录 CSS样式单词简写优化 标点符号优化 删除换行 CSS重用优化 CSS代码优化简写总结 一.CSS样式属性单词代码简写优化 - TOP 1.border(CSS边框)简写: 1).4个边边框宽度为1px,颜色为#000 border-color:#000; border-style:solid; border-width:1px 可以简写为:体感音乐放松椅