Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3 align elements into circle

I have a question about forming elements to form a circle, or align elements to form a circle, depending how you like it to be pronounce, now back to question:

There are couple of examples here on stackoverflow and on the internet regarding this question but any off these examples do not cover Bootstrap 3 responsive align elements to form a circle, I would like if someone can make an example out of mine working JSFiddle example (text needs to be a center of the circle, because I need to animate it), and make this using bootstrap grid system.

Is this possible, can you please explain to me how you do this so I can learn something out of this.

like image 236
copser Avatar asked Dec 04 '15 04:12

copser


1 Answers

TL;DR; http://jsfiddle.net/k7yxtpc7/

Edit with (very long?) explanation:

So we start off with a bootstrap's hierarchy:

<div class="container-fluid">
    <div class="row">
         <div class="circle_container col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-12">
         </div>
     </div>
</div>

The planetary of images will be put inside .circle_container. Our aim is to make sure the whole circle will respond to .circle_container's width changes and adapt correctly. This way any change Boostrap makes to the container will be reflected on the circle itself, making it Bootstrap-compliant.

First we have to prepare .circle_container a bit. Since it's a whole circle the container must be square-ish. We must find a way to make .circle_container's height to be always equal to its width. I do this by putting a square img inside .circle_container, then scale the img's size according to the container's width:

<div class="circle_container ...">
    <img class="transparent_square" src="http://i.stack.imgur.com/5Y4F4.jpg" width="2" height="2" />
</div>

.transparent_square{
    width: 100%;
    height: auto;
}

Note: I couldn't find a transparent square image on the web, so I had to make do with a white square. In your product a 2pxx2px transparent image is best.

Great, now we have a square container. But we've put a limiter on ourselves too. From now on, the img must be the only child of .circle_container that have a static (default) or relative position, because any further child will extend the container, destroying the square shape. Not a big deal though, since we'll position other children absolute anyway.

Next up is the central text bubble:

<div class="central_text text-center">
    <h3>Special for you</h3>
    <h5>Lorem ipsum</h5>
</div>

.central_text{
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    position: absolute;
}

The translate trick make use of the fact that percentile value in css transform use the element's pre-render width & height, while all other positioning rule use its parent's width & height instead. By giving the element left: 50%; top: 50% we put its top left corner at the center of its parent, then we translate it up and to the left by 50% of its own width and height, effectively centering the element within its parent. This is only 1 of several methods to center an element within a container, but it fits our situation best because the element is absolutely positioned.

Finally we reach the part where we create the circle. To sum up the trick here: we put the actual image inside a container, which has a pivot point at the center of the container, and position the image off to 1 side of the container equal to the radius of the circle. This way when we rotate the image's container, the image will be moved in a circle around the center of the container, like a drawing compass. After the image has reached our desired position, we rotate the image itself by the same degree in the other direction to compensate for the tilt in orientation, making the image upright again.

The container and image:

<div class="moon_container moon1"><img class="moon moon1" src="http://letscode.ghost.io/content/images/2015/09/stackoverflow.png"></div>
.moon_container{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    width: 20%; /* This is the final width of the image */
}

I set the width for .moon_container as 20% of .circle_container's width. This will be the width of the images in our final circle. Increasing or decreasing this number simply change the size of the image to your desire.

Now to offset the image from its container:

.moon{
    width: 100%;
    height: auto;
    
    /* The image can be relative positioned without breaking anything because its parent is absolute */
    position: relative;
    
    /* The radius of the circle. This is equal to 175%*20% = 35% of .circle_container's width */
    left: 175%;
}

Note that CSS's left use an element's direct parent's width as base unit. If you changed .moon_container's width in the previous part, the actual distance of the images will change as well.

Finally, rotations (I use moon2 as the example here because moon1 doesn't need to rotate):

/* Container rotate 45deg clockwise... */
.moon_container.moon2{
    /* 360/8 (the number of images) = 45deg */
    transform: translate(-50%, -50%) rotate(45deg);
}

/* ... while the image rotate 45deg counter-clockwise */
.moon.moon2{
    transform: rotate(-45deg);
}

Why transform: translate(-50%, -50%) rotate(45deg); and not transform: rotate(45deg);? Because we declared transform: translate(-50%, -50%); earlier for the .moon_container (the centering trick). If we only write transform: rotate(45deg); here, the CSS parser will override the previous rule with the new one, losing the translate part. So we have to append manually.

Repeat the process to all 8 images and we're done!

If you have undetermined number of images, simply use javascript to calculate this rotation part for each image.

I hope my explanation was useful for you. I've always been bad at explanation...

Edit 2: http://jsfiddle.net/k7yxtpc7/3/ Text change on hover version as per OP's request. There's only 1 thing to note in this part, that is

$("body").on({ 
    mouseenter : function(event){
      ...
    }, 
      mouseleave : function(event){
      ...
    }
}, ".moon");

It is good habit to bind all events on either 'body' or document, instead of binding them on the actual elements itself (the .moon). This way you:

  • Always use only 1 event listener for the hover event, instead of 8 (you can imagine how the number scale up on an actual product).
  • When you add more images later, you don't have to bind the event on the new .moon again.

Original Answer:

As the requirement is rather vague, I couldn't know if my solution would satisfy you. My solution is based on 3 assumptions:

  • The entire planetary of images are only based on view port width, similar to how Bootstrap handle its responsive design. If you want to take view port height into consideration maybe I can conjure up another version.
  • The images are scaled based on the Bootstrap container's width, in order to make sure there's enough space to display all images.
  • Typography uses Bootstrap's defaults.

The solution avoid using javascript at the cost of not being able to add/remove images on-the-fly. If a dynamic number of images is your intention, I will put calculations in.

Sexy animations compatible.

Unfortunately Bootstrap's center-block only center a block horizontally, I had to make use of the translate trick to center the pivot point.

.central_text{
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    position: absolute;
}

This is only an answer placeholder. I will write detailed explanation once we have a satisfactory solution.

like image 107
AVAVT Avatar answered Sep 21 '22 12:09

AVAVT