【问题标题】:Load CSS into a PHP file将 CSS 加载到 PHP 文件中
【发布时间】:2020-09-19 08:26:29
【问题描述】:

大家。我正在按照教程创建注册和登录系统,我是第一次这样做。我无法为 PHP 文件中的 HTML 表单设置样式。我只有一个文件夹,里面有 6 个文件(error.php、index.php、login.php、register.php、server.php、style.css) 到目前为止,我刚刚在 register.php 和 style.css 文件中编写了代码。 我从 register.php 文件中的控制台获取此消息: 1-“样式表http://localhost/registration/register.php/style.css 被加载为 CSS,即使它的 MIME 类型“text/html”不是“text/css”。”

2-“由于选择器错误,规则集被忽略。”

怎么了?我正在使用 XAMPP

这是代码:

注册.php:

<?php
include('server.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Registration system PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
  <h2>Register</h2>
</div>

<form method="post" action="register.php">
<?php
include('errors.php');
?>
 <div class="input-group">
    <label>Username</label>
    <input type="text" name="username" value="<?php
echo $username;
?>">
  </div>
  <div class="input-group">
    <label>Email</label>
    <input type="email" name="email" value="<?php
echo $email;
?>">
  </div>
  <div class="input-group">
    <label>Password</label>
    <input type="password" name="password_1">
  </div>
  <div class="input-group">
    <label>Confirm password</label>
    <input type="password" name="password_2">
  </div>
  <div class="input-group">
    <button type="submit" class="btn" name="reg_user">Register</button>
  </div>
  <p>
      Already a member? <a href="login.php">Sign in</a>
  </p>
  </form>
</body>
</html>

style.css:

    * {
  margin: 0px;
  padding: 0px;
}
body {
  font-size: 120%;
  background: #F8F8FF;
}

.header {
  width: 30%;
  margin: 50px auto 0px;
  color: white;
  background: #5F9EA0;
  text-align: center;
  border: 1px solid #B0C4DE;
  border-bottom: none;
  border-radius: 10px 10px 0px 0px;
  padding: 20px;
}
form, .content {
  width: 30%;
  margin: 0px auto;
  padding: 20px;
  border: 1px solid #B0C4DE;
  background: white;
  border-radius: 0px 0px 10px 10px;
}
.input-group {
  margin: 10px 0px 10px 0px;
}
.input-group label {
  display: block;
  text-align: left;
  margin: 3px;
}
.input-group input {
  height: 30px;
  width: 93%;
  padding: 5px 10px;
  font-size: 16px;
  border-radius: 5px;
  border: 1px solid gray;
}
.btn {
  padding: 10px;
  font-size: 15px;
  color: white;
  background: #5F9EA0;
  border: none;
  border-radius: 5px;
}
.error {
  width: 92%; 
  margin: 0px auto; 
  padding: 10px; 
  border: 1px solid #a94442; 
  color: #a94442; 
  background: #f2dede; 
  border-radius: 5px; 
  text-align: left;
}
.success {
  color: #3c763d; 
  background: #dff0d8; 
  border: 1px solid #3c763d;
  margin-bottom: 20px;
}

【问题讨论】:

  • 您是否尝试将href="style.css" 替换为href="./style.css",您必须将css 指向与您的php 文件相同的目录
  • Van Tho,同样的结果。
  • href="/registration/style.css"怎么样
  • Van Tho,成功了,非常感谢。为什么这有效?
  • 因为你的css文件在文件夹注册中,而不是registration/register.php/style.css,一般./style.css会起作用,但是这种情况你可能需要使用/registration/style.css

标签: php html css


【解决方案1】:

您可以尝试两种方法

更换你的线路

&lt;link rel="stylesheet" type="text/css" href="style.css"&gt;

1:指向当前目录

&lt;link rel="stylesheet" type="text/css" href="./style.css"&gt;

2:从根目录指向

&lt;link rel="stylesheet" type="text/css" href="/registration/style.css"&gt;

因为您的 css 文件位于文件夹 /registration/,而不是 /registration/register.php/

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多