How to ignore a certain element when determining parent's size

前端 未结 3 1459
无人共我
无人共我 2021-01-19 19:08

I have a div which is only as wide as its contents, but I\'m trying to make it so that a certain element is ignored when determining the div\'s size.



        
3条回答
  •  梦谈多话
    2021-01-19 19:38

    Add a big negative margin to the ignore element while setting the width.

    #parent {
      background:red;
      padding:5px;
      display:inline-block;
    }
    #parent > div {
      height:20px;
      background:blue;
      margin-bottom:5px;
    }
    
    .ignore {
      margin-right:-500px;
      
      /*to illustrate*/
      animation:change 2s linear infinite alternate;
    }
    
    @keyframes change {
      from {width:10px;}
      to {width:100px;}
    }

    Or use only the negative margin to set the width. Note that the final width will be the sum of the biggest width inside the parent + the margin you set (not only the margin)

    #parent {
      background:red;
      padding:5px;
      display:inline-block;
    }
    #parent > div {
      height:20px;
      background:blue;
      margin-bottom:5px;
    }
    
    .ignore {
      /*to illustrate*/
      animation:change 2s linear infinite alternate;
    }
    
    @keyframes change {
      from {margin-right:-10px;}
      to {margin-right:-100px;}
    }

提交回复
热议问题