在 CSS 中以圆形垂直和水平居中文本(如 iphone 通知徽章)

2023-03-15前端开发问题
21

本文介绍了在 CSS 中以圆形垂直和水平居中文本(如 iphone 通知徽章)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一种在 CSS3 中制作类似 iphone 的跨浏览器徽章的方法.我显然想为此使用一个 div,但替代解决方案会很好.重要的因素是它需要在所有浏览器中水平和垂直居中.

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.

关于这些通知的一个有趣的设计问题是它们不能具有指定的宽度(高度是固定的)——它们应该能够处理 [在 ascii 绘图中] (1) 和 (1000),其中 (1000) 不是完美的圆形,但看起来更像是一个胶囊.

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.

编辑:附加约束(来自 Steven):

EDIT: Additional constraints (from Steven):

  • 没有 JavaScript
  • 没有将 display 属性修改为 table-cell,这是有问题的支持状态

推荐答案

水平居中很简单:text-align: center;.可以通过将 line-height 设置为等于容器高度来实现元素内文本的垂直居中,但这在浏览器之间存在细微差别.在小元素上,比如通知徽章,这些更明显.

Horizontal centering is easy: text-align: center;. Vertical centering of text inside an element can be done by setting line-height equal to the container height, but this has subtle differences between browsers. On small elements, like a notification badge, these are more pronounced.

最好将 line-height 设置为等于 font-size(或稍小)并使用填充.你必须调整你的身高以适应.

Better is to set line-height equal to font-size (or slightly smaller) and use padding. You'll have to adjust your height to accomodate.

这是一个纯 CSS 的单一 <div> 解决方案,看起来很像 iPhone.它们随着内容而扩展.

Here's a CSS-only, single <div> solution that looks pretty iPhone-like. They expand with content.

演示:http://jsfiddle.net/ThinkingStiff/mLW47/

输出:

CSS:

.badge {
    background: radial-gradient( 5px -9px, circle, white 8%, red 26px );
    background-color: red;
    border: 2px solid white;
    border-radius: 12px; /* one half of ( (border * 2) + height + padding ) */
    box-shadow: 1px 1px 1px black;
    color: white;
    font: bold 15px/13px Helvetica, Verdana, Tahoma;
    height: 16px; 
    min-width: 14px;
    padding: 4px 3px 0 3px;
    text-align: center;
}

HTML:

<div class="badge">1</div>
<div class="badge">2</div>
<div class="badge">3</div>
<div class="badge">44</div>
<div class="badge">55</div>
<div class="badge">666</div>
<div class="badge">777</div>
<div class="badge">8888</div>
<div class="badge">9999</div>

这篇关于在 CSS 中以圆形垂直和水平居中文本(如 iphone 通知徽章)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6

缩放背景图像以适合 ie8 窗口
Scale background image to fit ie8 window(缩放背景图像以适合 ie8 窗口)...
2024-04-19 前端开发问题
11

IE7 (IETEster) 中的@fontface 无法正常工作
@fontface in IE7 (IETEster) not working properly(IE7 (IETEster) 中的@fontface 无法正常工作)...
2024-04-19 前端开发问题
9

Safari 5.1 打破 CSS 表格单元格间距
Safari 5.1 breaks CSS table cell spacing(Safari 5.1 打破 CSS 表格单元格间距)...
2024-04-19 前端开发问题
3