I am attempting to create some rectangular boxes with circular (not ovular) boxes inside those. Currently the behaviour I'm getting is this:three rectangle with ovals inside spanning the whole area of the rectangle they're inside of Whereas the behaviour I was expecting was circles (i.e. 1:1 aspect ratio). My code for this is as follows:

#portals {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
}

.portal {
    display: flex;
    justify-content: center;
    width: 30%;
    border-radius: 1rem;
    background-color: aqua;
    height:50rem;
    margin:1rem
}

.portal-icon {
    aspect-ratio: 1 / 1;
    width:90%;
    border-radius: 50%;
    background-color: white;
    margin:1rem
}
```
<div id="portals">
    <div class="portal" id="art" style="background-color: #ce87e8;">
        <div class="portal-icon" id="art"></div>
    </div>
    <div class="portal" id="code" style="background-color: #87c8e8;">
        <div class="portal-icon" id="code"></div>
    </div>
    <div class="portal" id="projects" style="background-color: #e89f87;">
        <div class="portal-icon" id="projects"></div>
    </div>
</div>

在查看其他类似的问题时,我的印象是,aspect-ratio是使用现代浏览器实现这一目标的正确方法,但在我使用弹性框的过程中,它似乎并不适合我,一旦我交换回标准框,圆圈功能,但我无法让圆圈在框中居中.

我也读到过使用padding-bottom技巧的内容,但它根本没有任何效果.除此之外,我还读过一些答案,指出在该属性中使用百分比不起作用,也不应该使用,或者它已经过时,应该使用aspect ratio.

基本上我不知道该怎么办,因为似乎只有两种可用的方法相互矛盾(从我所看到的),而应该使用的方法,但这两种方法都不适合我.

我还看到一些人提到可以使用JS实现这种行为,但我对此的经验为0,所以如果我能指出正确的方向,说明如果CSS不可能,我应该如何编写脚本来实现这一目标,那就太好了.

提前感谢.

推荐答案

您需要将align-items从默认stretch更改为center或根据需要更改任何其他值.

#portals {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
}

.portal {
    display: flex;
    justify-content: center;
    align-items: start;
    width: 30%;
    border-radius: 1rem;
    background-color: aqua;
    height:25rem; /* adjusted for demo */
    margin:1rem
}

.portal-icon {
    aspect-ratio: 1 / 1;
    width: 90%;
    border-radius: 50%;
    background-color: white;
    margin:1rem
}
<div id="portals">
    <div class="portal" id="art" style="background-color: #ce87e8;">
        <div class="portal-icon" id="art"></div>
    </div>
    <div class="portal" id="code" style="background-color: #87c8e8;">
        <div class="portal-icon" id="code"></div>
    </div>
    <div class="portal" id="projects" style="background-color: #e89f87;">
        <div class="portal-icon" id="projects"></div>
    </div>
</div>

Javascript相关问答推荐

获取POS餐厅会话用户/收银员

错误:找不到react-redux上下文值;请确保该组件包装在React原生Expo应用程序中的提供者中

为什么子组件没有在reaction中渲染?

添加/删除时React图像上传重新渲染上传的图像

从PWA中的内部存储读取文件

Chart.js V4切换图表中的每个条,同时每个条下有不同的标签.怎么做?

将状态向下传递给映射的子元素

在nextjs服务器端api调用中传递认证凭证

用JavaScript复制C#CRC 32生成器

手机上的渲染错误文本必须在文本组件中渲染,但在浏览器上没有问题<><>

我正在建立一个基于文本的游戏在react ,我是从JS转换.我怎样才能使变量变呢?

在HTML语言中调用外部JavaScript文件中的函数

闭包是将值复制到内存的另一个位置吗?

如何在不影响隐式类型的情况下将类型分配给对象?

将数组扩展到对象中

TypeError:无法读取未定义的属性(正在读取';宽度';)

按什么顺序接收`storage`事件?

FileReader()不能处理Firefox和GiB文件

TypeORM QueryBuilder限制联接到一条记录

为什么我的SoupRequest";被重置为初始值,以及如何修复它?