使用css实现常用的三角效果

项目中三角:

.breadcrumb{
height: 40px;
line-height: 40px;
padding: 0 20px;
border-top: 1px solid #f9c700;
.breadcrumb-title{
text-align: center;
font-size: @fontC;
//通过定义一个伪类after
&:after{
position: absolute;
content: '';
left: 89px;
top: 39px;
border-top: 9px solid @colorM;
//border-left和border-right换成透明色 才能形成三角形 不然是长方形
border-left: 12px solid transparent;
border-right: 12px solid transparent;
//background-color: red;
}
}

详细讲解

实现三角形的方式很多种。比较简单又比较常用的是利用伪类选择器,在网页上也有很多用到这种效果,比如tips信息提示框。下面是自己写的实心三角形,原理其实很简单,代码都能看懂。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.tri_top, .tri_right, .tri_bottom, .tri_left{
width: 150px;
height: 100px;
background: #CCCCCC;
border-radius: 8px;
margin: 50px 50px;
position: relative;
float: left;
}
.tri_top:before{
content: "";
width: 0px;
height: 0px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #CCCCCC;
position: absolute;
top: -10px;
left: 65px;
}
.tri_right:before{
content: "";
width: 0px;
height: 0px;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-left: 10px solid #CCCCCC;
position: absolute;
top: 40px;
left: 150px;
}
.tri_bottom:before{
content: "";
width: 0px;
height: 0px;
border-top: 10px solid #CCCCCC;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
position: absolute;
top: 100px;
left: 70px;
}
.tri_left:before{
content: "";
width: 0px;
height: 0px;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid #CCCCCC;
position: absolute;
top: 40px;
left: -10px;
}
</style>
</head>
<body>
<div class="tri_top"></div> <!--三角形在上边-->
<div class="tri_right"></div> <!--三角形在右边-->
<div class="tri_bottom"></div> <!--三角形在底边-->
<div class="tri_left"></div> <!--三角形在左边-->
</body>
</html>

空心三角形该怎样实现呢?看看以下代码,你会发现其实代码跟实心三角形的代码都是差不多。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.tri_top, .tri_right, .tri_bottom, .tri_left{
width: 150px;
height: 100px;
border: 1px solid #000000;
border-radius: 8px;
margin: 50px 50px;
position: relative;
float: left;
}
.tri_top:before{
content: "";
width: 0px;
height: 0px;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 15px solid #000000;
position: absolute;
top: -15px;
left: 65px;
}
.tri_top:after{
content: "";
width: 0px;
height: 0px;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
border-bottom: 14px solid #FFFFFF;
position: absolute;
top: -14px;
left: 66px;
}
.tri_right:before{
content: "";
width: 0px;
height: 0px;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 15px solid #000000;
position: absolute;
top: 39px;
left: 150px;
}
.tri_right:after{
content: "";
width: 0px;
height: 0px;
border-top: 14px solid transparent;
border-bottom: 14px solid transparent;
border-left: 14px solid #FFFFFF;
position: absolute;
top: 40px;
left: 149px;
}
.tri_bottom:before{
content: "";
width: 0px;
height: 0px;
border-top: 15px solid #000000;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
position: absolute;
top: 101px;
left: 69px;
}
.tri_bottom:after{
content: "";
width: 0px;
height: 0px;
border-top: 14px solid #FFFFFF;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
position: absolute;
top: 100px;
left: 70px;
}
.tri_left:before{
content: "";
width: 0px;
height: 0px;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-right: 15px solid #000000;
position: absolute;
top: 40px;
left: -15px;
}
.tri_left:after{
content: "";
width: 0px;
height: 0px;
border-top: 14px solid transparent;
border-bottom: 14px solid transparent;
border-right: 14px solid #FFFFFF;
position: absolute;
top: 41px;
left: -14px;
}
</style>
</head>
<body>
<div class="tri_top"></div> <!--三角形在上边-->
<div class="tri_right"></div> <!--三角形在右边-->
<div class="tri_bottom"></div> <!--三角形在底边-->
<div class="tri_left"></div> <!--三角形在左边-->
</body>
</html>

写在最后的一个道理: 三角形往哪个方向,那个方向无需设置border,而相反方向设置border颜色,相邻两边的border设为透明。这样就可实现各个方向的三角形

实心三角形利用CSS中的伪元素· :before实现,再利用border的transparent属性即可达到效果。而空心三角形是在空心三角形的基础上再加上伪元素:after实现。伪元素:before实现的是一个实心的三角形,伪元素:after实现的是空心的三角形,进而把实心的三角形覆盖,利用绝对定位的top与left的差值绝对了三角形线的粗细而达到如图的效果。

CSS伪类选择器实现三角形的更多相关文章

  1. CSS伪类选择器

    一.CSS伪类选择器用于给某些选择器添加效果语法规则:选择器:伪选择器例:a:link {color: #FF0000} 未访问的链接 a:visited {color: #00FF00} 已访问的链 ...

  2. 关于CSS伪类选择器

    #CSS伪类选择器 ##使用css伪类选择器需要注意的 使用css的伪类选择器来选择某元素时,需要特别注意 :first-child 和 :nth-child(n) 等时, 网页渲染是从语句的后面开始 ...

  3. CSS伪类选择器 - nth-child(an+b):

    CSS伪类选择器 - nth-child(an+b): 第一种:简单数字序号写法:nth-child(number)直接匹配第number个元素.参数number必须为大于0的整数.li:nth-ch ...

  4. CSS伪类选择器active模拟JavaScript点击事件

    一.说明 设置元素在被用户激活(在鼠标点击与释放之间发生的事件)时的样式. IE7及更早浏览器只支持a元素的:active,从IE8开始支持其它元素的:active. 另:如果需要给超链接定义:访问前 ...

  5. css伪类选择器的查找顺序

    当伪类选择器last-child.first-child无效时,就是因为不了解css伪类选择器的查找顺序造成选中某一元素失败. 先给出一段dom <body> <div>第一个 ...

  6. css伪类选择器及伪元素选择器

    1.类选择器 在css中可以使用类选择器把相同的元素定义成不同的样式.比如: 结果如下: 标题背景未变 2.伪类选择器 类选择器和伪类选择器的区别在于,类选择器我们定义的,而伪类选择器是CSS中已经定 ...

  7. CSS伪类选择器和伪元素选择器

    CSS的伪类选择器常用的是link/visited/hover/active,分别对应未访问.已访问过.鼠标悬停.鼠标按下时的样式,常用于链接,使用时要按此顺序依次写CSS,不能乱 a:link{ba ...

  8. css伪类选择器详细解析及案例使用-----伪类选择器(1)

    动态伪类选择器:E:link :选择匹配的E元素,并且匹配元素被定义了超链接并未被访问过.E:visited :选择匹配的E元素,而且匹配的元素被定义了连接并已被访问过.E:active :选择匹配的 ...

  9. css 伪类选择器制作登录框表单

    使用伪类选择器 制作鼠标悬停时文本框出现橙色虚线边框 制作鼠标激活时出现背景颜色淡橙色 使用css制作文本框圆角矩形效果,制作文本框背景图片,及背景不重复效果 <!DOCTYPE html> ...

随机推荐

  1. Python基础(API接口测试)

    import flask,json,pymysql from flask import request, jsonify, Response from datetime import datetime ...

  2. MAC VMware fusion 12.1.0 Centos7 网络配置

    虚拟机选择NAT模式 获取mac机器中vmnet8的gateway地址 找到 # NAT gateway address这一行,下面的ip就是gateway地址 cat /Library/Prefer ...

  3. CTF入门学习4->前端HTML基础

    Web安全基础 02 前端开发-HTML基础 浏览器对于上网者来说是一种直观.可视化的呈现.服务器发送数据到客户端,客户端需要处理这些数据,互联网就造就了这种数据语言--HTML. 02-00 概述 ...

  4. 快速从零开始整合SSM,小白包会(1)

    整合SSM,关键就是几个xml的配置. 准备: 1.   Idea(配置好tomcat,可以安装插件freeMybatis,提高效率,安装插件不难,百度经验就有) 2.   下载好数据库MySql,以 ...

  5. [bzoj2432]兔农

    将每一个重置为0的点作为一段,那么它会导致后面为以x x为开头的斐波拿起数列的东西,那么设这一段是以x为开头,要快速转移到下一段,就可以解决这道题目为了转移,我们要处理出下面的东西:1.求出x关于模k ...

  6. C#/VB.NET 将彩色PDF转为灰度PDF

    本文以C#代码为例介绍如何实现将彩色PDF文件转为灰度(黑白)的PDF文件,即 将PDF文档里面的彩色图片或者文字等通过调用PdfGrayConverter.ToGrayPdf()方法转为文档页面为灰 ...

  7. redis实现最简单的锁

    <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifact ...

  8. 『与善仁』Appium基础 — 14、Appium测试环境搭建

    目录 1.Appium测试环境搭建整体思路 (1)Android测试环境搭建 (2)Appium测试环境搭建 (3)测试脚本语言的环境搭建 2.Appium在Android端和IOS端的工作流程 (1 ...

  9. 一次奇怪的的bug排查过程

    公司对底层基础库进行了重构,线上稳定跑了几天,在查看订单系统的log时,有几条error信息非常的奇怪, orderID:80320180 statemachine error: no event [ ...

  10. Java跟JavaScript两者间的关系和区别

    想必很多人在刚接触和认识Java的时候,都好奇它与JavaScript之间的关系,长得这么像,难道是出自同门的升级版?嗯,他们的发明者确实颇具渊源,而且客观来说,JS确实相较Java更加简洁大众,但它 ...