How to Create a Word Style for Code Listings
🔍 WiseChecker

How to Create a Word Style for Code Listings

When you paste code into a Word document, the default formatting often breaks indentation, changes font sizes, and strips syntax coloring. Word does not have a built-in style designed for code, so each paste requires manual cleanup. This article shows you how to create a dedicated Word style for code listings that preserves monospaced font, consistent spacing, and a shaded background.

Key Takeaways: Building a Code Listing Style in Word

  • Create a new style via Home > Styles > Create a Style: Allows you to define font, spacing, and borders for code without affecting normal text.
  • Set font to Consolas or Courier New at 9 or 10 pt: Monospaced fonts align characters vertically, which is required for readable code.
  • Add a shaded background using Format > Border > Shading: Distinguishes code blocks from body text visually.

What a Code Style Does and Why You Need One

A Word style is a saved set of formatting instructions. When you apply a style to a paragraph, Word instantly sets the font, size, color, spacing, indentation, borders, and shading to the values you defined. For code listings, you need a style that uses a monospaced font, keeps line spacing tight, adds a subtle background color, and prevents Word from auto-correcting things like straight quotes to curly quotes or hyphen sequences to dashes.

Without a dedicated style, you must manually change the font to Consolas, reduce the font size, remove extra paragraph spacing, and add a border every time you paste code. The style eliminates this repetition. You can also share the style with colleagues by saving it in a template.

Prerequisites Before Creating the Style

You need a blank Word document or an existing document where you will paste code. The style you create will be available in that document only unless you save it to the Normal.dotm template. To make the style available in all new documents, select the option to save it to the template when you create the style. You should also decide on a specific monospaced font. Consolas is installed on Windows 10 and Windows 11 with Office. Courier New is available on all systems.

Steps to Create the Code Style in Word

Method 1: Create the Style From Scratch

  1. Open the Styles pane
    On the Home tab, click the small arrow in the bottom-right corner of the Styles group. This opens the Styles pane on the right side of the window.
  2. Start a new style
    Click the New Style button at the bottom of the Styles pane. It looks like a plus sign with a page. The Create New Style from Formatting dialog opens.
  3. Name the style
    In the Name field, type Code or Code Listing. Choose a name that is easy to find in the Quick Styles gallery.
  4. Set the style type
    In the Style type dropdown, select Paragraph. This ensures the style applies to entire paragraphs of code.
  5. Set the font and size
    In the Formatting section, choose Consolas from the font dropdown. Set the font size to 9 or 10 points. Smaller code blocks may use 9 pt to fit more text on screen.
  6. Set line spacing
    Click the Format button in the lower-left corner of the dialog. Choose Paragraph. In the Paragraph dialog, set Spacing Before and After to 0 pt. Set Line spacing to Single. Click OK.
  7. Add a shaded background
    Click Format again, then choose Border. In the Borders and Shading dialog, go to the Shading tab. Under Fill, select a light gray like Gray-10% or a custom RGB value of 240,240,240. Click OK.
  8. Add a left border for distinction
    In the same Borders dialog, go to the Borders tab. Under Setting, choose Custom. In the Preview area, click the left border button to add a vertical line. Set the Color to a dark blue or black, and the Width to 1 1/2 pt. Click OK twice to close both dialogs.
  9. Save the style to the template
    Back in the Create New Style from Formatting dialog, select New documents based on this template at the bottom. This makes the Code style available in every new document you create. Click OK.

Method 2: Create the Style by Modifying an Existing Code Block

  1. Paste a sample code block
    Paste a short code snippet into your document. Select the entire block.
  2. Format the selection manually
    Set the font to Consolas, size 10, single line spacing, and add a gray background via Home > Paragraph > Shading.
  3. Create a style from the selection
    Right-click the selected text. Choose Styles > Save Selection as a New Quick Style. Name it Code and click OK.
  4. Refine the style
    Open the Styles pane, right-click the Code style, and choose Modify. Add the left border and set spacing to 0 pt before and after as described in Method 1 steps 6 through 8.

Common Mistakes and Things to Avoid

Word Changes Straight Quotes to Curly Quotes

When you paste code, Word may convert straight quotes to curly quotes, which breaks most programming languages. To prevent this, go to File > Options > Proofing > AutoCorrect Options. On the AutoFormat As You Type tab, uncheck Straight quotes with smart quotes. Do this before pasting code.

Line Numbers or Indentation Are Lost

Word preserves spaces and tabs when pasting from a code editor. If indentation disappears, paste using Keep Source Formatting (the paste option with the clipboard and paintbrush icon). Do not use Merge Formatting or Keep Text Only.

The Style Does Not Appear in the Quick Styles Gallery

After creating the style, right-click it in the Styles pane and choose Add to Quick Style Gallery. This places a button on the Home tab so you can apply the style with one click.

Code Wraps Poorly When the Window Is Resized

Word wraps lines by default, which breaks long lines of code. To set the style to not wrap, modify the style, click Format > Paragraph, and on the Line and Page Breaks tab, check Don’t hyphenate. For very long lines, consider setting the page orientation to Landscape or using a smaller font size.

Setting Default Normal Style Custom Code Style
Font Calibri 11 pt Consolas 9 or 10 pt
Spacing Before/After 8 pt / 8 pt 0 pt / 0 pt
Line spacing 1.15 Single
Background fill None Gray-10% or 240,240,240
Left border None 1 1/2 pt dark line
Smart quotes Enabled Disabled via AutoCorrect Options

With the Code style saved to your template, you can apply it to any paragraph by selecting the text and clicking the Code button on the Home tab. To update all existing code blocks at once, modify the style and Word will reformat every paragraph using that style. The style works in Word for Microsoft 365, Word 2021, Word 2019, and Word 2016 on Windows and Mac.