gpt4 book ai didi

html - 我怎么能做一个斜方肌div

转载 作者:行者123 更新时间:2023-12-05 03:02:56 27 4
gpt4 key购买 nike

如何使用bootstrap框架用HTML、CSS做出下图的效果?

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

Trapezius div

my goal

最佳答案

您可以通过在 CSS 中绘制一个形状来做到这一点。

enter image description here

您可以在 CSS 中通过调整零宽度元素的不同边框(上、右、左下)来绘制这样的三 Angular 形。

示例: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 作为三 Angular 形的不同答案,所以您在这里:

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

关于html - 我怎么能做一个斜方肌div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54295878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com