简体   繁体   中英

CSS: align ul li items to center (with margin) without flex model

I have such code:

<ul class="tiles">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
</ul>

and css:

ul.tiles{
    width: 220px;
    height: 200px;
    list-style-type: none;
    background: #cecece;
    padding: 0;
}

li{
    display: inline-block;
    width: 50px;
    height: 50px;
    background: #8ac249;
    text-align: center;
    line-height: 50px;
    margin: 5px;
    float: left;
}

and it look so:

在此处输入图片说明

is it real to change it to such model (without flex box):

在此处输入图片说明

and how?

Simply add text-align:center to your ul . Text-align center can be useful for a number of situations and can be used for more than just text. It can be used on any inline or inline-block elements and can achieve some neat little things like this.

 ul.tiles{ width: 220px; height: 200px; list-style-type: none; background: #cecece; padding: 0; text-align:center; } li{ display: inline-block; width: 50px; height: 50px; background: #8ac249; text-align: center; line-height: 50px; margin: 5px; } 
 <ul class="tiles"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> 

ul.tiles{
    width: 220px;
    height: 200px;
    list-style-type: none;
    background: #cecece;
    padding: 0;`enter code here`
    text-align:center;
}

li{
    display: inline-block;
    width: 50px;
    height: 50px;
    background: #8ac249;
    text-align: center;
    line-height: 50px;
    margin: 5px;
}

put text-align:center; in ul.tiles

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