Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS circle without using fixed width and height

Tags:

html

css

I want to display the notification count inside a circle but I don't want it to have a fixed width so the circle can expand when there is a bigger number/text inside the circle.

.circle {
  height: 20px;
  width: 20px;
  line-height: 20px;
  border-radius: 50%;
  background-color: red;
  color: white;
  text-align: center;
}
<div class="circle">5</div>
<br>
<div class="circle">102</div>
like image 891
xperator Avatar asked Jan 25 '17 15:01

xperator


1 Answers

See this CSS only solution. Set the same value of min-width and min-height for 1 digit number. Use a pseudo element for vertical alignment and to maintain the square shape. With border-radius applies to the container for the circle.

.circle {
  display: inline-block;
  border-radius: 50%;
  min-width: 20px;
  min-height: 20px;
  padding: 5px;
  background: red;
  color: white;
  text-align: center;
  line-height: 1;
  box-sizing: content-box;
  white-space: nowrap;
}
.circle:before {
  content: "";
  display: inline-block;
  vertical-align: middle;
  padding-top: 100%;
  height: 0;
}
.circle span {
  display: inline-block;
  vertical-align: middle;
}
<div class="circle"><span>8</span></div>
<div class="circle"><span>64</span></div>
<div class="circle"><span>512</span></div>
<div class="circle"><span>4096</span></div>
like image 107
Stickers Avatar answered Oct 20 '22 14:10

Stickers