如何仅在父元素中应用转换?
我有超文本标记语言
<div id="mydiv" style="color: white; height: 1080px; width: 1920px; transform: translate(-27px, -323px) scale(0.972, 0.401);">
<div class="player_controls">
<a class="right carousel-control" href="#" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
</div>
</div>
此代码包含
transform: translate(-27px, -323px) scale(0.972, 0.401);
这适用于子元素PlayerControls,a标记。
这样做可以不影响子元素吗?
我尝试从Player_Controls中删除转换
#mydiv .player_controls{transform:''}
解决方案
子元素始终受父元素上的转换影响。这不是子级继承的,因此不能通过在子级上使用transform: ' '
或transform: none
来覆盖它。解决这个问题的一种方法是对子对象应用反向变换。在这里,由于scale
不应影响子项,因此应对其应用父项比例的倒数。
#mydiv,#mydiv2 {
height: 200px;
width: 200px;
background: red;
transform: translate(27px, 23px) scale(0.972, 0.401);
}
#mydiv .player_controls {
transform: scale(1.028, 2.493);
transform-origin: left top;
}
<h2>With reverse transform on child</h2>
<div id="mydiv">
<div class="player_controls">
<a class="right carousel-control" href="#" data-slide="next"><span class="glyphicon glyphicon-chevron-right">Some icon</span>Some text</a>
</div>
</div>
<h2>With no reverse transform on child</h2>
<div id="mydiv2">
<div class="player_controls">
<a class="right carousel-control" href="#" data-slide="next"><span class="glyphicon glyphicon-chevron-right">Some icon</span>Some text</a>
</div>
</div>
如果出于某种原因不能(或不愿意)对子元素应用反向转换,则必须更改结构并使该子元素成为同级元素。
.container {
position: relative;
}
#mydiv {
position: absolute;
top: 0px;
left: 0px;
height: 200px;
width: 200px;
background: red;
transform: translate(27px, 23px) scale(0.972, 0.401);
transform-origin: 0px 0px;
}
.player_controls {
position: absolute;
top: 23px;
left: 27px;
}
<div class="container">
<div id="mydiv">
</div>
<div class="player_controls">
<a class="right carousel-control" href="#" data-slide="next"><span class="glyphicon glyphicon-chevron-right">Some icon</span>Some text</a>
</div>
</div>
相关文章