html - 调整大小时裁剪 CSS 形状的两侧

标签 html css

我有这个三 Angular 形:

enter image description here

当浏览器变小时,它会以这种方式裁剪:

enter image description here

我希望它从左右两侧裁剪,这样文本仍然可见。

标记:

echo '<div class="triangle"><p class="season">SEASON '.substr($patch_array[$x][0],0,1).'</p></div>';

CSS:

.season{
    font-size: 16px;
    text-align: center;
    top: -35px;
    left: -60px;
    position: relative;
    width: 113px;
    margin: 0px;
    padding: 0px;
    color: white;
}
.triangle{
    width: 0;
    height: 0;
    clear: both;
    margin-left: auto;
    margin-right: auto;
    border-left: 300px solid transparent;
    border-right: 300px solid transparent;
    border-top: 45px solid #6699ff;
}

最佳答案

思路是将容器设置为相对位置,然后用伪元素绘制图形,图形和文字都设置为绝对位置,始终居中。

还做了一些小改进 - 将左右边框样式更改为 outset,它可以使线条在 Firefox 上看起来更平滑。

试试这个演示,调整输出框架的大小,看看形状和文本如何始终保持在中心。

JsFiddle Demo

.triangle {
    text-align: center;
    position: relative;
}
.triangle:before {
    width: 0;
    height: 0;
    border-left: 300px outset transparent;
    border-right: 300px outset transparent;
    border-top: 45px solid #6699ff;
    content: "";
    display: inline-block;
    position: absolute;
    left: 50%;
    margin-left: -300px;
}
.season {
    position: absolute;
    width: 100%;
    text-align: center;
    color: white;
    margin: 10px 0;
}
<div class="triangle">
    <p class="season">Hello World</p>
</div>

关于html - 调整大小时裁剪 CSS 形状的两侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30685596/

相关文章:

html - 如何在 Foundation 5 中制作全 Angular 子菜单

html - CSS 背景位置不会居中

javascript - 如何在javascript中定位外部div

c# - 具有最小宽度和水平滚动条的 ASP.NET ListBox

css - 将 css 文本转换为 : uppercase and capitalize work on any language without changing word meaning

javascript - 跳到 JSON 数组中的下一个对象并填充输入框

javascript - 使div在特定位置可拖动

jquery Iphone itunes 滚动效果

html - CSS - 元素有背景颜色,但也是透明的

html - 如何在div中设置背景