php - 将元素固定在中心但启用滚动

标签 php html css

我正在为我叔叔的公司写一个网站。我今天才开始。

**Update: I have fixed the website as I got an answer.
But to all who want to know, what I was dealing with, 
I put a code snippet below. Win-Win for all.**

我被困在一件事上。 首先看看这里:MyWebsiteIsHere

看看我的代码,它非常原始:

index.php

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <link rel="stylesheet" href="css/stylesheet.css">
</head>
<body>
 <?php include 'php/start.php'; ?>
 <?php include 'php/start2.php'; ?>
 <?php include 'php/start3.php'; ?>
</body>
</html>

php/start.php

<?php
  echo '<head><title>Super Express Logistics</title></head>';
  echo "<header style=\"background-color:pink\">
  <p class=\"centered\">Hello</p></header>";
?>

php/start2.php

<?php
  echo "<header style=\"background-color:brown\"><p class=\"centered\">Ola!
  </p></header>";
?>

php/start3.php

<?php
  echo "<header style=\"background-color:lightblue\">
  <p class=\"centered\">こんにちわ !</p></header>";
?>

css/stylesheet.css

header{
width: 100%;
height: 100vh;
background-color: #000000;
color: white;
}

.centered {
 position: fixed;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
 }

问题 正如您在我的网站(有点)中看到的那样,当您滚动时,中间会重叠一些奇怪的文本。所有三个文本都包含在 <p> SomeTextHere </p>

我把 position:fixedcss只是为了将文本放在完美的中心,但正因为如此,所有这些文本都会跳到中心

我可以喜欢,将每个文本的位置固定在其父级的中心,并且当我滚动时,它会与父级一起滚动吗?

header{
width: 100vw;
height: 100vh;
color:white;
}

.centered{
position:fixed;
top:50%;
left:50%;
transform: translate(-50%, -50%);
}
<html>
  <body>
    <header style="background-color:pink"><p class="centered"> Hi !</p></header>    
    <header style="background-color:brown;"><p class="centered"> Ola !</p></header>
    <header style="background-color:teal"><p class="centered"> こんにちわ !</p></header>
  </body>
</html>

最佳答案

您可以通过以下方式更新 CSS:

header{
position: relative;
width: 100vw;
height: 100vh;
color:white;
}

.centered{
position: absolute;
top:50%;
left:50%;
transform: translate(-50%, -50%);
}

关于php - 将元素固定在中心但启用滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47483616/

相关文章:

php - 从 PHP 读取 mercurial 存储库

php - 我无法回显包含值的 JSON 编码对象

php - 在本地主机上使用 crontab 的 cronjob 没有结果

php - 如何在 PHP 中链接变量和字符串

html - Fabric UI 复选框标签对齐

php - 我可以同时使用 Ajax、JS、HTML、PHP 和 MySQL 吗?

javascript - Jquery Resizable 将 DIV 高度降低到小于默认值(即将 minHeight 降低到大约 1px)

css - 我们可以在 css3 动画中移动背景图像吗?

javascript - 在一行中使用多个 CSS 单元

html - anchor 跨越所有页面,而不仅仅是文本