如何在CSS中使用线性渐变创建带有圆点纹理的背景?

huangapple go评论85阅读模式
英文:

How to create a polka dot texture with linear gradient as background in css?

问题

这是一个渐变背景:

  1. body {
  2. background: linear-gradient(to right, red , yellow);
  3. }

这是斑点背景的 CSS:

  1. body {
  2. background-color: #94fcc8;
  3. opacity: 1;
  4. background-image: radial-gradient(#7c7c50 0.9500000000000001px, transparent 0.9500000000000001px), radial-gradient(#7c7c50 0.9500000000000001px, #94fcc8 0.9500000000000001px);
  5. background-size: 38px 38px;
  6. background-position: 0 0,19px 19px;
  7. }

如何将它们合并在一起?

英文:

Here is a gradient background:

  1. body {
  2. background: linear-gradient(to right, red , yellow);
  3. }

and here is the polka dot css:

  1. body {
  2. background-color: #94fcc8;
  3. opacity: 1;
  4. background-image: radial-gradient(#7c7c50 0.9500000000000001px, transparent 0.9500000000000001px), radial-gradient(#7c7c50 0.9500000000000001px, #94fcc8 0.9500000000000001px);
  5. background-size: 38px 38px;
  6. background-position: 0 0,19px 19px;
  7. }

how do I somehow merge it together?

here is the codepen:
https://codepen.io/danichk/pen/YyVeXa

Thanks in advance.

答案1

得分: 1

翻译好的部分如下:

> background-image 接受逗号分隔的图像/渐变列表,background-....-blend-mode 属性也是如此。值的顺序很重要...

代码片段如下:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-css -->

  1. body {
  2. background: 小麦色;
  3. background-image: 径向渐变(鲑鱼 20%,透明 0), /* 圆点图案 */
  4. 径向渐变(鲑鱼 20%,透明 0),
  5. 线性渐变(向右, 红色, 黄色); /* 您的渐变 */
  6. background-size: 30px 30px, /* 圆点图案 */
  7. 30px 30px,
  8. 100% 100%; /* 您的渐变 */
  9. background-position: 0 0, 15px 15px;
  10. /* 如果没有第三组值,第三个渐变将默认为第一组值 (0 0) */
  11. }

<!-- end snippet -->

英文:

As commented

> background-image accepts a comma-delimited list of images/gradients, as do the background-.. and ..-blend-mode properties. Order of the values does matter...

snippet

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-css -->

  1. body {
  2. background: wheat;
  3. background-image: radial-gradient(salmon 20%, transparent 0), /* polka dots */
  4. radial-gradient(salmon 20%, transparent 0),
  5. linear-gradient(to right, red , yellow); /* your gradient */
  6. background-size: 30px 30px, /* polka dots */
  7. 30px 30px,
  8. 100% 100%; /* your gradient */
  9. background-position: 0 0, 15px 15px;
  10. /* Without a 3rd set of values, the 3rd gradient
  11. will default to the first set of values (0 0) */
  12. }

<!-- end snippet -->

答案2

得分: 0

  1. html {
  2. --g:/30px 30px 径向渐变(鲑鱼色 20%, #0000 0);
  3. 背景:
  4. 0 0 var(--g),15px 15px var(--g),
  5. 线性渐变(45度,红色,蓝色);
  6. 最小高度: 100%;
  7. }
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. html {
  2. --g:/30px 30px radial-gradient(salmon 20%, #0000 0);
  3. background:
  4. 0 0 var(--g),15px 15px var(--g),
  5. linear-gradient(45deg,red,blue);
  6. min-height: 100%;
  7. }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 23:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定