Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Swagger UI Theme Style Modification

Swagger UI Theme Style Modification

Swagger UI provides multiple ways to customize the interface style so the documentation better matches your company or product brand image.

In a Spring Boot project, you can create a custom CSS file and configure Swagger UI to use it:

Create a custom CSS file, for example src/main/resources/static/swagger-ui/custom.css:

Example

/* Modify the top bar color */

.swagger-ui

.topbar

{

 

background-color

:

#1e88e5

;

}

/* Modify link color */

.swagger-ui

.info

a

{

 

color

:

#1e88e5

;

}

/* Modify button style */

.swagger-ui

.btn

.execute

{

 

background-color

:

#43a047

;

 

color

:

white

;

}

/* Modify request method labels */

.swagger-ui

.opblock-get

.opblock-summary-method

{

 

background-color

:

#29b6f6

;

}

.swagger-ui

.opblock-post

.opblock-summary-method

{

 

background-color

:

#66bb6a

;

}

/* Modify the collapsible panel */

.swagger-ui

.opblock

{

 

border-radius

:

4px

;

 

box-shadow

:

0

1px

3px

rgba

(

0

,

0

,

0

,

0.12

)

;

}

/* Custom font */

.swagger-ui

{

 

font-family

:

'Roboto'

,

sans-serif

;

}

Reference the custom CSS in the Swagger configuration:

Example

public

SwaggerUiConfigParameters swaggerUiConfigParameters

(

)

{

   

return

new

SwaggerUiConfigParameters

(

)

            .

withConfigUrl

(

"/swagger-ui/custom.css"

)

;

}

For SpringDoc OpenAPI:

Example

application.

yml

springdoc

:

  swagger

ui

:

    path

:

/

swagger

ui.

html

    config

url

:

/

swagger

ui

/

custom.

css

Use a theme plugin

For more complex theme customization, you can use a Swagger UI theme plugin.

Install the theme plugin (in China you may consider other CDNs or host it locally):

Example

For an Express project:

Example

javascriptconst express

=

require

(

'express'

)

;

const

swaggerUi

=

require

(

'swagger-ui-express'

)

;

const

swaggerDocument

=

require

(

'./swagger.json'

)

;

const

options

=

{

  customCssUrl

:

'https://cdn.jsdelivr.net/npm/swagger-ui-themes@3.0.0/themes/3.x/theme-material.css'

}

;

app.

use

(

'/api-docs'

,

swaggerUi.

serve

,

swaggerUi.

setup

(

swaggerDocument

,

options

)

)

;

Common themes include:

Add a Logo and brand identity

Add a Logo in Spring Boot

Example

@Bean

public

OpenAPI customOpenAPI

(

)

{

   

return

new

OpenAPI

(

)

            .

info

(

new

Info

(

)

                    .

title

(

"API interface documentation"

)

                    .

version

(

"1.0.0"

)

                    .

description

(

"API interface detailed description"

)

                    .

contact

(

new

Contact

(

)

                            .

name

(

"Development team"

)

                            .

email

(

"team@example.com"

)

                            .

url

(

"https://example.com"

)

)

                   

// Add the x-logo extension

                    .

addExtension

(

"x-logo"

,

Map

.

of

(

                           

"url"

,

"/images/logo.png"

,

                           

"backgroundColor"

,

"#FFFFFF"

,

                           

"altText"

,

"Company Logo"

                   

)

)

)

;

}

Add a Logo in Express

Example

const

swaggerOptions

=

{

  swaggerOptions

:

{

    plugins

:

[

{

      statePlugins

:

{

        spec

:

{

          wrapSelectors

:

{

            info

:

(

ori

)

=>

(

...

args

)

=>

{

             

return

{

                ...

ori

(

...

args

)

,

               

'x-logo'

:

{

                  url

:

'/images/logo.png'

,

                  backgroundColor

:

'#FFFFFF'

,

                  altText

:

'Company Logo'

               

}

             

}

;

           

}

         

}

       

}

     

}

   

}

]

 

}

}

;

app.

use

(

'/api-docs'

,

swaggerUi.

serve

,

swaggerUi.

setup

(

swaggerDocs

,

swaggerOptions

)

)

;

Use a custom template

For deeper customization, you can use a custom Swagger UI template.

1. Create a custom template file src/main/resources/templates/swagger-ui.html:

Example

DOCTYPE html

<

html lang

=

"en"

<

head

<

meta charset

=

"UTF-8"

   

<

title

API Documentation Center

title

   

<

link rel

=

"stylesheet"

href

=

"https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.5.0/swagger-ui.css"

   

<

link rel

=

"stylesheet"

href

=

"/swagger-ui/custom.css"

   

<

link rel

=

"icon"

type

=

"image/png"

href

=

"/images/favicon.png"

sizes

=

"32x32"

   

<

style

        .

company

header

{

            padding

:

20px

;

            background

color

:

#f8f8f8

;

            text

align

:

center

;

            border

bottom

:

1px solid #ddd

;

       

}

        .

company

logo

{

            max

height

:

50px

;

       

}

        .

footer

{

            text

align

:

center

;

            padding

:

20px

;

            color

:

666

;

            font

size

:

12px

;

            border

top

:

1px solid #ddd

;

            margin

top

:

20px

;

       

}

   

style

head

<

body

<

div

class

=

"company-header"

<

img src

=

"/images/logo.png"

alt

=

"Company Logo"

class

=

"company-logo"

       

<

h1

API Documentation Center

h1

   

div

   

<

div id

=

"swagger-ui"

div

   

<

div

class

=

"footer"

<

p

©

2025

Company Name. All rights reserved.

p

       

<

p

If you have any questions, please contact

:

<

a href

=

"mailto:api@example.com"

api

@

example.

com

a

p

   

div

   

<

script src

=

"https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.5.0/swagger-ui-bundle.js"

script

   

<

script

        window.

onload

=

function

(

)

{

           

const

ui

=

SwaggerUIBundle

(

{

                url

:

'/v3/api-docs'

,

                dom_id

:

'#swagger-ui'

,

                deepLinking

:

true

,

                presets

:

[

                    SwaggerUIBundle.

presets

.

apis

,

                    SwaggerUIBundle.

SwaggerUIStandalonePreset

               

]

,

                layout

:

'BaseLayout'

,

                docExpansion

:

'none'

,

                tagsSorter

:

'alpha'

,

                operationsSorter

:

'alpha'

           

}

)

;

            window.

ui

=

ui

;

       

}

   

script

body

html

2. Configure the use of the custom template in a Spring Boot project:

Example

java@Configuration

public

class

SwaggerUIConfig

implements

WebMvcConfigurer

{

    @Override

   

public

void

addViewControllers

(

ViewControllerRegistry registry

)

{

        registry.

addViewController

(

"/swagger-ui/"

)

.

setViewName

(

"swagger-ui"

)

;

        registry.

addRedirectViewController

(

"/swagger-ui"

,

"/swagger-ui/"

)

;

   

}

}