I'm currently trying to write words over a triangle-shaped div that I created using CSS.
我正在尝试在我使用CSS创建的三角形div上写单词。
Here:
.triangle-topright {
width: 0;
height: 0;
border-top: 100px solid gray;
border-left: 100px solid transparent;
}
<div class="triangle-topright">view</div>
As you can see, view is misplaced as I want it to be inside the shape. I also want it to rotate so It's nice for the eye. I know that text rotation goes something like this (example):
正如您所看到的,视图是错误的,因为我希望它在形状内部。我也希望它旋转所以这对眼睛很好。我知道文本轮换就像这样(例子):
-moz-transform: rotate(-90deg);
Can I achieve the goal using CSS or do I need to add some JS?
我可以使用CSS实现目标还是需要添加一些JS?
Thanks a lot!
非常感谢!
3 个解决方案
#1
4
Use pseudo elements
使用伪元素
DEMO - 1
演示 - 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>
DEMO - 2
演示 - 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>
DEMO - 3
演示 - 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>
DEMO - 4
演示 - 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>
#2
2
Some of the answers above achieve the same already. Here is my JSfiddle you can play around with.
上面的一些答案已经达到了同样的效果。这是我可以玩的JSfiddle。
.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;
}
And the html
和HTML
<div class="container">
<div class="triangle"></div>
<div class="content">hello</div>
</div>
#3
1
Here's an example of doing it with just CSS.
这是一个用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;
}
#1
4
Use pseudo elements
使用伪元素
DEMO - 1
演示 - 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>
DEMO - 2
演示 - 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>
DEMO - 3
演示 - 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>
DEMO - 4
演示 - 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>
#2
2
Some of the answers above achieve the same already. Here is my JSfiddle you can play around with.
上面的一些答案已经达到了同样的效果。这是我可以玩的JSfiddle。
.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;
}
And the html
和HTML
<div class="container">
<div class="triangle"></div>
<div class="content">hello</div>
</div>
#3
1
Here's an example of doing it with just CSS.
这是一个用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;
}