使用 require_once(); 从另一个页面加载 php。 php 所做的是在提交表单后将一些文本回显到页面上。 (不在下方)CSS 位于同一页面上。 margin、width background-color 和 padding 声明有效,但 top 和 text align 无效。这是为什么?
<?php
$connection = @mysqli_connect("localhost","root","","R");
if($connection->connect_error){//show error if not connection failed
die("Connection failed: " . $connection->connect_error);
}
$sql="SELECT username FROM userinfo WHERE username LIKE '%" . $_POST['search'] . "%';";
$res=$connection->query($sql);
while($row=$res->fetch_assoc()){
echo "<div class='results'><center> Username: " . $row['username'] . "</center></div>";
}
mysqli_close($connection);
?>
CSS
.results {
position: relative;
top: 55%;
margin-top: 0px;
margin-bottom: 0px;
background-color: #36393E;
width: 20%;
text-align: center;
padding-bottom: 30px;
padding-top: 10px;
}
如果我发错地方或标题格式不正确,请原谅。我可以删除或重述问题。如果需要,我还可以添加所有代码。非常感谢
编辑
整个html代码
<!DOCTYPE HTML>
<head>
<?php
require_once('phpsearchcode.php');
?>
<style>
.results {
position: relative;
top: 55% !important;
margin-bottom: 0px;
background-color: #36393E;
width: 20%;
text-align: center !important;
padding-bottom: 30px;
padding-top: 10px;
}
a {
text-decoration: none;
color: #C4C4C4;
}
#logo {
top:40%;
position: absolute;
right: 33%;
}
html,body {
background-color: #282C35;
color: white;
margin: 0px;
}
input {
background-color: #282C35;
}
#rat {
background-color: white !important;
border: 0px;
transform: translate(-50%,-50%);
position: absolute;
top: 50%;
right:30%;
padding: 20px;
}
#bug {
border: 0px;
background-color: white;
transform: translate(-50%,-50%);
position: absolute;
top: 50%;
padding: 20px;
width:350px;
}
</style>
<title>
RS search
</title>
</head>
<body>
<center>
<img src="rs.png" id="logo"/><br />
<form action="search.php" method="post">
<input type="text" name="search" id="bug">
<input type="submit" name="submit" value="search" id="rat">
</form>
</center>
</body>
</html>
最佳答案
1.) 你的 <div class='results'>
是(根据 CSS 定义)20% 宽。文本居中发生在这 20% 内部,DIV 本身很可能会保持左对齐,总而言之,这看起来不像是居中文本。您可能想要删除那 20% 的宽度。
2.) top: 55%
需要为其父容器定义高度才能生效。
关于php - 为什么 CSS 不将内容从 php echo 输出移动到 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123124/