如何让React搜索栏与头部其他元素横向并排显示?
修复Header中咖啡图标与搜索栏并排显示的问题
问题根源在于.header-left容器未设置弹性布局,默认块级容器内的元素会垂直换行。只需给.header-left添加display: flex和align-items: center属性,就能让咖啡图标和搜索栏横向并排且保持垂直对齐。
修改后的完整CSS代码:
@import 'tailwindcss/base'; @import 'tailwindcss/components'; @import 'tailwindcss/utilities'; .header { background-color: #6858ca; color: #fff; padding: 10px 20px; display: flex; justify-content: space-between; align-items: center; } /* 新增这部分样式 */ .header-left { display: flex; align-items: center; } .coffee { color: #9a8edf; font-size: 24px; } .search-bar { display: flex; align-items: center; background-color: #52459c; border-radius: 5px; padding: 5px; margin-left: 10px; } .search-icon { color: #fff; } .search-input { border: none; background: none; color: #fff; outline: none; padding: 5px; margin-left: 5px; } .header-right { display: flex; align-items: center; } .menu-icon { color: #9a8edf; font-size: 24px; margin-right: 10px; } .user-name { color: #fff; font-weight: bold; }
逻辑说明
.header-left默认是块级元素(display: block),内部子元素会自动换行- 设置
display: flex后,容器转为弹性布局,子元素会沿默认的横向主轴排列 align-items: center确保子元素垂直居中,和右侧的用户信息区域保持视觉统一
内容的提问来源于stack exchange,提问作者IamYourFaja
相关产品推荐
相关产品推荐

