如何使用CSS在三角形div上书写?

时间:2015-02-04 20:22:27

标签: javascript html css css-shapes text-rotating

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

下面:



.triangle-topright {
  width: 0;
  height: 0;
  border-top: 100px solid gray;
  border-left: 100px solid transparent;
}

<div class="triangle-topright">view</div>
&#13;
&#13;
&#13;

正如您所看到的,视图错位,因为我希望它在形状内部。我也想让它旋转,这对眼睛来说很好看。我知道文本轮换就像这样(例子):

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

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

非常感谢!

3 个答案:

答案 0 :(得分:4)

使用伪元素

DEMO - 1

&#13;
&#13;
.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;
}
&#13;
<div class="triangle-topright">view</div>
&#13;
&#13;
&#13;

DEMO - 2

&#13;
&#13;
.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;
}
&#13;
<div class="triangle-topright">view</div>
&#13;
&#13;
&#13;

DEMO - 3

&#13;
&#13;
.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;
}
&#13;
<div class="triangle-topright">view</div>
&#13;
&#13;
&#13;

DEMO - 4

&#13;
&#13;
.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;
}
&#13;
<div class="triangle-topright">view</div>
&#13;
&#13;
&#13;

答案 1 :(得分: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>

答案 2 :(得分:1)

这是an example of doing it with just 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;
}