简体   繁体   English

HTML 和 CSS 流体圈

[英]HTML and CSS Fluid Circle

I am trying to create a fluid circle using HTML and CSS.我正在尝试使用 HTML 和 CSS 创建一个流体循环。 I am almost done but as it should be fluid and content inside is dynamic, it's changing its shape from circle to oval and others.我几乎完成了,但由于它应该是流动的并且里面的内容是动态的,所以它的形状从圆形变成了椭圆形等等。

 body { position: relative; }.notify { position: absolute; top: 100%; left: 20%; background: red; border: 2px solid white; border-radius: 50%; text-align: center; }.notify > div { padding: 20px; }
 <div class="notify"> <div> 12 </div> </div>

Can you help me please?你能帮我吗?

The border-radius:50% hack which you're using makes the assumption that the <div> is square prior to the rounded corners being applied, otherwise it will produce an oval rather than a circle, exactly as you've noted.您正在使用的border-radius:50% hack 假设<div>在应用圆角之前是方形的,否则它将产生椭圆而不是圆形,正如您所指出的那样。

Therefore, if you want the circle to remain circular as the content expands, you need to dynamically adjust the height to match the width.因此,如果您希望圆圈在内容扩展时保持圆形,则需要动态调整高度以匹配宽度。 You'd probably need to use Javascript to achieve this.您可能需要使用 Javascript 来实现这一点。

Also, please note that border-radius is not supported in older versions of IE, so users with IE6, IE7 or IE8 won't see your circle at all.另外请注意,旧版本的 IE 不支持border-radius ,所以使用 IE6、IE7 或 IE8 的用户根本看不到你的圈子。 (though there is a hack for it called CSS3Pie ) (虽然有一个叫做CSS3Pie的 hack )

Of course, adjusting the height will have the side effect of making the element take up more space vertically.当然,调整height会有一个副作用,就是让元素在垂直方向上占据更多的空间。 This may not be what you want;这可能不是您想要的; you may want the the circle to be the same size regardless of what content is in it?无论里面有什么内容,您可能希望圆圈大小相同? In this case, you should fix the height and width of the circle, and give the content position:absolute;在这种情况下,你应该固定圆的高度和宽度,并给出内容position:absolute; to prevent it from affecting the size of its parent.以防止它影响其父级的大小。

An alternative to using the border-radius hack to produce a circle would be to use SVG.使用border-radius hack 生成圆的另一种方法是使用 SVG。 SVG is a vector graphics format which is embedded into most browsers. SVG 是嵌入到大多数浏览器中的矢量图形格式。

Again, the notable exception is IE8 and earlier, but IE supports an alternative format called VML.同样,值得注意的例外是 IE8 和更早版本,但 IE 支持称为 VML 的替代格式。 Various scripts exist which can convert between SVG and VML, so you can produce a cross-browser solution with SVG plus Javascript.存在各种可以在 SVG 和 VML 之间转换的脚本,因此您可以使用 SVG 和 Javascript 生成跨浏览器解决方案。

If we're going to accept the Javascript is part of the solution, you could simply use a javascript library to draw it in the first place.如果我们要接受 Javascript 是解决方案的一部分,您可以简单地首先使用 javascript 库来绘制它。 My suggestion for this would be Raphael , which generates SVG or VML graphics according to the browser it's running it.我对此的建议是Raphael ,它会根据运行它的浏览器生成 SVG 或 VML 图形。

Hope that helps.希望有帮助。

You need to set both width and height to the maximum of these both, in order to render a square, that with 50% radius corners, results into a circle.您需要将widthheight都设置为这两者中的最大值,以渲染一个正方形,该正方形具有 50% 的半径角,从而形成一个圆形。

You can do this in jQuery:您可以在 jQuery 中执行此操作:

$(function() {
  var $elem = $(".notify > div");
  var maxSize = Math.max($elem.width(), $elem.height());

  $elem.width(maxSize).height(maxSize);
});

Try to change content (both in width and height) here尝试在此处更改内容(宽度和高度)

Example of a fluid circle using only HTML and CSS.仅使用 HTML 和 CSS的流体循环示例。 As mentioned in my comments to the question, the technique is explained in my blog post .正如我在对该问题的评论中提到的,该技术在我的博客文章中进行了解释。 Also as mentioned, Safari does not currently play nice with a border radius specified as a percentage.同样如前所述,Safari 目前在以百分比指定的边界半径时效果不佳。

The way as Jose Rui Santos did you can achieve your goal.Jose Rui Santos那样,您可以实现您的目标。 But do few changes in your css.但是对你的 css 做一些改变。

Like remove padding from .notify > div and adding styles like this:就像从.notify > div中删除填充并像这样添加 styles :

.notify > div
{    
    display: table-cell;
    vertical-align: middle;
}

and add padding into .notify class like this:并将填充添加到.notify class 中,如下所示:

.notify
{
    position: absolute;
    top: 100%;
    left: 20%;
    background: red;
    border: 2px solid white;
    border-radius: 50%;
    padding: 30px;
    text-align: center;

}

and Jquery code as mentioned by Jose Rui Santos :Jose Rui Santos提到的 Jquery 代码:

var $elem = $(".notify > div");
var maxSize = Math.max($elem.width(), $elem.height());
$elem.width(maxSize).height(maxSize);

See the working Demo: http://jsfiddle.net/2j5Ek/63/请参阅工作演示: http://jsfiddle.net/2j5Ek/63/

you need a way to enforce height / width otherwise it will just go oval... its possible!你需要一种方法来强制高度/宽度,否则它只会 go 椭圆形......它可能!

on this html在这个 html

<div class="notify">
    <div class="child">
        12334        
    </div>
</div>

this jQuery script should do it..这个 jQuery 脚本应该可以做到。

var cw = $('.child').width();
$('.child').css({
    'height': cw + 'px',
    'line-height': cw + 'px'
});

Forcing it's maximum height and weight by setting max-width:XXpx; max-height:XXpx通过设置max-width:XXpx; max-height:XXpx max-width:XXpx; max-height:XXpx will do the job. max-width:XXpx; max-height:XXpx将完成这项工作。

Please note that you may need to use CSS3 word-wrap: break-word;请注意,您可能需要使用 CSS3 word-wrap: break-word; to break the words.打破的话。 Cross-browser compatibility might be an issue.跨浏览器兼容性可能是个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM