I have created a basic star topology diagram using simple CSS and HTML. I have 2 problems with the code I have:
- When I try to add text to the bubbles, it overflows from the bubble and the text is not oriented based on the bubble.
- Instead of a simple bubble I would like a cloud shape.
Any suggestions/solutions would be highly appreciated.
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
figure {
--containerWidth: 500px;
--containerHeight: 500px;
width: var(--containerWidth);
height: var(--containerHeight);
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
figure div {
background: gray;
width: 140px;
height: 140px;
border-radius: 50%;
}
figure .connected-device {
--connectedDeviceWidth: 100px;
--connectedDeviceHeight: 100px;
width: var(--connectedDeviceWidth);
height: var(--connectedDeviceHeight);
position: absolute;
background: gold;
top: 0;
transform-origin: calc(var(--connectedDeviceHeight) / 2) calc(var(--containerHeight) / 2);
display: flex;
justify-content: center;
}
figure .connected-device::before {
content: '';
height: calc((var(--containerHeight) / 2) - var(--connectedDeviceHeight));
display: block;
border-right: 2px dashed grey;
position: relative;
top: var(--connectedDeviceHeight);
}
figure .connected-device:nth-of-type(2) {
transform: rotate(calc(1 / var(--number-connectedDevices) * 360deg));
}
figure .connected-device:nth-of-type(3) {
transform: rotate(calc(2 / var(--number-connectedDevices) * 360deg));
}
figure .connected-device:nth-of-type(4) {
transform: rotate(calc(3 / var(--number-connectedDevices) * 360deg));
}
figure .connected-device:nth-of-type(5) {
transform: rotate(calc(4 / var(--number-connectedDevices) * 360deg));
}
figure .connected-device:nth-of-type(6) {
transform: rotate(calc(5 / var(--number-connectedDevices) * 360deg));
}
figure .connected-device:nth-of-type(7) {
transform: rotate(calc(6 / var(--number-connectedDevices) * 360deg));
}
figure .connected-device:nth-of-type(8) {
transform: rotate(calc(7 / var(--number-connectedDevices) * 360deg));
}
<figure style="--number-connectedDevices: 8">
<div class="central-device"></div>
<div class="connected-device"></div>
<div class="connected-device"></div>
<div class="connected-device"></div>
<div class="connected-device"></div>
<div class="connected-device"></div>
<div class="connected-device"></div>
<div class="connected-device"></div>
<div class="connected-device"></div>
</figure>
2
Answers
Here is my attempt.
If each
.connected-device
had a sub-container, then I could rotate that in the opposite direction, but since that’s not the HTML given, instead I’ve used rotation for the stalks/lines/::before and translate for each.connected-device
.Not everything is calculated and that is because to work out the translations nicely, I would have to use trigonometric functions such as sin and these are relatively new and so could cause some issues with browser compatibility.
The code in the question is not responsive so this snippet alters things slightly by setting CSS variables – the width of the whole and of the bubbles being calculated. The maximum width is set at 500px as in the question but on narrower viewports this will become the width of the viewport (100vw).
Added text needs to be inside its own element so that it can be rotated back to be horizontal.
The rotation setting is put in just once – using the position of the element –n to calculate the actual degrees.
Just for demo the cloud is added as a background png image (this one is allowed for personal use only) but you may like to look into using a base64 image so you don’t need a separate file, and the image will be more responsive to different viewport sizes. It is added on a pseudo element so that it too can be rotated back to a horizontal position.