简体   繁体   中英

How to center a box to the center in CSS?

I have created a box using div and have 4 more div inside it:

HTML:

<div className='main__container'>
        <div className='item'>Image</div>
        <div className='item'>Name</div>
        <div className='item'>Price</div>
        <div className='item'>Quantity</div>
</div>

CSS:

.main__container {
    position: relative;
    text-align: center;
    margin: 0px auto;
    display: flex;
    background-color: white;
}

.item:not(:last-child) {
    margin-right: 100px;
}

This is the output which I get:

react_error

I want to center the whole box to the center. I tried to use margin: 0px auto but it isn't working.

Kindly comment if more information is needed.

perhaps

.main__container {
    // other 
    width: max-content;
    margin-left: auto;
    margin-right: auto;
    // other
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM