You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使Container内两个子Div同高,分别左对齐、右对齐?

解决Container内两个子Div同高且左右对齐的问题

直接通过调整PrimeNG的网格布局类即可实现需求,修改后的完整代码如下:

<div class="p-grid p-dir-col">
    <div class="p-col">
        <div class="p-grid p-justify-between">
            Test
        </div>
    </div>
    <div class="container p-grid p-justify-between p-align-items-center">
        <div *ngIf="searchLaunched" class="left-div p-col" style="padding: 0.5em; font-size: 1.25em;">
            {{ totalElements }} élément<span *ngIf="totalElements > 1">s</span> trouvé<span *ngIf="totalElements > 1">s</span>
        </div>
        <div *ngIf="canCreate" class="btn-table-crud-top p-col" style="padding: 0.5em; display: flex; justify-content: flex-end;">
            <p-button (click)="addUser()" label="Créer utilisateur" title="{{ tooltips.creer }}"></p-button>
        </div>
    </div>
</div>

<p-table>
Test
</p-table>

关键修改说明:

  • 给container添加p-grid p-justify-between p-align-items-center:启用flex布局,让两个子元素分别靠左右两端对齐,同时垂直居中对齐,保证高度一致
  • 给两个子div都添加p-col类,确保在flex网格中正确分配空间
  • 统一两个子div的padding值,移除按钮div原有的margin-top,避免垂直间距不一致
  • 给按钮div添加display: flex; justify-content: flex-end;,确保按钮始终靠右

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:03:28