css - 旋转图像后有太多空白

标签 css photo

我有一个问题。旋转图像后如何去除空白?

Photo with problem

enter image description here

代码:

.img-box{
  position:relative;
  width: 100%;
  border: 4px solid #333;
}

.img-box img{
  transform: rotate(44deg);
  width: 70%;
}
<div class="img-box">
  <img src="https://publicdomainvectors.org/photos/mystica-LightSaber-Fantasy-2.png" >
</div>

最佳答案

transform: rotate 仅在视觉上旋转图像,它在 HTML 中仍占用相同的空间。解决此问题的最简单方法是使图像 position: absolute; 并根据需要定位它。

.img-box{
  position:relative;
  width: 100%;
  border: 4px solid #333;
  position: relative;
  height: 40px;
}

.img-box img{
  transform: rotate(44deg) translateY(-50%);
  width: 70%;
  position: absolute;
  top: -40px;
}
<div class="img-box">
  <img src="https://publicdomainvectors.org/photos/mystica-LightSaber-Fantasy-2.png" >
</div>

关于css - 旋转图像后有太多空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022651/

相关文章:

html - CSS,使用显示 :table with before pseudo element

html - 我需要使用 css 隐藏树中的最后一个节点

flutter - 如何添加可拖动的 "textfield"以在 flutter 的图像上添加文本?

javascript - HTML:嵌入自动启动的循环幻灯片放映

android - 在三星 Galaxy s4 mini 上打开相机时 Android 应用程序崩溃

jquery - 如何在 <ul> 中的一个轴上对齐 <li> 元素?

CSS:何时使用 right: 0px 何时使用 float: 对吗?

jquery - Bootstrap datetimepicker 出现在窗口的左上角

ios - 创建收集的图像数组

objective-c - Objective C 反锐化蒙版滤镜 Xcode