简体   繁体   English

如何将清单与区域中心对齐?

[英]How to I align my list to the center of the area?

I have a two column list, but it's justified to the left. 我有一个两列的列表,但是在左边是合理的。 How do I make it centered on the page? 如何使其居中于页面?

Here's my jsfiddle: http://jsfiddle.net/huskydawgs/c2yqmfzt/5/ 这是我的jsfiddle: http : //jsfiddle.net/hus​​kydawgs/c2yqmfzt/5/

<p>
A banana is an edible fruit, botanically a berry,[1][2] produced by several kinds of large herbaceous flowering plants in the genus Musa.[3] (In some countries, bananas used for cooking may be called plantains.) The fruit is variable in size, color and firmness, but is usually elongated and curved, with soft flesh rich in starch covered with a rind which may be green, yellow, red, purple, or brown when ripe.</p>

<ul class="two-col-special">
    <li>First Category</li>
    <li>Second Category</li>
    <li>Third Category</li>
    <li>Fourth Category</li>
    <li>Fifth Category</li>
</ul>

<p>
    Worldwide, there is no sharp distinction between "bananas" and "plantains". Especially in the Americas and Europe, "banana" usually refers to soft, sweet, dessert bananas, particularly those of the Cavendish group, which are the main exports from banana-growing countries. By contrast, Musa cultivars with firmer, starchier fruit are called "plantains". In other regions, such as Southeast Asia, many more kinds of banana are grown and eaten, so the simple two-fold distinction is not useful and is not made in local languages.</p>

Here's the CSS: 这是CSS:

.two-col-special {
margin: 0;
padding: 0;
}

.two-col-special li {
    display: inline-block;
    width: 45%;
    margin: 0;
    padding: 0;
    vertical-align: top;
}

What you're likely looking for is: 您可能正在寻找的是:

.two-col-special { text-align:center; }

That simple. 这么简单。

I updated your fiddle here 在这里更新了你的小提琴

Edit 编辑

As 40Alpha kindly noted, if you're wanting to keep your columns in order, you can add a blank list item at the bottom of your unordered list. 正如40Alpha所指出的那样,如果您希望使各列保持顺序,则可以在无序列表的底部添加一个空白列表项。 A second fiddle here will show that potential fix. 这里的第二个小提琴将显示该潜在的修复方法。

 .two-col-special { margin: 0; padding: 0; text-align:center; } .two-col-special li { display: inline-block; width: 45%; margin: 0; padding: 0; vertical-align: top; } ul:nth-child(even) { padding-left:20px; } 
 <ul class="two-col-special"> <li>First Category</li> <li>Second Category</li> <li>Third Category</li> <li>Fourth Category</li> <li>Fifth Category</li> <li></li> </ul> 

If you're wanting to add 20px padding between columns, you can use css' nth-child selector: 如果要在列之间添加20px填充,则可以使用CSS的nth-child选择器:

ul:nth-child(even) {
    padding-left:20px;   
}

This will isolate your second and fourth list items, by way of getting the even numbered items, and add padding on the left side to them. 通过获得偶数编号的项目,这将隔离您的secondfourth列表项,并在它们的左侧添加填充。 Note too that this will also get your empty list item. 还要注意,这也会得到您的空列表项。

You Need to apply the CSS Setting: 您需要应用CSS设置:

text-align:center;

to the container, in this case to the .two-col-special Class 容器,在这种情况下为.two-col-special

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

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