简体   繁体   English

如何在网站的不同位置更改背景颜色?

[英]how can I change the background color in different places on the site?

(sorry for my bad english) (对不起,我的英语不好)

I would like to create a website where the main menu background was white, but when I went to the login area for example, the background color became black, how do I do this 我想创建一个主菜单背景为白色的网站,但是例如,当我进入登录区域时,背景颜色变为黑色,我该如何做?

PS: i`m new to HTML PS:我是HTML新手

if you make different pages and every page has it's own menu you can make inline style ex: in home page in login page 如果您创建不同的页面,并且每个页面都有其自己的菜单,则可以进行内联样式ex:在登录页面的主页中

but if you have one menu in your project you can you can take the url of window by javascript and check on it if you are in home make background white and else if you are in login page make background black ex: var menu = document.getElementById("menu") 但是,如果您的项目中有一个菜单,则可以使用javascript获取窗口的网址,然后检查它是否在家里,使背景为白色,否则在登录页面中使背景为黑色,例如:var menu = document。 getElementById(“ menu”)

if(window.location == "home/")
{
  menu.style.backgroundColor = "white";
}
else if (window.location == "login/") 
{
  menu.style.backgroundColor = "black";
}

you should write url true 您应该将网址写为true

A few pointers. 一些指针。

First, use a lot of DIVs (or section , aside , etc). 首先,使用很多DIV(或sectionaside等)。 DIVs are wrappers/containers that you put other HTML code into. DIV是您将其他HTML代码放入其中的包装器/容器。 You can style that container-full-of-code to have a different background color (or different padding, or different position, etc). 您可以设置该代码容器的样式,使其具有不同的背景颜色(或不同的填充或位置,等等)。 Most beginners make the mistake of using too few such containers. 大多数初学者会犯这样的错误:使用的容器太少。 (Sections and Asides work the same as DIVs, but are treated differently by search engines.) (Section和Asides与DIV的工作原理相同,但搜索引擎将其区别对待。)

Next, discover javascript (and its easier-to-use cousin, jQuery). 接下来,发现javascript(及其易于使用的表弟jQuery)。 If you want styling to change on-the-fly , that's javascript. 如果您想即时更改样式,则使用javascript。

jQuery is almost like another language, but it's actually a javascript library that gets turned into pure javascript at run-time - but you needn't worry about the details. jQuery几乎类似于另一种语言,但是它实际上是一个javascript库,在运行时变成了纯javascript-但您不必担心细节。 Just use it - many of us think it is a huge improvement on javascript, especially for beginners. 只需使用它-我们许多人认为它对javascript来说是一个巨大的改进,特别是对于初学者。 Note that it is not a one-or-the-other decision - you can mix/match pure javascript with jQuery code. 请注意,这不是一个或另一个决定-您可以将纯JavaScript与jQuery代码混合/匹配。 Do an hour tutorial on jQuery, and then repeat the tutorial ten times (until you have it mastered). 在jQuery上做一个小时的教程,然后重复该教程十次(直到您掌握了该教程)。

So, to your question. 所以,对你的问题。

You will put the main menu in a DIV - in fact, the whole menu can be made up of DIVs inside DIVs - but you will set the outer DIV background color to white. 您将主菜单放入DIV中-实际上,整个菜单可以由DIV内部的DIV组成-但您会将外部DIV背景颜色设置为白色。

When the login screen is presented (that sounds like javascript/jQuery in action to me...) you will use that same code to change the main menu background color. 当显示登录屏幕时(听起来像是javascript / jQuery,对我来说...),您将使用相同的代码来更改主菜单的背景颜色。 With jQuery, the code might look something like this: 使用jQuery,代码可能看起来像这样:

$('#login_button_ID').click(function(){
     $('#login_div_ID').show();
     $('#mainmenu_div_ID').css('background','black');
     // -OR- $('#mainmenu_div_ID').addClass('bgBlack');
});

Not so complicated, is it? 不是那么复杂,是吗?

Welcome to Stack Overflow! 欢迎使用Stack Overflow! You will want to learn CSS and HTML a little better to help make your site look more user-friendly. 您将需要更好地学习CSSHTML,以使您的网站看起来更加用户友好。 This is how you cam change the background color on a web page (using inline CSS), add the following code within your tags: 这是您在网页上(使用嵌入式CSS)更改背景颜色的方法,在代码中添加以下代码:

 <style>
    body {
        color: rgb(255, 255, 255); /* White Color */
    }

    login {
        color: rgb(0, 0, 0); /* Black Color */
    }

    </style>

You will need to identify your chosen (login) element in your CSS to enable you to make changes to it. 您将需要在CSS中标识您选择的(登录)元素,以使您能够对其进行更改。

The example above uses rgb color codes to select the desired color, however colors can also be represented as Hex (#ff0000), HSL and more. 上面的示例使用rgb颜色代码选择所需的颜色,但是颜色也可以表示为十六进制 (#ff0000), HSL等。

More information is available here: www.w3schools.com/html/htmlColors 可在此处获得更多信息: www.w3schools.com/html/html颜色

www.khanacademy.org/computing/computer-programming/html-css/ www.khanacademy.org/computing/computer-programming/html-css/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM