利用CSS伪元素写三角形

在做一些网页布局的时候,经常会遇到各种方向的三角形,常常令人头疼,今天给大家介绍一些利用CSS伪元素写各个方向三角形的方法

目录

一、CSS伪元素写三角形

1、向右的三角形

2、向上的三角形

3、向下的三角形

4、向左的三角形


一、CSS伪元素写三角形

1、向右的三角形

<!DOCTYPE html>
<html>
<!--根标签-->

<head>
    <!--不可视区域-->
    <meta charset='UTF-8'>
    <!--国际编码-->
    <meta name='Keyword' content='关键字'>
    <meta name='Description' content='描述'>
    <title>demo</title>
    <style>
        #triangle {
            margin: 100px;
            width: 100px;
            height: 100px;
            background-color: black;
            position: relative;
        }

        #triangle:before {
            position: absolute;
            content: "";
            width: 0;
            height: 0;
            top: 0px;
            left: 100px;
            border-top: solid 50px transparent;
            border-left: solid 50px black;
            border-bottom: solid 50px transparent;
        }
    </style>
</head>

<body>

    <div id="triangle">
    </div>


</body>

</html>

定位后这里的 top:0px;

                      left:100px

效果图

2、向上的三角形

<!DOCTYPE html>
<html>
<!--根标签-->

<head>
    <!--不可视区域-->
    <meta charset='UTF-8'>
    <!--国际编码-->
    <meta name='Keyword' content='关键字'>
    <meta name='Description' content='描述'>
    <title>demo</title>
    <style>
        #triangle {
            position: relative;
            margin: 100px;
            width: 100px;
            height: 100px;
            background-color: black;
        }

        #triangle:before {
            position: absolute;
            content: "";
            width: 0;
            height: 0;
            top: -50px;
            left: 0px;
            border-right: solid 50px transparent;
            border-left: solid 50px transparent;
            border-bottom: solid 50px black;
        }
    </style>
</head>

<body>

    <div id="triangle">
    </div>


</body>

</html>

定位后这里的 top:-50px;

                      left: 0px

效果图

3、向下的三角形

<!DOCTYPE html>
<html>
<!--根标签-->

<head>
    <!--不可视区域-->
    <meta charset='UTF-8'>
    <!--国际编码-->
    <meta name='Keyword' content='关键字'>
    <meta name='Description' content='描述'>
    <title>demo</title>
    <style>
        #triangle {
            position: relative;
            margin: 100px;
            width: 100px;
            height: 100px;
            background-color: black;
        }

        #triangle:before {
            position: absolute;
            content: "";
            width: 0;
            height: 0;
            top: 100px;
            left: 0px;
            border-top: solid 50px black;
            border-right: solid 50px transparent;
            border-left: solid 50px transparent;
        }
    </style>
</head>

<body>

    <div id="triangle">
    </div>


</body>

</html>

定位后这里的 top:100px;

                      left: 0px

效果图

4、向左的三角形

<!DOCTYPE html>
<html>
<!--根标签-->

<head>
    <!--不可视区域-->
    <meta charset='UTF-8'>
    <!--国际编码-->
    <meta name='Keyword' content='关键字'>
    <meta name='Description' content='描述'>
    <title>demo</title>
    <style>
        #triangle {
            position: relative;
            margin: 100px;
            width: 100px;
            height: 100px;
            background-color: black;
        }

        #triangle:before {
            position: absolute;
            content: "";
            width: 0;
            height: 0;
            top: 0px;
            left: -50px;
            border-top: solid 50px transparent;
            border-right: solid 50px black;
            border-bottom: solid 50px transparent;
        }
    </style>
</head>

<body>

    <div id="triangle">
    </div>


</body>

</html>

定位后这里的 top:0px;

                      left:-50px

效果图

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用CSS伪元素来绘制倒三角形,可以按照以下步骤进行操作: 1. 创建一个具有伪元素的元素。可以使用`::before`或`::after`伪元素来实现。 2. 使用`content: ''`属性来为伪元素创建内容。 3. 设置伪元素的位置属性,例如`position: absolute`,以便将其定位在正确的位置。 4. 使用`width: 0`和`height: 0`属性将伪元素的宽度和高度设置为0,这样它将不会显示任何内容。 5. 使用边框属性设置三角形的样式,例如`border-top`、`border-left`和`border-right`。 6. 根据需要设置边框的颜色和大小。 以下是一个示例代码,演示如何使用CSS伪元素绘制倒三角形: ```css .element::before { content: ''; position: absolute; bottom: -12px; /* 根据需要调整位置 */ left: 50%; /* 根据需要调整位置 */ width: 0; height: 0; border-top: 12px solid #092c53; /* 设置三角形的颜色 */ border-left: 6px solid transparent; /* 可根据需要调整大小 */ border-right: 6px solid transparent; /* 可根据需要调整大小 */ } ``` 请注意,上述代码中的`.element`是一个示例选择器,您可以根据实际情况将其替换为您要应用样式的元素的选择器。另外,您可以根据需要调整位置、大小和颜色的值来实现所需的倒三角形效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [css设置利用伪元素设置三角形](https://blog.csdn.net/weixin_52984349/article/details/125857702)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [css伪元素模拟一个三角](https://blog.csdn.net/anny_mei/article/details/119944904)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值