代码之家  ›  专栏  ›  技术社区  ›  Mani Kant Tiwari

css:在div元素中设置跨度的样式

  •  0
  • Mani Kant Tiwari  · 技术社区  · 5 年前

    我的html sode代码片段是

    <div id="head">
       <span id="span1"> </span>
       <span id="span2"> </span>
    </div>
    

    我不能让我的css工作。

    #head>#span1{
      //its not working.
    }
    

    我也试过

    #head1:nth-child(1){
       width: 200px;
       height: 300px;
       background-color: #0000FF;
    }
    
    #head1:nth-child(2){
       width: 300px;
       height: 300px;
       background-color: #0000FF;
     }
    

    请帮帮我。

    2 回复  |  直到 5 年前
        1
  •  1
  •   Jakub Muda    5 年前

    <span> 在内联元素中,因此需要设置 display:block 以做出适当的样式(以防您需要宽度和高度)。如果您有id,那么您在css中需要做的就是将这些id作为目标。

    #span1{
       width: 200px;
       height: 300px;
       background-color: yellow;
       display:block
    }
    
    #span2{
       width: 300px;
       height: 300px;
       background-color: red;
       display:block
     }
    <div id="head">
       <span id="span1"> </span>
       <span id="span2"> </span>
    </div>
        2
  •  1
  •   Amine KOUIS    5 年前

    尝试使用 #head span 全选 <span>

    #head span{
       width: 200px;
       height: 300px;
       background-color: #0000FF;
       display:inline-block
    }
    <div id="head">
       <span id="span1">Lorem Ipsum </span>
       <span id="span2">Lorem Ipsum </span>
    </div>

    尝试使用 #head #spanN 选择n <SPAN & GT;

    #head #span1{
       width: 200px;
       height: 300px;
       background-color: #0000FF;
       display:inline-block
    }
    
    #head #span2{
       width: 200px;
       height: 300px;
       background-color: #FF0000;
       display:inline-block
    }
    <div id="head">
       <span id="span1">Lorem Ipsum</span>
       <span id="span2">Lorem Ipsum</span>
    </div>