8

I have this example JSFIDDLE contains a circle, but I'm having trouble to make it responsive according to it's content, in the above example when the content is long, it overflows the circle.

.cd-single-point {
    margin:50px;
  position: absolute;
  border-radius: 50%;
}

.cd-single-point > a {
  position: relative;
  text-align:center;
  padding:2px;
  text-decoration: none;
  z-index: 2;
  display: block;
  width: 20px;
  height: 20px;
  border-radius: inherit;
  background: #d95353;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.3);
  -webkit-transition: background-color 0.2s;
  -moz-transition: background-color 0.2s;
  transition: background-color 0.2s;
}

.cd-single-point::after {
  /* this is used to create the pulse animation */
  content: '';
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  border-radius: inherit;
  background-color: transparent;
  -webkit-animation: cd-pulse 2s infinite;
  -moz-animation: cd-pulse 2s infinite;
  animation: cd-pulse 2s infinite;
}

How can I achieve this only with css?

web-tiki
  • 99,765
  • 32
  • 217
  • 249
Igor
  • 3,573
  • 4
  • 33
  • 55
  • 1
    The elegant solution isn't responsive according to it's content : http://jsfiddle.net/webtiki/tf79qoqp/3/ – web-tiki Aug 14 '14 at 07:44
  • Did you find a solution to your issue? Did my answer help? – web-tiki Aug 14 '14 at 23:44
  • Thanks @web-tiki, but see this: http://jsfiddle.net/Igaojsfiddle/70bznc75/1/. Even using min-height, we still have the same result. We can not leave the circle around tam so small... and try resize your browser. See my update on elegant solution. Hugs! – Igor Aug 15 '14 at 13:26
  • 1
    @lgao it is because you should be using `min-width` instead of `min-height` to keep a minimum size of the circle : http://jsfiddle.net/webtiki/70bznc75/3/ . The elegant solution still doesn't adapt it's size to the content. – web-tiki Aug 17 '14 at 11:27
  • Thanks @web-tiki, and had already applied it. What I mean is that the circle is not responsive. And really elegant solution is not responsive to the content, but for my case I will not need more than three characters within the circle, but I need his size suits. I removed the possible solution! – Igor Aug 19 '14 at 14:42

4 Answers4

4

You could use the padding technique on a pseudo element to keep the aspect ratio of the circle and make it responsive according to it's content :

DEMO

HTML :

<div class="wrap">
    <div class="in">+46546546</div>
</div>

CSS :

.wrap{
    color: #fff;
    position:relative;
    display:inline-block;
}
.in{
    padding:60% 10%;
    margin-top:-0.6em;
}
.in:after{
    content:'';
    position:absolute;
    top:0; left:0;
    width:120%;
    padding-bottom:120%;
    background-color:#D95353;
    border-radius:50%;
    z-index:-1;
}
Community
  • 1
  • 1
web-tiki
  • 99,765
  • 32
  • 217
  • 249
  • Thanks for your reply, but are you sure? http://jsfiddle.net/Igaojsfiddle/53bpm70g/1/ – Igor Aug 13 '14 at 18:44
  • @lgao well the circle gets bigger when you add content, centering the content is an other question, give me a minute, I'll look for a solution. – web-tiki Aug 13 '14 at 18:48
2

How about this fiddle? http://jsfiddle.net/ctwheels/bgut7411/9/

HTML

<ul>
    <li>
        <div class="cd-single-point"> <a class="cd-img-replace" href="#">
            <div class="takeNumber">+99</div>
        </a>

        </div>
    </li>
    <!-- .cd-single-point -->
</ul>



CSS

/* -------------------------------- 

Primary style

-------------------------------- */
 html * {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
body {
    font-size: 100%;
    font-family:"Roboto", sans-serif;
    color: #33435a;
    background-color: #3c4f6a;
}
.cd-single-point {
    margin:50px;
    position: absolute;
    border-radius: 50%;
}
.cd-single-point > a {
    position: relative;
    text-align:center;
    padding:5px;
    text-decoration: none;
    z-index: 2;
    display: block;
    min-width: 20px;
    min-height: 20px;
    border-radius: inherit;
    background: #d95353;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.3);
    -webkit-transition: background-color 0.2s;
    -moz-transition: background-color 0.2s;
    transition: background-color 0.2s;
}
.cd-single-point::after {
    /* this is used to create the pulse animation */
    content:'';
    position: absolute;
    z-index: 1;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    border-radius: inherit;
    background-color: transparent;
    -webkit-animation: cd-pulse 2s infinite;
    -moz-animation: cd-pulse 2s infinite;
    animation: cd-pulse 2s infinite;
}
@-webkit-keyframes cd-pulse {
    0% {
        -webkit-transform: scale(1);
        box-shadow: inset 0 0 5px 5px rgba(217, 83, 83, 0.8);
    }
    50% {
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0.8);
    }
    100% {
        -webkit-transform: scale(1.6);
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0);
    }
}
@-moz-keyframes cd-pulse {
    0% {
        -moz-transform: scale(1);
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0.8);
    }
    50% {
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0.8);
    }
    100% {
        -moz-transform: scale(1.6);
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0);
    }
}
@keyframes cd-pulse {
    0% {
        -webkit-transform: scale(1);
        -moz-transform: scale(1);
        -ms-transform: scale(1);
        -o-transform: scale(1);
        transform: scale(1);
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0.8);
    }
    50% {
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0.8);
    }
    100% {
        -webkit-transform: scale(1.6);
        -moz-transform: scale(1.6);
        -ms-transform: scale(1.6);
        -o-transform: scale(1.6);
        transform: scale(1.6);
        box-shadow: inset 0 0 1px 1px rgba(217, 83, 83, 0);
    }
}
.takeNumber {
    color:white;
    font-family:Verdana;
    font-size:12px;
    padding-top:3px;
}



JS

var numItems = $(".cd-single-point").length;
var myHeight, myWidth;
for (i = 0; i < numItems; i++) {
    myWidth = $(".cd-single-point>a:eq(" + i + ")").width();
    myHeight = $(".cd-single-point>a:eq(" + i + ")").height();
    if (myWidth > myHeight) {
        $(".cd-single-point>a:eq(" + i + ")").css({
            height: myWidth + "px"
        });
    }
    if (myWidth < myHeight) {
        $(".cd-single-point:eq(" + i + ")>a").css({
            width: myHeight + "px"
        });
    }
    $(".takeNumber:eq(" + i + ")").css({
        "line-height": myWidth - parseInt($(".cd-single-point>a:eq(" + i + ")").css("padding"), 10) + "px"
    });
}
ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • This is an answer very complete and very interesting. You thought of everything, including the alignment of the text! Thanks for your reply, even if made with .js – Igor Aug 13 '14 at 19:15
0

If you're not opposed to some javascript you could check the width of the contents and then set an equivalent height.

$(function(){
   var elementWidth = $(".takeNumber").width();
    $(".cd-img-replace").css("height", elementWidth);
});

FIDDLE

Jmh2013
  • 2,625
  • 2
  • 26
  • 42
  • Thanks for your reply, it works, but uses .js. Looks like we have a solution with CSS only our friend @web-tiki. – Igor Aug 13 '14 at 19:14
0

Demo

css

.cd-single-point {
    height: 30px;
    min-width: 30px;
    width: auto;
    line-height: 30px;
    text-align: center;
    margin:50px;
    position: absolute;
    border-radius: 50%;
}
.cd-single-point > a {
    display: block;
    height: 30px;
    min-width: 30px;
    width: auto;
    line-height: 30px;
    text-align: center;
    position: relative;
    text-decoration: none;
    z-index: 2;
    border-radius: inherit;
    background: #d95353;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.3);
    -webkit-transition: background-color 0.2s;
    -moz-transition: background-color 0.2s;
    transition: background-color 0.2s;
}
.takeNumber {
    color:white;
    font-family:Verdana;
    font-size:12px;
    padding: 0 5px;
}
4dgaurav
  • 11,360
  • 4
  • 32
  • 59
  • it does not look like a circle is not it? But anyway thanks for your reply. – Igor Aug 13 '14 at 19:13
  • I assumed that you need a circular label behind the numbers, beacause why will you want to increase the circle radius depending upon its content? I mean it doesn't required in design. Anyways Thanks for appreciation..:) – 4dgaurav Aug 13 '14 at 19:18