【问题标题】:Responsive CSS Circles响应式 CSS 圆圈
【发布时间】:2012-10-08 09:27:32
【问题描述】:

目标:

响应式 CSS 圆圈:

  1. 等半径缩放。
  2. 半径可以按百分比计算。
  3. 半径可以通过媒体查询来控制。

如果解决方案是 javascript,我仍然需要模拟媒体查询触发器。我不需要媒体查询,但我确实希望能够在某些宽度下按百分比控制半径:

@media (max-width : 320px) 
{
    .x2{padding: 50%;}
}

@media (min-width : 321px) and (max-width : 800px)
{
    .x2{padding: 25%;}
}

@media (min-width: 801px)
{
    .x2{padding: 12.5%;}
}

这是我目前所拥有的:

http://jsfiddle.net/QmPhb/

<div class="x1">
    <div class="x2">
        lol dude      
    </div>
    <div class="x2"></div>
    <div class="x2"></div>
    <div class="x2"></div>
</div>

.x1
{
    float:left;
    width:100%;
}

.x2
{
    display:block;
    float:left;
    padding: 12.5%;          //Currently being used to control radius.
    width:auto;
    height:auto;
    border-radius:50%;
    -moz-border-radius:50%;
    -webkit-border-radius:50%;
    -khtml-border-radius: 50%;
    background:#eee;
    text-align:center;
}

问题:

在此解决方案中,将内容添加到圆圈时:

  • 当缩放超过可用填充时,形状会扭曲。
  • 增加半径的大小。

更新:

我在这里为此构建了一个可行的解决方案: Responsive CSS Circles

【问题讨论】:

  • 您列出的问题是由width: auto; height: auto;引起的。固定尺寸可以接受吗?也许有相对单位?
  • 很难让它只与 CSS 一起工作,因为宽度和高度需要相同,而且由于它是响应式的,所以你无法分辨。
  • @bfavaretto 确实需要像示例一样按百分比计算半径或直径。
  • @coopersita 我并不反对使用 javascript,但我们仍然需要牢记媒体查询触发器。
  • @PraveenKumar 我不需要它们哈哈,但我确实希望能够在某些宽度下按百分比控制半径。即使解决方案使用 javascript。

标签: html css responsive-design css-shapes


【解决方案1】:

您不需要对此进行@media 查询。这是我的尝试,纯 CSS:

.x1 {
    overflow:hidden;
}
.x1 .x2 {
    display:block;
    float:left;
    padding: 12.5%;
    width:auto;
    height:auto;
    border-radius:50%;
    -moz-border-radius:50%;
    -webkit-border-radius:50%;
    -khtml-border-radius: 50%;
    background:#eee;
    text-align:center;
    position: relative;
}
.x1 .x2 span {
    position: absolute;
    width: 100%;
    left: 0;
    top: 48%;
    line-height: 1em;
    height: 1em;
    font-size: 100%;
    overflow: hidden;
}​

Fiddle

Full Screen

【讨论】:

  • 这肯定解决了溢出问题。我想我希望 Circles 堆叠 @ 25% 填充(2 行)和 50% 填充(4 行)。这个解决方案有可能吗?
  • 是的,为此我们需要使用媒体查询! :)
  • 希望这能为我需要的那种灵活性提供更好的背景。 jsfiddle.net/tgXKK
  • 我对该代码进行了多次修改,并尽我所能将其缩小。我添加了几个属性以允许子内容容器元素具有更大的灵活性。请参阅上面的修改。
  • 您的解决方案确实只解决了一半的问题。尽管它受到了很多关注,但这并不意味着当人们尝试在 jsfiddle 之外使用它时,它实际上是可移植的。我多次要求您修改答案以适应赏金,但您从未修改过。
【解决方案2】:

短代码

此解决方案减少了您的代码大小,但保留了功能。我保留了原来的.x#,去掉了不需要的.x0.x3.x6。因此,在最终解决方案中,您可能会重新编号(但我希望您看看消除了什么)。

任何需要不同topleft 设置的圆圈“分裂”的作品都需要有一个满足或超过.x2 &gt; div 选择器的选择器才能覆盖,因此我有.x2 &gt; .x7 等. 对于我的一些选择器。

(如下面的 cmets 所述,Chrome has bug issues 使用 OP 在赏金开始时发布的原始技术。这并不能解决这些问题,因此请在另一个浏览器中查看以下内容。)

Here's the modified fiddle.

HTML

<div class="x1">
        <div class="x2">
                <!-- BEG Content -->
                <div class="x4">
                    dude
                </div>
                <div class="x7">
                    dude
                </div>
                <div class="x8">
                    dude
                </div>
                <div class="x5">
                    dude
                </div>
                <!-- END Content -->
        </div>
        <div class="x2"></div>
        <div class="x2"></div>
        <div class="x2"></div>
    </div>

CSS

.x1 {
    margin:0px auto;
}
.x2 {
    overflow:hidden;
    display:block;
    float:left;
    width:auto;
    height:auto;
    position: relative;
    border-radius:50%;
    -moz-border-radius:50%;
    -webkit-border-radius:50%;
    -khtml-border-radius: 50%;
    background:#eee;
}

/* BEG Content */
.x2 > div {
    position: absolute;
    text-align: center;
    top: 0;
    left: 0;
}
.x4,.x5 {
    width:100%;
    height: 20%;
}
.x2 > .x7, .x2 > .x8 {
    width:50%;
    height: 60%;
    top: 20%;
}
.x4 {
    background-color:blue;
}
.x2 > .x5 {
    background-color:yellow;
    top: 80%;
}

.x7 {
    background-color:green;
}
.x2 > .x8 {
    background-color:orange;
    left: 50%;
}
/* END Content */
@media (max-width: 320px)
{
    .x2 {padding: 50%;}
}

@media (min-width: 321px) and (max-width: 800px)
{
    .x2 {padding: 25%;}
}

@media (min-width: 801px)
{
    .x1 {width:800px}
    .x2 {padding: 12.5%;}
}

编辑:基于 cmets,看来 OP 需要更像 the control this fiddle offers 的东西(在 Chrome 中不起作用;在此编辑时 OP 还没有回复让我知道这是否是所需的功能类型)。

【讨论】:

  • 啊,是的,我没说对。 .x2 div 堆叠良好。您还消除了.x3...这很棒。我的意思是使用覆盖在.x2 子元素中应用样式是不可取的。
  • @DanKanze--在这些圈子内的“布局”中,您想要多少灵活性而不是标准化?它们都一样吗?每一个都不一样?您的目标是“改进”和“更小”。好吧,我的代码变小了,但什么是“改进”?这在很大程度上取决于您想要什么功能。
  • 从开发人员的角度来看,理想解决方案将是一种易于构建(在 .x2 块内推出样式而无需覆盖)并优化任意代码(修剪fat like .x3 仅用作帮助类以适应 .x2 中的构建灵活性)。一个理想的解决方案是一个单一的类,使开发人员也可以像block一样使用.x2并包含它们。
  • @DanKanze--你在 Chrome 中查看过你原来的小提琴解决方案吗?由于this question 中提到的错误,它无法隐藏溢出。
  • @DanKanze--Here is a fiddle(在 Firefox 或 IE9+ 中查看,参见上面的评论)通过内容包装器上的 layout 类控制事物。你在说什么吗?当然不一定是尺寸“更小”,但布局类可以由开发者编写,用户应用的类。
【解决方案3】:

我知道这个解决方案与这里建议的有很大不同,但我仍然认为值得提出。

我使用图像作为掩码来创建圆形,并利用了这样一个事实,即当填充指定为百分比时,它是根据其父元素的宽度而不是高度来计算的。这使我能够创建一个完美的正方形。

按比例调整大小的圆圈演示here

HTML 代码

<div class="container">
    <img class="circle" src="circleImage.png">
</div>

CSS 代码

.container {
    position: relative;
    float: left;
    width: 50%;
    height: 0;
    padding-bottom: 50%;
    background-color: #bbb;

}

.circle { 
    position: absolute;
    width: 100%;
    left: 0;
    top: 0;
    height: auto;
    z-index: 1;
}

@media (max-width: 320px) {
    .container { width: 100%; padding-bottom: 100%; }
}

@media (min-width: 321px) and (max-width: 800px) {
    .container { width: 50%; padding-bottom: 50%; }
}

@media (min-width: 801px) {
    .container { width: 25%; padding-bottom: 25%; }
}

根据您的问题here

【讨论】:

  • 这个解决方案的 box-shadow 属性怎么样?
【解决方案4】:

解决方案:

http://jsfiddle.net/WTWrB/

DIV 结构:

我们在.x2 中使用overflow:hidden.x3 中溢出背景颜色 子元素。

注意内容从.x3开始

<div class="x0">
    <div class="x1">
        <div class="x2">
            <div class="x3">
                <!-- BEG Content -->
                <div class="x4">
                    dude
                </div>
                <div class="x6">
                    <div class="x7">
                        dude
                    </div>
                    <div class="x8">
                        dude
                    </div>
                </div>                
                <div class="x5">
                    dude
                </div>
                <!-- END Content -->
            </div>
        </div>
        <div class="x2"></div>
        <div class="x2"></div>
        <div class="x2"></div>
    </div>
</div>

CSS:

@media (max-width: 320px)
{
    .x2 {padding: 50%;}
}

@media (min-width: 321px) and (max-width: 800px)
{
    .x2 {padding: 25%;}
}

@media (min-width: 801px)
{
    .x1 {width:800px}
    .x2 {padding: 12.5%;}
}
.x0 {
    float:left;
    width:100%;
}
.x1 {
    margin:0px auto;
}
.x2 {
    overflow:hidden;
    display:block;
    float:left;
    width:auto;
    height:auto;
    position: relative;
    border-radius:50%;
    -moz-border-radius:50%;
    -webkit-border-radius:50%;
    -khtml-border-radius: 50%;
    background:#eee;
}
.x3 {
    position: absolute;
    width: 100%;
    left: 0;
    top:0;
    font-size: 100%;
    float:left;
    height:100%;
    background-color:red;
}
/* BEG Content */
.x3 div{float:left;}
.x4,.x5,.x6 {
    width:100%;
}
.x7,.x8 {
    width:50%;
    float:left;
    height:100%;
}
.x4,.x5,.x7,.x8 {
    text-align:center;
}
.x4 {
    background-color:blue;
    height:20%;
}
.x5 {
    background-color:yellow;
    height:20%;
}
.x6 {
    height:60%;
}
.x7 {
    background-color:green;
}
.x8 {
    background-color:orange;
}
/* END Content */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2020-09-07
    相关资源
    最近更新 更多