I need to create a page something like this. The blue circle is the main circle and green circles should place around the main circle. The green circles count is random (around 0 - 10). All green circles are connected to blue circle with a line.
I know to draw circle in CSS. I need to know,
How to place green circles around the blue circle
How to connect green circles to the blue circle
Is it possible to do with CSS. If not what is the way?
Thank you.
What you will need is a position: relative; container with child elements positioned absolute
Demo
Demo 2 (Using transform)
Explanation: What am doing here is using position: relative; on the parent element which is .ball_wrap, than am using position: absolute; for the child elements AS WELL AS the :after pseudo elements to connect the child elements with the parent. If you are not aware of the pseudo elements than take them as a virtual element, these elements do not exist literally in the DOM but they do render graphically. So am using display: block; as they are inline by default..along with content: "";... Rest, set them accordingly using top, right, bottom and left properties.
.ball_wrap {
position: relative;
margin: 150px;
width: 90px;
}
.green_ball {
background: #00C762;
height: 50px;
width: 50px;
border-radius: 50%;
border: 3px solid #ccc;
position: absolute;
}
.blue_ball {
background: #2F9BC1;
height: 80px;
width: 80px;
border-radius: 50%;
border: 3px solid #ccc;
}
.ball_wrap div:nth-of-type(2) {
top: 20px;
left: -100px;
}
.ball_wrap div:nth-of-type(2):after {
content: "";
display: block;
border-bottom: 1px solid #000;
position: absolute;
width: 50px;
right: -50px;
top: 50%;
}
.ball_wrap div:nth-of-type(3) {
top: 20px;
right: -100px;
}
.ball_wrap div:nth-of-type(3):after {
content: "";
display: block;
border-bottom: 1px solid #000;
position: absolute;
width: 50px;
left: -52px;
top: 50%;
}
.ball_wrap div:nth-of-type(4) {
right: 20px;
bottom: -100px;
}
.ball_wrap div:nth-of-type(4):after {
content: "";
display: block;
border-left: 1px solid #000;
position: absolute;
height: 50px;
left: 50%;
top: -52px;
}
Also you might take a look at these types of charts using jQuery
You can create the round shapes just with plain CSS:
html:
<div id="ball" class="border">
<div class="blue ball"> </div>
</div>
<div id="ball1" class="border">
<div class="green ball"> </div>
</div>
css:
.border
{
position: relative;
width: 115px;
height: 115px;
background: #e7e9e9;
border-radius: 100px;
border: 2px solid #d1d1d1;
}
.ball
{
position: absolute;
left: 9%;
top: 9%;
width: 90px;
height: 90px;
border-radius: 100px;
}
.blue
{
background: #2f9bc1;
border: 2px solid #266a8e;
}
.green
{
background: #00c762;
border: 2px solid #00be58;
}
#ball
{
top: 200px;
left: 300px;
}
Where you place each shape at the right position with position: relative; offset.
For the lines you could use HTML 5 canvas:
html:
<canvas id="myCanvas" class="line"></canvas>
javascript canvas:
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath();
context.moveTo(350, 150);
context.lineTo(50, 50);
context.stroke();
Where i use a position: absolute; for the line, so it doesn't push the shapes away and a z-index so it is beneath the shapes:
.line
{
position: absolute;
width: 320px;
z-index: -1;
}
jsFiddle
Here is a rough sample html in using canvas.
--- html ---
<div style="border:solid thick #000">
<canvas id="canvas"></canvas>
</div>
--- javascript ---
<script>
var ctx;
window.onload = function() {
canvas = document.getElementById('canvas');
if (!canvas || !canvas.getContext) {
return false;
}
canvas.width = 600;
canvas.height = 600;
ctx = canvas.getContext('2d');
ctx.strokeStyle = '#000';
ctx.fillStyle = "green";
ctx.translate(300, 300);
drawChildCircles(5);
ctx.arc(0, 0, 20, 0, Math.PI * 2);//center circle
ctx.stroke();
ctx.fill();
}
function drawChildCircles(n) {
var ang_unit = Math.PI * 2 / n;
ctx.save();
for (var i = 0; i < n; i++) {
ctx.rotate(ang_unit);
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(100,0);
ctx.arc(100, 0, 20, 0, Math.PI * 2);
ctx.stroke();
ctx.fill();
}
ctx.restore();
}
</script>
However, I believe that the best way is to use svg with d3.js,
especially if you want to draw some data visualization or relation map.
Here's another example using svg elements. SVG elements are very good for these kinda cases.
You'll get more info over here.
If you're trying to do some visualizations out of it. I'd suggest you to get along with d3. A javascript library that used svg elements to create amazing visualization.
HTML:
<div id="container">
<svg id="red">
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>
</svg>
<svg id="red-line">
<line x1="130" y1="50" x2="300" y2="50" style="stroke:rgb(255,0,0);stroke-width:2"/>
</svg>
<svg id="blue">
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="blue"/>
</svg>
<svg id="blue-line">
<line x1="130" y1="50" x2="260" y2="50" style="stroke:rgb(255,255,0);stroke-width:2"/>
</svg>
<svg id="green">
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="green"/>
</svg>
<svg id="green-line">
<line x1="100" y1="0" x2="100" y2="70" style="stroke:rgb(255,0,255);stroke-width:2"/>
</svg>
<svg id="yellow">
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="yellow"/>
</svg>
CSS :
#container {
position: relative;
margin: 150px 0 0 250px;
}
#container svg {
position: absolute;
}
#blue {
top: -150px;
}
#green {
left: -200px;
}
#yellow {
left: 200px;
}
#blue-line {
margin-left: -200px;
}
#green-line {
margin-top: -60px;
}
Related
I want to draw these shapes using CSS and I'm having a bit of trouble
I'm trying the way above:
CSS:
.menu-animation{
border-radius: 50%;
display: inline-block;
height: 40px;
width: 40px;
background-color: #000000;
position: relative;
left: 0px;
}
.menu-animation2{
border-radius: 50%;
display: inline-block;
height: 29px;
width: 23px;
background-color: #000000;
position: absolute;
left: 9px;
top: 26px;
}
If you really want a CSS only solution, you can create black circles with your border-radius: 50%; approach, combine them with a black rectangle and simulate the round cut-out on both sides with white circles. This is how it works:
The single circle elements can be created using the pseudo elements ::before and ::after. With some positioning, the circles position can be adjusted properly.
This is a working example:
.drop {
background: black;
margin: 40px;
height: 20px;
width: 14px;
position: relative;
z-index: 10;
}
.drop::before,
.drop::after {
content: '';
position: absolute;
background: black;
border-radius: 50%;
}
.drop::before {
width: 30px;
height: 30px;
top: -25px;
left: -7px;
}
.drop::after {
width: 20px;
height: 20px;
top: 10px;
left: -3px;
}
.gaps::before,
.gaps::after {
content: '';
position: absolute;
background: white;
border-radius: 50%;
z-index: 10;
}
.gaps::before {
width: 22px;
height: 22px;
top: -3px;
left: -21px;
}
.gaps::after {
width: 22px;
height: 22px;
top: -2px;
left: 13px;
}
<div class="drop">
<div class="gaps"></div>
</div>
Although this is nearly perfect, I would recommend using SVG for this problem, as you can create a smooth outline and you don't have to bother with positioning, sizes and responsive design.
This would be extremely difficult using CSS but there are other solutions. You can attempt to draw them using JavaScript and the HTML5 Canvas element. Or the easier solution would be to create the illustration in a program like Adobe Illustrator, export the image as an SVG file. Get the SVG code from the file (Adobe Illustrator does that for you), it is basically contains the path of the vector. You can then add all the information in an SVG HTML tag and view the element. CSS then allows you to interact with the SVG element.
If you can live with SVG you can do it like this, even animated:
var circle2 = document.getElementById('circle2');
setInterval(function() {
circle2.style.transition="unset";
circle2.style.transform = "translate(0, 0)";
setTimeout(function() {
circle2.style.transition="transform 1400ms ease-in";
circle2.style.transform = "translate(0, 230px)";
}, 0);
}, 1400);
<div style="overflow: hidden">
<svg viewBox="0 0 200 200" width="200" height="200">
<defs>
<filter id="goo-filter">
<feGaussianBlur stdDeviation="8" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 18 -7" />
</filter>
</defs>
<g fill="black" filter="url(#goo-filter)">
<circle id="circle1" cx="100" cy="40" r="20" />
<circle id="circle2" cx="100" cy="40" r="12" />
</g>
</svg>
<div>
I have a schematic as follows:
How can I use css and html to draw out such a thing?
I tried some of the ways, but it can not create the border for the triangle, I want the triangles to have borders, and I can set the border to two sides or one edge or all the edges of those triangles. .
Here is the code that I tried with the first pentagon:
<style type="text/css">
div {
position: relative;
width: 200px;
height: 150px;
background: #4275FF;
}
div:after {
content: '';
position: absolute;
width: 0;
height: 0;
border-top: 75px solid transparent;
border-bottom: 75px solid transparent;
border-left: 25px solid #4275FF;
right: -25px;
}
</style>
<div></div>
Another alternative
div {
position: relative;
width: 200px;
height: 150px;
background: #EEE;
border: 1px dashed #777;
position: relative;
}
div.v2 {
border-right: 0px;
}
div:after {
content: '';
z-index: -1;
transform: rotate(135deg);
background: inherit;
border: inherit;
width: 106px;
height: 106px;
top: 21px;
right: -53px;
position: absolute;
}
<div></div>
<div class="v2"></div>
I think the SVG is the best way to create the border for the triangle.
See the code, 'polyline' create a triangle. three 'line' are the border of the triangle, and you can change the color of these lines through the style-stroke-color.
<svg>
<polyline points="10,10 50,50 10,90" style="fill:#006600;stroke:#fff;" />
<line x1="10" y1="10" x2="50" y2="50" style="stroke:#ff0000;" stroke-width="2" />
<line x1="50" y1="50" x2="10" y2="90" style="stroke:#00ff00;" stroke-width="2" />
<line x1="10" y1="10" x2="10" y2="90" style="stroke:#0000ff;" stroke-width="2" />
</svg>
How to transform block in CSS? Pseudo-elements is need or not? I try to create block look like block on the picture below. I can't create such block as on the picture below.
This is my code:
.transform_items {
width: 40%;
height: 80px;
position: relative;
margin: 0 auto;
perspective: 600px;
margin-top: 150px;
left: 50px;
}
.block,
.block::before{
display: block;
position: absolute;
margin: 0 auto;
}
.block {
border: 5px solid transparent;
width: 350px;
height: 60px;
}
.block::before {
content: '';
border: 5px solid #52B352;
transform: rotateY(30deg);
top: -5px;
right: -5px;
bottom: -5px;
left: -35px;
}
.block a {
font-size: 24px;
}
<div class="transform_items">
<div class="block"><a>Block</a></div>
</div>
The expected result:
If you can use SVG (1), it could be like this
codePen
svg #block {
stroke: orange;
fill: none;
stroke-width: 5
}
svg text {
font-size: 25px
}
<svg version="1.1" x="0px" y="0px" width="274px" height="84px" viewBox="0 0 274 84">
<polygon id="block" points="33,13 245,24 245,60 29,64 " />
<text x="100" y="50">BLOCK</text>
</svg>
You can also save the svg code as a .svg file,without the text element, and use it as background-image for the div that contains your text
Read this to learn how to use svg in different ways: https://css-tricks.com/using-svg/
(1) Browser support for SVG is a little better than browser support for transform, caniuse: SVG
I want to have a design where my main div(box) will be looking something like below, where i don't want my right top border to be chucked off. Can you please help me in designing the same.
You could use :after pseudo element
div {
width: 250px;
height: 150px;
border: 2px solid #677D50;
border-bottom: 20px solid #677D50;
margin: 50px;
background: white;
position: relative;
}
div:after {
content: '';
position: absolute;
right: 0;
top: 0;
width: 70px;
height: 70px;
background: white;
border-bottom: 2px solid #677D50;
transform: rotate(46deg) translateY(-52px);
}
<div></div>
Or SVG
rect {
fill: #677D50;
}
polygon {
fill: none;
stroke: #677D50;
stroke-width: 2;
stroke-miterlimit: 10;
}
<svg x="0px" y="0px" width="400px" height="250px" viewBox="0 0 400 250">
<polygon points="378,230 24.5,230 24.5,20.5 339,20.5 378,52.5 " />
<rect x="24.5" y="203" width="353.5" height="27" />
</svg>
I'm trying to design a profile image shape just like this
but my code given me the following design
I'm worried about the white space inside the border and the shape here is code
.doctor-profile-photo {
width: 210px;
height: 210px;
border-radius: 60px/140px;
border: 5px solid #000;
box-shadow: 0px 2px 5px #ccc;
}
.doctor-profile-photo img {
width: 100%;
height: 100%;
border-radius: 60px/140px;
}
<div class="doctor-profile-photo">
<img src="http://weknowyourdreams.com/images/bird/bird-09.jpg" alt="">
</div>
This gives pretty similar output to what you want. Try tweaking the values of border-radius and height-width to achieve exactly what you want.
<style>
#pic {
position: relative;
width: 130px;
height: 150px;
margin: 20px 0;
background: red;
border-radius: 50% / 10%;
color: white;
text-align: center;
text-indent: .1em;
}
#pic:before {
content: '';
position: absolute;
top: 10%;
bottom: 10%;
right: -5%;
left: -5%;
background: inherit;
border-radius: 5% / 50%;
}
</style>
<div id="pic"></div>
Here's a useful link : https://css-tricks.com/examples/ShapesOfCSS/
SVg path and pattern
You can create your shape with a single path. I used a quadratic Bezier curve.
Path MDN
I added an image to the svg using a image tag and pattern tag.
This is then using inside the path with this fill="url(#img1)".
The defs tag is used to hide elements we are not using directly.
<svg viewBox="0 0 100 100" width="400px" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="400" height="400">
<image xlink:href="http://lorempixel.com/400/400/" x="0" y="0" width="100px" height="100px" />
</pattern>
</defs>
<path d="M15,15 Q 50,0 85,18 100,50 85,85 50,100 18,85 0,50 15,15Z" fill="url(#img1)" />
</svg>