《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(三)

简介: 《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(三)

11-菱形图片


介绍两种使用 css 制作菱形图片的方案,相对于设计师提供裁切好的图片,更加灵活


1687780619623.png


基于变形的方案


/**
 * Diamond images — via transforms
 */
<div class="diamond">
    <img src="http://placekitten.com/200/300" />
</div>
.diamond {
    width: 250px;
    height: 250px;
    transform: rotate(45deg);
    overflow: hidden;
    margin: 100px;
}
.diamond img {
    max-width: 100%;
    transform: rotate(-45deg) scale(1.42);
    z-index: -1;
    position: relative;
}


裁切路径方案


1687780630152.png


/**
 * Diamond images — via clip-path
 */
img {
    max-width: 250px;
    margin: 20px;
    -webkit-clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
    clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
    transition: 1s;
}
img:hover {
    -webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
    clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
}


12-切角效果


背景知识:css 渐变,background-size, '条纹背景'

切角是一种流行的设计风格,使用css 制作切角可以实现更加灵活和多样的颜色效果


渐变方案


1687780642915.png


<div>Hey, focus! You’re er!</div>
/**
 * Beveled corners — with gradients
 */
div {
    background: #58a;
    background: linear-gradient(135deg, transparent 15px, #58a 0) top left,
                linear-gradient(-135deg, transparent 15px, #58a 0) top right,
                linear-gradient(-45deg, transparent 15px, #58a 0) bottom right,
                linear-gradient(45deg, transparent 15px, #58a 0) bottom left;
    background-size: 50% 50%;
    background-repeat: no-repeat;
    padding: 1em 1.2em;
    max-width: 12em;
    color: white;
    font: 150%/1.6 Baskerville, Palatino, serif;
}


弧形切角


渐变技巧的一个变种


1687780659178.png



/**
 * Scoop corners
 */
div {
    background: #58a;
    background: radial-gradient(circle at top left, transparent 15px, #58a 0) top left,
              radial-gradient(circle at top right, transparent 15px, #58a 0) top right,
              radial-gradient(circle at bottom right, transparent 15px, #58a 0) bottom right,
              radial-gradient(circle at bottom left, transparent 15px, #58a 0) bottom left;
background-size: 50% 50%;
background-repeat: no-repeat;
padding: 1em 1.2em;
max-width: 12em;
color: white;
font: 130%/1.6 Baskerville, Palatino, serif;
}


内联 SVG 与 border-image 方案


1687780671312.png


/**
 * Beveled corners — with border-image and an inline SVG
 */
div {
    border: 21px solid transparent;
    border-image: 1 url('data:image/svg+xml,\
                          <svg xmlns="http://www.w3.org/2000/svg" width="3" height="3" fill="%2358a">\
                          <polygon points="0,1 1,0 2,0 3,1 3,2 2,3 1,3 0,2" />\
                          </svg>');
    background: #58a;
    background-clip: padding-box;
    padding: .2em .3em;
    max-width: 12em;
    color: white;
    font: 150%/1.6 Baskerville, Palatino, serif;
}


裁切路径方案


1687780683275.png


强烈推荐这种,可以比较方便的制作大量不同颜色的切角图片,只需要改变背景色就好。


/**
 * Beveled corners — with clip-path
 */
div {
    background: #58a;
    -webkit-clip-path: 
            polygon(20px 0, calc(100% - 20px) 0, 100% 20px, 100% calc(100% - 20px),
            calc(100% - 20px) 100%,
            20px 100%, 0 calc(100% - 20px), 0 20px);
    clip-path:
                    polygon(20px 0, calc(100% - 20px) 0, 100% 20px, 100% calc(100% - 20px),
                    calc(100% - 20px) 100%,
                    20px 100%, 0 calc(100% - 20px), 0 20px);
    padding: 1em 1.2em;
    max-width: 12em;
    color: white;
    font: 150%/1.6 Baskerville, Palatino, serif;
}


13-梯形标签页


背景知识:基本的3D变形,“平行四边形”

方案1: 伪元素制作两条斜边,border  制作上下平行边 方案2: 3D 旋转,普通的3D 旋转会有副作用比如 文字的变形


1687780697598.png



<nav>
    <a href="#">Home</a>
    <a href="#" class="selected">Projects</a>
    <a href="#">About</a>
</nav>
<main>
    Content area
</main>
<nav class="left">
    <a href="#">Home</a>
    <a href="#" class="selected">Projects</a>
    <a href="#">About</a>
</nav>
<main>
    Content area
</main>
<nav class="right">
    <a href="#">Home</a>
    <a href="#" class="selected">Projects</a>
    <a href="#">About</a>
</nav>
<main>
    Content area
</main>
/**
 * Trapezoid tabs
 */
body {
    padding: 40px;
    font: 130%/2 Frutiger LT Std, sans-serif;
}
nav {
    position: relative;
    z-index: 1;
    padding-left: 1em;
}
nav > a {
    position: relative;
    display: inline-block;
    padding: .3em 1em 0;
    color: inherit;
    text-decoration: none;
    margin: 0 -.3em;
} 
nav > a::before,
main {
    border: .1em solid rgba(0,0,0,.4);
}
nav a::before {
    content: ''; /* To generate the box */
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    z-index: -1;
    border-bottom: none;
    border-radius: .5em .5em 0 0;
    background: #ccc linear-gradient(hsla(0,0%,100%,.6), hsla(0,0%,100%,0));
    box-shadow: 0 .15em white inset;
    transform: scale(1.1, 1.3) perspective(.5em) rotateX(5deg);
    transform-origin: bottom;
}
nav a.selected { z-index: 2;}
nav a.selected::before {
    background-color: #eee;
    margin-bottom: -.08em;
}
main {
    display: block;
    margin-bottom: 1em;
    background: #eee;
    padding: 1em;
    border-radius: .15em;
}
nav.left > a::before {
    transform: scale(1.2, 1.3) perspective(.5em) rotateX(5deg);
    transform-origin: bottom left;
}
nav.right { padding-left: 2em; }
nav.right > a::before {
    transform: scale(1.2, 1.3) perspective(.5em) rotateX(5deg);
    transform-origin: bottom right;
}


14-简单的饼图


1687780715519.png


基于transtorm 和 animation


/**
 * Animated pie chart
 */
.pie {
    width: 100px; height: 100px;
    border-radius: 50%;
    background: yellowgreen;
    background-image: linear-gradient(to right, transparent 50%, currentColor 0);
    color: #655;
}
.pie::before {
    content: '';
    display: block;
    margin-left: 50%;
    height: 100%;
    border-radius: 0 100% 100% 0 / 50%;
    background-color: inherit;
    transform-origin: left;
    animation: spin 3s linear infinite, bg 6s step-end infinite;
}
@keyframes spin {
    to { transform: rotate(.5turn); }
}
@keyframes bg {
    50% { background: currentColor; }
}


1687780729711.png


/**
 * Static pie charts
 */
.pie {
    display: inline-block;
    position: relative;
    width: 100px;
    line-height: 100px;
    border-radius: 50%;
    background: yellowgreen;
    background-image: linear-gradient(to right, transparent 50%, #655 0);
    color: transparent;
    text-align: center;
}
@keyframes spin {
    to { transform: rotate(.5turn); }
}
@keyframes bg {
    50% { background: #655; }
}   
.pie::before {
    content: '';
    position: absolute;
    top: 0; left: 50%;
    width: 50%; height: 100%;
    border-radius: 0 100% 100% 0 / 50%;
    background-color: inherit;
    transform-origin: left;
    animation: spin 50s linear infinite, bg 100s step-end infinite;
    animation-play-state: paused;
    animation-delay: inherit;
}


SVG 方案


1687780741318.png


/**
 * Pie charts — with SVG
 */
.pie {
    width: 100px;
    height: 100px;
    display: inline-block;
    margin: 10px;
    transform: rotate(-90deg);
}
svg {
    background: yellowgreen;
    border-radius: 50%;
}
circle {
    fill: yellowgreen;
    stroke: #655;
    stroke-width: 32;
}
@keyframes grow { to { stroke-dasharray: 100 100 } }
.pie.animated circle {
    animation: grow 2s infinite linear;
}


15-单侧投影


单侧投影


1687780752565.png


/**
 * One-sided shadow
 */
div {
    width: 1.6in;
    height: 1in;
    background: #fb3;
    box-shadow: 0 5px 4px -4px black;
}


邻边投影


1687780763082.png


/**
 * One-sided shadow
 */
div {
    width: 1.6in;
    height: 1in;
    background: #fb3;
    box-shadow: 3px 3px 6px -3px black;
}


双侧投影


1687780774257.png


/**
 * One-sided shadow
 */
div {
    width: 1.6in;
    height: 1in;
    background: #fb3;
    box-shadow: 5px 0 5px -5px black,
               -5px 0 5px -5px black;
}


目录
相关文章
|
14天前
|
前端开发 容器
《CSS 简易速速上手小册》第6章:高级 CSS 技巧(2024 最新版)
《CSS 简易速速上手小册》第6章:高级 CSS 技巧(2024 最新版)
21 0
|
4月前
|
前端开发
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(二)
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(二)
|
4月前
|
前端开发 JavaScript 容器
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(一)
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(一)
|
11月前
|
前端开发
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(四)
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(四)
53 0
|
11月前
|
前端开发 JavaScript
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(二)
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(二)
66 0
|
11月前
|
前端开发 容器
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(一)
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(一)
44 0
|
3天前
|
缓存 移动开发 前端开发
【专栏:HTML与CSS前端技术趋势篇】HTML与CSS在PWA(Progressive Web Apps)中的应用
【4月更文挑战第30天】PWA(Progressive Web Apps)结合现代Web技术,提供接近原生应用的体验。HTML在PWA中构建页面结构和内容,响应式设计、语义化标签、Manifest文件和离线页面的创建都离不开HTML。CSS则用于定制主题样式、实现动画效果、响应式布局和管理字体图标。两者协同工作,保证PWA在不同设备和网络环境下的快速、可靠和一致性体验。随着前端技术进步,HTML与CSS在PWA中的应用将更广泛。
|
3天前
|
前端开发 JavaScript 开发者
【专栏:HTML与CSS前端技术趋势篇】前端框架(React/Vue/Angular)与HTML/CSS的结合使用
【4月更文挑战第30天】前端框架React、Vue和Angular助力UI开发,通过组件化、状态管理和虚拟DOM提升效率。这些框架与HTML/CSS结合,使用模板语法、样式管理及组件化思想。未来趋势包括框架简化、Web组件标准采用和CSS在框架中角色的演变。开发者需紧跟技术发展,掌握新工具,提升开发效能。
|
3天前
|
前端开发 JavaScript UED
【专栏:HTML 与 CSS 前端技术趋势篇】Web 性能优化:CSS 与 HTML 的未来趋势
【4月更文挑战第30天】本文探讨了CSS和HTML在Web性能优化中的关键作用,包括样式表压缩、选择器优化、DOM操作减少等策略。随着未来趋势发展,CSS模块系统、自定义属性和响应式设计将得到强化,HTML新特性也将支持复杂组件构建。同时,应对浏览器兼容性、代码复杂度和性能功能平衡的挑战是优化过程中的重要任务。通过案例分析和持续创新,我们可以提升Web应用性能,创造更好的用户体验。
|
3天前
|
移动开发 前端开发 UED
【专栏:HTML与CSS前端技术趋势篇】渐进式增强与优雅降级在前端开发中的实践
【4月更文挑战第30天】前端开发中的渐进式增强和优雅降级是确保跨浏览器、跨设备良好用户体验的关键策略。渐进式增强是从基础功能开始,逐步增加高级特性,保证所有用户能访问基本内容;而优雅降级则是从完整版本出发,向下兼容,确保低版本浏览器仍能使用基本功能。实践中,遵循HTML5/CSS3规范,使用流式布局和响应式设计,检测浏览器特性,并提供备选方案,都是实现这两种策略的有效方法。选择合适策略优化网站,提升用户体验。