【问题标题】:Responsive circle based on it's content基于内容的响应圈
【发布时间】:2014-10-07 05:52:46
【问题描述】:

我有这个例子JSFIDDLE包含一个圆圈,但是我很难根据它的内容做出响应,在上面的例子中,当内容很长时,它会溢出圆圈.

.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;
}

我如何仅使用 css 实现这一目标?

【问题讨论】:

  • 优雅的解决方案根据其内容没有响应:jsfiddle.net/webtiki/tf79qoqp/3
  • 您找到解决问题的方法了吗?我的回答有帮助吗?
  • 感谢@web-tiki,但请参阅:jsfiddle.net/Igaojsfiddle/70bznc75/1。即使使用最小高度,我们仍然得到相同的结果。我们不能让 tam 周围的圆圈如此之小......并尝试调整浏览器的大小。请参阅我对优雅解决方案的更新。拥抱!
  • @lgao 这是因为你应该使用min-width 而不是min-height 来保持圆圈的最小尺寸:jsfiddle.net/webtiki/70bznc75/3。优雅的解决方案仍然无法根据内容调整其大小。
  • 谢谢@web-tiki,已经申请了。我的意思是圈子没有反应。真正优雅的解决方案是对内容不敏感,但就我而言,圆圈内不需要超过三个字符,但我需要他的尺码套装。我删除了可能的解决方案!

标签: css responsive-design geometry css-shapes


【解决方案1】:

您可以在伪元素上使用padding technique 来保持圆的纵横比并使其根据内容做出响应:

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;
}

【讨论】:

  • 感谢您的回复,但您确定吗? jsfiddle.net/Igaojsfiddle/53bpm70g/1
  • @lgao 好吧,当您添加内容时,圆圈会变大,将内容居中是另一个问题,请稍等,我会寻找解决方案。
【解决方案2】:

这个小提琴怎么样? 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"
    });
}

【讨论】:

  • 这是一个非常完整且非常有趣的答案。你想到了一切,包括文本的对齐方式!感谢您的回复,即使是使用 .js 制作的
【解决方案3】:

如果您不反对某些 javascript,您可以检查内容的宽度,然后设置等效高度。

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

FIDDLE

【讨论】:

  • 感谢您的回复,它有效,但使用.js。看起来我们有一个只有我们的朋友@web-tiki 的 CSS 解决方案。
【解决方案4】:

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;
}

【讨论】:

  • 它看起来不像是一个圆圈是吗?不过还是谢谢你的回复。
  • 我假设您需要在数字后面添加一个圆形标签,因为您为什么要根据其内容来增加圆半径?我的意思是它在设计中不需要。无论如何感谢您的赞赏..:)
猜你喜欢
  • 2015-07-07
  • 2014-12-07
  • 2016-04-19
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
相关资源
最近更新 更多