简体   繁体   English

如何在html div中居中对齐内联元素?

[英]How do you center align inline elements inside an html div?

I have a bunch of spans inside a fixed width div. 我在固定宽度div中有一堆跨度。 I would like to let the spans flow inline as they do by design. 我想让跨度像设计中那样直接内联。 However, I would like them to be centered on each line instead of being left-aligned. 但是,我希望它们以每条线为中心,而不是左对齐。 How could I achieve this using css? 如何使用CSS实现此目标?

<div>
         <span>text1</span>
         <span>text2</span>
         <span>text3</span>
         <span>text4</span>
         <span>text5</span>
         <span>text6</span>
         <span>text7</span>
         <span>text8</span>
         <span>text9</span>
         <span>text10</span>
</div>

Just set text-align: center to the div <div class="dummy"> . 只需将text-align: center设置为div <div class="dummy"> Something like this... 像这样

.dummy {
    width: 500px;
    text-align: center;
}

jsFiddle DEMO jsFiddle演示

Hope this is what you are after. 希望这就是你所追求的。

Set a width for the div containing the spans and do this: 设置包含跨度的div的宽度,然后执行以下操作:

span {
        width: 100%;
        text-align: center;
        float: left;
        clear: both;
    }

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

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