我怎样才能使斜方肌格

时间:2019-01-21 18:29:22

标签: html css bootstrap-4

如何使用bootstrap框架在HTML,CSS下效果图?

我需要两个相邻的具有梯形形状的div(或由对角线分隔)。两者都需要有边框。

Trapezius div

my goal

2 个答案:

答案 0 :(得分:3)

您可以通过在CSS中绘制形状来实现。

enter image description here

您可以通过使用宽度为零的元素的不同边框(上,右,左下)来在CSS中绘制这样的三角形。

示例:https://css-tricks.com/snippets/css/css-triangle/

在下面的示例中,我使用伪元素:after来实现此效果:

/* 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>

更新

但是正如您在评论中指出的那样,您想要一个使用div2作为三角形的不同答案,所以您在这里:

/* 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>

更新2

您在评论中显示的图片也包含真实边框。这需要改变方法。新方法仍然使用:before,但为其添加了边框,并将其旋转了45度。

该想法基于以下示例:https://kilianvalkhof.com/2017/design/sloped-edges-with-consistent-angle-in-css/

想像一下:

enter image description here

代码如下:

/* 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>

答案 1 :(得分:0)

只需使用 border-right ,如以下代码段所示,并请参见结果:

.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>