Gradient Flags
Published 9 years, 4 months pastWith the news today of the Supreme Court’s decision in Obergefell v. Hodges, and my recent completion of the part of CSS:The Definitive Guide that deals with gradients, I thought I’d create a couple of flags using linear gradients.
First, I’ll define the basic box of the flags. The dimensions are the same as those specified for the U.S. flag, 1:1.9. I added a couple of box shadows for visual constrast with the background.
div {height: 10em; width: 19em; margin: 3em;
box-shadow: 0 0 0.5em rgba(0,0,0,0.25),
0.4em 0.6em 0.5em rgba(0,0,0,0.25);}
Okay, with that set, first up is what’s often called the pride flag, which is to say the “rainbow flag”. There’s an interesting history to the design of the flag, but I’m going to stick with “the six-color version popular since 1973”, as Wikipedia puts it.
For such a flag, we just need color stripes with hard stops between them. That means putting the color stops right up against each other, like so:
div#rainbow {
background: linear-gradient(180deg,
red 0%, red 16.7%,
orange 16.7%, orange 33.3%,
yellow 33.3%, yellow 50%,
green 50%, green 66.7%,
blue 66.7%, blue 83.3%,
purple 83.3%, purple 100%);
}
The first red 0%
isn’t really necessary, nor is the last purple 100%
, but I left them in for the sake of consistency. You could remove them both in order to make the CSS a little smaller, and still get the same result. I decided to go from red to purple, as the spectrum is usually described, which meant having the gradient ray point from top to bottom. Thus 180deg
, although to bottom
would be completely equal in this case.
Now for the US flag. In this case, things get a little more interesting. I’ll note right up front that I’m not going to put in any stars, in order to keep this simple and gradient-focused, and yet it’s still interesting. We could use a repeating linear gradient, like so:
repeating-linear-gradient(180deg,
#B22234, #B22234 0.77em, white 0.77em, white 1.54em)
That would then cause each red-white pair of stripes to repeat vertically forever. Because the specified stripes are manually calculated to be 1/13th of the height of the overall flag (10em
), they’ll just fit like they should.
The problem there is that if the overall flag size ever changes, like if the height and weight are converted to pixels, the stripes will get out of sync with the dimensions of the flag. Fortunately, we don’t have to rely on ems here; we can use percentages. Thus:
repeating-linear-gradient(180deg,
#B22234, #B22234 7.7%, white 7.7%, white 15.4%)
Ta-da! The stripes are the right sizes, and scale with any changes to the height and width of the flag, and repeat as required.
That’s all well and good, but we still need the blue canton (as it’s called). Since the canton will be on top of the stripes, it actually needs to come first in the comma-separated value list for background-image
. That gives us:
background-image:
linear-gradient(0deg, #3C3B6E, #3C3B6E),
repeating-linear-gradient(180deg,
#B22234, #B22234 7.7%, white 7.7%, white 15.4%);
Wait. A blue-to-blue gradient? Well, yes. I want a consistent blue field, and one way to create that is a gradient that doesn’t actually grade. It’s a quick way to create a solid-color image that can be sized and positioned however we like.
So, now we size and position the canton. According to the official design specifications for the flag, the canton is the height of seven stripes, or 53.85% the height of the overall flag, and 40% the width of the flag. That means a background-size
of 40% 53.85%
. The stripes we then have to size at 100% 100%
, in order to make sure they cover the entire background area of the flag. Finally, we position the canton in the top left; the stripes we can position anywhere along the top. so we’ll leave them top left
as well.
The final result:
div#usa {
background-image:
linear-gradient(0deg, #3C3B6E, #3C3B6E),
repeating-linear-gradient(180deg,
#B22234, #B22234 7.7%, white 7.7%, white 15.4%);
background-size: 40% 53.85%, 100% 100%;
background-repeat: no-repeat;
background-position: top left;
}
And if you, like Bryan Fischer, believe that morally speaking “6/26 is our 9/11”, you can move the canton from top left
to bottom left
in order to invert the flag, which is permitted “as a signal of dire distress in instances of extreme danger to life or property”.
(Of course, it’s a lot easier to do that with background positioning since I didn’t include the stars. If the stars were there, then we’d have left the canton’s position alone and done a rotateX()
transform instead.)
So, there you go: two gradient flags. You can see both (along with the “distress” variant) on this test page. If you’ve any desire to use any or all of them, go ahead. No copyright, trademark, patent, etc., etc. is asserted, implied, etc., etc. I just wanted to have a little fun with gradients, and thought I’d share.