html - 围绕可变高度的 div 画一个完美的圆

标签 html jquery css

我对此进行了相当多的研究,但似乎找不到一个好的、可靠的答案来找到如何围绕可变高度的 div 元素制作一个响应式圆圈

使用 vw 单位可以轻松制作简单的响应式圆圈。

<div style="height:20vw; width:20vw"></div>

但是,我希望使用元素的最小高度并在该 div 周围有一个圆圈。

创建响应式圆圈的另一种方法是使用类似于下面代码片段的内容,但我再次无法调整它以适应可变高度(同样,我不能使用 vh 单位作为div 的高度将会改变。

.square {
  position: relative;
  width: 10%;
  background: gray;
  border-radius: 50%;
}

.square:after {
  content: "";
  display: block;
  padding-bottom: 100%;
}

.content {
  position: absolute;
  width: 100%;
  height: 100%;
}
<div class="square">
  <div class="content">

  </div>
</div>

我正在尝试创建如下所示的内容,其中圆圈永远不会切入 div 的 Angular (具有大约 10px 的填充)。我个人试图避免使用 javascript,并且更喜欢仅使用 css 的方法,但这似乎是不可避免的。也许唯一的解决方案是使用 jquery 计算元素的高度,以便将其应用于包装元素?

enter image description here

我正在玩这个:

.square {
  position: absolute;
  top: 50%;
  display: inline-block;
  left: 50%;
  transform: translate(-50%, -50%);
  min-height: 100px;
  border-radius: 50%;
  background: url('https://i.imgur.com/2dxaFs9_d.webp?maxwidth=640&shape=thumb&fidelity=medium');
  background-size: 100% 100%;
  padding: 20px;
}

.content {
  width: 300px;
  min-height: 100px;
  background: tomato;
}
<div class="square">
  <div class="content">
    Hello!<br>
    <br><br><br>This has a variable height but fixed width<br><br><br>Hello
  </div>
</div>

最佳答案

Clip-path 如果您考虑纯色,可以轻松做到这一点。

调整元素大小,圆圈将跟随:

.box {
  width: 200px;
  height: 200px;
  overflow: hidden;
  resize: both;
  background: blue;
  box-shadow: 0 0 0 200vmax red;
  clip-path: circle(71%);
  margin: 100px auto;
}
<div class="box"></div>

了解魔数(Magic Number)71%的相关问题:clip-path:circle() radius doesn't seem to be calculated correctly


要使用图像,我们可以考虑伪元素。您还可以依靠 calc() 添加偏移量:

.box {
  width: 200px;=
  resize: both;
  clip-path: circle(calc(71% + 10px));
  margin: 100px auto;
  position: relative;
  font-size:35px;
  color:#fff;
}
/* the background layer */
.box::before {
  content:"";
  position: absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background:blue;
}

/* the image layer */
.box::after {
  content:"";
  position: fixed; /* to make sure the image cover all the screen */
  z-index:-2;
  top:0;
  bottom:0;
  left:0;
  right:0;
  background:url(https://picsum.photos/id/1015/1000/1000) center/cover no-repeat;
}
<div class="box" contenteditable="true"> Edit this<br>text </div>

关于html - 围绕可变高度的 div 画一个完美的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62983632/

相关文章:

html - 如何强制嵌套 div 完全适合父级?

javascript - 如何使kinetic.js全屏背景 View

javascript - 在 Chrome 和 Safari 上,菜单仍处于悬停状态

html - 划线/删除线穿过整个 HTML 表格行

html - 有没有办法在文本前加下划线空格

javascript - jQuery - 获取具有特定类的表行数

php - 我有一个表,我用它通过 ajax 或 jquery 更新 mysql,适用于 firefox、opera、chrome 和 safari,但不适用于 IE8

jquery - 让 jquery datepicker 填充 2 个外部字段

javascript - 悬停其他内容时下拉/显示一个区域的特定内容

html - 使 div 视频背景淡出 - 这可能吗?