如何使用CSS在另一个内部制作一个圆圈

时间:2014-03-14 13:40:37

标签: css css3

我正在尝试使用css在另一个圆圈内创建一个圆圈,但我遇到了一个完全居中的问题。我很亲密,但仍然没有。有什么想法吗?

<div id="content">
    <h1>Test Circle</h1>
    <div id="outer-circle">
        <div id="inner-circle">
            <span id="inside-content"></span>
        </div>
    </div>
</div>

这是我的CSS:

#outer-circle {
    background: #385a94;
    border-radius: 50%;
    height:500px;
    width:500px;
}
#inner-circle {
    position: relative;
    background: #a9aaab;
    border-radius: 50%;
    height:300px;
    width:300px;
    margin: 0px 0px 0px 100px;
}

另外,这是一个小提琴: http://jsfiddle.net/972SF/

15 个答案:

答案 0 :(得分:26)

塔达!

在CSS评论中解释:

 #outer-circle {
   background: #385a94;
   border-radius: 50%;
   height: 500px;
   width: 500px;
   position: relative;
   /* 
    Child elements with absolute positioning will be 
    positioned relative to this div 
   */
 }
 #inner-circle {
   position: absolute;
   background: #a9aaab;
   border-radius: 50%;
   height: 300px;
   width: 300px;
   /*
    Put top edge and left edge in the center
   */
   top: 50%;
   left: 50%;
   margin: -150px 0px 0px -150px;
   /* 
    Offset the position correctly with
    minus half of the width and minus half of the height 
   */
 }
<div id="outer-circle">
  <div id="inner-circle">

  </div>
</div>

答案 1 :(得分:18)

你不需要CSS3中的额外元素

你可以使用一个元素和一个盒子阴影来完成所有操作。

JSFiddle Demo.

<强> CSS

#outer-circle {
    background: #385a94;
    border-radius: 50%;
    height:300px;
    width:300px;
    position: relative;
    box-shadow: 0 0 0 100px black;
    margin:100px;
}

答案 2 :(得分:3)

在外圈上使用position: relative,在内圈上使用position:absolute,并将所有偏移设置为相同的值。让高度和宽度的自动计算处理其余部分(JSFiddle):

#outer-circle {
    position:relative;
    background: #385a94;
    border-radius: 50%;
    height:500px;
    width:500px;
}
#inner-circle { 
    position:absolute;
    background: #a9aaab;
    border-radius: 50%;
    right: 100px;
    left: 100px;
    top: 100px;
    bottom: 100px;
    /* no margin, no width, they get automatically calculated*/
}

答案 3 :(得分:2)

似乎top是您唯一需要改变的地方 - &gt; http://jsfiddle.net/972SF/12/

#inner-circle {
    position: relative;
    background: #a9aaab;
    border-radius: 50%;
    height:300px;
    width:300px;
    top: 100px; /* <--- */
    margin: 0px 0px 0px 100px;
}

答案 4 :(得分:2)

只需使用box-shadow即可获得所需的效果:

小提琴演示:http://jsfiddle.net/972SF/16/

html简化为:

<div id="content">
    <h1>Test Circle</h1>
    <div id="circle">
    </div>
</div>

的CSS:

#circle {
    margin: 10em auto;
    background: #385a94;
    border-radius: 50%;
    height:200px;
    width:200px;
    -webkit-box-shadow: 1px 1px 0px 100px black;
       -moz-box-shadow: 1px 1px 0px 100px black;
            box-shadow: 1px 1px 0px 100px black;
}

简单易用,确保您的圈子始终完美地位于彼此旁边。

您可以通过将box-shadow上的第4个属性(100px)更改为您想要的内容来更改圆的大小。

答案 5 :(得分:1)

看看这个fiddle

自动计算中心

#outer-circle {
    background: #385a94;
    border-radius: 50%;
    height:500px;
    width:500px;
    display:table-cell;
    vertical-align:middle;
}
#inner-circle {
    display:inline-block;
    background: #a9aaab;
    border-radius: 50%;
    height:300px;
    width:300px;
}

答案 6 :(得分:1)

以下是带有外边框的圆圈的示例。

HTML:

<div id="inner-circle"></div>

样式:

    #inner-circle {
    background: #385a94;
    border : 2px solid white;
    border-radius: 50%;
    height:30px;
    width:30px;
    position: relative;
    box-shadow: 0 0 0 1px #cfd1d1;
    }

查看结果: JSFiddle

答案 7 :(得分:1)

使用CSS转换属性解决了此问题:

您可以参考此JS fiddle link以获得以下输出:   http://jsfiddle.net/suprabhasupi/74b1ptne/ Circle inside circle

div {
  border-radius: 50%;
  /* border: 1px solid red; */
}

.circle1 {
  position: relative;
  width: 300px;
  height: 300px;
  background-color: red;
}

.circle2 {
  transform: translate(25%, 25%);
  width: 200px;
  height: 200px;
  background-color: green;
}

.circle3 {
   transform: translate(48%, 46%);
  width: 100px;
  height: 100px;
  background-color: blue;
}
<div class="circle1">
  <div class="circle2">
    <div class="circle3">
    
    </div>
  </div>
</div>

答案 8 :(得分:1)

如果您只想使用一个div在圆内添加圆,则使用框阴影。

div {
  border-radius: 50%;
  box-shadow: 0px 0px 0px 10px red, 0px 0px 0px 20px green, 0px 0px 0px 30px yellow, 0px 0px 0px 40px pink;
  width: 100px;
  height:100px;
  margin: 3em;
}
<div></div>

答案 9 :(得分:0)

尝试,

 #inner-circle {
    position: absolute;
    background: #a9aaab;
    border-radius: 50%;
    height:300px;
    width:300px;
    margin: 15% 0px 0px 100px;
}

这是您更新的JSFIDDLE

答案 10 :(得分:0)

请参阅我如何定位Div,Just border-radius应该执行Job

.outer{width:500px;height:500px;background:#f00;border-radius:50%;position:relative;top:0;left:100;}
.inner{width:250px;height:250px;background:#000;border-radius:50%;position:absolute;top:125;left:125;}



   <div class="outer">

      <div class="inner">


      </div>

    </div>

DEMO

答案 11 :(得分:0)

尝试给内圈一个top:50%而不是margin-top:来自内圈高度的一半的负值。

http://jsfiddle.net/972SF/19/

答案 12 :(得分:0)

解决了!完全按照你想要的方式:

DEMO:http://jsfiddle.net/aniruddha153/RLWua/

HTML:

<div id="content">
   <div id="outer-circle">
      <div id="inner-circle">
      </div>
   </div>
</div>

CSS:

#content {
   position: relative;
   width: 100%;
   padding-bottom: 100%;
}

#outer-circle {
   position: absolute;
   width: 50%;
   height: 50%;
   background-color: #000000;
   border-radius: 50%;
}

#inner-circle{
  margin-top: 25%;
  margin-left: 25%;
  position: absolute;
  width: 50%;
  height: 50%;
  background-color: #e5e5e5;
  border-radius: 50%;
}

答案 13 :(得分:0)

您可以使用 CSS 的 top 和 left 属性将其居中。

body {
    width: 100%
    margin:0px;
    text-align: center;
}
#content {
    width: 500px;
    margin-left: auto;
    margin-right: auto;
}
#outer-circle {
    background: #385a94;
    border-radius: 50%;
    height:200px;
    width:200px;
}
#inner-circle {
    position: relative;
    background: #a9aaab;
    border-radius: 50%;
    height:100px;
    width:100px;
  top:50px;
  left:50px;
}
<div id="content">
    <h1>Test Circle</h1>
    <div id="outer-circle">
        <div id="inner-circle">
            <span id="inside-content"></span>
        </div>
    </div>
</div>

答案 14 :(得分:0)

你可以用简单的 3 行 css 来做到这一点

 .outer-circle{
    display: flex;
    justify-content: center;
    align-items: center;
  }

如果你想把所有东西都放在内圈中,做你在外圈里做的事情

有关完整源代码,请查看此JSFIDDLE

相关问题