How to create a circular element with horizontal alignment using CSS in a Bootstrap

Looking to center a CSS circle with an image and label overlay in the circle while ensuring it stays horizontally centered in a bootstrap column. Any tips or suggestions are appreciated.

For reference, please visit this JSFIDDLE link

<div class="container">
<div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4">
        <div class="circle1Wrapper">
            <div class="circle-textSmall bubble1outer">
                <div> <span class="bubbleIconSmall">
                        <img src="http://lorempixel.com/40/40/" />
                    </span><span class="bubbleHeadSmall">label</span>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4"></div>
</div>

CSS:

.circle1Wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    z-index: 9999;
    margin: auto;
    border: 1px solid;
}
.bubble1outer {
    position: absolute;
}
.circle-textSmall div {
    width: 125px;
}
.circle-textSmall div {
    float: left;
    width: 250px;
    padding-top: 15%;
    line-height: 1em;
    margin-top: -0.5em;
    text-align: center;
    color: #000;
}
span.bubbleIconSmall > img {
    width: 45%;
    height: auto;
}
.circle-textSmall:after {
    width: 125px;
    padding-bottom: 125px;
    margin-left: 50%;
}
.circle-textSmall:after {
    content:"";
    display: block;
    width: 250px;
    height: 0;
    padding-bottom: 250px;
    background: #ccc;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    border-radius: 50%;
}

Desired outcome for the design:

Answer №1

@metaxos, I had intended to leave this as a comment, but it turned out to be quite lengthy.

Even if you have found a solution that works for you, I suggest considering tidying up the code; observe how the original example trimmed down most of the code and retained just one div:

  • .innerwrapper is redundant (why not apply that style directly to #myCircleDiv?);
  • The same goes for the div containing the image (you can apply that style directly to the image!);
  • Additionally, the img itself can be eliminated too ( utilize it as the background of #myCircleDiv).

This is my perspective (feel free to disregard it), however, I believe aiming for something cleaner and easier to maintain would be beneficial rather than a more intricate and unnecessary structure (unless explicitly requested by the user/customer). Simplicity is key.

In line with this approach, the following (view it in action on this jsfiddle):

<!-- HTML -->
<div id="myCircleDiv">LABEL</div>

/* CSS */
#myCircleDiv {
    width:250px;
    height:250px;
    border-radius:50%;
    display:inline-block;
    line-height:375px;
    text-align:center;
    color:white;
    background:url("http://lorempixel.com/50/50/") #ccc no-repeat 50% 38px;
}

Appears neater compared to the following:

<!-- HTML -->
<div id="myCircleDiv">
    <div class="innerWrapper">
        <div>
            <img src="http://lorempixel.com/50/50/" />
        </div>
        <div>LABEL</div>
    </div>
</div>

/* CSS */
#myCircleDiv {
    width:250px;
    height:250px;
    border-radius:50%;
    display:inline-block;
    background-color:#ccc;
    background-size:250px 250px;
    line-height:250px;
    text-align:center;
    color:white;
}

.innerWrapper {
    width: 100%;
    height: 250px;
}
.innerWrapper div {
    float: left;
    height: 125px;
    width: 100%;
    line-height: 125px;
}

.innerWrapper div img {
    margin-top: 38px;
}

The outcome remains identical. However, once again... that's simply my viewpoint :)

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

Displaying negative values highlighted in red on Datatables platform

After successfully integrating the datatables on my VF page, I now have one final requirement: to display any negative values in red and bold within numerical columns. In my Salesforce implementation, I am using <apex:datatable> for my table. Each nu ...

Combining Images and Navigation in Next.js using Tailwind CSS

I am facing an issue where the image from a particular section overlaps with the Navbar dropdown on mobile devices. Adding z-50 to the navbar did not solve the problem as expected. What I want is for the image to remain below the dropdown menu when it is ...

Toggle the div if the if statement is true; otherwise, hide the broken

click: function(event, data) { $('#clicked-state') .text('You clicked: '+data.name); if (data.name == "VA") { $('#va').toggle(); } else { $('#va').style.d ...

What is the proper way to reference automatically generated class names within nested style rules when using makeStyles in Material-UI?

When using makeStyles, how can I reference generated classnames when creating nested rules? For instance, if I have a "container" class with nested "item" elements and I want to use the generated "item" class in the style definition. The approach that wor ...

What could be causing the responsiveness issue with my react slick carousel?

https://i.sstatic.net/Qv0Pg.jpg import React from "react"; import Slider from "react-slick"; import "slick-carousel/slick/slick.css"; import "slick-carousel/slick/slick-theme.css"; import Link from "next/link&qu ...

Reposition icons accordingly while incorporating a new set of icons

After creating a new icon set font, I noticed that the icons are not positioning correctly and appear smaller than expected when loaded: https://i.stack.imgur.com/1OsAL.png https://i.stack.imgur.com/zmLQq.png I attempted to adjust the .icon class by add ...

Having trouble unselecting the previous item when selecting a new item in Reactjs

I have a list of items and I want to change the background color of the currently selected item only. The previously selected item should deselect. However, at the moment, all items are changing when I click on each one. Can anyone help me solve this issue ...

Launching a web application directly from a USB drive

Exploring the world of Javascript frameworks and nodejs, I recently encountered a unique requirement that got me thinking about their practical application. The requirements are as follows: --I need to create a lightweight website that can be run from a U ...

Show the chosen option when the textarea is no longer in focus

My form includes a text box and a button: <p><textarea rows="4" cols="30">Aliquam erat volutpat.</textarea></p> <p><input type="button" value="Submit"></p> When the user selects text in the textarea and then cl ...

The menu lacks responsiveness

I'm looking to create a responsive menu on my website where the button that opens the navigation will be hidden in "desktop mode". I came across some information on w3school, but I seem to have made an error. Can you assist me with this, please? Thank ...

Tips for Fixing CSS Issues

My HTML / CSS project on JS Fiddle is encountering a few issues jsfiddle ZyBZT. The footer with the class "footer" is not displaying at the bottom of the page. The background image specified by url('http://i.imgur.com/z5vCh.png') is not showing ...

How can I optimize Javascript and CSS that are being used on my website but are not physically hosted on my website?

On my website, I have a plugin called "Contact Us" that was created and is being operated by Dropifi. Lately, I've been working on optimizing my site for SEO/Speed using Google's PageSpeed Insights tool. I enabled compression with GZip for all el ...

What is the method for adjusting the text color in an HTML document?

Is there a way to modify the text color of this statement: You Have Already Earned Credit For This Link? <div id=\"myform\" style=\"visibility: hidden;\"> <font size=2><b>You Have Already Earned ...

Bootstrap struggles to create panels of uniform size

Here is the code snippet I am currently working with: <div class="col-md-4"> <div class="panel panel-default"> <div class="panel-heading"> <h4><i class="fa fa-fw fa-tasks"></i> Extreme Performance</ ...

Enable selecting text within a Bootstrap table

When I first encountered the issue, selecting text from my bootstrap table seemed like an impossible task. My initial attempts to address this involved focusing on CSS, and I managed to find a somewhat limited solution utilizing webkit-select-user Howe ...

How to apply jQuery addClass to only the following target element and not all elements on the entire webpage

Currently, I am using jQuery to create an animation effect on a hidden element (which is an image description) so that it becomes visible when hovering over an image. Although the code snippet I found does work, it has a downside - all the hidden image de ...

Unable to display image using HTML and CSS

I want to display two images (download and view) on top of another set of images. Here is the HTML code: <div class="row"> <div class="col-md-3 col-sm-4 col-xs-6 backgrounddownload backgroundview"> <a href="{site_url}scents/baobab/ ...

How can I establish a backup plan for an image with transparency?

I currently have a dilemma with using a 20px fallback image for loading images. Transparent images seem to appear blurry once they are loaded due to the background image. The use of dominant color or traced SVG does not solve this issue either. How can I i ...

"Ensuring a DIV element has a height of 100% within

Is there a way to set and enforce a div's height to be 100% of the browser window, another div, or a table cell without resorting to complicated hacks found on Stack Overflow or other websites? ...

Issues encountered in loading JavaScript with jQuery script

I am facing an issue with my jQuery script not loading correctly. When I click on the calculate button, nothing happens as expected. I made sure to copy and paste jQuery directly into the file for offline use, and also created a personal console due to res ...