子元素在父元素中水平垂直居中:
- margin去实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.items{
width: 300px;
height: 300px;
background-color: red;
overflow: hidden;
}
.title{
width: 100px;
height: 100px;
background-color: #008000;
margin: 100px;
}
</style>
</head>
<body>
<div class="items">
<div class="title"></div>
</div>
</body>
</html>
- padding去实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.items{
width: 300px;
height: 300px;
background-color: red;
padding: 100px;
box-sizing: border-box;
}
.title{
width: 100px;
height: 100px;
background-color: #008000;
}
</style>
</head>
<body>
<div class="items">
<div class="title"></div>
</div>
</body>
</html>
- 用定位去实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.items{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.title{
width: 100px;
height: 100px;
background-color: #008000;
position: absolute;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<div class="items">
<div class="title"></div>
</div>
</body>
</html>
- 另一种绝对定为
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.items{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.title{
width: 100px;
height: 100px;
background-color: #008000;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
</style>
</head>
<body>
<div class="items">
<div class="title"></div>
</div>
</body>
</html>
- 表格法(面试可以用)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.items{
width: 300px;
height: 300px;
background-color: red;
display:table-cell ;
vertical-align: middle;
}
.title{
width: 100px;
height: 100px;
background-color: #008000;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="items">
<div class="title"></div>
</div>
</body>
</html>