Mohammad Aghayari
Mohammad Aghayari

Reputation: 1160

how to make triangle div?

I want to make div like triangle. I describe clear my question by images in blow.

my code is:

<div class="rest_pack">
                        <img width="100%" src="<?= Yii::$app->request->baseUrl . '/files/upload/3.png' ?>">
                        <div class="row side_info">
                            <div class="top">
                                ساندویچ مخصوص
                            </div>
                            <div class="bottom">
                                5,500 
                                تومان
                            </div>
                        </div>
                    </div>

css:

.rest_pack .side_info{
    position: absolute;
    width: 100%;
    background-color: #FFF;
    top: 100px;
    opacity: 0.8;

}
.rest_pack .side_info .top{
    text-align: center;
    font-weight: bold;
    font-size: 17px;
    color: #3131AA;
    padding-top:5px;
}
.rest_pack .side_info .bottom{
    text-align: center;
    font-weight: bold;
    font-size: 16px;
    color: #F63440;
    padding-top:5px;
}

The result is: enter image description here

but I want something like this. I want to make red DIV.

enter image description here

Upvotes: 2

Views: 1861

Answers (3)

Shady Alset
Shady Alset

Reputation: 5714

You can do it using :after selector and border-left, border-bottom to adjust triangle as you need.

like this:

.title{
  position: absolute;
  padding:25px;
  bottom:0;
  right:0;
  z-index:1;
}
div {
  position: relative;
  width: 500px;
  height: 300px;
  background-image: url("https://i.sstatic.net/ys1Jo.png");
}
div:after {
  position: absolute;
  content: '';
  width: 0;
  height: 0;
  bottom: 0;
  border-left: 500px solid transparent;
  border-bottom: 130px solid rgb(0, 114, 255);
  -moz-transform: scale(0.999);
  -webkit-backface-visibility: hidden;
}
<div class="test">
    <span class="title">Enter Text Here</span>
</div>

Upvotes: 1

lomboboo
lomboboo

Reputation: 1233

Got it from CSS Tricks https://css-tricks.com/snippets/css/css-triangle/

<div class="arrow-up"></div>
<div class="arrow-down"></div>
<div class="arrow-left"></div>
<div class="arrow-right"></div>

CSS goes here :

.arrow-up {
width: 0; 
height: 0; 
border-left: 5px solid transparent;
border-right: 5px solid transparent;

border-bottom: 5px solid black;
}

.arrow-down {
width: 0; 
height: 0; 
border-left: 20px solid transparent;
border-right: 20px solid transparent;

border-top: 20px solid #f00;
}

.arrow-right {
width: 0; 
height: 0; 
border-top: 60px solid transparent;
border-bottom: 60px solid transparent;

border-left: 60px solid green;
}

.arrow-left {
width: 0; 
height: 0; 
border-top: 10px solid transparent;
border-bottom: 10px solid transparent; 

border-right:10px solid blue; 
}

Just change values and you get whatever Triangle you want

Upvotes: 2

Dhaval Patel
Dhaval Patel

Reputation: 1096

Please try below code

HTML

<div class="main">
  <a rel="nofollow" href="https://i.sstatic.net/ys1Jo.png"><img alt="enter image description here" src="https://i.sstatic.net/ys1Jo.png">   </a>
</div>

CSS

.main {  border: 1px solid red; border-radius: 5px 0 5px 5px; }

Upvotes: 0

Related Questions