在 CSS 中将文本垂直和水平居中在圆圈中(如 iphone 通知徽章)
我正在寻找一种在 CSS3 中制作类似 iphone 的跨浏览器徽章的方法。我显然想使用一个 div 来实现这一点,但其他解决方案也可以。重要的因素是它需要在所有浏览器中水平和垂直居中。
关于这些通知的一个有趣的设计问题是它们不能有指定的宽度(高度是固定的)——它们应该能够处理[在 ascii 绘图中] (1) 和 (1000),其中 (1000) 不是一个完美的圆,但看起来更像是一个胶囊。
编辑:附加约束(来自 Steven):
- 没有 JavaScript
- 没有对表格单元格的显示属性进行修改,这是有问题的支持状态
I'm looking for a way of to do a cross-browser iphone-like badge in CSS3. I'd obviously like to use one div for this, but alternative solutions would be fine. The important factor is that it needs to be horizontally and vertically centered in all browsers.
An interesting design issue about these notifications is that they cannot have a specified width (height is fixed) - they should be able to handle [in ascii drawing] (1) and (1000), where (1000) is not a perfectly rounded circle, but instead looks more like a capsule.
EDIT: Additional constraints (from Steven):
- No JavaScript
- No mangling of the display property to table-cell, which is of questionable support status
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
水平居中很简单:
text-align: center;
。元素内文本的垂直居中可以通过将 line-height 设置为等于容器高度来完成,但这在浏览器之间存在细微的差异。在通知徽章等小元素上,这些效果更加明显。更好的方法是将
line-height
设置为font-size
(或稍小)并使用填充。您必须调整身高以适应。这是一个纯 CSS 的单个
解决方案,看起来非常像 iPhone。它们随着内容的扩展而扩展。
演示:http://jsfiddle.net/ThinkingStiff/mLW47/
输出:
CSS:
HTML:
Horizontal centering is easy:
text-align: center;
. Vertical centering of text inside an element can be done by settingline-height
equal to the container height, but this has subtle differences between browsers. On small elements, like a notification badge, these are more pronounced.Better is to set
line-height
equal tofont-size
(or slightly smaller) and use padding. You'll have to adjust your height to accomodate.Here's a CSS-only, single
<div>
solution that looks pretty iPhone-like. They expand with content.Demo: http://jsfiddle.net/ThinkingStiff/mLW47/
Output:
CSS:
HTML:
现代解决方案
结果是,圆圈永远不会变形,并且文本完全保持在圆圈的中间 - 垂直和水平。
Tailwind CSS
使用 TailwindCSS 来实现相同的效果。这很简单...
Modern Solution
The result is that the circle never gets distorted and the text stays exactly in the middle of the circle - vertically and horizontally.
Tailwind CSS
To achieve the same use TailwindCSS. This is as simple as...
有趣的问题!虽然有很多关于水平和垂直居中 div 的指南,但显然缺乏对居中 div 的宽度未确定的主题的权威处理。
让我们应用一些基本的约束:
table-cell
的显示属性进行修改,这是有问题的支持状态鉴于此,我进入争论的是使用
inline- block
display 属性将跨度在预定高度的绝对定位 div 内水平居中,在传统的top: 50% 中在父容器内垂直居中; margin-top: -123px
时尚。标记:
div >分区> span
CSS:
来源:http://jsfiddle.net/38EFb/
一种替代解决方案,不不需要无关的标记,但这很可能会产生比使用行高属性解决的问题更多的问题。不要这样做。但它作为学术注释包含在此处: http://jsfiddle.net/gucwW/
Interesting question! While there are plenty of guides on horizontally and vertically centering a div, an authoritative treatment of the subject where the centered div is of an unpredetermined width is conspicuously absent.
Let's apply some basic constraints:
table-cell
, which is of questionable support statusGiven this, my entry into the fray is the use of the
inline-block
display property to horizontally center the span within an absolutely positioned div of predetermined height, vertically centered within the parent container in the traditionaltop: 50%; margin-top: -123px
fashion.Markup:
div > div > span
CSS:
Source: http://jsfiddle.net/38EFb/
An alternate solution that doesn't require extraneous markups but that very likely produces more problems than it solves is to use the line-height property. Don't do this. But it is included here as an academic note: http://jsfiddle.net/gucwW/
如果您的内容高度未知,但您知道容器的高度。以下解决方案效果非常好。
HTML
CSS
示例
http://jsfiddle.net/thenewconfection/eYtVN/
新手要显示的一个问题:inline-block ; [span] 和 [p] 没有 html 空白,因此 span 不占用任何空间。另外,我还在 CSS hack 中添加了用于 IE 的显示内联块。希望这对某人有帮助!
If you have content with height unknown but you know the height the of container. The following solution works extremely well.
HTML
CSS
EXAMPLE
http://jsfiddle.net/thenewconfection/eYtVN/
One gotcha for newby's to display: inline-block; [span] and [p] have no html white space so that the span then doesn't take up any space. Also I've added in the CSS hack for display inline-block for IE. Hope this helps someone!
这是一个与 zurb Foundation CSS 框架配合良好的平面徽章示例
注意:您可能需要调整不同字体的高度。
http://jsfiddle.net/jamesharrington/xqr5nx1o/
魔法酱!
Here is an example of flat badges that play well with zurb foundation css framework
Note: you might have to adjust the height for different fonts.
http://jsfiddle.net/jamesharrington/xqr5nx1o/
The Magic sauce!
这是我使用的代码,实际上在我的文本
HTML
CSS周围创建了一个圆圈
Here's the code I used that ACTUALLY created a circle around my text
HTML
CSS