简体   繁体   English

使用 flex-box 时如何保持图标居中? 我有一个案例,我在一个页面中有 2 个图标,在另一个页面中有 3 个图标用于反应项目

[英]How can I keep my icon centered while using flex-box? I have a case where I have 2 icons in 1 page and 3 icons in another for a react project

I want to keep the second div fixed at the center but when I have 2 divs the last div is moving to the left.我想将第二个 div 固定在中心,但是当我有 2 个 div 时,最后一个 div 会向左移动。 I am using justify-content: space-between for the divs.我正在为 div 使用 justify-content: space-between。 I understand it will push the last div to the left when I have 2 divs instead of 3, how can I keep the second div in the same position (close to the center) at all times?我知道当我有 2 个 div 而不是 3 个 div 时,它会将最后一个 div 向左推,如何始终将第二个 div 保持在同一个 position (靠近中心)中?

   <div className={classes.container}>            
            {!!ls?.fields?.difficulty && ( //div 1
                <LabeledIcon
                cardType="lsCard"
                icon={ls.fields.difficulty}
                difficultyLevel={ls.fields.difficulty}>
                {l.fields.difficulty}
              </LabeledIcon>              
            )}
            {!!ls?.fields?.time && ( //div2                
              <LabeledIcon
                cardType="lsCard"
                icon="schedule"
                messageId="lsDuration"
                messageValues={ls.fields}
              />                        
            )}
            {!!ls.comment_count && ( //div3
              <LabeledIcon
                cardType="lsCard"
                icon="chat"
                messageId="lsComments"
                messageValues={ls}
              />
            )}
            </div>
          </div>

Try Css Gird container: { display: 'grid', grid: 'auto / 1fr 1fr', justifyContent: 'center', gridAutoFlow: 'column' } Else you will have to wrap the icon inside a div.试试 Css container: { display: 'grid', grid: 'auto / 1fr 1fr', justifyContent: 'center', gridAutoFlow: 'column' }否则你将不得不将图标包装在一个 div 中。 align them to flex-start and center the icon inside them将它们对齐到 flex-start 并在其中居中图标

https://jsfiddle.net/2vwjpsab/ https://jsfiddle.net/2vwjpsab/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加图标Fontawesome做出反应? - How can I add icons Fontawesome in react? 如何在占位符中使用 React 图标? - How can I use React icons in Placeholder? 我如何渲染反应图标 - How Can I Render react-icons 如何使用 vue.js 将成功图标添加到我的 web 页面? - How can I add success icons to my web page using vue.js? 在Mxgrapg自定义图标中,如何为自定义图标添加标题? - In Mxgrapg custom icon how can I add title for custom Icons? 我有两张图标幻灯片,当第一张幻灯片中的图标以不同的分辨率溢出时,如何将图标移动到下一张幻灯片? - I have two slides of icons and how to move the icons to next slide when the icons are overflowing in first slide at different resolutions? 我如何有一个搜索框,人们可以在其中搜索我站点内的页面? - How can I have a search box where people can search for pages within my site? 我从一个父组件渲染 5 列,它们都有不同的图标。 我如何有条件地让他们做出反应? - I'm rendering 5 columns from a single parent component and they all have different icons. How do i conditionally render them in react? 我如何在我的项目中使用 useanimations.com 提供的图标 - How I can use icons provided by useanimations.com in my project 如何在 reactjs 中使用“react-web-vector-icons”? - How can I use 'react-web-vector-icons' in reactjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM