设body标签中只有一行字:“十分钟课堂”。源代码不可以修改。要让文本相对屏幕,水平,垂直,绝对居中。
<body>十分钟课堂</body>
那么介绍以下7种方法:
(以下顺序基于代码的实用性)
方法1:flex方法
body{
display: flex;
align-items:center;
justify-content: center;
min-height: 100vh;
}
方法2:grid方法
body{
display: grid;
place-items:center;
min-height: 100vh;
}
方法3:line-height方法
body{
text-align: center;
line-height: 100vh;
}
方法4:table方法
html{
display: table;
height: 100vh;
width: 100%;
body{
display: table-cell;
text-align: center;
vertical-align: middle;
}
}
方法5:position方法
html{
height: 100vh;
body{
position: absolute;
top:50%;
left:50%;
transform: translate(-50%,-50%);
}
}
方法6:margin方法
body{
line-height: 1em;
margin-top: calc(50vh - 0.5em);
text-align: center;
}
方法7:JavaScript方法
通过js包围文本,多一级标签,这样就可以为所欲为了。
addEventListener('load',function(){
const a = document.getElementsByTagName("body")[0].innerHTML;
document.getElementsByTagName("body")[0].innerHTML = '<span>'+a+'</span>';
})
那么,方法就更多了,用一个跟上面的有点不一样的样式:
body{
height: 100vh;
display: grid;
span{
margin: auto;
}
}
评论区
发表新的留言
您可以留言提出您的疑问或建议。
您的留言得到回复时,会通过您填写的邮箱提醒您。