Create a circular shape using CSS that dynamically adjusts its size to match the width

Can you help me with a CSS challenge? I need to create circles around elements on a webpage, but my code is not working perfectly. The width of the circle is off and I can't seem to center it properly.

  • The width does not match the content (it is always too big), and
  • I am struggling to get it to center on the child element

Here is the code I currently have:

<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Circle Demo</title>;

</head>

<body>
<style>
div.ccc {
  display: run-in;
  position: relative;
}

div.ccc:after {
  display: inline-block;
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  bottom: -50px;
  left: -50px;
  opacity: 0.7;

  border: 5px solid red;
  border-radius: 50%;
  padding: 10px;
}


</style>


<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg" width="10%">
</div>

<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
</div>

</body>

This code produces imperfect results as the circles are not centered on the images and the widths are incorrect.

Is it possible to correct this using just CSS?

https://i.sstatic.net/kDCOB.jpg

Answer №1

To ensure the div works properly, set it to display:inline-block.

By default, divs are block-level elements, taking up 100% of the width.


Update: The issue arises from using % to size the image, which relies on the parent's width. In this case, we aim to adjust the parent based on the child, so that approach won't be effective.

The closest solution is to avoid sizing the image in %. Instead, display the div as inline-block.

div.ccc {
  position: relative;
  display:inline-block;
}

div.ccc:after {
  display: inline-block;
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  bottom: -50px;
  left: -50px;
  opacity: 0.7;

  border: 5px solid red;
  border-radius: 50%;
  padding: 10px;
}

.small{
  width:200px;
}
<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg" class="small">
</div>

<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
</div>

If resizing in % is crucial, consider adding another container and sizing that instead.

div.ccc {
  position: relative;
  display:inline-block;
}

div.ccc:after {
  display: inline-block;
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  bottom: -50px;
  left: -50px;
  opacity: 0.7;

  border: 5px solid red;
  border-radius: 50%;
  padding: 10px;
}

.img-container{
  display:inline-block;
}

.img-container img{width:100%}

.small{
  width:200px;
}
<div class="ccc">
  <div class="img-container small">
    <img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
  </div>
</div>

<div class="ccc">
  <div class="img-container">
    <img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
  </div>
</div>

Answer №2

Typically, the default width of a div element is 100%, which explains why your circles appear so large and off-center within the container.

Have you considered applying the red circle border directly to the img tag instead of the div? This would involve placing the circular design on the child element initially.

Alternatively, you could adjust the div to match the size of its content by utilizing display: inline-block within the .ccc class.

If neither of these solutions works for you, it's likely that there is no purely CSS-based method to achieve the desired result.

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

Unable to adjust iframe height

Below is a snippet of code that I am working with: <style type="text/css"> .div_class { position:absolute; border:1px solid blue; left:50%; margin-left:-375px; margin-top:100px; height:300px; width:500px; overflow:hidden; } .iframe_class { position: ...

Using HTML video to fill entire div container

I'm currently facing an issue with using an HTML video as a fullscreen background replacement for an image. The problem is that the video overflows and covers the entire page instead of staying within the constraints of the header section. Here is th ...

Tips for invoking a JavaScript class method from an HTML document

I've created a Typescript class that dynamically inserts an HTML element to a page after it's loaded. Here is the code snippet: const calcElement: string = ` <div class="container"> <span class="cc-title">Field</span> ...

Image not found in PDF created from HTML to PDF conversion

I am currently utilizing the HTML-pdf library to convert an HTML page into a PDF format. While the dynamic content is being displayed in the generated PDF, there seems to be an issue with the image not appearing as expected. Despite trying various methods, ...

Add HTML syntax highlighting to a text area for coding purposes

I am facing a seemingly straightforward issue, but I worry that it may be too complex for me to handle on my own. My goal is to incorporate a textarea into a webpage where users can input HTML code, click a button, and have the interpreted HTML displayed i ...

Instructions for making a crossword-inspired grid using a high quantity of divs or alternative elements

I am looking to create a grid on the screen of a web browser or mobile device, with each grid item containing just one letter. It's similar to a crossword puzzle, but taking up most of the screen. I've tried using divs for this purpose, but it se ...

How can I feature an image at the top of the page with large 3 and jQuery in FireFox?

I am interested in showcasing a single image at the forefront of the page when it is selected. While there are numerous plug-ins that offer this feature, many come with unnecessary extras like galleries, video support, and thumbnails which I do not requir ...

What is the best way to send JSON data to Sass in ReactJS?

In my current project, I am developing a React/Redux application with Webpack that showcases UI components and enables users to adjust the colors of those components. The styling is being done using CSS Modules with scss. My preference is to keep all the s ...

What causes the button's frame color to change when I click on it?

I am facing an issue with a button I created. When I click the button, the frame color changes and I cannot figure out why. I have attached images showing the button before and after it is clicked. Before: https://i.sstatic.net/5jpB1.png After: https://i ...

When attempting to view an .html file, SVG images may not appear on

I've encountered a common issue that I can't seem to solve. After creating a website using HTML, CSS, and JS, I uploaded it to the hosting server via FileZilla. Everything is working perfectly on the live site. However, when I downloaded the fi ...

Centralizing the Accordion header and expansion icon in React with Material-UI

I'm currently incorporating the Accordion feature from Material-UI library into my React project: My goal is to ensure that the accordion summary (header with "Accordion 1", "Accordion 2") is centered, and the expand icon (arrow) is also centered rig ...

Creating the perfect layout with CSS: A step-by-step guide

As I work on refining my layout to achieve the desired look, let me share my initial idea before delving into the issue at hand. Currently, I am attempting to create something similar to this: https://i.stack.imgur.com/rtXwm.png This is how it appears a ...

Change the way a button interacts with a different element

Currently, I am utilizing SlickSlider from http://kenwheeler.github.io/slick/ and attempting to integrate the Next and Prev buttons with other elements (specifically spans within another container). I have attempted the following: $('button.next&apo ...

What is the best way to increase the size of the left margin?

The h1 text needs to be positioned more towards the middle of the screen, but I'm having trouble with the left margin. Adjusting the margin size in pixels and percentages doesn't seem to have any effect on the page. Changing the background color ...

Ways to ensure that a div, header, container, and other elements completely cover the viewport

Currently, I am attempting to create a header that spans the entire viewport regardless of screen size. This header will be split horizontally into 3 equal sections, each occupying one third of the header space. I experimented with setting the header hei ...

Calculate the combined total of two inputted values instantly

Is there a way to add two separate numerical inputs together in real-time and display the sum in a third variable? Here's a clearer explanation: First Variable: 100 (input) Second Variable: 150 (input) Sum: 250 (calculated, not input) Any suggesti ...

What are the steps to adjust image size using CSS in an email signature?

Having trouble with my email signature. It appears fine on my end, but when the recipient replies, the images are oversized and the css is not rendering correctly in their response. I'm puzzled as to why this is happening. Here's the code snippe ...

The effectiveness of border-radius is limited to only one side of the div

After experimenting with applying the border-radius property to a div, I encountered an issue where the border radius only worked on one side when the radius value was too large. How can I resolve this problem while maintaining the border-radius value on a ...

Updating the current date at midnight or on the new day in JavaScript can be achieved using specific date and time

I have a web watch that is supposed to work 24/7 and display the digital time along with the current day. However, my issue is that when the time passes midnight, the current day does not update and I am forced to restart the entire application. I attempt ...

Formatting Outlook HTML Signatures

I'm currently designing an HTML email signature for Outlook, but I'm having trouble with formatting and ensuring it looks consistent across different devices. When I preview the HTML in my browser, it appears exactly as I intended. However, I am ...