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

CSS按钮无法居中求助:已尝试Flex等方法均无效

按钮居中失效问题排查与解决

问题概述

尝试在CSS中实现按钮水平居中,但始终无法成功。已尝试display: flex; align-items: center; justify-content: center;等Flex布局方案,参考多篇文章仍无效,推测存在样式冲突导致按钮无法居中。当前效果为按钮左对齐,未与上方输入框保持居中对齐。

相关代码

CSS代码

* {
    margin:0;
    padding: 0;

}

body {
    font-family: "Roboto", sans-serif;
    background: #bef2ff;
}


.nav {  
    background: #4f7097; 
    
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 2rem;
    padding: 1rem;;

}

.nav a{
    color: inherit;
    text-decoration: none;
}

.nav ul{
    padding: 0;
    margin: 0;
    list-style: none;
    display: flex;
    gap: 1rem;
}

.site-title {
    font-size: 2rem;
}

section{
    margin: 2rem 0 1rem 0;
}

.controls{
    display: flex;
    align-items: center;
    flex-direction: column;
}

input{
    text-align: center;
    padding: 0.5rem;
    margin-bottom: 1rem;
}


button{
    padding: 0.5rem 1rem;
    background: #1bf5af;
    color: rgb(0, 0, 0);
    border: none;
    font-family: "Arial", sans-serif;
    font-size: 1rem;
    cursor: pointer;
    transition: 0.4s;
    border-radius: 10px;
}


button:hover {
    transform: scale(1.05);
    background-color: rgb(4, 205, 205);
}

.getplan{
    display: flex;
    align-items: center;
    flex-direction: column;
}


main{
    display: flex;
    flex-direction: column;
    align-items: center;
}

h1{
    text-align: center;
    margin-bottom: 2rem;
}

.nutrients ul{
    display: flex;
    width: 35rem;
    justify-content: space-evenly;
}

ul{
    list-style: none;
}

.meals{
    display: flex;
}

img{
    width: 100%;
    margin-bottom: 1rem;
}

article{
    display: flex;
    flex-direction: column;
    padding: 2rem;
    margin: 0 1rem;
    max-width: 18rem;
    box-shadow: 0 4px 8 px 2px rgb(79, 76, 76, 0.15) 
}

.instrucitons{
    font-size: 0.9rem;
    margin-bottom: 1rem;
}

a{
    text-decoration: none;
    color: rgb(0, 0, 0);
    width: fit-content;
    padding: 0.5rem 1rem;
    cursor: pointer;
    transition: 0.4s;
   
}

a:hover {
    transform: scale(1.05);
    background-color: rgb(4, 205, 205);
}

@media only screen and (max-width: 550px){
    .meals{
        align-items: center;
    }

    .nutrients ul{
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }
    
}

a.class1 {
    padding: 0.5rem 1rem;
    background: #1bf5af;
    color: rgb(0, 0, 0);
    border: none;
    font-family: "Arial", sans-serif;
    font-size: 1rem;
    cursor: pointer;
    transition: 0.4s;
    border-radius: 10px;
}

React组件代码

<section className='controls'>
  <input 
  type="number"
  placeholder='Number of Calories'
  onChange={handleChange}/>
</section>
<button onClick={getMealData}>Get daily meal plan</button>
{mealData && <MealList mealData={mealData}/> }
</div>
);
}

export default App;

解决方法

以下三种方案任选其一即可解决问题:

  1. 利用已有.controls容器的居中样式
    将按钮移入controlssection内部,直接复用该容器的flex-direction: column; align-items: center;样式实现居中:
<section className='controls'>
  <input 
  type="number"
  placeholder='Number of Calories'
  onChange={handleChange}/>
  <button onClick={getMealData}>Get daily meal plan</button>
</section>
{mealData && <MealList mealData={mealData}/> }
  1. 使用已定义的.getplan类
    给按钮包裹一个容器,并应用你已定义好的.getplan类(该类已包含居中所需的Flex属性):
<section className='controls'>
  <input 
  type="number"
  placeholder='Number of Calories'
  onChange={handleChange}/>
</section>
<div className='getplan'>
  <button onClick={getMealData}>Get daily meal plan</button>
</div>
{mealData && <MealList mealData={mealData}/> }
  1. 直接给按钮添加居中样式
    修改CSS中的button选择器,添加块级显示和左右自动margin,实现水平居中:
button{
    padding: 0.5rem 1rem;
    background: #1bf5af;
    color: rgb(0, 0, 0);
    border: none;
    font-family: "Arial", sans-serif;
    font-size: 1rem;
    cursor: pointer;
    transition: 0.4s;
    border-radius: 10px;
    /* 添加以下两行 */
    display: block;
    margin: 0 auto;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 11:07:04