【发布时间】:2021-11-16 15:49:32
【问题描述】:
我有一个相对于其父级定位的按钮。问题是当父宽度改变时,我的按钮不再在中心,我不能使用margin: 0 auto
我将展示一些图片来说明我的意思:
看看餐点和汉堡如何不居中,顺便说一句,它们在 400 像素的屏幕宽度上居中
.card-container {
width: 45%;
margin-bottom: 1rem;
background: #fff;
border: 1px solid #707070;
border-radius: 20px;
}
.card-container * {
margin: 0;
padding: 0 8px;
}
/* top-part */
.top-part {
font-size: 1.1rem;
font-weight: 700;
text-align: center;
padding: 2rem 0;
border-bottom: 1px solid #808080;
}
/* meal & burger */
.middle-part {
text-align: center;
position: absolute;
-webkit-box-align: center;
-webkit-box-pack: center;
display: -webkit-box;
}
img {
width: 12em;
height: 10em;
}
.meal-burger-btn {
position: relative;
background: #fff;
border: 1px solid #808080;
border-radius: 50%;
width: 3.5rem;
padding: 1.3em 0;
top: -28px;
left: 15%;
margin: auto;
}
<div class="card-container">
<div class="top-part">
<p>crazy burger</p>
</div>
<div class="middle-part">
<button class="meal-burger-btn">meal</button>
<button class="meal-burger-btn">burger</button>
</div>
<div class="bottom-part">
<p class="price price-active">2.0 bd</p>
<p class="price">1.7 bd</p>
<div class="card-image-container">
<img src="https://www.freepngimg.com/thumb/burger/4-2-burger-png-file.png" alt="">
</div>
<p class="ingredients">ingredients:</p>
<p class="ingredients-2">Beef patty - Toasted burger buns - Bacon - Onion rings - Cheddar cheese - Crazy Sauce</p>
</div>
</div>
我尝试了 w3school 解决方案,但没有成功,我也尝试过其他堆栈溢出问题也没有成功
【问题讨论】:
标签: html css web frontend web-frontend