We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Learn more about funding links in repositories.
Report abuse
There was an error while loading. Please reload this page.
第93天 写出div在不固定高度的情况下水平垂直居中的方法?
我知道的有两种方法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <style> * { padding: 0; margin: 0; } /* flex居中 */ .tith1 { display: flex; justify-content: center; align-items: center; background: red; } /* table居中 */ .tith2 { text-align: center; width: 100%; display: table; background: blue; } .tith2 > span { display: table-cell; vertical-align: middle; } </style> <body> <p class="tith1"> <span>123</span> </p> <p class="tith2"> <span>123</span> </p> </body> </html>
父盒子相对定位 子盒子绝对定位: position:absolute; left:50%; top:50%; transform:translate(-50%,-50%);
父盒子相对定位,子盒子绝对定位和margin position:absolute; left:0; top:0; right:0; bottom:0; margin:auto;
自从用了弹性布局,我的居中对齐只有弹性布局。 <style>.box{css: display:flex;flex-flow: row nowarp;justify-content: center; align-items: center;}</style> <div class="box"><div></div></div>
<style>.box{css: display:flex;flex-flow: row nowarp;justify-content: center; align-items: center;}</style>
<div class="box"><div></div></div>
不知道以后flex布局普及后,老的布局方式还有多大的用武之地
大家都很棒 我这里有一种新的方法
.child{ width: 100px; height: 100px; background-color: #f60; position: relative; left: 50%; top: 50%; transform: translate(-50%,-50%); }
<div class="box"> <div>一小块</div> </div>
<style> *{ margin: 0; padding: 0; } .box{ width: 500px; height: 500px; background-color: #11b0ff; } /* flex */ .box{ display: flex; justify-content: center; align-items: center; } /* 行内块属性 */ /* .box{ text-align: center; } .box::after{ content: ''; display: inline-block; height: 100%; vertical-align: middle; } .box div{ vertical-align: middle; display: inline-block; } */ /* 表格 */ /* .box{ display: table; text-align: center; } .box div{ display: table-cell; vertical-align: middle; } */ /* 定位 */ /* 可以达到效果,但是无法撑满.box */ /* .box{ position: relative; } .box div{ position:absolute; left:50%; top:50%; transform:translate(-50%,-50%); } */ </style>
<template> <div class="father"> <div class="son"> 11111111111 <br> 11111111111 <br> </div> </div> </template> <script> export default {}; </script> <style scoped> /* 第一种 flex */ .father { height: 100vh; display: flex; justify-content: center; align-items: center; } .son { width: 100px; background-color: #ccc; } /* 第二种 transform+absolute */ .father { height: 100vh; position: relative; } .son { width: 100px; background-color: #ccc; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style>
flex
.father{ display: flex; justify-content: center; align-items: center; }
2: 利用transform属性进行位移:
transform
.father{ position: relative; } .children{ position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
父盒子相对定位 子盒子绝对定位: position:absolute; left:50%; top:50%; transform:translate(-50%,-50%); 父盒子相对定位,子盒子绝对定位和margin position:absolute; left:0; top:0; right:0; bottom:0; margin:auto;
第二种子元素需要有固定高宽
1,flex 2,absolute 3,table
relative
div
absolute
left/right/top/bottom
margin: auto
table-cell
CodePen 地址:https://codepen.io/Konata9/pen/OJLbMyv?editors=1100
万能定位:position各个坐标全部为0,margin: 0 auto
利用flex布局: .father{ display: flex; justify-content: center; align-items: center; } 2: 利用transform属性进行位移: .father{ position: relative; } .children{ position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
Activity
huangsw0411 commentedon Jul 18, 2019
我知道的有两种方法
KongXiaoYan03 commentedon Jul 18, 2019
父盒子相对定位
子盒子绝对定位:
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
父盒子相对定位,子盒子绝对定位和margin
position:absolute;
left:0;
top:0;
right:0;
bottom:0;
margin:auto;
NicholasBaiYa commentedon Jul 18, 2019
自从用了弹性布局,我的居中对齐只有弹性布局。
<style>.box{css: display:flex;flex-flow: row nowarp;justify-content: center; align-items: center;}</style>
<div class="box"><div></div></div>
shejiJiang commentedon Jul 18, 2019
不知道以后flex布局普及后,老的布局方式还有多大的用武之地
HuoXiaoYe commentedon Jul 18, 2019
大家都很棒 我这里有一种新的方法
tonyChenHey commentedon Jul 18, 2019
nowherebutup commentedon Jul 18, 2019
xxf1996 commentedon Jul 18, 2019
flex
布局:2: 利用
transform
属性进行位移:Vi-jay commentedon Aug 1, 2019
第二种子元素需要有固定高宽
jiamianmao commentedon Aug 13, 2019
1,flex
2,absolute
3,table
Konata9 commentedon Aug 21, 2019
relative
,div
使用absolute
配合transform
实现。relative
,div
使用absolute
设置left/right/top/bottom
为 0 配合margin: auto
实现。flex
实现。table-cell
实现。CodePen 地址:https://codepen.io/Konata9/pen/OJLbMyv?editors=1100
seho-dev commentedon Nov 14, 2019
万能定位:position各个坐标全部为0,margin: 0 auto
smile-2008 commentedon Jun 30, 2021