相关知识点:
- 定位 position
- CSS3伪元素
- inherit 属性
- CSS3变形 transform
效果图:
实现代码:
HTML
<body>
<div class="callout">请按正确格式填写</div>
</body>
CSS
.callout {
position: relative;
top: 5em; /*单位em相对父元素字体大小 此处即<body> 16px*/
width: 20em;
height: 12.6em;
background-color: #F8BCFF;
border: 3px solid #000;
text-align: center;
line-height: 12.6em;
}
.callout::before {
content: "";
position: absolute;
top: -1.25em;
left: 1em;
padding: 1em;
background-color: inherit;
border: inherit;
border-right: 0;
border-bottom: 0;
transform: rotate(45deg);
}
缺陷:
小三角形效果是通过伪元素inherit继承
父元素颜色相同的背景和边框达到的视觉上的效果,其实“小三角形”是“小正方形“,因此可能会遮罩住部分文本元素。