HTML <p>和<span> - 元素背景颜色不起作用

时间:2017-08-03 06:27:38

标签: html css

我想在p元素中创建一些span-elements。这些span-elements应该具有与p-elements不同的背景颜色。

代码:

&#13;
&#13;
<p style="background-color:blue"> <!-- color not working -->
  <span style="background-color:red"><H2>boo-title</H2>  <!-- color not working -->
    <script language="JavaScript">
      document.write("boo");
    </script>
  </span>
  <span style="background-color:green"><H2>foo-title</H2> <!-- color working -->
    <script language="JavaScript">
      document.write("foo");
    </script>
  </span>
</p>
&#13;
&#13;
&#13;

为什么背景颜色有时会起作用,有时却不起作用,尽管它的定义方式相同?当我这样定义它时,我该如何使它工作?

4 个答案:

答案 0 :(得分:1)

将span标记放在new RsaEncryptionCookieTransform(e.ServiceConfiguration.ServiceCertificate) 标记内,并避免使用div标记。

p

答案 1 :(得分:0)

h2p内有span无效。因此,将p标记替换为div

<div style="background-color:blue"> <!-- color not working -->
  <span style="background-color:red"><H2>boo-title</H2>  <!-- color working -->
    <script language="JavaScript">
      document.write("boo");
    </script>
  </span>
  <span style="background-color:green"><H2>foo-title</H2> <!-- color working -->
    <script language="JavaScript">
      document.write("foo");
    </script>
  </span>
</div>

答案 2 :(得分:0)

永远不要将块元素放在内联元素中。在创建任何部分时,请遵循HTML准则或w3标准规则。

<div style="background-color:blue"> <!-- color not working -->
  <H2>boo-title</H2><span style="background-color:red">  <!-- color working -->
    <script language="JavaScript">
      document.write("boo");
    </script>
  </span>
  <H2>foo-title</H2><span style="background-color:green"> <!-- color working -->
    <script language="JavaScript">
      document.write("foo");
    </script>
  </span>
</div>

答案 3 :(得分:0)

      document.getElementById('boospan').innerHTML="boo";
      document.getElementById('foospan').innerHTML="foo";
  
<div style="background-color:blue">
    <H2>
        <span style="background-color:red">boo-title</span><br/>
        <span id="boospan" style="background-color:yellow"></span>
    </H2>
    <H2>
        <span style="background-color:green">foo-title</span><br/>
        <span id="foospan" style="background-color:orange"></span>
    </H2>
</div>