使用 css、css-doodle 或 bootstrap 创建图表
Create chart using css, css-doodle, or bootstrap
我希望使用 CSS 生成 12 宫图表的矩形形状。我能使用的最好的是下面的 CSS-涂鸦代码;即使它无处不完美。
如何实现下面的示例图像?该结构必须是单一结构,我想为所有房屋添加文本。
<css-doodle >
:doodle {
overflow: hidden;
height:14em; width:16em;
@grid: 4;
}
:container {
grid-gap: 1px;
transform: rotate(45deg) scale(1.5);
}
background: #d0262e;
</css-doodle>
您可以使用此代码实现对角线
.childDiv {
border:1px solid gray;
width:100px;
height:100px;
position:relative;
}
.childDiv:after {
position:absolute;
content:"";
border-top:1px solid black;
width:141px;
transform: rotate(45deg);
transform-origin: 0% 0%;
}
这将为您提供从左上角到右下角的对角线。您可以将其进一步旋转 90° 以获得从右上角到左下角的对角线。
width:141px
是由于毕达哥拉斯定理。我把 sides 取为 100px,因此,斜边(在我们的例子中是对角线)必须是 100√2,大约是 141.
您应该 divide 您的占星图分为 4 divs,如下所示:
您可以将上述代码应用于这 4 个 div(标记为蓝色),并根据需要获取对角线。此外,您需要删除某些 div 的某些边框。看到这个:
要让 div 组成一个正方形,您总共需要 6 个 div。其中 4 个将是上图中标记的 divs,2 个将是父级 divs,每个 divs 将包含 2 个 divs。
每个父项 div 将在每一行中保留 2 divs。将代码视为:
<div id='parentDiv1'>
<div class='div1 childDiv'></div>
<div class='div2 childDiv'></div>
</div>
<div id='parentDiv2'>
<div class='div1 childDiv'></div>
<div class='div2 childDiv'></div>
</div>
在您的 CSS 文件中,您应该添加以下代码:
#parentDiv1, #parentDiv2 {
width: 100px;
height: 100px;
float: left;
}
.div1 {
float: left;
}
.div2 {
float: left;
}
这是一个包含一个元素和一些背景技巧的想法。它也是响应式的,您可以调整元素的大小并且结构将保持不变。我还考虑过 order
来正确放置元素
.box {
width:280px;
height:180px;
border:3px solid;
display:flex;
flex-wrap:wrap;
background:
linear-gradient(to top right,transparent calc(50% - 2px), #000 calc(50% - 1px) calc(50% + 1px),transparent calc(50% + 2px)),
linear-gradient(to top left ,transparent calc(50% - 2px), #000 calc(50% - 1px) calc(50% + 1px),transparent calc(50% + 2px));
background-size:50% 50%;
counter-reset:num;
overflow:hidden;
resize:both;
}
.box span {
flex-grow:1;
flex-basis:50%;
height:25%;
display:flex;
align-items:center;
justify-content:center;
}
.box span:nth-child(3),
.box span:nth-child(5),
.box span:nth-child(9),
.box span:nth-child(11) {
flex-basis:25%;
}
.box span:nth-child(2),
.box span:nth-child(6),
.box span:nth-child(8),
.box span:nth-child(12) {
height:12.5%;
}
span:before {
content:counter(num);
counter-increment:num;
}
<div class="box">
<span style="order:4"></span>
<span style="order:1"></span>
<span style="order:3"></span>
<span style="order:6"></span>
<span style="order:8"></span>
<span style="order:11"></span>
<span style="order:9"></span>
<span style="order:12"></span>
<span style="order:10"></span>
<span style="order:7"></span>
<span style="order:5"></span>
<span style="order:2"></span>
</div>
你也可以玩 mask 和位置 :
* {
box-sizing: border-box;
}
article {
min-width: 30vmax;
min-height: 20vmax;
height: 80vh;
width: 80vw;
border: solid;
counter-reset: divs;
overflow: hidden;
background: black;
}
div {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%); /* make your diamond shape*/
background: lightblue;
counter-increment: divs;
height: 50%;
position: relative;
transform: scale(0.99); /* will show parent's bacground alike borders*/
}
div:nth-child(odd) {
background: lightgreen;
}
div:before {
content: counter(divs);
margin: auto;
}
div:nth-child(2) {
order: -1;
top: -25%;
left: 0;
padding-top: 10%;
}
div:nth-child(1) {
left: -25%;
}
div:nth-child(3) {
left: -25%;
top: -50%;
padding-left: 15%;
}
div:nth-child(4) {
left: -50%;
top: -25%
}
div:nth-child(5) {
left: -25%;
top: -50%;
padding-left: 15%
}
div:nth-child(6) {
left: -50%;
top: -25%;
padding-bottom: 10%;
}
div:nth-child(7) {
left: 25%;
top: -100%;
}
div:nth-child(8) {
left: 0%;
top: -75%;
padding-bottom: 10%;
}
div:nth-child(9) {
left: 75%;
top: -150%;
padding-right: 15%;
}
div:nth-child(10) {
left: 0%;
top: -175%;
}
div:nth-child(11) {
left: 75%;
top: -250%;
padding-right: 15%;
}
div:nth-child(12) {
left: 0;
top: -275%;
padding-top: 10%;
}
/* swap position */
article:hover {
background: tomato;
}
article:hover div:nth-child(2) {
left: 50%;
}
article:hover div:nth-child(12) {
left: -50%;
}
article:hover div:nth-child(3) {
left: 75%;
padding: 0 10% 0 0;
}
article:hover div:nth-child(11) {
left: -25%;
padding: 0 0 0 10%;
}
article:hover div:nth-child(4) {
left: 0%;
}
article:hover div:nth-child(10) {
left: -50%;
}
article:hover div:nth-child(5) {
left: 75%;
padding: 0 10% 0 0;
}
article:hover div:nth-child(9) {
left: -25%;
padding: 0 0 0 10%;
}
article:hover div:nth-child(6) {
left: 0%;
}
article:hover div:nth-child(8) {
left: -50%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<article class="row flex-wrap m-auto">
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
</article>
我希望使用 CSS 生成 12 宫图表的矩形形状。我能使用的最好的是下面的 CSS-涂鸦代码;即使它无处不完美。 如何实现下面的示例图像?该结构必须是单一结构,我想为所有房屋添加文本。
<css-doodle >
:doodle {
overflow: hidden;
height:14em; width:16em;
@grid: 4;
}
:container {
grid-gap: 1px;
transform: rotate(45deg) scale(1.5);
}
background: #d0262e;
</css-doodle>
您可以使用此代码实现对角线
.childDiv {
border:1px solid gray;
width:100px;
height:100px;
position:relative;
}
.childDiv:after {
position:absolute;
content:"";
border-top:1px solid black;
width:141px;
transform: rotate(45deg);
transform-origin: 0% 0%;
}
这将为您提供从左上角到右下角的对角线。您可以将其进一步旋转 90° 以获得从右上角到左下角的对角线。
width:141px
是由于毕达哥拉斯定理。我把 sides 取为 100px,因此,斜边(在我们的例子中是对角线)必须是 100√2,大约是 141.
您应该 divide 您的占星图分为 4 divs,如下所示:
您可以将上述代码应用于这 4 个 div(标记为蓝色),并根据需要获取对角线。此外,您需要删除某些 div 的某些边框。看到这个:
要让 div 组成一个正方形,您总共需要 6 个 div。其中 4 个将是上图中标记的 divs,2 个将是父级 divs,每个 divs 将包含 2 个 divs。
每个父项 div 将在每一行中保留 2 divs。将代码视为:
<div id='parentDiv1'>
<div class='div1 childDiv'></div>
<div class='div2 childDiv'></div>
</div>
<div id='parentDiv2'>
<div class='div1 childDiv'></div>
<div class='div2 childDiv'></div>
</div>
在您的 CSS 文件中,您应该添加以下代码:
#parentDiv1, #parentDiv2 {
width: 100px;
height: 100px;
float: left;
}
.div1 {
float: left;
}
.div2 {
float: left;
}
这是一个包含一个元素和一些背景技巧的想法。它也是响应式的,您可以调整元素的大小并且结构将保持不变。我还考虑过 order
来正确放置元素
.box {
width:280px;
height:180px;
border:3px solid;
display:flex;
flex-wrap:wrap;
background:
linear-gradient(to top right,transparent calc(50% - 2px), #000 calc(50% - 1px) calc(50% + 1px),transparent calc(50% + 2px)),
linear-gradient(to top left ,transparent calc(50% - 2px), #000 calc(50% - 1px) calc(50% + 1px),transparent calc(50% + 2px));
background-size:50% 50%;
counter-reset:num;
overflow:hidden;
resize:both;
}
.box span {
flex-grow:1;
flex-basis:50%;
height:25%;
display:flex;
align-items:center;
justify-content:center;
}
.box span:nth-child(3),
.box span:nth-child(5),
.box span:nth-child(9),
.box span:nth-child(11) {
flex-basis:25%;
}
.box span:nth-child(2),
.box span:nth-child(6),
.box span:nth-child(8),
.box span:nth-child(12) {
height:12.5%;
}
span:before {
content:counter(num);
counter-increment:num;
}
<div class="box">
<span style="order:4"></span>
<span style="order:1"></span>
<span style="order:3"></span>
<span style="order:6"></span>
<span style="order:8"></span>
<span style="order:11"></span>
<span style="order:9"></span>
<span style="order:12"></span>
<span style="order:10"></span>
<span style="order:7"></span>
<span style="order:5"></span>
<span style="order:2"></span>
</div>
你也可以玩 mask 和位置 :
* {
box-sizing: border-box;
}
article {
min-width: 30vmax;
min-height: 20vmax;
height: 80vh;
width: 80vw;
border: solid;
counter-reset: divs;
overflow: hidden;
background: black;
}
div {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%); /* make your diamond shape*/
background: lightblue;
counter-increment: divs;
height: 50%;
position: relative;
transform: scale(0.99); /* will show parent's bacground alike borders*/
}
div:nth-child(odd) {
background: lightgreen;
}
div:before {
content: counter(divs);
margin: auto;
}
div:nth-child(2) {
order: -1;
top: -25%;
left: 0;
padding-top: 10%;
}
div:nth-child(1) {
left: -25%;
}
div:nth-child(3) {
left: -25%;
top: -50%;
padding-left: 15%;
}
div:nth-child(4) {
left: -50%;
top: -25%
}
div:nth-child(5) {
left: -25%;
top: -50%;
padding-left: 15%
}
div:nth-child(6) {
left: -50%;
top: -25%;
padding-bottom: 10%;
}
div:nth-child(7) {
left: 25%;
top: -100%;
}
div:nth-child(8) {
left: 0%;
top: -75%;
padding-bottom: 10%;
}
div:nth-child(9) {
left: 75%;
top: -150%;
padding-right: 15%;
}
div:nth-child(10) {
left: 0%;
top: -175%;
}
div:nth-child(11) {
left: 75%;
top: -250%;
padding-right: 15%;
}
div:nth-child(12) {
left: 0;
top: -275%;
padding-top: 10%;
}
/* swap position */
article:hover {
background: tomato;
}
article:hover div:nth-child(2) {
left: 50%;
}
article:hover div:nth-child(12) {
left: -50%;
}
article:hover div:nth-child(3) {
left: 75%;
padding: 0 10% 0 0;
}
article:hover div:nth-child(11) {
left: -25%;
padding: 0 0 0 10%;
}
article:hover div:nth-child(4) {
left: 0%;
}
article:hover div:nth-child(10) {
left: -50%;
}
article:hover div:nth-child(5) {
left: 75%;
padding: 0 10% 0 0;
}
article:hover div:nth-child(9) {
left: -25%;
padding: 0 0 0 10%;
}
article:hover div:nth-child(6) {
left: 0%;
}
article:hover div:nth-child(8) {
left: -50%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<article class="row flex-wrap m-auto">
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
<div class="col-6 d-flex"> </div>
</article>