Flex-end 不适用于显示 flex 的 div 内的按钮
Posted
技术标签:
【中文标题】Flex-end 不适用于显示 flex 的 div 内的按钮【英文标题】:Flex-end is not working on a button inside a div with display of flex 【发布时间】:2022-01-12 05:26:13 【问题描述】:所以我觉得我已经尽一切努力让这个按钮进入 div 的底部,但似乎没有任何效果。如果我将容器 div 切换为 align-content: flex-end 一切都将移至底部,但我试图将 div 保持在顶部并且只按下按钮。我正在使用 React 和 CSS 模块。
import styles from './Total.module.css';
const Total = ( tipTotal, total, handleReset ) =>
return (
<div className=styles.total>
<div className=styles.totalTipAmount>
<div className=styles.totalLeft>
<div className=styles.totalTop>Tip Amount</div>
<div className=styles.totalBottom>/ person</div>
</div>
<div className=styles.totalRight>$tipTotal</div>
</div>
<div className=styles.toalAmount>
<div className=styles.totalLeft>
<div className=styles.totalTop>Total</div>
<div className=styles.totalBottom>/ person</div>
</div>
<div className=styles.totalRight>$total</div>
</div>
<button className=styles.totalReset onClick=handleReset>
RESET
</button>
</div>
);
;
export default Total;
.total
display: flex;
align-self: center;
flex-direction: column;
width: 80%;
min-width: 80%;
margin: 30px 0;
background-color: var(--Very-dark-cyan);
border-radius: 15px;
padding: 30px 15px 5px 15px;
.total div
display: flex;
justify-content: space-between;
margin-bottom: 10px;
justify-self: flex-start;
.totalLeft
display: flex;
flex-direction: column;
.totalTop
color: var(--White);
font-size: medium;
margin-bottom: 5px !important;
.totalBottom
font-size: small;
color: var(--Dark-blueish-cyan);
.totalRight
font-size: xx-large;
color: var(--Strong-cyan);
.total button
background-color: var(--Strong-cyan);
color: var(--Very-dark-cyan);
font-size: large;
font-weight: 700;
width: 100%;
max-width: 100%;
@media (min-width: 900px)
.total
height: 100%;
grid-column-start: 2;
grid-row-start: 1;
grid-row-end: 4;
justify-self: center;
.total button
align-self: flex-end;
【问题讨论】:
【参考方案1】:remove width:100% from button tag css
【讨论】:
【参考方案2】: .total
height: 100%;
grid-column-start: 2;
grid-row-start: 1;
grid-row-end: 4;
justify-self: center;
position:relative;
.total button
align-self: flex-end;
position: absolute;
right: 20px;
【讨论】:
【参考方案3】:body, html
height: 100%;
.total
display: flex;
flex-direction: column;
width: 80%;
min-width: 80%;
margin: 30px 0;
background-color: var(--Very-dark-cyan);
border-radius: 15px;
padding: 30px 15px 5px 15px;
height: 80%;
border: 1px solid red;
justify-content: space-between;
.totalTipAmount, .totalAmount
display: flex;
justify-content: space-between;
margin-bottom: 10px;
.totalLeft
display: flex;
flex-direction: column;
.totalTop
color: var(--White);
font-size: medium;
margin-bottom: 5px !important;
.totalBottom
font-size: small;
color: var(--Dark-blueish-cyan);
.totalRight
font-size: xx-large;
color: var(--Strong-cyan);
.totalReset
justify-self: flex-end;
border: 1px solid blue;
.total button
background-color: var(--Strong-cyan);
color: var(--Very-dark-cyan);
font-size: large;
font-weight: 700;
width: 100%;
max-width: 100%;
@media (min-width: 900px)
.total
height: 100%;
grid-column-start: 2;
grid-row-start: 1;
grid-row-end: 4;
justify-self: center;
.total button
align-self: flex-end;
<div class="total">
<div class="stuffAtTheTop">
<div class="totalTipAmount">
<div class="totalLeft">
<div class="totalTop">Tip Amount</div>
<div class="totalBottom">/ person</div>
</div>
<div class="totalRight">$tipTotal</div>
</div>
<div class="totalAmount">
<div class="totalLeft">
<div class="totalTop">Total</div>
<div class="totalBottom">/ person</div>
</div>
<div class="totalRight">$total</div>
</div>
</div>
<button class="totalReset">
RESET
</button>
</div>
</div>
【讨论】:
以上是关于Flex-end 不适用于显示 flex 的 div 内的按钮的主要内容,如果未能解决你的问题,请参考以下文章
justify-content: flex-end 的 Safari 问题;对齐项目:flex-end;
Flexbox child align-self:Safari 中的 flex-end 问题