开发者

How to CSS if..else to change body background-color

开发者 https://www.devze.com 2023-04-05 15:31 出处:网络
I got a CSS-related issue.. How can we perform if/else in CSS... for example, i have a CSS file named main.css and two HTML pages namely a.html and b.html.

I got a CSS-related issue..

How can we perform if/else in CSS... for example, i have a CSS file named main.css and two HTML pages namely a.html and b.html.

Inside body {} element (main.css) , the background-color will always be #dad4d4. For a.html, the body will inherit this color property, but for b.html, i want the body background-co开发者_运维百科lor to be changed to #eae1e2.

How to make this a success?


You cannot parse functions within a standard .css file.

However, a solution to what you want to do:

body.a {
    background-color: red;
}

body.b {
    background-color: blue;
}

Then simply use <body class="a"> or <body class="b">on each respective page.


Put an id or class attribute on your <body> element.

For example, on a.html it would be <body id="a"> and on b.html it would be <body id="b">. You can then do:

body#a {
    background-color: blue;
}

body#b {
    background-color: green;
}


You could give your body a class to achieve this. In a.html your body will have class 'a', in b.html class 'b'. Each class can then have it's own properties.

As far as I know if else is not possible in CSS

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号