【问题标题】:Set div background color with only html仅使用 html 设置 div 背景颜色
【发布时间】:2020-09-24 17:43:01
【问题描述】:

是否可以在css上设置div bg颜色,只是纯html。 任何建议都会有所帮助...

body {
    background-color: #6B6B6B;
    margin: 50px;
    
    font-family: Arial;
    color: white;
    font-size: 14px;
    font-weight: 100;
    line-height: .2;
    letter-spacing: 1px;
}
<div style="height:100px;" style="background-color:red;">text</div>

【问题讨论】:

  • 您可以使用内联样式,但我不知道您为什么要这样做。
  • with on css 你的意思是without css
  • 在正文标签中使用bgColor="#6B6B6B"

标签: html styles background-color divide


【解决方案1】:

欢迎使用 StackOverflow。我知道你是 SO 的新贡献者。

注意:

您正在使用 两个样式属性 来标记它是 无效..!!!

<div style="height:100px;" style="background-color:red;">text</div>

而不是使用(如果使用不同的 css 样式,由; 分隔,如下所示)

<div style="height:100px;background:red;">text</div>

body 标签中使用bgColor="#6B6B6B" 来改变body 的背景颜色但是不能只用html 改变Div 背景。 你可以使用内联css,即

但是

HTML bgcolor 属性用于设置 HTML 元素的 背景颜色r。 Bgcolor 是随着层叠样式表的实施而弃用的属性之一。

它支持标签,如 支持的标签:

支持的标签喜欢

&lt;body&gt;,&lt;marquee&gt;,&lt;table&gt;,&lt;tbody&gt;,&lt;td&gt;,&lt;tfoot&gt;,&lt;th&gt;,&lt;thead&gt;,&lt;tr&gt;

使用 bgColor 进行演示

<body bgColor="#6B6B6B">
  <div style="height:100px;background:red;">text</div>
</body>

不使用 bgColor 的演示

<body style="background:#6B6B6B">
  <div style="height:100px;background:red;">text</div>
</body>

注意:

您可以在 CSS 中使用 backgroundbackground-color 属性添加背景颜色

【讨论】:

  • 你想要这样吗?
  • 没有纯 HTML 解决方案。您必须使用 CSS。您可能会发现这个 html set background color 教程很有用。
【解决方案2】:

工作代码

body {
    background-color: #6B6B6B;
    margin: 50px;
    
    font-family: Arial;
    color: white;
    font-size: 14px;
    font-weight: 100;
    line-height: .2;
    letter-spacing: 1px;
}
&lt;div style="height:100px;background-color:red"&gt;text&lt;/div&gt;

内联样式的语法被破坏了。您指定了两次样式。

【讨论】:

    【解决方案3】:

    添加这个 CSS

    body {
        background-color: #6B6B6B;
        margin: 50px;
        font-family: Arial;
        color: white;
        font-size: 14px;
        font-weight: 100;
        line-height: .2;
        letter-spacing: 1px;
    }
    .mydiv {
        background:red;
        padding:5px;
        color:white;
    }
    

    在 HTML 中

    <div class="mydiv">text</div>
    

    【讨论】:

    • thx 伙计们,你们都非常有帮助...还在学习中)
    • @Yummi 如果您只想使用 html 来执行此操作,请查看:&lt;div style="background:red;padding:5px;color:white;text-align:center;"&gt;text&lt;/div&gt;
    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多