html – 隐藏在CSS边框形状后面的文本

我正在尝试使用背景颜色创建一个围绕文本的h1样式.

    h1.skew {
        padding-left: 10px;
        text-decoration: none;
        color: white;
        font-weight: bold;
        display: inline-block;
        border-right: 50px solid transparent;
        border-top: 50px solid #4c4c4c;
        height: 0;
        line-height: 50px;
    }
    <h1 class=skew>HELLO WORLD

https://jsfiddle.net/zo32q98n/

此时,文本显示在背景下方.如何使文本显示在棕色背景中?

最佳答案

由于边框高度为50px,因此您可以在内部插入相同数量的负边距:

h1.skew::before {
  content: '';
  display: block;
  margin-top: -50px;
}
body {
  background: #ddd;
}
h1.skew {
  padding-left: 10px;
  text-decoration: none;
  color: white;
  font-weight: bold;
  display: inline-block;
  border-right: 50px solid transparent;
  border-top: 50px solid #4c4c4c;
  height: 0;
  line-height: 50px;
}
h1.skew::before {
  content: '';
  display: block;
  margin-top: -50px;
}
<h1 class=skew>HELLO WORLD

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些