css实现div内凹角样式

平常的开发中我们一般使用到圆角都是外凸的,即border-radius属性。而如果有内凹角的情况,我们一般的考虑实现方法有2种。一种是直接使用背景图片,一种是使用css。

用到的属性则是background或background-image 结合径向渐变radial-gradient。示例:

1 background-image: radial-gradient(200px at 50px 0px, #fff 50px, #4169E1 50px);

而对于径向渐变,主要是3个参数控制。

一个是原点和大小。大小类似border-radius的感觉,原点使用at表示可以指定点的坐标,或使用left、right、top、bottom来表示。

其次是两个颜色和透明度、大小等。这里50px或百分比,亲测第一个只有50才是半圆角,而第二个50px貌似影响不大。

示例:

欲实现图中浅绿色的四角内凹样式,没有素材图片。

开始:首先给这个div设置好宽高并设置相对定位。然后在其内部放4个绝对定位的元素,设置其宽高等于内凹的尺寸,之后将他们绝对定位固定到4角。然后就是使用上边的内凹样式。

需要注意的是,上边两个很简单就完成了,但是下边两个角,只设置了位置和内凹位置之后是下边这样的:

这就需要再旋转一下。

完整代码如下:

css:

 1 .notice_box_cls #commonNotice {
 2     background-color: #E8F7F2;
 3     padding: 20px 10px;
 4 }
 5 .notice_box_cls .notice_body0 {
 6     position: relative;
 7 }
 8 .notice_box_cls .notice_body {
 9     background:radial-gradient(15px at left top,#fff 50px,#E8F7F2 50%);
10     position: absolute;
11     left: 0;
12     top: 0;
13     width: 15px;
14     height: 15px;
15 }
16 .notice_box_cls .notice_body1 {
17     background:radial-gradient(15px at right top,#fff 50px,#E8F7F2 50%);
18     position: absolute;
19     right: 0;
20     top: 0;
21     width: 15px;
22     height: 15px;
23 }
24 .notice_box_cls .notice_body2 {
25     background:radial-gradient(15px at right bottom,#fff 50px,#E8F7F2 50%);
26     position: absolute;
27     left: 0;
28     bottom: 0;
29     width: 15px;
30     height: 15px;
31     transform: rotate(90deg);
32 }
33 .notice_box_cls .notice_body3 {
34     background:radial-gradient(15px at left bottom,#fff 50px,#E8F7F2 50%);
35     position: absolute;
36     right: 0;
37     bottom: 0;
38     width: 15px;
39     height: 15px;
40     transform: rotate(270deg);
41 }

html:

 1 <div class="notice">
 2                                     <div class="">
 3                                         <div class="notice_title">
 4                                             <span>通知公告</span>
 5                                         </div>
 6                                         <div class="notice_body0">
 7                                             <ul id="commonNotice">
 8                                                 <!--  <li><a href="#" class="fl">湖北省教育信息化S1</a><i class="fr">2017-5-13</i></li>
 9                                             <li><a href="#" class="fl">湖北省教育信息化S2</a><i class="fr">2017-5-13</i></li>
10                                             <li><a href="#" class="fl">湖北省教育信息化S3</a><i class="fr">2017-5-13</i></li>
11                                             <li><a href="#" class="fl">湖北省教育信息化S4</a><i class="fr">2017-5-13</i></li>
12                                             <li><a href="#" class="fl">湖北省教育信息化S5</a><i class="fr">2017-5-13</i></li>
13                                             <li><a href="#" class="fl">湖北省教育信息化S6</a><i class="fr">2017-5-13</i></li>  -->
14                                             </ul>
15                                             <div class="notice_body">
16                                             </div>
17                                             <div class="notice_body1">
18                                             </div>
19                                             <div class="notice_body2">
20                                             </div>
21                                             <div class="notice_body3">
22                                             </div>
23                                         </div>
24                                     </div>
25                                   </div>

 

posted @ 2019-06-26 10:47  TheFirstDream  阅读(15792)  评论(1编辑  收藏  举报