How to reference nested classes with css?

前端 未结 5 1729
故里飘歌
故里飘歌 2021-02-20 02:04

When using css, how can I specify a nested class.

Here is my html code:

相关标签:
5条回答
  • 2021-02-20 02:18

    In CSS, classes need to be prefixed by a ., ids need to be prefixed by #, and elements don't need to be prefixed at all.

    This is how you need to declare your CSS:

    .box1 .box-body i {
       width: 30px;
    }
    

    Note that box1 and box-body are both classes, while i is an element.


    Example:

    <div class="class"></div>
    <div id="id"></div>
    <div></div>
    

    I have listed three different <div> elements. This is how they would be accessed in CSS:

    // first div
    .class {
    [some styling]
    }
    
    // second div
    #id {
    [some styling]
    }
    
    // third div
    div {
    [some styling]
    }
    
    0 讨论(0)
  • 2021-02-20 02:26

    Selectors for classes in CSS need a ".":

    .box1 .box-body i{/*your style*/}
    

    Maybe you should take a look at this page:

    Selectors

    0 讨论(0)
  • 2021-02-20 02:27
       <style type="text/css">
       .box1 .box-body i{width: 30px;}
       </style>
    
    0 讨论(0)
  • 2021-02-20 02:37

    The classes in your CSS need periods before them. Note i doesn't since it's an element not a class.

    <style type="text/css">
        .box1 .box-body i{width: 30px;}
    </style>
    
    0 讨论(0)
  • 2021-02-20 02:45

    You just need to know how css selectors work. Here is brief description about css selectors.

    In your case,

    .box .box-body i{
      width:30px;
    }
    

    space between two selectors defines second element is child of first. In your case, element i is child element of element which has box-body class. and that element is child element of class which has .box class.

    0 讨论(0)
提交回复
热议问题