在固定元素CSS中居中显示文本



我有这个代码:

body {
margin: 0;
}
.menu {
background: black;
height: 100vh;
width: 240px;
position: fixed;
top: 0;
color: white;
z-index: 2;
}

main {
padding-left: 240px;
text-align: center;
}

main .footer {
position: fixed;
background-color: gray;
bottom: 0;
width: 100%;
color: white;
text-align: center; /* doesn't work? */
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="menu">Menu</div>
<main>
<h1>Example</h1>
<p>TEXT.</p>
<p>TEXT.</p>
<div class="footer">
<p>Fixed footer with cented text.</p>
</div>
</main>
</body>
</html>

我不知道怎么可能,但我无法将文本居中放在页脚中:/添加文本align:centre并不重要;到.footer.footer p。有什么想法吗?谢谢

解决方案:

body {
margin: 0;
}
.menu {
background: black;
height: 100vh;
width: 240px;
position: fixed;
top: 0;
color: white;
z-index: 2;
}

main {
padding-left: 240px;
text-align: center;
}

main .footer {
position: fixed;
background-color: gray;
bottom: 0;
/* del width: 100%; */
color: white;
text-align: center;
/*  add this */
left:240px;
right:0;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="menu">Menu</div>
<main>
<h1>Example</h1>
<p>TEXT.</p>
<p>TEXT.</p>
<div class="footer">
<p>Fixed footer with cented text.</p>
</div>
</main>
</body>
</html>

感谢@Temani Afif

最新更新