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

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

6-复杂的背景图案


桌布图案


1687780484295.png



/**
 * Checkerboard
 */
background: #eee;
background-image: 
  linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0),
  linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0);
background-position: 0 0, 15px 15px;
background-size: 30px 30px;
min-height: 100%;
/**
 * Polka dot
 */
background: #655;
background-image: radial-gradient(tan 20%, transparent 0),
                  radial-gradient(tan 20%, transparent 0);
background-size: 30px 30px;
background-position: 0 0, 15px 15px;


棋盘


1687780495889.png



/**
 * Checkerboard
 */
background: #eee;
background-image: 
  linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0),
  linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0);
background-position: 0 0, 15px 15px;
background-size: 30px 30px;
min-height: 100%;
/**
 * Checkerboard with SVG
 */
background: #eee url('data:image/svg+xml,\
            <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" fill-opacity=".25" >\
            <rect x="50" width="50" height="50" />\
            <rect y="50" width="50" height="50" />\
            </svg>');
background-size: 30px 30px;


角向渐变


1687780508116.png



/**
 * Conic gradients test
 * PASS if green gradient, FAIL if red.
 */
background: red;
background: conic-gradient(limegreen, green, limegreen);
min-height: 100%;


7-伪随机背景


介绍:重复的平铺图案虽然美观,但是不太自然,下面介绍增加随机性背景的方法。


1687780521737.png



/**
 * Pseudorandom stripes
 */
background: hsl(20, 40%, 90%);
background-image: 
    linear-gradient(90deg, #fb3 11px, transparent 0),
    linear-gradient(90deg, #ab4 23px, transparent 0),
    linear-gradient(90deg, #655 23px, transparent 0);
background-size: 83px 100%, 61px 100%, 41px 100%;


8-连续的图像边框


介绍:通常这种效果是借助 双 dom 来实现,一个作为背景图,一个作为内容;我们的改进方案是基于一个元素来实现的。


1687780534551.png



/**
 * Basic border-image demo
 */
div {
  border: 40px solid transparent;
  border-image: 33.334% url('data:image/svg+xml,<svg xmlns="http:%2F%2Fwww.w3.org%2F2000%2Fsvg" width="30" height="30"> \
                        <circle cx="5" cy="5" r="5" fill="%23ab4"%2F><circle cx="15" cy="5" r="5" fill=" %23655"%2F> \
                        <circle cx="25" cy="5" r="5" fill="%23e07"%2F><circle cx="5" cy="15" r="5" fill=" %23655"%2F> \
                        <circle cx="15" cy="15" r="5" fill="hsl(15, 25%, 75%)"%2F> \
                        <circle cx="25" cy="15" r="5" fill=" %23655"%2F><circle cx="5" cy="25" r="5" fill="%23fb3"%2F> \
                        <circle cx="15" cy="25" r="5" fill=" %23655"%2F><circle cx="25" cy="25" r="5" fill="%2358a"%2F><%2Fsvg>');
  padding: 1em;
  max-width: 20em;
  font: 130%/1.6 Baskerville, Palatino, serif;
}
div:nth-child(2) {
  margin-top: 1em;
  border-image-repeat: round;
}


1687780548665.png


利用上面的条纹背景


/**
 * Vintage envelope themed border
 */
div {
    padding: 1em;
    border: 1em solid transparent;
    background: linear-gradient(white, white) padding-box,
                repeating-linear-gradient(-45deg, red 0, red 12.5%, transparent 0, transparent 25%, 
                  #58a 0, #58a 37.5%, transparent 0, transparent 50%) 0 / 6em 6em;
    max-width: 20em;
    font: 100%/1.6 Baskerville, Palatino, serif;
}


9-自适应的椭圆


介绍:border-radius 其实可以单独指定水平和垂直半径,用斜杠(/)分隔这两个值即可。利用这个特性可以创建椭圆圆角。


椭圆


1687780563532.png



/**
 * Flexible ellipse
 */
div {
    width: 20em;
    height: 10em;
    background: #fb3;
    border-radius: 50%;
}


半椭圆


1687780571511.png


/**
 * Flexible half ellipse
 */
div {
    display: inline-block;
    width: 16em;
    height: 10em;
    margin: 1em;
    background: #fb3;
    border-radius: 50% / 100% 100% 0 0;
}
div:nth-of-type(2) { border-radius: 50% / 0 0 100% 100%; }
div:nth-of-type(3) { border-radius: 100% 0 0 100% / 50%; }
div:nth-of-type(4) { border-radius: 0 100% 100% 0 / 50%; }


四分之一椭圆


1687780587663.png


/**
 * Flexible quarter ellipse
 */
div {
    display: inline-block;
    width: 16em;
    height: 10em;
    margin: 1em;
    background: #fb3;
    border-radius: 100% 0 0 0;
}
div:nth-of-type(2) { border-radius: 0 100% 0 0; }
div:nth-of-type(3) { border-radius: 0 0 100% 0; }
div:nth-of-type(4) { border-radius: 0 0 0 100%; }


10-平行四边形


背景知识: 基本的 css 变形 transform: skewx()


1687780599585.png


潜套元素方案


可以解决文字变形的问题


/**
 * Parallelograms — with extra HTML element
 */
<a href="#yolo" class="button"><div>Click me</div></a>
<button class="button"><div>Click me</div></button>
.button { transform: skewX(45deg); }
.button > div { transform: skewX(-45deg); }
.button {
    display: inline-block;
    padding: .5em 1em;
    border: 0; margin: .5em;
    background: #58a;
    color: white;
    text-transform: uppercase;
    text-decoration: none;
    font: bold 200%/1 sans-serif;
}


伪元素方案


/**
 * Parallelograms — with pseudoelements
 */
<a href="#yolo" class="button"><div>Click me</div></a>
<button class="button"><div>Click me</div></button>
.button {
    position: relative;
    display: inline-block;
    padding: .5em 1em;
    border: 0; margin: .5em;
    background: transparent;
    color: white;
    text-transform: uppercase;
    text-decoration: none;
    font: bold 200%/1 sans-serif;
}
.button::before {
    content: ''; /* To generate the box */
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    z-index: -1;
    background: #58a;
    transform: skew(45deg);
}


目录
相关文章
|
17小时前
|
前端开发 容器
《CSS 简易速速上手小册》第6章:高级 CSS 技巧(2024 最新版)
《CSS 简易速速上手小册》第6章:高级 CSS 技巧(2024 最新版)
24 0
|
17小时前
|
前端开发
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(二)
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(二)
|
17小时前
|
前端开发 JavaScript 容器
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(一)
【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)(一)
|
11月前
|
前端开发
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(四)
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(四)
55 0
|
11月前
|
前端开发
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(三)
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(三)
55 0
|
11月前
|
前端开发 容器
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(一)
《css揭秘》- 47个不可不知的 css 技巧(上篇0-19)(一)
45 0
|
17小时前
|
移动开发 HTML5
HTML5/CSS3粒子效果进度条代码
HTML5/CSS3进度条应用。这款进度条插件在播放进度过程中出现粒子效果,就像一些小颗粒从进度条上散落下来
16 0
HTML5/CSS3粒子效果进度条代码
|
17小时前
|
移动开发 前端开发 JavaScript
:掌握移动端开发:HTML5 与 CSS3 的高效实践
:掌握移动端开发:HTML5 与 CSS3 的高效实践 “【5月更文挑战第6天】”
26 1
|
17小时前
|
缓存 移动开发 前端开发
【专栏:HTML与CSS前端技术趋势篇】HTML与CSS在PWA(Progressive Web Apps)中的应用
【4月更文挑战第30天】PWA(Progressive Web Apps)结合现代Web技术,提供接近原生应用的体验。HTML在PWA中构建页面结构和内容,响应式设计、语义化标签、Manifest文件和离线页面的创建都离不开HTML。CSS则用于定制主题样式、实现动画效果、响应式布局和管理字体图标。两者协同工作,保证PWA在不同设备和网络环境下的快速、可靠和一致性体验。随着前端技术进步,HTML与CSS在PWA中的应用将更广泛。
|
17小时前
|
前端开发 JavaScript 开发者
【专栏:HTML与CSS前端技术趋势篇】前端框架(React/Vue/Angular)与HTML/CSS的结合使用
【4月更文挑战第30天】前端框架React、Vue和Angular助力UI开发,通过组件化、状态管理和虚拟DOM提升效率。这些框架与HTML/CSS结合,使用模板语法、样式管理及组件化思想。未来趋势包括框架简化、Web组件标准采用和CSS在框架中角色的演变。开发者需紧跟技术发展,掌握新工具,提升开发效能。

热门文章

最新文章