Prevent children from inheriting rotate transformation in CSS

后端 未结 3 1258
误落风尘
误落风尘 2020-12-06 11:10

I am performing a CSS transform: rotate on a parent, yet would like to be able to negate this effect on some of the children - is it possible without using the reverse rotat

相关标签:
3条回答
  • 2020-12-06 11:45

    I believe that you are going to need to fake it using a second child, the specification does not seem to allow for the behavior you would like, and I can understand why the position of a child element has to be affected by a transform to its parent.

    This isn't the most elegant of solutions, but I think you're trying to do something that the specification is never going to allow. Take a look at the following fiddle for my solution:


    .parent {
      position: relative;
      width: 200px;
      height: 150px;
      margin: 70px;
    }
    
    .child1 {
      background-color: yellow;
      width: 200px;
      height: 150px;
      -webkit-transform: rotate(30deg);
      -moz-transform: rotate(30deg);
      -o-transform: rotate(30deg);
      -ms-transform: rotate(30deg);
      transform: rotate(30deg);
    }
    
    .child2 {
      position: absolute;
      top: 30px;
      left: 50px;
      background-color: green;
      width: 70px;
      height: 50px;
    }
    <div class="parent">
      <div class="child1"></div>
      <div class="child2"></div>
    </div>

    0 讨论(0)
  • 2020-12-06 11:52

    If you want to apply transforming effects on a parent without affecting its children, you can simply animate a parent's pseudo-element like this:

    .parent {
      display: inline-block;
      position: relative;
    }
    
    .parent::before {
      content: "";
      background: #fab;
    
      /* positioning / sizing */
      position: absolute;
      left: 0;
      top: 0;
    
      /* 
            be aware that the parent class have to be "position: relative"
            in order to get the width/height's 100% working for the parent's width/height.                
      */
      width: 100%;
      height: 100%;
    
      /* z-index is important to get the pseudo element to the background (behind the content of parent)! */
      z-index: -1;
      transition: 0.5s ease;
      /* transform before hovering */
      transform: rotate(30deg) scale(1.5);
    }
    
    .parent:hover::before {
      /* transform after hovering */
      transform: rotate(90deg) scale(1);
    }
    

    This actually worked for me. JSFiddle

    0 讨论(0)
  • 2020-12-06 11:57

    May be you have to write like this:

    .child {
        position: absolute;
        top: 30px;
        left: 50px;
        background-color: green;
        width: 70px;
        height: 50px;
        -webkit-transform: rotate(-30deg);
        -moz-transform: rotate(-30deg);
        -o-transform: rotate(-30deg);
        -ms-transform: rotate(-30deg);
        transform: rotate(-30deg);
    }
    

    Check this for more http://jsfiddle.net/XSHmJ/1/

    UPDATED

    You can sue :after & :before psuedo class for this.

    check this http://jsfiddle.net/XSHmJ/4/

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