Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded divs show edges on smaller sizes

Tags:

html

css

Relevant Codesandbox

I have been seeing a pattern in my app where when I create rounded divs, they sometimes appear to have edges when they are smaller sizes. See the image below of the highlighted code. Why is this happening and is there a way to fix it? Thanks.

enter image description here

index.tsx:

import * as React from "react";
import { render } from "react-dom";
import "./styles.css";

const App = () => (
  <>
    <div className="container">
      <div className="one" />
      <div className="one" />
      <div className="one" />
    </div>
    <div className="container">
      <div className="two" />
      <div className="two" />
      <div className="two" />
    </div>
    <div className="container">
      <div className="three" />
      <div className="three" />
      <div className="three" />
    </div>
    <div className="container">
      <div className="four" />
      <div className="four" />
      <div className="four" />
    </div>
  </>
);

render(<App />, document.getElementById("root"));

styles.css:

.one,
.two,
.three,
.four {
  background: red;
  border-radius: 50%;
}

.container {
  display: flex;
}

.one {
  width: 48px;
  height: 48px;
  margin: 48px;
}

.two {
  width: 16px;
  height: 16px;
  margin: 16px;
}

.three {
  width: 8px;
  height: 8px;
  margin: 8px;
}

.four {
  width: 4.8px;
  height: 4.8px;
  margin: 4.8px;
}
like image 551
Jimmy Avatar asked Jun 30 '20 16:06

Jimmy


People also ask

How do I make a Div stay the same size?

if you set width:500px; and height:300px; (or whatever size you want) the div should stay the same size. Also, you can set overflow:hidden; so that if whatever is in the div goes beyond those bounds, it is not shown, and does not resize the div. Show activity on this post. Try this one.

Why are my divs different sizes?

It happens because your <main> have a fixed height: 400px. If you remove it height rule, it goes up, but due to your float rule, the element would be undocked. Try this solution: jsfiddle.net/alexndreazevedo/cqx09mam The problem is a set of rules: float, position, display... Too complex to explain.

Why do my divs have no height?

The reason why the height or the containers is 0 is because you are floating all the content inside them and floated elements don't expand the height (or width) of their parents.


Video Answer


2 Answers

I can provide a solution using radial-gradient.

enter image description here

.one,
.two,
.three,
.four {
  background: red;
  border-radius: 50%;
}

.container {
  display: flex;
}

.one {
  width: 48px;
  height: 48px;
  margin: 48px;
}

.two {
  width: 16px;
  height: 16px;
  margin: 16px;
}

.three {
  width: 8px;
  height: 8px;
  margin: 8px;
}

.four {
  width: 4.8px;
  height: 4.8px;
  margin: 4.8px;
}

.five {
  width: 5px; /* allow to some margin to prevent distortion. so, a little bit larger than 4.8px */
  height: 5px;
  margin: 4.8px;
  background: radial-gradient(
    circle at 50% 50%,
    red 0 2.4px, /* 2.4 is radius. our actual intended result 2.4 * 2 = 4.8 */
    transparent 2.6px 100% /* add a little bit transition (2.6 - 2.4 = 0.2px) for smoothness.  */
  );
}

.six {
  width: 2.7px;
  height: 2.7px;
  margin: 2.3px;
  background: radial-gradient(
    circle at 50% 50%,
    red 0 1.15px, /* for 2.3px width/height circle */ 
    transparent 1.35px 100%
  );
}
<div class="container">
  <div class="one"></div>
  <div class="one"></div>
  <div class="one"></div>
</div>
<div class="container">
  <div class="two"></div>
  <div class="two"></div>
  <div class="two"></div>
</div>
<div class="container">
  <div class="three"></div>
  <div class="three"></div>
  <div class="three"></div>
</div>
<div class="container">
  <div class="four"></div>
  <div class="four"></div>
  <div class="four"></div>
</div>
<div class="container">
  <div class="five"></div>
  <div class="five"></div>
  <div class="five"></div>
</div>
<div class="container">
  <div class="six"></div>
  <div class="six"></div>
  <div class="six"></div>
</div>

Why we should add transition in radial-gradient?

In the two circles below, you can see more clearly why we need to add transitions.

enter image description here

div {
  height: 300px;
  width: 300px;
  display: inline-block;
}

div:nth-child(1){
  background: radial-gradient(circle at 50% 50%, red 0 150px, transparent 150px 100%);
}

div:nth-child(2){
  /*                                             added 1px transition */
  background: radial-gradient(circle at 50% 50%, red 0 149px, transparent 150px 100%);
}
<div></div>
<div></div>
like image 139
doğukan Avatar answered Oct 19 '22 16:10

doğukan


Here is another idea more flexible where I will consider the use of clip-path and CSS variables.

Instead of using margin, I make the element bigger in width and with clip-path I show only the circular part I want.

.container {
  display: flex;
}
.container * {
  background: red;
  width: calc(var(--s)*3); /* width + 2*margin */
  clip-path: circle(calc(var(--s)/2)); /* radius = width/2 */
}
/* keep the square ratio */
.container *::before {
  content: "";
  display: block;
  padding-top: 100%;
}
/**/
.one {--s: 48px;}
.two {--s: 16px;}
.three {--s: 8px;}
.four {--s: 4.8px;}
.five {--s: 2.3px;}
<div class="container">
  <div class="one"></div>
  <div class="one"></div>
  <div class="one"></div>
</div>
<div class="container">
  <div class="two"></div>
  <div class="two"></div>
  <div class="two"></div>
</div>
<div class="container">
  <div class="three"></div>
  <div class="three"></div>
  <div class="three"></div>
</div>
<div class="container">
  <div class="four"></div>
  <div class="four"></div>
  <div class="four"></div>
</div>
<div class="container">
  <div class="five"></div>
  <div class="five"></div>
  <div class="five"></div>
</div>
like image 4
Temani Afif Avatar answered Oct 19 '22 17:10

Temani Afif