html - 文本位于 div 元素背景后面

标签 html css

在我有这段代码的那一刻我有点困惑:

HTML:

<div class="ms-layer ms-caption chevron">97.2%</div>

CSS:

.chevron {
  position: relative;
  top: 5px !important;
  text-align: center;
  padding: 10px;
  margin-bottom: 6px;
  height: 200px;
  width: 200px;
  color: red;
  font-size: 6em;
}

.chevron:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 51%;
  background: blue;
  -webkit-transform: skew(0deg, 6deg);
  -moz-transform: skew(0deg, 6deg);
  -ms-transform: skew(0deg, 6deg);
  -o-transform: skew(0deg, 6deg);
  transform: skew(0deg, 6deg);
}
.chevron:after {
  content: '';
  position: relative;
  top: 0;
  right: 0;
  height: 100%;
  width: 50%;
  background: blue;
  -webkit-transform: skew(0deg, -6deg);
  -moz-transform: skew(0deg, -6deg);
  -ms-transform: skew(0deg, -6deg);
  -o-transform: skew(0deg, -6deg);
  transform: skew(0deg, -6deg);
}

现在您可以看到,人字形由两部分组成,前类和后类。

我遇到的问题是我添加到 div 的文本位于人字形后面,但我希望它位于顶部。我以前没有真正使用过 CSS 形状,所以我有点困惑为什么它不起作用。

如您所见,我将 div 位置的一侧设置为绝对位置,将另一侧设置为相对位置,虽然有所不同,但并没有解决问题。

任何帮助都会非常感谢:)

最佳答案

试试这段代码

.chevron:before {
    background: none repeat scroll 0 0 blue;
    content: "";
    height: 100%;
    position: absolute;
    right: 0;
    top: 0;
    transform: skew(0deg, -6deg);
    width: 50%;
    z-index: -1;
}

关于html - 文本位于 div 元素背景后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25118615/

相关文章:

css - 如何修复 WebKit 中的闪烁过渡?

Php变量不保存数据

php - 将表单值提交到数据库、php

html - 将图像添加到流体网格(响应式)布局列会弄乱移动屏幕上的宽度

html - 无法在右侧 float 菜单图标,但左侧仍有图标

asp.net - 如何强制垂直文本为水平文本?

html - iframe 不知何故无法工作(显示为空白页)

html - svg 图像中的跨浏览器嵌入字体

html - 创建调色板,<img> 或 <div> 或 <canvas>?

javascript - 防止对象缩放