div右上角显示数字
时间: 2023-12-10 15:38:27 浏览: 35
您可以使用CSS中的position和top/right属性来实现这个效果。首先,将要显示数字的div元素的position属性设置为relative或absolute,然后使用top和right属性来将其定位到右上角。最后,将数字放在这个div中,通过CSS样式设置数字的样式。以下是一个示例代码:
HTML代码:
```html
<div class="notification">
<span class="badge">5</span>
</div>
```
CSS代码:
```css
.notification {
position: relative;
}
.notification .badge {
position: absolute;
top: -10px;
right: -10px;
background-color: red;
color: white;
padding: 5px;
border-radius: 50%;
font-size: 12px;
}
```
这里我们将要显示数字的div元素的class设置为"notification",数字的class设置为"badge"。在CSS中,我们将"notification"元素的position属性设置为relative,这样它就可以相对于它的父元素进行定位。我们将"badge"元素的position属性设置为absolute,top和right属性设置为负值,这样就可以将它定位到父元素的右上角。然后我们设置了数字的样式,包括背景颜色、字体颜色、内边距、边框半径和字体大小等。