I have the following:
* {
font-family: Arial, Helvetica, sans-serif;
}
.x-y-center {
display: flex !important;
justify-content: center !important;
align-items: center !important;
text-align: center !important;
}
.g {
font-size: 55px !important;
width: 100px;
height: 100px;
border-radius: 50% !important;
transition: all .2s ease-in-out;
margin: 25px 0px 25px 0px;
}
.d {
font-size: 25px !important;
transition: all .2s ease-in-out;
width: 40px;
height: 40px;
border-radius: 50% !important;
margin: 25px 0px 5px 0px;
}
.g:hover,
.d:hover {
transform: scale(1.1);
box-shadow: 10px 10px 5px #888888;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css?v=1">
<div class="container ">
<div class="g x-y-center btn btn-primary">
<b>G</b>
</div>
<div class="d x-y-center btn btn-success">
<b>D</b>
</div>
<div class="d x-y-center btn btn-success">
<b>D</b>
</div>
<div class="d x-y-center btn btn-success">
<b>D</b>
</div>
</div>
I would like to be able to achieve this:
I was trying with jquery
, but I fall into an infinity of conditionals ( if-else if-else
) validating coordinates and positions.
How can I do it in a simple and dynamic way?
We first assign a position
absoluta
to all the elements.container > {position: absolute;
so that we can then manipulate and apply a basicrotación
y transformation to themtraslación
. in addition to applying some additional properties such asborder-radius
to give it a circle style among others. Remember that the degrees applicable to the rotation can vary depending on the elements you have,(360/cantidadelementos) = TotalGradosaplicables
in my example there are 6 elements = 60 applicable degrees,You can actually do this with CSS and, although you'll get rid of if-elses, you won't get rid of doing it with positions (at least with this solution).
What I propose is that you position the main circle with
position: relative
and encompass the rest of the green circles within it, positioning them withposition: absolute
.In this way, since an element positioned with
position: absolute
can be positioned by reference to the positioned parent element, we can position the green circles with respect to the blue circle. To do this you will need to make use of thetop
,left
,bottom
and propertiesright
.What is the advantage of doing it this way?
That wherever you position the blue circle the green circles will be positioned relative to it and you won't have to recalculate the coordinates where the blue circle is again.
Example with three circles around the blue circle: