'Hexagonal CSS background
This is not a question about how to make an individual elements hexagon shaped. There are a lot of those.
This is a question to see if there's a way to create a pure-css hexagon background.
I've gotten kind of close by creating triangles:
*{
margin:0;
padding:0;
border-width:0;
}
html{
height:100%;
}
body{
height:100%;
background: repeating-linear-gradient(120deg, transparent 0px, transparent 44px, rgba(0, 85, 130, 0.5) 50px, transparent 56px, transparent 100px), repeating-linear-gradient(240deg, transparent 0px, transparent 44px, rgba(0, 85, 130, 0.5) 50px, transparent 56px, transparent 100px), repeating-linear-gradient(0deg, transparent 0px, transparent 44px, rgba(0, 85, 130, 0.5) 50px, transparent 56px, transparent 100px), #fffcfc;
background-position:34px, 0, 0;
background-attachment:fixed,fixed,fixed;
}
But there are a couple issues:
- Background size affects line placement.
- It's triangles, and I cannot think of a way to "cover" the lines that appear inside the hexagons formed by them.
Is there a way to accomplish this, or should I give up and use an image?
Solution 1:[1]
See this article: http://lea.verou.me/css3patterns/#honeycomb
Demo to make this future-proof in case the link is taken down:
body {
width: 100%;
height: 100%;
background:
radial-gradient(circle farthest-side at 0% 50%,#fb1 23.5%,rgba(240,166,17,0) 0)21px 30px,
radial-gradient(circle farthest-side at 0% 50%,#B71 24%,rgba(240,166,17,0) 0)19px 30px,
linear-gradient(#fb1 14%,rgba(240,166,17,0) 0, rgba(240,166,17,0) 85%,#fb1 0)0 0,
linear-gradient(150deg,#fb1 24%,#B71 0,#B71 26%,rgba(240,166,17,0) 0,rgba(240,166,17,0) 74%,#B71 0,#B71 76%,#fb1 0)0 0,
linear-gradient(30deg,#fb1 24%,#B71 0,#B71 26%,rgba(240,166,17,0) 0,rgba(240,166,17,0) 74%,#B71 0,#B71 76%,#fb1 0)0 0,
linear-gradient(90deg,#B71 2%,#fb1 0,#fb1 98%,#B71 0%)0 0 #fb1;
background-size:40px 60px;
}
Here is the SCSS version (https://jsfiddle.net/ajnd782w/):
$primary-color: #fb1;
$line-color: #B71;
$transparent: transparent;
$size: 30px;
body {
width: 100%;
height: 100%;
background:
radial-gradient(circle farthest-side at 0% 50%,$primary-color 23.5%,$transparent 0)($size * .7) $size,
radial-gradient(circle farthest-side at 0% 50%,$line-color 24%,$transparent 0)($size * .6129) $size,
linear-gradient($primary-color 14%,$transparent 0, $transparent 85%,$primary-color 0)0 0,
linear-gradient(150deg,$primary-color 24%,$line-color 0,$line-color 26%,$transparent 0,$transparent 74%,$line-color 0,$line-color 76%,$primary-color 0)0 0,
linear-gradient(30deg,$primary-color 24%,$line-color 0,$line-color 26%,$transparent 0,$transparent 74%,$line-color 0,$line-color 76%,$primary-color 0)0 0,
linear-gradient(90deg,$line-color 2%,$primary-color 0,$primary-color 98%,$line-color 0%)0 0 $primary-color;
background-size:($size * 1.333) ($size * 2);
}
Never give up. :)
Solution 2:[2]
Here is Jonathan's amazing answer from above, with variables:
/* source code by Johnathan at:
https://stackoverflow.com/questions/50277743/hexagonal-css-background
*/
:root{
--honeyColor: #fb1;
--honeyBorderColor: #B71;
--size: 30px;
}
body {
width: 100vw;
height: 100vh;
display: block;
position:relative;
background:
radial-gradient(circle farthest-side at 0% 50%, var(--honeyColor) 23.5%,transparent 0)calc(var(--size)*0.7) var(--size),
radial-gradient(circle farthest-side at 0% 50%,var(--honeyBorderColor) 24%,transparent 0)calc(var(--size)*19/30) var(--size),
linear-gradient(var(--honeyColor) 14%,transparent 0, transparent 85%, var(--honeyColor) 0)0 0,
linear-gradient(150deg, var(--honeyColor) 24%,var(--honeyBorderColor) 0,var(--honeyBorderColor) 26%,transparent 0,transparent 74%,var(--honeyBorderColor) 0,var(--honeyBorderColor) 76%, var(--honeyColor) 0)0 0,
linear-gradient(30deg, var(--honeyColor) 24%,var(--honeyBorderColor) 0,var(--honeyBorderColor) 26%,transparent 0,transparent 74%,var(--honeyBorderColor) 0,var(--honeyBorderColor) 76%, var(--honeyColor) 0)0 0,
linear-gradient(90deg,var(--honeyBorderColor) 2%, var(--honeyColor) 0, var(--honeyColor) 98%,var(--honeyBorderColor) 0%)0 0 var(--honeyColor);
background-size:calc(var(--size)*4/3) calc(var(--size)*2);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | lior bakalo |