将一个固定的“箭头”元素置于另一个固定的“圆圈”元素中心

问题描述 投票:0回答:2

我无法将箭头置于红色圆圈内。如何将一个固定元素置于另一个固定元素中?

的jsfiddle:https://jsfiddle.net/sebastian3495/xtj9cga2/4/

html, body {
  height: 1000px;
  width: 100%;
}
#a {
  width: 100%;
  height: 100%;
  border: 1px solid black;
  position:relative;
}
#wrapper {
  position: absolute;
  top: 50vh;
}
#b {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: red;
  position: fixed;
}
#c {
  border: solid black;
  border-width: 0 3px 3px 0;
  position: fixed;
  width: 50px;
  height: 50px;
  transform: rotate(-45deg);
}
<div id="a">
  <div id="wrapper">
    <i id="b"></i>
    <i id="c"></i>
  </div>
</div>
html css position
2个回答
1
投票

您可以简单地使用下面的代码,然后您可以轻松地将箭头居中并调整尺寸:

.arrow {
  background:red;
  width:100px;
  height:100px;
  border-radius:50%;
  position:fixed;
  top:100px;
  left:50px;
}
.arrow::before {
  content:"";
  position:absolute;
  top:50%;
  left:50%;
  width:50%;
  height:50%;
  border-top:3px solid;
  border-right:3px solid;
  /*75% instead of 50% since we need to center half the shape so 50% then 25%*/
  transform:translate(-75%,-50%) rotate(45deg);
  
}
<div class="arrow"></div>

如果没有伪元素,您仍然可以简化更多:

.arrow {
  width:100px;
  height:100px;
  padding:35px 35px 0 0;
  border-radius:50%;
  position:fixed;
  top:100px;
  left:50px;
  background:
    linear-gradient(#000,#000) top right/77% 3px,
    linear-gradient(#000,#000) top right/3px 77%,
    red;
  background-repeat:no-repeat;
  background-origin:content-box;
  transform:rotate(45deg);
  box-sizing:border-box;
}
<div class="arrow"></div>

0
投票

调整i标签的顶部和左侧位置,id =“c”

#c {
    border: solid black;
    border-width: 0 3px 3px 0;
    position: fixed;
    width: 50px;
    height: 50px;
    left:3%;
    top:60vh;
    transform: rotate(-45deg);
}
© www.soinside.com 2019 - 2024. All rights reserved.