如何将元素放在圆角中间?

时间:2017-03-11 10:49:58

标签: css3

enter image description here

我有一个元素(灰色的)120px / 120px,边界半径为50%,我想把另一个元素(橙色的)放在左上边框的中间。我怎么能这样做?

这是我的代码:

  top: 0;
  left: 60px;
  transform: rotate(45deg);

但元素只围绕它的中心移动。

修改

工作样本:jsfiddle

4 个答案:

答案 0 :(得分:3)

我设法通过使用一些数学来实现它:

  1. 将元素放入top=0 ; left:50%;
  2. translateX x = - (height/2 * cos(45deg)) = -60 * 0.707 = -42px
  3. 翻译y = height/2 * (1 - sin(45deg)) = 60*(1 - 0.707) = 18px
  4. translateX和translateY -50%到中心;
  5. transform: translateX(-50%) translateY(-50%) translateX(-42px) translateY(18px);

    修改

    要使解决方案具有响应性(如果大圆是40vh / 40vh):

    transform: translateX(-50%) translateY(-50%) translateX(calc(-40vh/2 * 0.707)) translateY(calc(40vh/2 * 0.3));

    以下是一个工作示例:jsfiddle

    body {
      padding-top: 50px;
    }
    .big-circle {
      width: 40vh;
      height: 40vh;
      border-radius: 50%;
      background-color: gray;
      position: relative;
    }
    
    .small-circle {
      position: absolute;
      width: 30px;
      height: 30px;
      background-color: coral;
      top:0;
      left: 50%;
      border-radius: 50%;
      transform: translateX(-50%) translateY(-50%) translateX(calc(-40vh/2 * 0.707)) translateY(calc(40vh/2 * 0.3));
      color: #fff;
      text-align: center;
      line-height: 30px;
    }
    <div class="big-circle">
      <div class="small-circle">
      12
      </div>
    </div>

答案 1 :(得分:2)

根据评论更新

你可以这样做,使用你旋转的包装器,它也会响应

body {
  padding: 50px;
  display: flex;
}
.big-circle, .small-circle {
  position: relative;  
  width: 40vh;
  height: 40vh;
  border-radius: 50%;
  background-color: gray;
}
.small-circle {
  position: absolute;
  left: 0; top: 0;
  transform: rotate(45deg);
}
.small-circle div {
  position: absolute;
  width: 30px;
  height: 30px;
  background-color: coral;
  top: calc(50% - (30px / 2));
  left: -15px;
  border-radius: 50%;
  line-height: 30px;
  transform: rotate(-45deg);      /*  this will reverse the rotation/content  */
}
.big-circle-content {
  position: relative;
  width: 40vh;
  height: 40vh;
  line-height: 40vh;
  border-radius: 50%;
  padding: 0 10px;
  box-sizing: border-box;
}
<div class="big-circle">
  <div class="small-circle">
    <div>123</div>
  </div>
  <div class="big-circle-content">
    Content
  </div>
</div>

<击> 你可以这样做,使用伪,也会响应

Updated fiddle

body {
  padding: 50px;
  display: flex;
}
.big-circle, .small-circle {
  width: 40vh;
  height: 40vh;
  border-radius: 50%;
  background-color: gray;
  position: relative;  
}
.small-circle {
  transform: rotate(45deg);
}
.big-circle:nth-child(2) .small-circle {
  transform: rotate(135deg);
}
.small-circle::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 30px;
  background-color: coral;
  top: calc(50% - (30px / 2));
  left: -15px;
  border-radius: 50%;
}
<div class="big-circle">
  <div class="small-circle">  
  </div>
</div>

<div class="big-circle">
  <div class="small-circle">  
  </div>
</div>

如果big-circle不包含其他元素,您可以删除small-circle元素并在rotate

上设置伪和big-circle

body {
  padding: 50px;
  display: flex;
}
.big-circle {
  width: 40vh;
  height: 40vh;
  border-radius: 50%;
  background-color: gray;
  position: relative;  
  transform: rotate(45deg);
}
.big-circle:nth-child(2) {
  transform: rotate(135deg);
}
.big-circle::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 30px;
  background-color: coral;
  top: calc(50% - (30px / 2));
  left: -15px;
  border-radius: 50%;
}
<div class="big-circle">
</div>

<div class="big-circle">
</div>

答案 2 :(得分:1)

只需使用以下代码修改代码:

<强>更新:

.small-circle {
  position: absolute;
  width: 30px;
  height: 30px;
  background-color: coral;
  top:12%;
  left: 20%;
  border-radius: 50%;
  transform: translateX(-50%) translateY(-50%);
}

答案 3 :(得分:0)

&#13;
&#13;
body {
  padding-top: 50px;
}
.big-circle {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background-color: gray;
  position: relative;
}

.small-circle {
  position: relative;
  width: 30px;
  height: 30px;
  background-color: coral;
  top: 5px;
  left: 5px;
  border-radius: 50%;
  //transform: translateX(-50%) translateY(-50%);
}
&#13;
<div class="big-circle">
  <div class="small-circle">
  
  </div>
</div>
&#13;
&#13;
&#13;