php - 如何为 html 和 php 使用外部 css 样式表

标签 php html css alignment

我正在尝试使用外部样式表将 h3 元素和“profile_content”div-id 置于“profile”类的中心。我知道如何只用一个 html 和 css 文件来做到这一点,但是当涉及到 php 时,我感到很困惑!请你也能帮我解决语法问题,这样我就知道以后该怎么做了。

php代码

<?php
    session_start();

    // Connect to the database with '$mysqli' as the connection variable name
    $mysqli = new mysqli ("localhost", "root", "", "folder");

    //Check connection
    // '$mysqli->connect_errno' = Returns the error code from last connect call
    // '->' is used to access an object method or property
    if ($mysqli -> connect_errno) {
        die('Connect Error: ' . $mysqli -> connect_errno);
    }
?>

<html>
    <header>
        <link rel = "stylesheet" type = "text/css" href = "mystyle_friends.css">
    </header>
    <div class = "heading">
    <h1>FRIENDS PAGE</h1>
    </div>


    <p>Welcome  <?php  echo $_SESSION['username'] ?></p>



    <div class = "profile_content">

        <div id = profile_heading>
        <h3>Profiles</h3>
        </div>

        <?php 
            //Perform query against database. Requires 2 parameters = (connection, query)
            $result = mysqli_query($mysqli, "SELECT * FROM users");
            while ($row = mysqli_fetch_array($result)) {
                echo "<div id = 'profile_data'>";
                echo "<p> First Name: " .$row ["user_f_name"] .  "</p>";//$user_first = $row["user_f_name"];
                echo "<p> Surname: " .$row ["user_surname"] .  "</p>";//$user_sur =  $row["user_surname"];
                echo "<p> Score: " .$row ["user_score"] .  "</p>";//$user_score = $row["user_score"];
                echo '<img src="data:user_images/jpeg;base64,' . base64_encode( $row['user_image'] ) . '" />';
                echo "</div>";
            }
        ?>

    </div>

css 外部样式表

.heading {
    text-align: center;
}

.profile {
    border-style: solid;
    border-width: 5px;

}

h3. profile {
    text-align: center;
}

#profile_content. profile {
    text-align: center;
}

最佳答案

冬青通心粉!

首先更正您的 HTML:

<?php
    session_start();

    // Connect to the database with '$mysqli' as the connection variable name
    $mysqli = new mysqli ("localhost", "root", "", "folder");

    //Check connection
    // '$mysqli->connect_errno' = Returns the error code from last connect call
    // '->' is used to access an object method or property
    if ($mysqli -> connect_errno) {
        die('Connect Error: ' . $mysqli -> connect_errno);
    }
?>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="mystyle_friends.css">
    </head>
    <body>
        <div class="heading">
            <h1>FRIENDS PAGE</h1>
        </div>

        <p>Welcome <?php echo $_SESSION['username'] ?></p>

        <div class="profile_content">
            <div id="profile_heading">
                <h3>Profiles</h3>
            </div>

            <?php 
                //Perform query against database. Requires 2 parameters = (connection, query)
                $result = mysqli_query($mysqli, "SELECT * FROM users");
                while ($row = mysqli_fetch_array($result)) {
                    echo "<div id=\"profile_data\">";
                    echo "<p>First Name: " . $row["user_f_name"] . "</p>";       //$user_first = $row["user_f_name"];
                    echo "<p>Surname: " . $row["user_surname"] . "</p>";       //$user_sur =  $row["user_surname"];
                    echo "<p>Score: " . $row["user_score"] . "</p>";           //$user_score = $row["user_score"];
                    echo "<img src=\"data:user_images/jpeg;base64," . base64_encode( $row['user_image'] ) . "\" />";
                    echo "</div>";
                }
            ?>
        </div>
    </body>
</html>

之后你必须修复你的 CSS-Selectors:

// Selectors:
// element {} Elements can appear as many times as you need them per Document
// .class {} Classes can appear as many times as you need them per Document
// #id {} IDs can appear just once per Document

.heading {
    text-align: center;
}

#profile_heading h3 {
    text-align: center
}

.profile_data {
    text-align: center;
}

// There is no Element with the Class "profile"
/*
.profile {
    border-style: solid;
    border-width: 5px;
}
*/

// There is no Element inside of the h3 with the Class "profile"
/*
h3. profile {
    text-align: center;
}
*/

// There is no Element with ID "profile_content"
/*
#profile_content .profile {
    text-align: center;
}
*/

但首先:https://www.google.ch/?gfe_rd=cr&ei=6eISWYObOqLC8gfkzp3wBg#q=html+css+for+dummies

关于php - 如何为 html 和 php 使用外部 css 样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43888452/

相关文章:

css - HTML 中 div 中的文本

php - 而 mysqli_fetch_assoc 在有列而不是行时工作

php - CodeIgniter 全局变量

php - php中的持久本地域套接字

javascript - 将 Javascript 变量发布到 PHP 文档

android - 带有 Html.fromhtml 的 div 标签

html - 并非每个元素都会溢出

html - 与基线和文本区域垂直对齐

css - Sass 无效 CSS 错误 : "expected expression"

css - 悬停效果仅适用于第一个 SVG 路径