dourou9477 2017-05-10 09:25
浏览 164
已采纳

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

I'm trying to center the h3 element and "profile_content" div-id in the "profile" class using external style sheets. I know how to do it with just an html and css file but I get stumped when php gets involved! Please could you help me with the syntax as well so I know what to do in the future.

php code

<?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 external style sheet

.heading {
    text-align: center;
}

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

}

h3. profile {
    text-align: center;
}

#profile_content. profile {
    text-align: center;
}
  • 写回答

3条回答 默认 最新

  • douchuanghan1344 2017-05-10 09:53
    关注

    Holly Macaroni!

    First correct your 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>
    

    After that you have to fix your 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;
    }
    */
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊