3

How could I make the effect of below picture with HTML, CSS using the the bootstrap framework?

I need two adjacent divs with trapezoid shape (or separated by a diagonal line). Both need to have a border.

Trapezius div

my goal

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Amir Kian
  • 155
  • 1
  • 10
  • Does [Shape with a slanted side (responsive)](https://stackoverflow.com/q/30441122/1016716) help? – Mr Lister Jan 21 '19 at 18:33
  • not actually; both dive with together will make a banner for me – Amir Kian Jan 21 '19 at 18:47
  • 1
    can you be more precise and give more context? actually your code is showing an easy shape with color but your are commenting that you need image and using bootstrap. Can you share more code? – Temani Afif Jan 21 '19 at 19:18
  • @AmirKian, based on the comments and the answer, I edited the question to be more precise. You have a couple of close requests on the question which have been cast due to not having described well what you ask **in the question itself**. In general, the question body should contain most of the required information. Event though your English might not be good, try to be more descriptive, it always helps :) The topic is interesting, and you 've got an excellent answer, but in order to help others most of the issue must be understandable without reading comments ;) – Ivaylo Slavov Jan 21 '19 at 21:38

2 Answers2

4

You can do this by drawing a shape in CSS.

enter image description here

You can draw such a triangle in CSS by playing with different borders (top, right, bottom left) of an element that has zero width.

Example: https://css-tricks.com/snippets/css/css-triangle/

In the example below I use the pseudo element :after for this effect:

/* Apply styles to both DIVs */
.container > div {
  width: 50%;
  float:left;
  font-weight: bold;
  padding-left: 10px;
  /* include padding in the height/width */
  box-sizing: border-box;
  margin: 0;
}

.container {
/* One way to make the DIV height extend to full heihgt of `float:left` DIVs inside it. Not the only way */
  clear: both;
}

.container div:first-child {
  background: #66ff66;
  /* The triangle will be position:absolute, so it requires a `position:relative` parent */
  position: relative;
  /* We are drawing a full rectangle later, so we hide the rest of it */
  overflow: hidden;
}

.container div:last-child {
  background: #ff6666;
}

.container div:first-child:after {
  position: absolute;
  display: block;
  content: ' ';
  padding: inherit;
  box-sizing: border-box;

  /* Change below units (you can use px not just em) 
    to make the line  become at different angles */
  border-top: 1.3em solid transparent;
  border-bottom: 1.3em solid transparent;
  border-right: 1.3em solid #ff6666;

  right: 0;
  top: 0;
}
<div class="container">
 <div>div١</div>
 <div>div٢</div>
</div>

Update

But as you indicated in the comment, you wanted a different answer that uses div2 for the triangle, so here you are:

/* Apply styles to both DIVs */
.container > div {
  width: 50%;
  float:left;
  font-weight: bold;


  /* include padding in the height/width */
  box-sizing: border-box;
  margin: 0;
}

.container {
/* One way to make the DIV height extend to full heihgt of `float:left` DIVs inside it. Not the only way */
  clear: both;
}

.container div:first-child {
  background: #66ff66;
  padding-left: 10px;

}

.container div:last-child {
  background: #ff6666;
  position: relative;
  padding-left: 1.3em;
}

.container div:last-child:before {
  position: absolute;
  content: '';.
  width: 0;
  height: 0;

  box-sizing: border-box;

  /* Change below units (you can use px not just em) 
    to make the line  become at different angles */
  border-top: 1.3em solid #66ff66;
  border-bottom: 1.3em solid transparent;
  border-right: 1.3em solid transparent;

  top: 0;
  left: 0;
}
<div class="container">
 <div>div١</div>
 <div>div٢</div>
</div>

Update 2

The picture you showed in comments also included real borders. This requires changing the approach. The new approach still uses :before, but adds border to it, and rotates it 45 degrees.

The idea is based on an example from: https://kilianvalkhof.com/2017/design/sloped-edges-with-consistent-angle-in-css/

To imagine it:

enter image description here

Here's the code:

/* Apply styles to both DIVs */
.container > div {
  width: 50%;
  float:left;
  font-weight: bold;


  /* include padding in the height/width */
  box-sizing: border-box;
  margin: 0;
}

.container {
/* One way to make the DIV height extend to full heihgt of `float:left` DIVs inside it. Not the only way */
  clear: both;
}

.container div:first-child {
  background: #66ff66;
  padding-left: 10px;
  border: 1px solid;
  border-right: none;
}

/* 
  The following assumes diemnsions 1.3em * 1.3em
  Your real case can change the number
*/

.container div:last-child {
  background: #ff6666;
  position: relative;
  border: 1px solid;
  border-left: none;
  padding-left: calc(1.5 * 1.3em);
  overflow: hidden;
}

.container div:last-child:before {
  position: absolute;
  content: '';
  width: calc(2 * 1.3em);
  height: calc(2 * 1.3em);

  box-sizing: border-box;
  background: #66ff66;

  border: 1px solid ;
  transform:rotate(45deg);

  margin-top: -1.3em;
  margin-left: -1.3em;
  left: 0;
  top: 0;
}
<div class="container">
 <div>div١</div>
 <div>div٢</div>
</div>
Meligy
  • 35,654
  • 11
  • 85
  • 109
  • It has a problem for me, that little triangle must be a part of div2 because I want to use a background image for dive 2 and that little triangle is a part of background image – Amir Kian Jan 21 '19 at 19:14
  • Then do the same thing but revert directions, and instead of `:after` use `before` – Meligy Jan 21 '19 at 19:16
  • to make my means clear, please look at the image below div2 has a background image https://photos.app.goo.gl/xonvUFJ98Goy6Tob7 – Amir Kian Jan 21 '19 at 19:26
  • I updated my answer to make the triangle part of div2. – Meligy Jan 21 '19 at 19:31
  • It works and I now see what you have done. But is there any simpler way and is there any other method? – Amir Kian Jan 21 '19 at 19:49
  • 1
    Simpler? Not sure. Other? Yes. If you need borders like your picture, you'll have to change the approach slightly. I added "Update 2" to address that. – Meligy Jan 21 '19 at 19:50
  • 1
    Also see linked article in Update 2. I hope that one is the final answer to your question. – Meligy Jan 21 '19 at 19:51
  • 1
    And added a picture that explains how the approach in Update 2 works. – Meligy Jan 21 '19 at 19:57
  • It seems like that is the only way and the simplest way;thanks – Amir Kian Jan 21 '19 at 20:02
0

just use border-right like following code snippet and see result :

.parent{
  width: 100%;
  display: flex;
  background-color: #01579b;
}

.div1 {
  width: 30%;
  border-bottom: 100px solid #000;
  border-right: 50px solid transparent;
}

.div2 {
  width: 70%;
  height: 100px;
}
<div class="parent">
  <div class="div1"></div>
  <div class="div2"></div>
</div>
Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36