如何更改三个不同div的主体背景



我有一个名为shtype_porosine.php的php文件,还有一个外部css文件style.css。我用来登录网站的php文件有三个不同的div标记。对于第一个(div id="container_dyte"),主体背景颜色必须是白色,但对于包含的另外两个,则包括('include/administrator_index_nsp.php');和include('include/login_forma.php');身体颜色必须是黑色。

我不确定a是否可以在同一个php文档中使用body标记三次,如果可以的话,那么问题就解决了(在前面的问题"how to css if else to change body background color"中,我发现了body class="),但如果不能,那么解决方案是哪一个?以下是php文件shtype_porosine.php 的代码

谢谢

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>website</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <?php
        session_start();
        // this is for an administrator
        if(isset($_SESSION['auser'])){
    ?>
    <div id="container_dyte">
        <span>Something here...</span>
    </div>
    <?php
    }else{
        // these is for a user
        if(isset($_SESSION['p_user'])){
            include('include/administrator_index_nsp.php');
        }else{
            // this is the login form to apear if fails the adminstrator and the user
            include('include/login_forma.php');
        }
    }
    ?>
</body>
</html>

检查是否设置了$_SESSION[ 'auser'],如果设置了,则使用白色background-color制作主体。

否则,制作一个带有黑色background-color的实体。

像这样:

<?php 
session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>website</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<?php
if(isset($_SESSION['auser'])){ ?>
    <body style='background-color:white'>
<?php } else { ?>
    <body style='background-color:black'>
 <?php }
        if(isset($_SESSION['auser'])){
    ?>
    <div id="container_dyte">
        <span>Something here...</span>
    </div>
    <?php
    }else{
        // these is for a user
        if(isset($_SESSION['p_user'])){
            include('include/administrator_index_nsp.php');
        }else{
            // this is the login form to apear if fails the adminstrator and the user
            include('include/login_forma.php');
        }
    }
    ?>
</body>
</html>
<body class="<?= isset($_SESSION['auser']) ? 'white' : 'black';?>">
<body style="background-color:<?= isset($_SESSION['auser']) ? 'white' : 'black';?>">

您不必更改任何"body"。只需为每个div设置背景色css属性:

<div id="container_dyte" style="background-color:white">

文件中包括/administrator_index_nsp.php

<div style="background-color:black">

文件中包括/login_forma.php

<div style="background-color:black">

简单方法:
为div提供不同的ID名称并使用CSS。

HTML

<div id="the_id">

CSS

#the_id {
    background-color:white;
}

中等方式:
制作一个具有特殊ID或CLASS的容器div,并设置一个CSS。

CSS

#container div { 
    background-color: black;
}
#container:first-child { 
    background-color: red;
}
#container:last-child { 
    background-color: white;
}

艰难的道路:
如果您有这个页面的css规范,您可以选择div number by style属性。

body:nth-child(1) { background-color: black; };
body:nth-child(2) { background-color: red; };
body:nth-child(3) { background-color: white; };

如果其他页面的css是ment,请不要使用此答案。

为了解决这个问题,包含一个自定义css,它将为<body>标记应用所需的背景色。例如:

if (mode=='mode1')
{
   echo "<div id='div1'></div>";
   echo "<style type='text/css'> body { background-color:red } </style>";
}
if (mode=='mode2')
{
   echo "<div id='div2'></div>";
   echo "<style type='text/css'> body { background-color:yellow } </style>";
}
if (mode=='mode3')
{
   echo "<div id='div3'></div>";
   echo "<style type='text/css'> body { background-color:blue } </style>";
}

因此,使用此逻辑,您可以根据

显示的div设置主体的背景颜色

最新更新