css - 旋转后的 div 超出其父 div

标签 css rotatetransform

我想在另一个 div 中旋转一个 div,但旋转后的 div 出现在另一个 div 之上。我附上一张图片以便更好地理解它。 This is what I have

And this is what I want

这是我的代码:

.offer{
    display: block;
    color: #ffffff;
    text-align: center;
    padding: 5px 0;
    position: absolute;
    top: 23px;
    z-index: 100;
    left: -33px;
    width: 140px;
    box-shadow: 0 1px 10px #666;
    -ms-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
    -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(SizingMethod='auto expand', M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476)";
    background: -moz-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #1e5799), color-stop(1%, #e09013), color-stop(100%, #e5b300));
    background: -webkit-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: -o-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: -ms-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: linear-gradient(to bottom, #1e5799 0%, #e09013 1%, #e5b300 100%);
}
.content{
    margin:0 auto;
    top:50px;
    position: relative;
    width: 20%;
    height: 200px;
    background-color: lightgray;
}
<div class="content">
  <div class="offer">Offer</div>
</div>

最佳答案

只需将 overflow:hidden 添加到您的 .content

.offer{
    display: block;
    color: #ffffff;
    text-align: center;
    padding: 5px 0;
    position: absolute;
    top: 23px;
    z-index: 100;
    left: -33px;
    width: 140px;
    box-shadow: 0 1px 10px #666;
    -ms-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
    -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(SizingMethod='auto expand', M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476)";
    background: -moz-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #1e5799), color-stop(1%, #e09013), color-stop(100%, #e5b300));
    background: -webkit-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: -o-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: -ms-linear-gradient(top, #1e5799 0%, #e09013 1%, #e5b300 100%);
    background: linear-gradient(to bottom, #1e5799 0%, #e09013 1%, #e5b300 100%);
}
.content{
    margin:0 auto;
    top:50px;
    position: relative;
    width: 20%;
    height: 200px;
    background-color: lightgray;
    overflow: hidden;
}
<div class="content">
  <div class="offer">Offer</div>
</div>

关于css - 旋转后的 div 超出其父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40804881/

相关文章:

javascript - void element.offsetWidth 的用途是什么?

kinect - 使用 Kinect 进行图像处理

javascript - 旋转图像 "overlaps"其他html元素

xcode - 进度 View xcode的高度

css - 如果没有元素宽度超过 960px 容器,为什么会出现水平滚动条?

css - 当输入字段仅以 css 为焦点时切换标签样式

html - 如何在表格列标题处垂直旋转文本

Java图形角度文本

php - 如何将输入的文本转换为图像?

php - Firebug 显示位于 "wp-login.php #3 (line 25)"中的 CSS 源代码。找不到这个文件