我使用不同的div标签通过CSS3绘制了一个云,我试图向整个形状添加边框,但是由于每个形状都有自己的边界,我遇到了麻烦,如何将边框应用于整个云?

HTML:

<div id="cloud">
  <div id="bottom_c"></div>
  <div id="right_c"></div>
  <div id="left_c"></div>
</div>

CSS:
* {
  margin: 0;
  padding: 0;
  border: 0;
}

body{
  background-color: #4ca3ff;
}
#cloud {
  position: absolute;

}
#bottom_c {
  position: relative; top: 200px; left: 500px;
  width: 350px;
  height: 150px;
  background-color: #fff;
  border-radius: 100px;
  border: solid 5px black;
  z-index: 100;
}
#right_c{
  position: absolute; top: 140px; left: 640px;
  width: 150px;
  height: 150px;
  border-radius: 100%;
  background-color: #fff;
  border: solid 5px black;
}
#left_c{
  position: absolute; top: 170px; left: 550px;
  width: 100px;
  height: 100px;
  border-radius: 100%;
  background-color: #fff;
  border: solid 5px black;
}

图片:

最佳答案

您可以执行此操作而无需任何其他元素。只需使用大小和圆形与顶部云气泡相同的::before::after伪元素即可。 z-index将所有内容保留在正确的层中。

演示: CSS将边框应用于云形状吗?-LMLPHP

输出:

CSS:

body{
    background-color: #4ca3ff;
}

#cloud {
    height: 230px;
    margin: 40px;
    position: relative;
    width: 400px;
}

#cloud div {
    border: solid 5px black;
}

#bottom_c {
    background-color: #fff;
    border-radius: 100px;
    height: 150px;
    position: absolute;
    top: 100px;
    width: 350px;
    z-index: 0;
}

#right_c{
    background-color: #fff;
    border-radius: 100%;
    height: 150px;
    left: 140px;
    position: absolute;
    top: 40px;
    width: 150px;
    z-index: -1;
}

#left_c{
    background-color: #fff;
    border-radius: 100%;
    height: 100px;
    left: 50px;
    position: absolute;
    top: 70px;
    width: 100px;
    z-index: -1;
}

#cloud::before {
    background-color: white;
    border-radius: 50%;
    content: '';
    height: 100px;
    left: 55px;
    position: absolute;
    top: 75px;
    width: 100px;
    z-index: 1;
}

#cloud::after {
    position: absolute; top: 45px; left: 145px;
    background-color: white;
    border-radius: 50%;
    content: '';
    width: 150px;
    height: 150px;
    z-index: 1;
}

HTML:

<div id="cloud">
  <div id="bottom_c"></div>
  <div id="right_c"></div>
  <div id="left_c"></div>
</div>

10-05 21:04
查看更多