使用::before和::after来完成尖角效果_html/css_WEB-ITnose

WBOY
发布: 2016-06-24 11:36:21
原创
2005 人浏览过

一、目标

目标完成下图效果:

二、完成

1、分析

在::before和::after伪元素的用法一文中有说到使用::befrore和::after可以完成一个六边形。这个案例是用一个#star-six完成一个正三角形,通过::after实现一个倒三角形,然后绝对定位放好位置。我们接下来也用这个思路完成。

2、完成

第一步,先完成如下基本的效果,实现一个在浏览器中居中的文本

代码如下:

<style>.middle{text-align:center;}.title{background-color:black;color:#f3e14f;display:inline-block;font-size:18px;height:40px;line-height:40px;padding:0 50px;}</style><div class="middle"><h5 class="title">升级有好礼</h5></div>
登录后复制

第二步,做左右两边尖尖的效果。可以根据做五角星的代码修改。

代码如下:

<style>#star-three {  width: 0;  height: 0;  border-left: 100px solid transparent;  border-top: 50px solid red;  border-bottom: 50px solid red;/*  position: relative;*/}</style><div id="star-three"></div>
登录后复制

第三步,通过::before来实现第二步中的效果。

代码还是第二步中的代码,但是是通过::before来实现记得写上content:""。然后调整一下尺寸。

.title::before{  width: 0;  height: 0;  border-left: 40px solid transparent;  border-top: 20px solid red;  border-bottom: 20px solid red;  content: "";}
登录后复制

如果给border-left设置蓝色会发现效果如下。【原理还不是很懂,不知道这个高度是因为什么原因??】

结果这是什么鬼?不是想要的效果。此时需要调整布局。

第四步,使用绝对定位调整布局。

.title设置relative,.title::before设置absolute。效果如下。

第五步,通过left调整。

.title{background-color:black;color:#f3e14f;display:inline-block;font-size:18px;height:40px;line-height:40px;position:relative;padding:0 50px;}.title::before{  width: 0;  height: 0;  border-left: 40px solid transparent;  border-top: 20px solid red;  border-bottom: 20px solid red;  content: "";  position:absolute;  left:-40px;}
登录后复制

第六步,同理,通过::after实现右边效果。

.title::after{  width: 0;  height: 0;  border-right: 40px solid transparent;  border-top: 20px solid red;  border-bottom: 20px solid red;  content: "";  position:absolute;  right:-40px;}
登录后复制

第七步,改下颜色就好了。

完整代码如下:

<!DOCTYPE html><html><head><meta charset="utf-8"/><title>demo of starof</title><style>.middle{text-align:center;}.title{background-color:black;color:#f3e14f;display:inline-block;font-size:18px;height:40px;line-height:40px;position:relative;padding:0 50px;}.title::before{  width: 0;  height: 0;  border-left: 40px solid transparent;  border-top: 20px solid black;  border-bottom: 20px solid black;  content: "";  position:absolute;  left:-40px;}.title::after{  width: 0;  height: 0;  border-right: 40px solid transparent;  border-top: 20px solid black;  border-bottom: 20px solid black;  content: "";  position:absolute;  right:-40px;}</style></head><body><div class="middle">  <h5 class="title">升级有好礼</h5></div></body></html>
登录后复制

效果

 

本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:有问题欢迎与我讨论,共同进步。

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!