-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用CSS让一个元素水平垂直居中 #42
Comments
设置元素样式为: |
行内元素居中 text-align: center + line-height; |
1、块级元素,设置宽高,需要谁居中,给其设置 margin: 0 auto; |
display:flex; |
|
|
1.单行文本,只需要将文本行高(line-height)和所在区域高度(height)设为一致即可
2.父元素高度不确定,只能靠内部文本内容撑开。可以设置margin,padding值相等
3.父级高度固定的时,设置父级div的display属性:display: table;;然后再添加一个div包含文本内容,设置其display:table-cell;和vertical-align:middle 4、flex的垂直居中,这种做法可以不定义内部元素的高度 5.使用absolute+transform |
|
1、 使用position/transform/top/left( 已知宽高) .parent{
display: grid;
}
.left {
justify-self: center;
align-self: center;
/* 以上可以简写为 place-self: center;*/
height: 100px;
width: 100px;
/* float: left; */
background-color: blueviolet;
} |
父元素 子元素 利用
|
解决方案有如下几种 绝对定位: position:abaolute;
left:50%:
top:50%:
margin-left: -width/2;
margin-top: -width/2; 间距居中 position-absolute: 上下左右都为0;
margin:auto; flex display:flex
jusicitify-content:center;
align-items:center; display:table-cell display:table-cell;
vertical-align:middle
text-align:center; 单行文本居中:
|
No description provided.
The text was updated successfully, but these errors were encountered: