You can add code to Figma by using the Code Inspector within the right-hand properties panel or by exporting code snippets from specific components. This enables you to directly generate CSS, React, Vue, or Swift code based on your design, eliminating the need to manually translate pixels into syntax.

What You'll Need

  • A Figma account (free or paid).
  • A device with a web browser (Chrome, Safari, Edge, Firefox) or the Figma desktop app installed.
  • Selection of design layers (frames, components, or text) ready to be coded.

Step-by-Step

  1. Select the layer. Click on the frame, component, or specific group of layers in your canvas that you want to generate code for. Ensure the layer is fully selected; you cannot generate code for a single stroke or a fill color without selecting the parent frame or group.
  2. Open the Code Inspector. Look at the right-hand side of the Figma interface. Locate the panel titled "Code". If it is not visible, click the "Properties" tab at the top of that panel to switch the view, or use the keyboard shortcut Cmd/Ctrl + P (Cmd on Mac, Ctrl on Windows/Linux) to open a command palette and search for "Code Inspector" if it has been hidden.
  3. Choose the code language. In the top left of the Code Inspector panel, you will see a dropdown menu labeled "Type" or "Language". Click this menu to select your target language. Common options include CSS, React, Vue, SwiftUI, and Swift UI.
  4. Select the code format. Just below the language selector, you will see options for the output format. You can choose between React Functional Component (which creates a reusable function), React Class Component, Styled Components, Emotion, Styled JSX, or Vanilla CSS. The choice here depends on which framework your development team uses. For a standard web project, "CSS" is often the best starting point.
  5. Adjust the style properties. Figma will generate a list of properties on the right side of the panel. You can manually tweak these values to fine-tune the output. For example, you can change the font size, color, or padding. These changes will instantly update the code snippet in the main preview area.
  6. Copy the code. Once you are satisfied with the generated code, click the "Copy” button at the top right of the inspector panel. This copies the syntax to your clipboard, ready to be pasted directly into your project's code editor.
  7. Paste into your editor. Open your text editor (VS Code, Sublime Text, etc.) and paste the code. Ensure you are pasting it into the correct file—usually a component file or a global stylesheet, depending on your project's structure.
  8. Review for missing assets. Figma generates code based on the styles and properties you selected. It does not automatically include custom fonts or external images. If your design uses a font that is not installed on your computer, the code will use a fallback font. You will need to manually ensure the font files are linked or imported in your project.

Common Mistakes

One of the most common errors is selecting the wrong parent layer. If you try to generate code for a single rectangle inside a larger frame, Figma may not generate the dimensions correctly or may omit the relative positioning. Always select the frame or group that contains the elements you want to code.

Another mistake is ignoring the naming conventions. By default, Figma uses generic names like "Frame 1" or "Rectangle 2". To make the code readable, it is highly recommended to rename your layers in the Layers panel before generating the code. This ensures that the generated class names or component names are descriptive and useful for your team.

Tips

Utilize the Auto Layout features. When using Auto Layout, the generated code will include layout properties like padding, gap, and alignment. This creates a much more robust and maintainable codebase than manual absolute positioning.

Use the Design Tokens feature for consistency. If you have a large design system, you can create tokens for colors and spacing. When you generate code, these tokens will be referenced in the output, making it easier to apply global changes later.

Troubleshooting

If the code inspector does not appear, ensure you have selected a frame or component. The inspector is disabled for simple shapes like rectangles or lines because they do not have complex code properties.

If the generated code does not match your design, check the Prototype tab in the right-hand panel. Sometimes interactions or triggers set in the Prototype tab can interfere with the visual representation used for code generation. Try disabling these interactions temporarily to see if the code output aligns with the design.

Frequently Asked Questions

Does Figma generate code for images?

No, Figma does not automatically generate code for images or icons. You must manually export these assets as PNG, SVG, or other formats and then reference them in your CSS or code.

Can I generate code for iOS apps?

Yes, Figma supports iOS and Swift UI code generation. Ensure you select the appropriate language in the inspector. Note that the output is often a UI component structure, and you may need to add logic in Xcode to handle user interactions.

Is the code export free?

Yes, the code export feature is available to all Figma users, including those on the free plan.

Can I customize the generated class names?

Yes, you can rename your layers. Figma will use the layer name as the basis for the generated class name. However, it will strip out special characters to ensure the class name is valid in most programming languages.

Why is the code output empty?

An empty output usually means that the selected layer has no style properties set. For example, a frame with no fill, border, or shadow, and no text inside it, will generate empty code. Try selecting a frame with content like text or an image.

Does the code work on mobile browsers?

The code generated by Figma is standard CSS and can be used for mobile web development. However, Figma does not generate native mobile app code (like .apk or .ipa files). For native apps, you must use a specialized plugin or export the design as a prototype to be programmed in a native IDE.