div背景色更改 闪烁

One of the most common things you may have to do as a web developer is to change the background-color of an HTML element. But it may be confusing to do if you do not understand how to use the CSS background-color property.

作为Web开发人员,您可能要做的最常见的事情之一就是更改HTML元素的背景颜色。 但是,如果您不了解如何使用CSS background-color属性,可能会造成混淆。

In the article, we discuss

在本文中,我们讨论

  • the default background color value of an HTML element HTML元素的默认背景色值
  • how to change the background color of a div, which is a very common element如何更改div的背景颜色,这是非常常见的元素
  • which parts of the CSS box model are affected by the background-color property, and

    CSS盒子模型的哪些部分受background-color属性影响,以及

  • the different values this property can take. 此属性可以采用的不同值。

元素的默认背景色 (Default Background Color of an Element)

The default background color of  a div is transparent. So if you do not specify the background-color of a div, it will display that of its parent element.

div的默认背景颜色是transparent 。 因此,如果不指定div的背景色,它将显示其父元素的背景色。

更改Div的背景颜色 (Changing the Background Color of a Div)

In this example, we will change the background colors of the following divs.

在此示例中,我们将更改以下div的背景颜色。

<div class="div-1"> I love HTML </div>
<div class="div-2"> I love CSS </div>
<div class="div-3"> I love JavaScript </div>

Without any styling, this will translate to the following visually.

没有任何样式,它将在视觉上转换为以下内容。

Let's change the background color of the divs by adding styles to the classes. You can follow along by trying the examples in an HTML file.

让我们通过向类中添加样式来更改div的背景颜色。 您可以尝试在HTML文件中尝试使用示例。

<style>.div-1 {background-color: #EBEBEB;}.div-2 {background-color: #ABBAEA;}.div-3 {background-color: #FBD603;}
</style><body><div class="div-1"> I love HTML </div><div class="div-2"> I love CSS </div><div class="div-3"> I love JavaScript </div>
</body>

This will result in the following:

这将导致以下结果:

Cool! We have successfully changed the background color of this div. Next, let's get to know more about this property. Let's see how the background-color property affects parts of the CSS-box model.

凉! 我们已成功更改了该div的背景颜色。 接下来,让我们更多地了解此属性。 让我们看看background-color属性如何影响CSS-box模型的各个部分。

背景颜色和CSS Box模型 (Background Color and the CSS Box Model)

According to the CSS box model, all HTML elements can be modeled as rectangular boxes. Every box is composed of 4 parts as shown in the diagram below.

根据CSS框模型,所有HTML元素都可以建模为矩形框。 每个盒子由4个部分组成,如下图所示。

You can read up on the box model if you are not familiar with it. The question is, which part of the box model is affected when you change the background color of a div? The simple answer is the padding area and the content area. Let's confirm this by using an example.

如果您不熟悉盒子模型,则可以阅读。 问题是,当您更改div的背景颜色时,盒子模型的哪一部分会受到影响? 简单的答案是填充区域和内容区域。 让我们通过一个例子来确认这一点。

<style>body {background-color: #ABBAEA;}div {height: 200px;margin: 20px;border: 5px solid;background-color: #FBD603;}
</style>
<body><div><p>This is the parent div which contains the div we are testing</p><div><p>This example shows that changing the background color of a div does not affect the border and margin of the div.</p></div></div>
</body>

This will result in:

这将导致:

From the example above, we can see that the margin area and the border area are not affected by the change in background color. We can change the color of the border using the border-color property. The margin area remains transparent and reflects the background color of the parent container.

从上面的示例中,我们可以看到空白区域和边框区域不受背景颜色变化的影响。 我们可以使用border-color属性更改边框的颜色。 边距区域保持透明,并反映父容器的背景色。

Finally, let's discuss the values the background-color property can take.

最后,让我们讨论background-color属性可以采用的值。

背景色值 (Background-color Values)

Just like the color property, the background-color property can take six different values. Let's consider the three most common values with an example. In the example, we set the background-color of the div to red with different values.

就像color属性一样,background-color属性可以采用六个不同的值。 让我们以一个示例考虑三个最常见的值。 在示例中,我们将div的背景色设置为具有不同值的红色。

<style>/* Keyword value/name of color */.div-1 {background-color: red;}/* Hexadecimal value */.div-2 {background-color: #FF0000;  }/* RGB value */.div-3 {background-color: rgb(255,0,0);}</style><body><div class="div-1"><p>The background property can take six different values.</p></div><div class="div-2"><p>The background property can take six different values.</p></div><div class="div-3"><p>The background property can take six different values.</p></div>
</body>

Notice that they all result with the same background color.

请注意,它们的结果都是相同的背景色。

Other values the background-color property can take include HSL value, special keyword values and global values. Here are examples of each of them.

background-color属性可以采用的其他值包括HSL值,特殊关键字值和全局值。 这里是每个例子。

/* HSL value */
background-color: hsl(0, 100%, 25%;/* Special keyword values */
background-color: currentcolor;
background-color: transparent;/* Global values */
background-color: inherit;
background-color: initial;
background-color: unset;

You can read more on each of these values here.

您可以在此处阅读有关这些值的更多信息 。

额外注意 (Extra Note)

When setting the background color of an element, it is important to ensure that the contrast ratio of the background color and the color of the text it contains is high enough. This is to ensure that people with low vision can easily read the text.

设置元素的背景色时,重要的是要确保背景色和其包含的文本颜色的对比度足够高。 这是为了确保弱视人士可以轻松阅读文本。

Consider these two divs.

考虑这两个div。

The contrast between the background color of the first div and the color of the text is not high enough for everyone to see. So unless you are the only one using the website you are building and you have very good eyesight, you should avoid such color combinations.

第一个div的背景颜色与文本颜色之间的对比度不够高,每个人都看不到。 因此,除非您是唯一使用该网站的人,并且您的视力很好,否则应避免这种颜色组合。

The second div has a much better contrast ratio between the background color and the color of the text . Thus, it is more accessible and clearer for people to read.

第二个div在背景颜色和文本颜色之间具有更好的对比度。 因此,它使人们更容易阅读和阅读。

结论 (Conclusion)

In this article, we saw how you can change the background-color of a div. We also discussed which parts of the CSS box model are affected by the change in background-color. Finally, we discussed the values the background-color property can take.

在本文中,我们看到了如何更改div的背景颜色。 我们还讨论了CSS盒子模型的哪些部分受背景颜色变化的影响。 最后,我们讨论了background-color属性可以采用的值。

I hope you found this article useful. Thanks for reading.

希望本文对您有所帮助。 谢谢阅读。

翻译自: https://www.freecodecamp.org/news/html-background-color-tutorial-how-to-change-a-div-background-color-explained-with-code-examples/

div背景色更改 闪烁

div背景色更改 闪烁_HTML背景色教程–如何更改Div背景色,并通过代码示例进行了说明相关推荐

  1. div和div之间画横线_HTML如何在两个div标签中间画一条竖线

    近日在画一个界面的时候,遇到一个需求:在界面当中画一条竖线,且这条竖线在高度上需要自动占满整个父div(即这条竖线的高度和两个div中较高的一个等高). 往常我们画一条横线直接用标签 即可,当画一条竖 ...

  2. 中文代码示例之Vuejs入门教程(一)

    为了检验中文命名在主流框架中的支持程度, 在vuejs官方入门教程第一部分的示例代码中尽量使用了中文命名. 所有演示都在本地测试通过, 源码在这里. 下面省略了很多原教程的说明内容, 而着重于代码示例 ...

  3. php表格制作底纹怎么做,HTML表格标记教程(36):表头的背景色属性BGCOLOR

    HTML表格标记教程(36):表头的背景色属性BGCOLOR 互联网   发布时间:2008-10-17 18:57:03   作者:佚名   我要评论 标记用于设定表格中某一表头的属性.见下表所示: ...

  4. html如何设置表表头背景色,HTML表格标记教程(36):表头的背景色属性BGCOLOR

    标记用于设定表格中某一表头的属性.见下表所示: 表头的 标记属性 通过BGCOLOR属性,可以设定表头的背景颜色. 基本语法> 语法解释 定义颜色的时候,可以使用英文颜色名称或十六进制颜色值表现 ...

  5. html的表格的属性设置颜色,HTML表格标记教程(20):行的背景色属性BGCOLOR

    HTML表格标记教程(20):行的背景色属性BGCOLOR 互联网   发布时间:2008-10-17 18:56:42   作者:佚名   我要评论 通过BGCOLOR属性,可以设定行的背景颜色. ...

  6. html div css盒子,CSS盒子模式一(DIV布局快速入门)_html/css_WEB-ITnose

    中心最新招了几十个人,我也成小旋风了,看来这个山头还得呆几天.毕竟有几十口子人哪,咋能撒手不管啊!我就先发个贴热热身!也希望前几届的师哥师姐多 多指教. 我们在网页设计中常听的属性名:内容(conte ...

  7. R语言ggplot2可视化自定义可视化结果的背景色(background color)实战:改变整个画板(panel)的背景色、改变图像(plot)区域的背景色

    R语言ggplot2可视化自定义可视化结果的背景色(background color)实战:改变整个画板(panel)的背景色.改变图像(plot)区域的背景色 目录

  8. vba 窗体所有组件 enabled_Csharp设计闪烁窗体制作教程

    大家好,今天为大家带来C#怎么制作炫酷的闪烁窗体,教程如下.常用QQ的用户都会知道,如果当前信息窗体并不是活动窗体,但有消息时,窗体就会闪烁,引起用户注意,本文章讲述实现此功能.运行程序点击开始. 过 ...

  9. Java点击按钮div缩放_[Java教程]怎样给div增加resize事件

    [Java教程]怎样给div增加resize事件 0 2016-10-31 11:00:04 当浏览器窗口被调整到一个新的高度或宽度时,就会触发resize事件,这个事件在window上面触发,那么如 ...

最新文章

  1. flask框架如何实现修改密码和免密登录功能
  2. mybatis @sqlprovider
  3. GridView类容器中的DropDownList联动
  4. el-cascader超出屏幕问题
  5. Maven的下载与安装
  6. 使用dd工具对磁盘RAID5和10进行I/O性能测试
  7. C++ 多线程下的单例模式
  8. VS快捷键及调试方法(含VAssistX快捷键)
  9. python中digits用法_在sklearn digits examp中使用自己的图像
  10. excel两个指标相关性分析_相关分析与回归分析 Excel 和 R计算皮尔逊相关系数(Pearson correlation)...
  11. 让Mac文本编辑器成为HTML编辑器
  12. android生成透明图片格式,Android编程实现图片透明的方法
  13. Android5.0 下拉通知栏快捷开关的添加(必看)
  14. WPCMS插件自动采集发布文章WordPress插件
  15. 【解决方法(最详细)】WIN10系统使用Teamviewer远程连接时被检测出商业用途
  16. graylog+kafka+zookeeper(单机测试及源码),kafka+zookeeper组件部署(二)
  17. c 系统语言改为中文,主编设置win7系统英文版改成中文语言的操作技巧
  18. 启动任务管理器两种方法
  19. mongoDB常用查询更新删除语句
  20. 计算机培训研修心语,学子榜样 | 2018级国家励志奖学金程春旭:不忘初心,砥砺前行...

热门文章

  1. 效率提升:用 Notion 实现个人任务管理监控
  2. python qq群消息监控_python 监控微博用户更新+钉钉提醒
  3. 新手必看! iSpring 安装问题疑难解答(上)
  4. 微服务系统设计——数据模型与系统架构设计
  5. 2020年直播电商数据报告_抖音VS快手
  6. CentOS安装unimrcp-1.7.0
  7. Forest一款轻量级HTTP客户端框架
  8. 让金融互联网-P2P网贷融资量增长10倍的广告宣传公益活动
  9. 【C语言项目实战】C语言实现文件加密
  10. 关于EBS中ERP 工作台释放工单时Date的关系和验证