Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS apply border to a cloud shape?

I drew a cloud via CSS3 using different div tags I am trying to add a border to the whole shape but I am having trouble since every shape get its own border how can I apply a border to the whole cloud?

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;
}

Image:

CSS cloud shape

like image 260
JeeG Avatar asked May 12 '13 15:05

JeeG


People also ask

How do you make a 3D border in CSS?

We can assign a 3D groove border to an element using the following syntax. Syntax: border-style: groove; Approach: In this example, we will give the grooved border to the heading h1.

How do you add a border in SVG?

Draw a <rect> round the image which is fill="none". You can use the stroke of the <rect> as the border. This is the correct answer when it comes to svg:image.

Can you add borders in CSS?

The CSS border properties allow you to specify the style, width, and color of an element's border.


2 Answers

You can do it without any additional elements. Just use the ::before and ::after pseudo-elements with the same size and round shape as the top cloud bubbles. z-index keeps everything in the right layer.

Demo: jsFiddle

Output:

output

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>
like image 66
ThinkingStiff Avatar answered Sep 30 '22 12:09

ThinkingStiff


Thank you for the original solution! I needed to create multiple clouds and dynamically resize and recolor them, so I adapted to original solution as follows:

  1. I made the clouds resizable by using percentages values for the height, width, top and left properties. The .cloud class uses padding-top to adjust the height of the cloud relative to the cloud's width.
  2. I made the :before and :after pseudo-elements divs.
  3. I changed the id selectors to class selectors
  4. And I reorganized the properties so they're easier to read.

I hope this helps someone. Here's the code:

Output

I don't yet have the reputation to post images :/. So here's a link to the output: http://imgur.com/nN9dBiQ

CSS:

.cloud {
    position: relative;
    width: 100%;
    padding-top: 57.5%;
    box-sizing: border-box;
}

.cloud_bottom,
.cloud_left,
.cloud_right {
    border: solid 5px black; 
}

.cloud_bottom,
.cloud_left,
.cloud_right,
.cloud_leftCircle,
.cloud_rightCircle {
    background-color: #fff;
}

.cloud_bottom {
    position: absolute;
    top: 43.48%;
    height: 65.2%;
    width: 87.5%;
    border-radius: 100px;
    z-index: 0;
}

.cloud_left {
    position: absolute;
    top: 30.43%;
    left: 12.5%;
    height: 43.48%;
    width: 25%;
    border-radius: 100%;
    z-index: -1;
}

.cloud_right {
    position: absolute;
    top: 17.39%; 
    left: 35%;
    height: 65.2%;
    width: 37.5%;
    border-radius: 100%;
    z-index: -1;
}

.cloud_leftCircle {
    position: absolute; 
    top: 32.61%;
    left: 13%;
    height: 43.48%;
    width: 25%;
    border-radius: 50%;
    z-index: 1;
}

.cloud_rightCircle {
    position: absolute;
    top: 23.48%;
    left: 35%;
    height: 65.21%;
    width: 37.5%;
    border-radius: 50%;
    z-index: 1;
}

HTML:

<div class="firstCloud cloud">
    <div class="cloud_bottom"></div>
    <div class="cloud_left"></div>
    <div class="cloud_right"></div>
    <div class="cloud_leftCircle"></div>
    <div class="cloud_rightCircle"></div>
</div>

<div class="secondCloud cloud">
    <div class="cloud_bottom"></div>
    <div class="cloud_left"></div>
    <div class="cloud_right"></div>
    <div class="cloud_leftCircle"></div>
    <div class="cloud_rightCircle"></div>
</div>

JavaScript:

function updateCloudColor(cloudElement, color) {
    cloudElement.children().css("background-color", color);
}

$(window).load(function () {
    updateCloudColor($(".firstCloud"), "red");
    updateCloudColor($(".secondCloud"), "blue");
});
like image 26
Ryan James Avatar answered Sep 30 '22 12:09

Ryan James