css :before :after的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。
项目中有时会遇到伪类,即:before,:after,如果要实现在每行文字的左右两边加上-就可以使用:before :after
在这里插入图片描述
html代码:

<ul>
        <li>我是li1</li>
        <li>我是li1</li>
    </ul>
ul {
  width: 200px;
}
li {
  list-style: none;
  background: #ccc;
  margin-bottom: 5px;
}
li::before {
  content: '-';
  color: red;
}
li::after {
  content: '-';
  color: blue;
}

有时会实现各种小三角,效果为:
在这里插入图片描述
html:

<div class="top-triangle"></div>
<div class="bottom-triangle"></div>
<div class="left-triangle"></div>
<div class="right-triangle"></div>

css为:

/* 上三角 */
.top-triangle {
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-bottom: 20px solid yellow;
    margin-bottom: 5px;
}
/* 下三角 */
.bottom-triangle {
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-top: 20px solid yellow;
}
/* 左三角 */
.left-triangle {
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-left: 20px solid yellow;
}
/* 右三角 */
.right-triangle {
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-right: 20px solid yellow;
}

有时会实现微信对话,效果为:
在这里插入图片描述
这里html代码为:

<!-- 对话框 -->
    <div class="left">
        <p>最近怎么样</p>
    </div>
    <div class="right">
        <p>挺好的,最近有点忙,没有怎么联系你</p>
    </div>

css代码为:

.left,.right{
   min-height: 40px;
   position: relative;
   display: table;
   text-align: center;
   border-radius: 7px;
   background-color: #9EEA6A;
   margin: 0;
}
.left {
   left: 10px;
}
.left::before,
.right::after {
   position: absolute;
   content: '';
   display: inline-block;
   width: 0;
   height: 0;
   border: 8px solid transparent;
   top: 11px;
   
}
.left::before {
   border: 8px solid transparent;
   border-right: 8px solid #9EEA6A;
   left: -16px;
}
.right::after {
   right: -16px;
   border-left: 8px solid #9EEA6A;
}
.left p,
.right p {
   display: table-cell;
   vertical-align: middle;
   padding: 0 10px;
}
.right {
   right: -150px;
}
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值