美文网首页
2020-05-20 CSS伪类元素罕见案例收纳(一)

2020-05-20 CSS伪类元素罕见案例收纳(一)

作者: 荼蘼toome | 来源:发表于2020-05-20 15:52 被阅读0次

1.父子元素悬停特效

由于伪元素属于其父元素,因此存在一些不寻常的用例


设计的时候有一个Section Title 在他的左边有一个浅蓝的小圆圆,将鼠标悬停在Section Title上,圆圈变大
参考代码
.section-title:before {
    content: "";
    width: 20px;
    height: 20px;
    background: blue;
    /* Other styles */
}

.section-title:hover:before {
    transform: scale(1.2);
}

2.制作缩略图

HTML
<section class="hero">
    <p>Hello, my name is Ahmad. I’m a UX Designer and Front End Developer that enjoys the intersection between design and code. I write on <a href="www.ishadeed.com" class="link-1">ishadeed.com</a> and <a href="www.a11ymatters.com" class="link-2">a11ymatters.com</a> on CSS, UX Design and Web Accessibility.</p>
</section>

1.向hero元素添加padding

为伪元素保留空间,所以添加padding

2.对伪元素进行绝对定位

为了绝对定位它们,我需要定义哪个父类是相对的父类。它应该被添加到hero中 。

.hero部分中的position: relative是影响伪元素的。

3.添加伪元素

最后一步是添加伪元素及其悬停效果:

.link-1 {
  color: #854FBB;
}

@media (min-width: 700px) {
  .link-1:after {
    content: "";
    position: absolute;
    right: 0;
    top: 20px;
    width: 150px;
    height: 100px;
    background: currentColor;
    opacity: 0.85;
    transition: 0.3s ease-out;
  }

  .link-1:hover {
    text-decoration: underline;
  }

  .link-1:hover:after {
    transform: scale(1.2);
    opacity: 1;
  }
}

使用了currentColor作为伪元素背景色。如果你不知道这个关键字,它表示继承其父元素的color值。所以在任何时候,我想要改变链接的颜色,只改变一次是很容易的。

3.增加可点击区域的大小

通过向链接添加一个伪元素,链接周围的可点击区域将变得更大。这是非常有用的,将增强用户的体验。

4.叠加层

类似背景图像的元素,上层有一个渐变的叠加层

.hero {
  position: relative;
  height: 300px;
  background: url("image.jpg") center/cover;
}
.hero:after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(180deg, #851717 0%, #30328C 100%);
  mix-blend-mode: color;
}

5.包裹的阴影

image.png
创建div

<div class="elem"></div>

body{
    width: 100%;
    height: 100%;
    background-color: #eee;
}
    .elem {
     position: relative;
     display: flex;
     align-items: center;
     max-width: 400px;
     background: #fff;
     padding: 2rem 1rem;
     font-size: 1.5rem;
     margin: 2rem auto;
     text-align: center;
     box-sizing: border-box;
}
添加伪类元素

为每个元素添加:before:after伪元素 ,其宽度为50%

.elem:before,
.elem:after {
    content: "";
    position: absolute;
    top: 2px;
    width: 50%;
    height: 100%;
}

.elem:before {
    left: 0;
    background: grey;
}

.elem:after {
    right: 0;
    background: #000;
}

添加transform: skew(x),其中X为2度。对于其中之一,X应该为负数以实现所需的效果。


image.png

源码

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="elem"></div>
</body>

<script type="text/javascript">
    
</script>

<style>
body{
    width: 100%;
    height: 100%;
    background-color: #eee;
}
    .elem {
     position: relative;
     display: flex;
     align-items: center;
     max-width: 400px;
     background: #fff;
     padding: 2rem 1rem;
     font-size: 1.5rem;
     margin: 2rem auto;
     text-align: center;
     box-sizing: border-box;
}

.elem:before,
.elem:after {
    content: "";
    position: absolute;
    top: 2px;
    width: 50%;
    height: 100%;
    z-index: -1;
    background: linear-gradient(to bottom,transparent,#000);
    filter: blur(3px);
    opacity: 0.3;
}

.elem:before {
    left: 0;
    transform: skewY(-2deg);
    /*background: grey;*/
}

.elem:after {
    right: 0;
    transform: skewY(2deg);
    /*background: #000;*/
}
</style>
</html>
另一种方式

另一种选择,即在伪元素:before和:after之间交换skewY值。

相关文章

  • 2020-05-20 CSS伪类元素罕见案例收纳(一)

    1.父子元素悬停特效 由于伪元素属于其父元素,因此存在一些不寻常的用例 设计的时候有一个Section Title...

  • css伪元素

    css 伪元素 详述: 这篇文章是继 css的伪类 与 css 伪类选择器 后的 CSS伪元素总结, 意在总结连贯...

  • part2: CSS基础-练习

    CSS全称: cascading style sheets 谈谈css伪类与伪元素 这是我见过最全的伪类和伪元素总...

  • 伪元素&伪类

    css引入伪类和伪元素概念是为了格式化文档树以外的信息。 伪元素&伪类: css引入伪类和伪元素概念是为了格式化文...

  • 伪元素&伪类

    css引入伪类和伪元素概念是为了格式化文档树以外的信息。 伪元素&伪类:css引入伪类和伪元素概念是为了格式化文档...

  • 伪元素和伪类

    伪元素和伪类 什么是伪元素? CSS 在渲染文档的时候,伪元素可以通过 css 给 HTML 添加一个元素(叫标签...

  • 伪类和伪元素

    伪类:CSS 伪类用于向某些选择器添加特殊的效果。 伪元素:CSS 伪元素用于向某些选择器设置特殊效果。 1、伪类...

  • CSS

    伪类的语法: CSS 类也可与伪类搭配使用。 伪元素

  • 伪元素和伪类问题

    1.单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素。2.css2伪类和伪元素都是用单引号,所有的浏...

  • CSS进阶知识点--CSS3伪类选择器和伪元素

    伪类选择器 动态伪类(锚点伪类、用户行为伪类) UI元素状态伪类 CSS3结构伪类 否定选择器 伪元素 动态伪类 ...

网友评论

      本文标题:2020-05-20 CSS伪类元素罕见案例收纳(一)

      本文链接:https://www.haomeiwen.com/subject/rwagohtx.html