CSS 如何在中心对齐 ul 标签内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16127131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to align ul tag content in center
提问by Mercurial
I had been trying to align content of #refer
's ul
content in the center of the screen using CSS.
我一直在努力的对齐内容#refer
的ul
使用CSS屏幕的中心内容。
<div id="refer" style="border:1px solid red">
<ul>
<li><a href="#" class="circlelink">One</a>
</li>
<li><a href="#" class="circlelink">Two</a>
</li>
<li><a href="#" class="circlelink">Three</a>
</li>
</ul>
</div>
.circlelink {
display:block;
width:100px;
height:100px;
border-radius:50px;
font-size:20px;
color:#fff;
line-height:100px;
text-align:center;
text-decoration:none;
background:linear-gradient(to bottom, #719ECE, #719FCE);
}
.circlelink:hover {
text-decoration:none;
background:#719ECE;
box-shadow: 0 0 8px rgba(0, 0, 0, .8);
-webkit-transform: scale(1.05, 1.07);
-moz-transform: scale(1.05, 1.07);
}
#refer {
position:absolute;
top:20%;
width:100%;
margin-left:auto;
margin-right:auto;
}
#refer ul {
clear:both;
margin: 0;
padding: 0;
}
#refer ul li {
display:block;
position: relative;
float: left;
height: auto;
width: 200px;
padding: 0;
margin-left: 10px;
list-style: none;
text-align: center;
white-space: nowrap;
}
回答by Jason Lydon
This worked for me in your fiddle:
这在你的小提琴中对我有用:
* {
padding:0;
margin:0;
}
#refer {
margin:20% 0 0;
width:100%;
/*
position:absolute;
top:20%;
width:100%;
margin-left:auto;
margin-right:auto;
*/
}
#refer ul {
text-align:center;
/*
clear:both;
margin: 0;
padding: 0;
*/
}
#refer ul li {
display:inline-block;
position: relative;
/* float: left; */
height: auto;
width: 100px;
padding: 0;
margin:0 50px;
list-style: none;
text-align: center;
white-space: nowrap;
border:#0f0 solid 1px;
}
.circlelink {
display:block;
width:100px;
height:100px;
border-radius:50px;
font-size:20px;
color:#fff;
line-height:100px;
text-align:center;
text-decoration:none;
background:linear-gradient(to bottom, #719ECE, #719FCE);
}
.circlelink:hover {
text-decoration:none;
background:#719ECE;
box-shadow: 0 0 8px rgba(0, 0, 0, .8);
-webkit-transform: scale(1.05, 1.07);
-moz-transform: scale(1.05, 1.07);
}
回答by AzDesign
code : jsfiddle
代码:jsfiddle
All you have to do is :
您所要做的就是:
- Add
text-align:center;
into#refer ul
- Change
display:block;
intodisplay:inline-block;
in#refer ul li
- Remove
float:left;
from#refer ul li
- 添加
text-align:center;
到#refer ul
- 更改
display:block;
成display:inline-block;
在#refer ul li
- 删除
float:left;
从#refer ul li
Additionally, make sure the #refer ul
provide min-width
that provide required width for all circle to make sure it's not collapsed if browser window too small.
此外,请确保#refer ul
提供min-width
为所有圆圈提供所需宽度以确保它不会在浏览器窗口太小时折叠。
Additionally, it is better if you remove the width
from your #refer ul li
. Let the content <a>
specify the width
此外,最好width
从#refer ul li
. 让内容<a>
指定宽度
回答by Rajender Joshi
.circlelink {
display:block;
width:100px;
height:100px;
border-radius:50px;
font-size:20px;
color:#fff;
line-height:100px;
text-align:center;
text-decoration:none;
background:linear-gradient(to bottom, #719ECE, #719FCE);
}
.circlelink:hover {
text-decoration:none;
background:#719ECE;
box-shadow: 0 0 8px rgba(0, 0, 0, .8);
-webkit-transform: scale(1.05, 1.07);
-moz-transform: scale(1.05, 1.07);
}
#refer {
position:absolute;
top:20%;
width:100%;
margin-left:auto;
margin-right:auto;
text-align:center;
}
#refer ul {
clear:both;
margin: 0;
padding: 0;
}
#refer ul li {
display:inline-block;
position: relative;
height: auto;
width: 200px;
padding: 0;
margin-left: 10px;
list-style: none;
text-align: center;
white-space: nowrap;
}
回答by Wayne Tun
wrapping your ulwith a divwill help
用div包裹你的ul会有所帮助
http://jsfiddle.net/ptMwH/11/here check this
http://jsfiddle.net/ptMwH/11/这里检查这个
<div id="refer" style="border:1px solid red">
<div class="wrapper">
<ul>
<li><a href="#" class="circlelink">One</a>
</li>
<li><a href="#" class="circlelink">Two</a>
</li>
<li><a href="#" class="circlelink">Three</a>
</li>
</ul>
</div>
</div>