如何使用外部CSS样式表的HTML和PHP

时间:2017-05-10 09:25:04

标签: 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;
}

3 个答案:

答案 0 :(得分:2)

Holly Macaroni!

首先纠正您的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:
// 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

答案 1 :(得分:1)

首先,整理你的HTML:

 <div class="profile_content">
    <div id="profile_heading">
        <h3>Profiles</h3>
    </div>
    ...Your SQL Stuff...
</div>

现在以你的h3为中心,你需要:

.profile_content h3
{
    text-align:center;
}

你当前的CSS在某些方面是不正确的:你有“h3。profile”这没有任何意义(h3点?还没有元素'profile')。您还引用了一个不存在的“#profile_content”。

答案 2 :(得分:1)

    <!DOCTYPE html>
<html>

<head>
    <title><?php echo $title; ?></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="<?php echo base_url();?>/css/bootstrap.min.css">
  

将以上代码添加到php文件的顶部并将文件另存为.php而不是.html