如何使浮动元素居中?
我正在实现分页,它需要居中。问题是链接需要显示为块,因此需要浮动。但是, text-align: center;
对它们不起作用。我可以通过给包装器 div 填充 left 来实现它,但是每个页面都有不同的页面数,所以这是行不通的。这是我的代码:
.pagination {
text-align: center;
}
.pagination a {
display: block;
width: 30px;
height: 30px;
float: left;
margin-left: 3px;
background: url(/images/structure/pagination-button.png);
}
.pagination a.last {
width: 90px;
background: url(/images/structure/pagination-button-last.png);
}
.pagination a.first {
width: 60px;
background: url(/images/structure/pagination-button-first.png);
}
<div class='pagination'>
<a class='first' href='#'>First</a>
<a href='#'>1</a>
<a href='#'>2</a>
<a href='#'>3</a>
<a class='last' href='#'>Last</a>
</div>
<!-- end: .pagination -->
为了得到这个想法,我想要什么:
I'm implementing pagination, and it needs to be centered. The problem is that the links need to be displayed as block, so they need to be floated. But then, text-align: center;
doesn't work on them. I could achieve it by giving the wrapper div padding of left, but every page will have a different number of pages, so that wouldn't work. Here's my code:
.pagination {
text-align: center;
}
.pagination a {
display: block;
width: 30px;
height: 30px;
float: left;
margin-left: 3px;
background: url(/images/structure/pagination-button.png);
}
.pagination a.last {
width: 90px;
background: url(/images/structure/pagination-button-last.png);
}
.pagination a.first {
width: 60px;
background: url(/images/structure/pagination-button-first.png);
}
<div class='pagination'>
<a class='first' href='#'>First</a>
<a href='#'>1</a>
<a href='#'>2</a>
<a href='#'>3</a>
<a class='last' href='#'>Last</a>
</div>
<!-- end: .pagination -->
To get the idea, what I want:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
删除
float
并使用inline-block
可能会解决您的问题:(删除以
-
开头的行并添加以开头的行>+
。)inline-block
可以跨浏览器工作,即使在 IE6 上,只要该元素最初是内联元素即可。引用自 quirksmode:
这通常可以有效地替代浮动:
来自 W3C 规范:
Removing
float
s, and usinginline-block
may fix your problems:(remove the lines starting with
-
and add the lines starting with+
.)inline-block
works cross-browser, even on IE6 as long as the element is originally an inline element.Quote from quirksmode:
this often can effectively replace floats:
From the W3C spec:
由于多年来我使用了我在一些博客中学到的老技巧,很抱歉我不记得给他学分的名字了。
无论如何,要使浮动元素居中,这应该可行:
您需要这样的结构:
诀窍是给左浮动以使容器根据内容改变宽度。比的是位置问题:相对,在两个容器上留下 50% 和 -50%。
好处是它是跨浏览器的,应该可以在 IE7+ 上运行。
Since many years I use an old trick I learned in some blog, I'm sorry i don't remember the name to give him credits.
Anyway to center floating elements this should work:
You need a structure like this:
the trick is giving float left to make the containers change the width depending on the content. Than is a matter of position:relative and left 50% and -50% on the two containers.
The good thing is that this is cross browser and should work from IE7+.
居中浮动很容易。只需使用容器的样式:
更改浮动元素的边距:
或者
并保留其余部分不变。
对于我来说,这是显示菜单或分页等内容的最佳解决方案。
优点:
任何元素(块、列表项等)的跨浏览器
简单性
缺点:
@arnaud576875 在这种情况下,使用 inline-block 元素会很好地工作(跨浏览器),因为分页只包含锚点(内联),没有列表项或 div:
优点:
弱点:
内联块元素之间的间隙 - 它的工作方式与单词之间的空格相同。它可能会导致计算容器宽度和样式边距时出现一些问题。间隙宽度不是恒定的,而是特定于浏览器的(4-5 像素)。
为了消除这个差距,我将添加到 arnaud576875 代码(未完全测试):
.pagination{ 字间距:-1em; }
.pagination a{ 字间距:.1em; }
Centering floats is easy. Just use the style for container:
change the margin for floating elements:
or
and leave the rest as it is.
It's the best solution for me to display things like menus or pagination.
Strengths:
cross-browser for any elements (blocks, list-items etc.)
simplicity
Weaknesses:
@arnaud576875 Using inline-block elements will work great (cross-browser) in this case as pagination contains just anchors (inline), no list-items or divs:
Strengths:
Weknesses:
gaps between inline-block elements - it works the same way as a space between words. It may cause some troubles calculating the width of the container and styling margins. Gaps width isn't constant but it's browser specific (4-5px).
To get rid of this gaps I would add to arnaud576875 code (not fully tested):
.pagination{ word-spacing: -1em; }
.pagination a{ word-spacing: .1em; }
it won't work in IE6/7 on block and list-items elements
设置容器的
width
(以px
为单位)并添加:参考。
Set your container's
width
inpx
and add:Reference.
使用 Flex
Using Flex
我认为最好的方法是使用
margin
而不是display
。即:
检查结果和代码:
http://cssdeck.com/labs/d9d6ydif
I think the best way is using
margin
instead ofdisplay
.I.e.:
Check the result and the code:
http://cssdeck.com/labs/d9d6ydif
您还可以通过更改
.pagination
来实现此目的,将“text-align: center”替换为两到三行 css,用于左、变换以及根据具体情况确定位置。You can also do this by changing
.pagination
by replacing "text-align: center" with two to three lines of css for left, transform and, depending on circumstances, position.IE7 不支持
inline-block
。您必须添加:
IE7 doesn't know
inline-block
.You must add:
将其添加到您的样式中
*如果您的容器宽度是 50px,则添加 25px,如果容器宽度是 10em,则添加 5em。
Add this to you styling
*If your container width is 50px put 25px, if it is 10em put 5em.
步骤 1
创建两个或多个您想要的 div,并给它们一个明确的宽度,例如每个 100px,然后将其向左或向右浮动
步骤 2
然后将这两个 div 扭曲到另一个 div 中,并给它 200px 的宽度。对此 div 应用 margin auto。
繁荣,效果很好。检查上面的例子。
step 1
create two or more div's you want and give them a definite width like 100px for each then float it left or right
step 2
then warp these two div's in another div and give it the width of 200px. to this div apply margin auto.
boom it works pretty well. check the above example.
只需添加
到我的 css 菜单中
也可以实现居中技巧
Just adding
into my css menu of
did the centering trick too