最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

css水平居中和垂直居中_html/css

来源:动视网 责编:小采 时间:2020-11-27 15:55:15
文档

css水平居中和垂直居中_html/css

css水平居中和垂直居中_html/css_WEB-ITnose:水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display:inline-block;和display:flex;方法1:在父级上设置text-align:ce
推荐度:
导读css水平居中和垂直居中_html/css_WEB-ITnose:水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display:inline-block;和display:flex;方法1:在父级上设置text-align:ce


水平居中:
内联元素:text-align:center;相对于父级居中显示
块级元素:margin:0 auto;但是需要同时width,否则无法看到效果
多个块级元素居中:在此想要探讨一下display:inline-block;和display:flex;
方法1:在父级上设置text-align:center;在元素上设置:display:inline-block;同时为了使文字向左显示,设置了text-align:left;在浏览器中的显示为,子元素高度自适应,底部对齐。
方法2:在子元素上设置display:center;justify-content:center;子元素的高度一致,与元素中内容最多的高度相同。
二种方法在谷歌,火狐,360均兼容,但是第2种方法在IE10中不兼容,之前的IE版本可想也是不兼容的。当然在此也要处理由于浮动导致离层,父级背景颜色加不上去的问题。

 1 
2 3 I'm an element that is block-like with my siblings and we're centered in a row. 4 5 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 8 9 I'm an element that is block-like with my siblings and we're centered in a row.10 11

 1 
2 3 I'm an element that is block-like with my siblings and we're centered in a row. 4 5 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 8 9 I'm an element that is block-like with my siblings and we're centered in a row.10 11

 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-top和padding-bottom的值相等
方法2:设置height和line-height的值相等
方法3:固定高度可以有一种处理方式position:absolute;top:50%;height:100px;margin-top:-50px;如果高度不固定,也可以使用js动态获取高度来。
还有css3的一些属性,考虑到兼容性问题不在叙述。

文档

css水平居中和垂直居中_html/css

css水平居中和垂直居中_html/css_WEB-ITnose:水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display:inline-block;和display:flex;方法1:在父级上设置text-align:ce
推荐度:
标签: 居中 水平居中 css
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top