纯CSS实现UI设计中常见的丝带效果

[复制链接]
七夏(UID:1) 发表于 2024-12-30 17:51:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

一、前言

网页中的丝带效果在设计中扮演着多重角色,其作用可以归纳为以下几个方面:

1. 视觉吸引与装饰

  • 增强视觉吸引力:丝带效果以其独特的形态和色彩,能够迅速吸引用户的注意力,成为页面上的视觉焦点。
  • 装饰作用:作为装饰元素,丝带能够提升网页的整体美观度,使页面看起来更加生动有趣。

2. 信息引导与层次划分

  • 信息引导:通过丝带效果的指向性设计,可以引导用户按照特定的路径浏览网页内容,提高信息的可读性和易读性。
  • 层次划分:丝带效果还可以用于区分页面上的不同区域或内容层级,帮助用户更好地理解页面结构和信息组织。

3. 风格塑造与氛围营造

  • 风格塑造:丝带元素在网页设计中常被用于塑造特定的风格,如古典老式、清新文艺、复古等,使网页呈现出独特的视觉效果。
  • 氛围营造:通过丝带的设计和色彩搭配,可以营造出不同的氛围和情感,如节日的喜庆、促销的热闹、活动的庄重等。

4. 强调与突出

  • 重点强调:丝带效果能够突出页面上的重要信息或元素,如优惠活动、新品推荐等,引导用户关注。
  • 品牌标识:在丝带设计中融入品牌元素或色彩,可以加强品牌识别度,提升品牌形象。

5. 用户体验提升

  • 增加互动性:通过CSS等技术实现的丝带效果,如折叠、展开等动态效果,可以增加用户的互动体验,提高用户的参与度和满意度。
  • 提升视觉流畅性:合理的丝带设计可以使页面看起来更加流畅和连贯,减少用户的视觉疲劳感。

在开发中遇到丝带效果,你还在让UI给你切图吗?下面就介绍几种CSS实现网页中常见的丝带效果的实现方式。

二、基础布局

基础布局如下:

<div class="box">
    <div class="ribbon">
        温馨提示
    </div>
</div>
.box {
    position: relative;
    width: 300px;
    height: 160px;
    margin: 60px;
    border-radius: 5px;
    background-color: #fff;
    z-index: 0;
    cursor: pointer;
    border: 1px solid #f6f6f6;
    transition: .5s;
}

三、常见丝带效果

1

图片

.ribbon {
    position: absolute;
    left: -8px;
    top: 16px;
    width: 100px;
    height: 30px;
    background-color: #57DD43;

    /* 内容居中 */
    display: flex;
    justify-content: center;
    align-items: center;

    &::before,
    &::after {
        content: "";
        position: absolute;
    }

    &::before {
        top: -8px;
        left: 0;
        height: 0;
        width: 0;
        border-bottom: 8px solid #57DD43;
        border-left: 8px solid transparent;
        opacity: .8;
    }

    &::after {
        right: -15px;
        height: 0;
        width: 0;
        border-top: 15px solid transparent;
        border-bottom: 15px solid transparent;
        border-left: 15px solid #57DD43;
    }
}

2

图片

.ribbon {
    position: absolute;
    left: -10px;
    top: 16px;
    width: calc(100% + 20px);
    height: 36px;
    background-color: #1890ff;

    /* 内容居中 */
    display: flex;
    justify-content: center;
    align-items: center;

    &::before, &::after {
      content: '';
      position: absolute;
    }

    &::before {
      left: 0;
      height: 0;
      width: 0;
      border-top: 10px solid #1890ff;
      border-left: 10px solid transparent;
      bottom: -10px;
    }

    &::after {
      right: 0;
      bottom: -10px;
      height: 0;
      width: 0;
      border-top: 10px solid #1890ff;
      border-right: 10px solid transparent;
  }
}

3

图片

<div class="box">
    <div class="ribbon">
        <span>温馨提示</span>
    </div>
</div>
.ribbon {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 150px;
    height: 150px;
    overflow: hidden;

    &::before, &::after {
      content: "";
      position: absolute;
    }

    &::before {
      left: 8px;
      width: 40px;
      height: 8px;
      border-radius: 8px 8px 0px 0px;
      background-color: var(--ribbon-primary-color);
      opacity: .6;
    }

    &::after {
      border-radius: 0px 8px 8px 0px;
      width: 8px;
      height: 40px;
      right: 0px;
      bottom: 8px;
      background-color: #57DD43;
      opacity: .6;
    }

    & > span {
      position: absolute;
      top: 20%;
      right: -40%;
      z-index: 2;
      width: 150%;
      height: 40px;
      overflow: hidden;
      transform: rotate(45deg);
      border: 1px dashed;
      box-shadow: 0 0 0 3px #57DD43, 0px 21px 5px -18px rgba(0, 0, 0, 0.6);
      background: #57DD43;

      /* 文本居中 */
      display: flex;
      justify-content: center;
      align-items: center;
      color: white;
    }
}

4

图片

.ribbon {
    position: absolute;
    top: -6px;
    left: 16px;
    width: 40px;
    padding: 0 10px;
    box-sizing: border-box;
    text-align: center;
    background: #ff9900;

    &::before,
    &::after {
        content: '';
        position: absolute;
    }

    &::before {
        top: 0;
        right: -6px;
        height: 0;
        width: 0;
        border-bottom: 6px solid #ff9900;
        border-right: 6px solid transparent;
        opacity: .8;
    }

    &::after {
        height: 0;
        width: 0;
        left: 0;
        bottom: -20px;
        border-left: 20px solid #ff9900;
        border-right: 20px solid #ff9900;
        border-bottom: 20px solid transparent;
    }
}

5

图片

.ribbon {
    position: absolute;
    top: 12px;
    left: -5px;
    padding: 2px 10px;
    background-color: orangered;
    font-size: 12px;
    color: #fff;

    &::before {
        content: "";
        position: absolute;
        left: 0;
        bottom: -4px;
        border-top: 4px solid orangered;
        border-left: 4px solid transparent;
    }
}

图片

.ribbon {
    position: absolute;
    top: 10%;
    right: -5px;
    padding: 2px 10px;
    background-color: orangered;
    font-size: 12px;
    color: #fff;

    &::before {
        content: "";
        position: absolute;
        right: 0;
        bottom: -4px;
        border-top: 4px solid orangered;
        border-right: 4px solid transparent;
    }
}

6

图片

<div class="box">
    <div class="ribbon">
        <span>温馨提示</span>
    </div>
</div>
.ribbon {
    position: absolute;
    top: -6px;
    right: 10px;

    &::after {
        content: "";
        display: block;
        position: absolute;
        width: 0;
        height: 0;
        border-left: 53px solid transparent;
        border-right: 53px solid transparent;
        border-top: 10px solid #57DD43;
    }

    &>span {
        position: relative;
        display: inline-block;
        width: 90px;
        height: 60px;
        line-height: 60px;
        text-align: center;
        background: #57DD43;
        border-top-right-radius: 8px;

        &::before,
        &::after {
            content: "";
            position: absolute;
        }

        &::before {
            left: -6px;
            top: 0;
            width: 6px;
            height: 6px;
            background: #57DD43;
        }

        &::after {
            left: -8px;
            top: 0;
            width: 8px;
            height: 6px;
            border-radius: 8px 8px 0 0;
            background: #57DD43;
        }
    }
}

7

图片

<div class="box">
    <div class="ribbon">
        <span>新品</span>
    </div>
</div>
.ribbon {
    position: absolute;
    top: 0;
    right: 0;
    width: 45px;
    height: 45px;
    background: #57DD43;
    clip-path: polygon(0 0, 100% 100%, 100% 0);
    span {
        font-size: 12px;
        position: absolute;
        top: 6px;
        right: 2px;
        display: inline-block;
        transform: rotate(45deg)
    }
}

图片

<div class="box">
    <div class="ribbon">
        <span>新品</span>
    </div>
</div>
.ribbon {
    position: absolute;
    top: 0;
    left: 0;
    width: 45px;
    height: 45px;
    background: #57DD43;
    clip-path: polygon(0 0, 0 100%, 100% 0);

    span {
        font-size: 12px;
        position: absolute;
        top: 6px;
        right: 16px;
        display: inline-block;
        transform: rotate(-45deg)
    }
}

网页中的丝带效果在视觉吸引、信息引导、风格塑造、强调与突出以及用户体验提升等方面都发挥着重要作用。设计师在运用丝带效果时,应根据网页的整体风格和设计需求进行灵活搭配和创新设计,以达到最佳的视觉效果和用户体验。

小时候,看腻了农村的牛和马,长大后,来到了城里,才知道原来到处都是牛马!
全部回复0 显示全部楼层
暂无回复,精彩从你开始!

快速回帖

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于楼主

管理员
  • 主题

    924
  • 回答

    359
  • 积分

    2515
虚位以待,此位置招租

商务推广

    此位置招租 黑粉猫影院-免费看电影 此位置招租 此位置招租 此位置招租 此位置招租 此位置招租 此位置招租 此位置招租 此位置招租