Skip to content

Instantly share code, notes, and snippets.

@shaomingquan
Created May 23, 2019 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaomingquan/e0fe8a0ab0787c3dca238836b79fda59 to your computer and use it in GitHub Desktop.
Save shaomingquan/e0fe8a0ab0787c3dca238836b79fda59 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>vertical center</title>
<style>
/* parent */
.p {
background-color: antiquewhite;
position: relative;
height: 200px;
margin-bottom: 10px;
}
/* child */
.c {
background-color: aquamarine;
}
.m1 > div {
position: absolute;
width: 800px;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
/* size must fixed */
height: 90px;
}
.m2 > div {
width: 800px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
/* css3 ability required, although almost all mobile browser supported */
}
/* it works, but more complex dom structure */
.m3 {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.m3 > div {
/* inline required */
display: inline-block;
width: 800px;
}
.m3-outer {
width: 100%;
display: table;
margin-bottom: 10px;
}
/* flex is a perfect method */
.m4 {
display: flex;
align-items: center;
justify-content: center;
}
.m4 > div {
width: 800px;
/* or just "margin auto" */
/* margin: auto; */
}
.m5 > div {
width: 800px;
height: 90px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -400px;
margin-top: -45px;
/* worse method, fixed size required */
}
.m6 {
text-align: center;
}
.m6::before {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.m6 > div {
display: inline-block;
width: 800px;
vertical-align: middle;
}
.m7 {
/* inline text */
line-height: 200px;
text-align: center;
}
</style>
</head>
<body>
<div class="p m1">
<div class="c">This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.</div>
</div>
<div class="p m2">
<div class="c">This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.</div>
</div>
<div class="m3-outer">
<div class="p m3">
<div class="c">This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.</div>
</div>
</div>
<div class="p m4">
<div class="c">This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.</div>
</div>
<div class="p m5">
<div class="c">This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.</div>
</div>
<div class="p m6">
<div class="c">This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.</div>
</div>
<div class="p m7">This website stores cookies on your computer</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment