Skip to content
TabShedKitsMy desk

One Gradient, Four Platforms: CSS, Tailwind, SwiftUI and Android XML

Updated 5 min readBy

Design a two-color linear gradient once and copy matching code for CSS, Tailwind, SwiftUI and Android XML, including the Android angle quirk.

Gradient GeneratorMake CSS gradients and copy the code
Open Gradient Generator
On this page
  1. What the tool does
  2. Step by step
  3. What each snippet looks like
  4. Where to paste each one
  5. The Android angle explained
  6. Making a gradient that looks good
  7. Limits and workarounds
  8. Common problems
  9. A worked example: a hero background for web and app
  10. Testing the result on each platform
  11. FAQ

A designer sends you a gradient as a screenshot, and you need it in a website and in two mobile apps. Writing the same gradient three times by hand invites small mismatches: a reversed direction, a color typed with the wrong digit, an angle that turns the wrong way on Android. The CSS Gradient Generator creates the code for all four targets from one set of choices, so the result is the same everywhere. This guide explains what it makes and how to use each snippet.

What the tool does

The tool makes a two-color linear gradient. You choose a Start color, an End color and a Direction, and a large preview updates live. The directions are Left to right, Right to left, Top to bottom and Bottom to top. That is all it does: no radial gradients, no extra color stops and no free angles. For those you edit the code by hand after copying it.

Step by step

  1. Pick the start and end colors with the color boxes. They default to red and blue, so change them right away.
  2. Choose a direction.
  3. Press the copy button next to the target you need. A status line confirms what was copied.
  4. Paste it where it belongs (see below).

What each snippet looks like

With the default red-to-blue left-to-right gradient, the four snippets are:

TargetCode
CSSbackground: linear-gradient(to right, #ff0000, #0000ff);
Tailwind CSSbg-gradient-to-r from-[#ff0000] to-[#0000ff]
SwiftUILinearGradient(gradient: Gradient(colors: [Color(red: 255 / 255, green: 0 / 255, blue: 0 / 255), Color(red: 0 / 255, green: 0 / 255, blue: 255 / 255)]), startPoint: .leading, endPoint: .trailing)
Android XML<gradient android:angle="0" android:startColor="#ff0000" android:endColor="#0000ff" android:type="linear"/>

Where to paste each one

  • CSS: inside a rule, for example .hero { background: ...; }. It works on any element.
  • Tailwind: as classes on an element. The colors are written as arbitrary values in square brackets, so you do not need to add them to your config. If you use Tailwind v4 the gradient utility names may differ, so check your version's docs.
  • SwiftUI: as a view, for example as a background or the content of a shape. The color values are written as red, green and blue fractions, which is what the Color initializer expects.
  • Android XML: the gradient element belongs inside a shape drawable file (a <shape> root) in your drawable folder, which you then use as a view background.

The Android angle explained

Android counts the angle counter-clockwise from left-to-right, which surprises people who know CSS. The tool converts the direction for you:

Direction in the toolandroid:angle
Left to right0
Bottom to top90
Right to left180
Top to bottom270

If you hand-edit the angle, keep to multiples of 45 degrees, which Android's gradient shape expects.

Making a gradient that looks good

  • Keep the two colors related. Neighbouring hues, or a light and dark version of the same hue, give smooth and calm results. Opposite colors such as red and green meet in a muddy gray in the middle.
  • Mind the text. The gradient changes brightness across the area, so test text against both ends. The Color Contrast Checker helps; check the lightest and the darkest point.
  • Watch for banding. Soft gradients with very close colors can show visible steps on some screens. Increase the distance between the colors a little or add a subtle texture in your design.
  • Start from a real palette. Pull the colors from a brand logo or photo with the Image Color Palette tool.

Limits and workarounds

Need three colors? Copy the CSS and add a stop by hand: linear-gradient(to right, #ff0000, #ffff00, #0000ff). Need a diagonal? Replace to right with to bottom right or an angle like 135deg. Need a radial gradient? Use radial-gradient(circle, #ff0000, #0000ff). The tool will not generate these for you, but its first two colors are a handy starting point to copy from.

Common problems

  • The copy button did nothing. The browser may block clipboard access. The status line says "Copy failed"; select the code and copy it manually.
  • The gradient is reversed on one platform. Re-copy the snippet for that platform instead of editing another one; each is generated with the right start and end points.
  • The Tailwind class has no effect. Make sure Tailwind scans the file where you pasted the class and that the class is not built from string pieces.

A worked example: a hero background for web and app

A designer asks for a calm blue gradient behind the top section of both the website and the mobile app. You pick a deep blue as the Start color and a lighter blue as the End color, and set the direction to Top to bottom. On the web, you paste the CSS line into the hero rule. In the Android app, you paste the gradient element with an angle of 270 into a shape drawable, which is how Android expresses top to bottom. In the iOS app, you use the SwiftUI snippet with the top and bottom start and end points. All three show the same dark-to-light movement, because they were generated from one choice, and a later color change means regenerating once and pasting three times.

Testing the result on each platform

Gradients are rendered by each platform's own engine, so tiny differences are normal: color management, dithering and the way the midpoint is calculated differ slightly between a browser, Android and iOS. Compare them on a real device at the size they will be used rather than judging from a screenshot on one screen. If a difference bothers you, adjust the colors a little until the middle of the gradient looks the same on all of them.

FAQ

Is anything saved or sent?

No. The gradient is generated in your browser and not stored.

Can I export an image?

No; the tool gives code only.

What color format does it use?

HEX in the CSS, Tailwind and Android snippets, and 0 to 255 values divided by 255 in SwiftUI.