百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

css 伪元素 css 伪元素实现拖拽条样式

myzbx 2024-12-19 15:02 21 浏览

伪元素共有5个,分别是::before、::after、::first-letter、::first-line和::selection

first-letter

::first-letter主要用于为文本的首字母添加特殊样式

注意:::first-letter 伪元素只适用于块级元素。

first-line

::first-line 伪元素用于向文本的首行添加特殊样式。

注意:::first-line 伪元素只能应用于块级元素。

selection

::selection 伪元素匹配用户选择的元素部分。也就是给我们鼠标滑动选中的部分设置样式,它可以设置以下属性

  • color
  • background
  • cursor
  • outline

before/after

::before用于在元素内容之前插入一些内容,::after用于在元素内容之后插入一些内容,其他方面的都相同。写法就是只要在想要添加的元素选择器后面加上::before或::after即可,有些人会发现,写一个冒号和两个冒号都可以有相应的效果,那是因为在css3中,w3c为了区分伪类和伪元素,用双冒号取代了伪元素的单冒号表示法,所以我们以后在写伪元素的时候尽量使用双冒号。

这些添加不会出现在DOM中不会改变文档内容不可复制,仅仅是在css渲染层加入。所以不要用:before或:after展示有实际意义的内容,尽量使用它们显示修饰性内容,例如图标。

举例:网站有些联系电话,希望在它们前加一个icon?,就可以使用:before伪元素.

二、content属性

不同于其他伪元素,::before和::after在使用的时候必须提供content属性可以为字符串和图片,也可以是空,但不能省略该属性,否则将不生效。

伪类元素的display是默认值inline,可以通过设置display:block来改变其显示。

content可取以下值。

1、string 书名号

使用引号包一段字符串,将会向元素内容中添加字符串。如:a:after{content:""}

举例:

<!DOCTYPE html><meta charset="utf-8" /><style type="text/css">
p::before{
    content: "《";
    color: blue;
}
p::after{
    content: "》";
    color: blue;
}
</style>
<p>平凡的世界</p>

2、attr() 链接

通过attr()调用当前元素的属性,比如将图片alt提示文字或者链接的href地址显示出来。

<style type="text/css">
a::after{
    content: "(" attr(href) ")";
 }
 </style>
 <a href="http://www.cnblogs.com/starof">starof</a>

3、url()/uri() 用于引用媒体文件

举例:“百度”前面给出一张图片,后面给出href属性。

<style>
a::before{
    content: url("https://www.baidu.com/img/baidu_jgylogo3.gif");}
a::after{
    content:"("attr(href)")";}
a{
    text-decoration: none;}</style>
---------------------------
<body><a href="http://www.baidu.com">百度</a></body>    

效果:

4、counter()**调用计数器

调用计数器,可以不使用列表元素实现序号功能。

配合counter-increment和counter-reset属性使用:

h2:before { 
  counter-increment: chapter; 
  content: "Chapter " 
  counter(chapter) ". " 
 }

代码:

<style>
body{
    counter-reset: section;
}
h1{
    counter-reset: subsection;
}
h1:before{
    counter-increment:section;
    content:counter(section) "、";
}
h2:before{
    counter-increment:subsection;
    content: counter(section) "." counter(subsection) "、";
}
</style>
------------------------------------------------
<body>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>
<h1>XML tutorials</h1>
<h2>XML</h2>
<h2>XSL</h2>
</body>   

效果:

了解更多可参考:https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters

三、使用

做出各种图形效果

举例:一个六角星

<style>
#star-six {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
  position: relative;
}
#star-six::after {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid red;
  position: absolute;
  content: "";
  top: 30px;
  left: -50px;
}
</style>

<body><div id="star-six"></div></body>

#star-six的div是一个正三角行,#star-six::after是一个倒三角形,通过绝对定位,调整其位置即可实现六角星的效果。

打印网页的URL

<style>
@media print {
  a[href]:after {
    content: " (" attr(href) ") ";
  }
}
</style>
<body>
<a href="http://www.baidu.com">百度</a>
</body>  

图标

举例:网站有些联系电话,希望在它们前加一个icon?,就可以使用:before伪元素,如下:

<!DOCTYPE html><meta charset="utf-8" />
<style type="text/css">
    .phoneNumber::before {
      content:'\260E';
      font-size: 15px;
    }
    </style>
    <p class="phoneNumber">12345645654</p>

动画

制作一款特殊的鼠标滑入滑出效果

这个效果还是之前一个朋友从某网站看到之后问我能不能实现,我去那个网站查看了代码学会的,觉得很有趣,特意分享给大家。

可以先看一下效果

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>

        .h-button {
            z-index: 1;
            position: relative;
            overflow: hidden;
        }

        .h-button:before,
        .h-button:after {
            content: "";
            width: 0;
            height: 100%;
            position: absolute;
            filter: brightness(.9);
            background-color: inherit;
            z-index: -1;
        }

        .h-button:before {
            left: 0;
        }

        .h-button:after {
            right: 0;
            transition: width .5s ease;
        }

        .h-button:hover::before {
            width: 100%;
            transition: width .5s ease;
        }

        .h-button:hover::after {
            width: 100%;
            background-color: transparent;
        }

        .submit {
            width: 100px;
            height: 40px;
            color: #fff;
            line-height: 40px;
            text-align: center;
            background: #00baca;
            margin: 50px;
        }
    </style>
</head>
<body>
<div class="h-button submit">提交</div>
</body>
<script>

</script>
</html>

禁用网页ctrl+f搜索

有些时候,我们不想要用户使用ctrl+f搜索我们网页内的内容,必须在一些文字识别的网页小游戏里,我们又不想把文字做成图片,那么就可以使用这个属性,使用::before和::after渲染出来的文字,不可选中也不能搜索。当然这个低版本浏览器的兼容性我木有试,谷歌浏览器和safari是可以实现不能选中不可搜索的效果的。

拿上面的示例进行尝试,可以看到,我们使用伪元素添加的[问题]两个字,就无法使用浏览器的搜索工具搜到。

相关推荐

首次被击毁!低调但先进的S-350,为何活得比韩国仿版差这么多?

【军武次位面】作者:乐乐2月18日,乌克兰军方网站发布了其前线炮兵侦察旅,在顿涅茨克地区攻击俄军S-350防空系统的现场视频。这也是这款地位独特的先进防空系统,第一次确认在战场上被摧毁——考虑到近三年...

Windows 10 LTSC 2021 vs 2019:哪个更适合你?

本内容来源于@什么值得买APP,观点仅代表作者本人|作者:闪电神龙微软近日发布了2024年11月份ISO镜像,包括Windows1124H2、Windows1022H2以及Server2025...

叛变投敌?俄军最先进隐身无人机S-70,在乌东上空被苏-57击落!

【军武次位面】作者:天狼2024年10月5日,乌克兰东部战区传来一条令人震惊的消息:一架俄罗斯最先进的隐身无人机S-70“猎人-B”在乌东上空被击落,令人意外的是,击落它的竟然是俄罗斯自己的战斗机。这...

自动驾驶车祸致1死1伤!特斯拉被判赔偿2.43亿美元

当地时间8月1日,美国佛罗里达州一个陪审团裁定,美国电动汽车制造商特斯拉应为2019年一辆配备自动驾驶系统的ModelS所致的致命车祸承担部分责任,并判令该公司向一名遇难女性的家属及一名伤者支付约2...

HP488DZ 无绳电锤钻(18V)牧田DTD156SFJ

HP488DZ无绳电锤钻(18V)牧田DTD156SFJHP488DZ无绳电锤钻(18V)HP488DZ特征HP488D是一款基于HP457D开发的无绳电锤,采用18V锂离子电池供电。其...

FJK-SJRFPZS防爆阀位行程开关级

解答常见误区在工业自动化和安全控制领域,FJK-SJRFPZS防爆阀位行程开关等级是一个关乎设备安全与运行效率的重要参数。许多用户在选择和应用这类开关时,可能对其等级分类存在一些误解。本文将通过通俗易...

China&#39;s PLA aerobatic team to perform in Thailand for 50th anniversary of bilateral diplomatic ties

TIANJIN,March2(Xinhua)--TheBayiAerobaticTeamoftheChinesePeople'sLiberationArmy(PLA)A...

JD.com Enters Travel and Hospitality With Supply Chain-Focused Strategy

TMTPOST--JD.comhasofficiallythrownitshatintoChina’sfiercelycompetitivetravelandhospita...

JD.com Drives Robotics Funding Frenzy With Investments in LimX Dynamics, Spirit AI, and EngineAI

TMTPOST--JD.comisdoublingdownonembodiedintelligence,catalyzinganewwaveoffundinginChi...

JD.com opens first JD Mall in Beijing, steps up offline retail push

bySongJiananJD.comhaslauncheditsfirstJDMALLinBeijing,expandingitsofflineretailfootpr...

JD.com&#39;s food delivery fleet tops 120,000 full-time riders

JD.com'sfull-timefooddeliveryfleethassurpassed120,000ridersandisexpectedtoexceed150,00...

China willing to share military equipment achievements with friendly countries: defense ministry

BEIJING,July8(Xinhua)--Chinahasalwaystakenaprudent,responsibleapproachtomilitaryexpor...

FJK-SJRFPZS防爆阀位行程开关等级

解答常见误区在工业自动化和安全控制领域,FJK-SJRFPZS防爆阀位行程开关等级是一个关乎设备安全与运行效率的重要参数。许多用户在选择和应用这类开关时,可能对其等级分类存在一些误解。本文将通过通俗易...

JD&#39;s 618 Festival Smashes Records as AI Powers Next-Gen Retail Engine

AsianFin–JD.com’s2025“618ShoppingFestival”wrappedupwithrecord-breakingmomentum,drivenby...

JD’s Food Delivery Blitz Shakes Meituan as Founder Wang Xing Vows to Win at All Costs

Credit:CFPAsianFin--JD.comInc.isturninguptheheatinChina'sfooddeliverywars,andfounder...