2

我目前正在尝试在使用 CSS 创建的三角形 div 上写字。

这里:

.triangle-topright {
  width: 0;
  height: 0;
  border-top: 100px solid gray;
  border-left: 100px solid transparent;
}
<div class="triangle-topright">view</div>

如您所见,视图放错了位置,因为我希望它位于形状内。我也希望它旋转,这样对眼睛很好。我知道文本旋转是这样的(示例):

-moz-transform: rotate(-90deg);

我可以使用 CSS 实现目标还是需要添加一些 JS?

非常感谢!

4

3 回答 3

4

使用伪元素

演示 - 1

.triangle-topright {
  width: 100px;
  height: 100px;
  transform: rotateZ(45deg);
  margin: 20px;
  position: relative;
  z-index: 1;
  text-align: center;
  line-height: 64px;
}

.triangle-topright:before {
  content: '';
  z-index: -1;
  position: absolute;
  top:0;
  left: 0;
  width: 0;
  height: 0;
  transform: rotateZ(-45deg);
  border-right: 100px solid gray;
  border-bottom: 100px solid transparent;
}
<div class="triangle-topright">view</div>

演示 - 2

.triangle-topright {
  width: 100px;
  height: 100px;
  margin: 20px;
  position: relative;
  z-index: 1;
  text-align: center
}

.triangle-topright:before {
  content: '';
  z-index: -1;
  position: absolute;
  top:0;
  left: 0;
  width: 0;
  height: 0;
  border-top: 100px solid gray;
  border-left: 100px solid transparent;
}
<div class="triangle-topright">view</div>

演示 - 3

.triangle-topright {
  width: 100px;
  height: 100px;
  transform: rotateZ(90deg);
  margin: 20px;
  position: relative;
  z-index: 1;
  text-align: center
}

.triangle-topright:before {
  content: '';
  z-index: -1;
  position: absolute;
  top:0;
  left: 0;
  width: 0;
  height: 0;
  border-top: 100px solid gray;
  border-left: 100px solid transparent;
}
<div class="triangle-topright">view</div>

演示 - 4

.triangle-topright {
  width: 100px;
  height: 100px;
  transform: rotateZ(90deg);
  margin: 20px;
  position: relative;
  z-index: 1;
  text-align: center
}

.triangle-topright:before {
  content: '';
  z-index: -1;
  position: absolute;
  top:0;
  left: 0;
  width: 0;
  height: 0;
  border-top: 100px solid gray;
  border-right: 100px solid transparent;
}
<div class="triangle-topright">view</div>

于 2015-02-04T20:29:42.707 回答
2

上面的一些答案已经达到了同样的效果。这是我的 JSfiddle,你可以玩弄。

http://jsfiddle.net/tmtg1oc8/

.container {
    position: relative;
}

.triangle {
    position: absolute;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
    border-top: 100px solid gray;
    border-left: 100px solid transparent;
    z-index: -1;
}

.content {
    padding: 40px;
}

和 html

<div class="container">
    <div class="triangle"></div>
    <div class="content">hello</div>
</div>
于 2015-02-04T20:32:27.630 回答
1

这是一个仅使用 CSS 的示例

HTML

<section class="page_block">
    <div class="row">
        <div class="col-md-1">
            <div id="header">
                <h1 class="verticaltext">
                    Vert Text
                </h1>
            </div>
        </div>
        <div class="col-md-11">
            <p>Column Somethin?</p>
        </div>
    </div>
</section>

CSS

.page_block {
    margin:0px;
    padding:10px;
}
#header { 
    position: relative; 
}
.verticaltext {
    transform: rotate(45deg);
    transform-origin: right, top;
    -ms-transform: rotate(45deg);
    -ms-transform-origin:right, top;
    -webkit-transform: rotate(45deg);
    -webkit-transform-origin:right, top;
    position: absolute; 
    color: #ed217c;
}
于 2015-02-04T20:26:44.197 回答