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



我目前正试图在使用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?

非常感谢!

使用伪元素

演示-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>

上面的一些答案已经达到了相同的效果。这是我的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>

这里有一个只使用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;
}

相关内容

  • 没有找到相关文章

最新更新