Arrange elements in a half-moon shape with CSS and HTML

I am in the process of designing a webpage that will showcase 5 rounded components, each with different sizes, arranged in a semi-circle pattern. I have included an image to give an idea of how I want the layout to appear, with placeholder circles indicating where custom images will be placed. The plan is to dynamically load the images through JavaScript and Angular, hence why I have currently defined them in the HTML.

At the moment, I am manually rounding the edges of the images, adjusting their sizes individually, and using 'transform: translate(x,y)' to position them in a semi-circle formation. However, this method lacks flexibility, as resizing the images would entail recalculating the positioning for a circle shape.

This is the current code snippet I am working with:

<div class="round top1st" style="background-image: url('image1.png');"></div>
<div class="round top2nd" style="background-image: url('image2.png');"></div>
.
.
.

.round {
    border-radius:50% 50% 50% 50%;  
    overflow: hidden;
    background-position: center center;
    background-repeat: no-repeat;
    background-size:cover;
}

.top1st {
    width: 180px;
    height: 180px;
    transform: translate(0px,300px);
}

.top2nd {
    width: 150px;
    height: 150px;
    transform: translate(100px, -30px);
}
.
.
.

Is there a more efficient approach to achieve this? It's important to note that the circular images will vary in size and will be loaded dynamically from a database query using Angular on the client-side.

Answer №1

If it were up to me, I would approach it like this.

Let's say your container has a specified height (in pixels or percentage), I suggest using percentages for the rest of the sizes and positioning the circles using position:absolute, specifically with properties like top and bottom instead of translate (also in percentage values).

By doing this, your circles will adjust perfectly to any screen size.

The only challenge lies in making sure the circle maintains its perfect shape when you have a width defined in percentage. In this case, I recommend using a bit of jQuery to calculate the actual width of the element and assign the same value to the height:

var cw = $('.top1st').width();
$('.top1st').css({
    'height': cw + 'px'
});

CSS styling:

.container {
    width:100%;
    height:400px;
    position:relative;
    background-color:#ddd;
}
.round {
    border-radius:50% 50% 50% 50%;  
    overflow: hidden;
    background-position: center center;
    background-repeat: no-repeat;
    background-size:cover;
    background-color:red;
    display:inline-block;
    position:absolute;
}
.top1st {
    width: 20%;
    bottom:10%;
    left:8%;
}
.top2nd {
    width: 18%;
    bottom:40%;
    left:28%;
}
.top3nd {
    width: 16%;
    bottom:45%;
    left:50%;
}
.top4nd {
    width: 14%;
    bottom:35%;
    left:70%;
}
.top5nd {
    width: 12%;
    bottom:15%;
    left:85%;
}

These values are just placeholders; you can adjust them to suit your design preferences. Check out the result on this FIDDLE.

The only minor drawback is that if you resize the window, the circles might lose their shape until you refresh the page. This shouldn't be a significant issue since users typically don't frequently resize their windows; they'll still see the circles correctly positioned on any device they use.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Stop unwanted clicking on inactive buttons in Angular

I want to make sure that disabled buttons cannot be clicked by users who might try to remove the disabled attribute and trigger an action. Currently, I have this code to handle the situation: <button [disabled]="someCondition" (click)="executeAction()" ...

Tips for inserting a PHP variable into a Bootstrap CCS class to change the background image dynamically

I currently have CSS code set up for a full background image. I am now looking to dynamically change the background image by passing a PHP variable to the URL. Can someone please help me with this? Here is my current code: .imgback { padding-top:140px; ...

What is the best way to incorporate a label onto the border of a div while ensuring responsiveness?

I am struggling to design a straightforward appreciation screen with bordered text. The current css I have does not adjust responsively when the screen size changes, causing it to appear misaligned. Below are my CSS and component codes. React JS is being u ...

Is it necessary to have all font-family files for a font to function properly?

Let's say I have a font pack like Font-Awesome, which provides me with various file types such as eot, woff, woff2, or svg. From what I understand, we only need one file for each specific font weight to function properly. My question is: If we only i ...

Change the Bootstrap components according to the size of the screen

Is there a built-in Bootstrap feature to change an element's class based on screen size? I'm working on a webpage with scrollable card elements. On desktop, the cards are arranged horizontally, but on mobile they are stacked vertically, requirin ...

Angular Filtering: Enhancing User Search Experience [Part 2]

When attempting to import the 'CommonModule', I encountered the same error message. However, when trying to write "of" between car and cars, it highlights the word filter as an error and displays: No pipe found with name 'filter'. I am ...

The secondary linked stylesheet is not affecting the layout

I'm encountering an issue with linking a second stylesheet to my HTML document, and I'm struggling to identify the (hopefully glaringly obvious) problem. My method of linking stylesheets in the head is as follows: <!DOCTYPE HTML PUBLIC "-//W ...

How can I close the sidebar with a single tap anywhere outside of it?

Is there a way to hide the sidebar when clicking anywhere on the screen? Can I show text when hovering over an image instead of having it always visible? function openNav() { document.getElementById("mySidenav").style.width = "300px"; document.getE ...

Positioning a div beyond the visible area without altering its original width, with the assistance of the Skrollr plugin

I'm currently experimenting with a parallax website using Skrollr. My goal is to achieve a curtain effect where two divs slide open left and right as I scroll down. The issue I'm facing is that when I scroll, the divs extend beyond the 100% widt ...

Posting data to an HTML file with a foreign key matching the input value in Express.js

My discussion platform features topics and comments. Each comment has its own unique topic_id. Currently, I am able to post comments for the initial topic, but encountering issues when adding new comments for other topics. I am looking for guidance on effi ...

Fixed columns with horizontal scrolling to prevent Datatables columns from overlapping

I'm facing an issue with my data table created using datatables. I need to fix the first two columns, but when I do so, the other two columns overlap them while scrolling horizontally. If I remove the fixed columns, the scrolling works correctly witho ...

Ways to update a component's state by clicking a button located on a different component

Seeking help with changing a parent state value when clicking a button. Although there are numerous similar queries out there, I'm really struggling to grasp the concept. I've attempted lifting the state up but haven't quite nailed it yet. ...

React doesn't properly display JSON data

I am facing an issue with the code below that is supposed to display data from a JSON file. Instead of showing the desired data, it only displays the h1 with curly braces. Here is the faulty code: import prod from "../../data/produtos.json"; exp ...

Scaled-down navigation when scrolling

Is there a way to make the navigation bar shrink when scrolling, similar to how it functions on this website? The transition on this site is so seamless. I'm guessing it's achieved with JQuery. ...

Enhance the navbar brand with a unique lighting effect on the background

I'm currently working on a website and I'm interested in incorporating a lighting effect that resembles the one in the image linked below. I've been struggling to figure out where to begin and haven't been able to find any relevant info ...

Getting URL parameters dynamically without reloading the page

Here's a particular issue I'm encountering: I've implemented the following function to fetch the GET Parameters from a URL. $(document).ready(function() { function getParam(variable) { var query = window.location.search.sub ...

I prefer my information to be arranged neatly and separated by spaces

Is there a way to display data neatly formatted with space between each entry? I'm not sure why id one is not being selected I prefer using the append method so I can dynamically add content later on How can I organize data into two columns, wit ...

What is the best way to extract the numerical value from a JavaScript object?

I'm currently working on a web form for a currency exchange demonstration. I have included a snippet of HTML and Javascript code that I have initially implemented. <!DOCTYPE html> <html lang="en"> <head> <meta charset="ut ...

Using AngularJS to bind radio buttons to ng-model

Here is a snippet of my HTML code where I attempted to bind the "formFriendsAll" scope to the ng-model. <form ng-submit="submitForm()" > <div class="col-sm-3"> <div class="form-group"> <label>Which Persons to show?< ...

Issue with the camera functionality in phonegap 3.3.0 API on IOS platform

I am currently in the process of developing an application for iPad that will allow users to capture and store photos. I have encountered some difficulties while using the PhoneGap camera API library, as my code is not generating any errors which makes i ...