Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a triangular background with CSS

I have following html block

<div class=""header>
    <i class="icon-star"></i>
    <h1>some text goes here</h1>
</div>

and these css styles

h1 {
    display: inline-block;
}
i {
    display: inline-block;
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 200px 0 0 200px;
    border-color: transparent transparent transparent red;

}

and also i am using fontawesome icon fonts. What i am trying to achieve is thisenter image description here

This is what i have tried jsfiddle . i completely failed to do this. Could someone please help me?

like image 458
It worked yesterday. Avatar asked Oct 21 '22 01:10

It worked yesterday.


2 Answers

you have used h1 in your html and h2 in your css!

this may help

h1 {
    margin: -130px 0 0 200px;
}
i {
    display: inline-block;
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 200px 0 0 200px;
    border-color: transparent transparent transparent red;
}

fiddle

like image 108
Milad Hosseinpanahi Avatar answered Oct 24 '22 05:10

Milad Hosseinpanahi


This JFiddle might get you a little closer. Please note that this requires your triangle to have a fixed height, so I could use a line-height:

#text {
float: right;
line-height: 200px;
vertical-align: middle;
margin-right: 150px;
font-weight: bold;
font-size: 20px;

Do you want to create the star in the triangle using CSS as well?

like image 35
Benjamin Avatar answered Oct 24 '22 05:10

Benjamin