简体   繁体   English

是否可以仅使用 CSS transform 属性来调整元素的 width 属性而不拉伸它?

[英]Is it possible to use CSS transform property only to adjust an element's width property without stretching it?

What I want to achieve is to use only transform on #b:hover to implement the very behavior of #a:hover , here is the code:我想要实现的是仅在#b:hover上使用transform来实现#a:hover的行为,代码如下:

 div#a, div#b { width:100px; height:30px; background:#ccc; } div#a:hover { width:150px; } div#b:hover { transform:scaleX(1.5); }
 <div id="a">Test1</div> <div id="b">Test2</div>

There are now two problems:现在有两个问题:

  1. The text is stretched.文本被拉伸。
  2. The div is not expanding from the left point but from the center so it breaks out its container on its left side. div不是从左侧而是从中心展开,因此它在左侧突破了它的容器。

Is that possible?那可能吗?

If you can consider and extra wrapper you can do it如果你可以考虑和额外的包装你可以做到

 div#a, div#b { width:100px; height:30px; background:#ccc; } div#a:hover { width:150px; } div#b:hover { transform: scaleX(1.5); transform-origin: left; /* change the origin to be left */ } div#b:hover > div { transform: scaleX(calc(1/1.5)); /* the opposite transform to keep the text */ transform-origin: inherit; }
 <div id="a">Test1</div> <div id="b"><div>Test2</div></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM