Skip to main content

How to Match the Microsoft Teams Theme in a Web Page with TeamsJS

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

Teams has a light, a dark and a high contrast theme, and a web page inside Teams, in a tab or a dialog, has to look right in all of them. The page can ask Teams which theme is on with the Teams JavaScript library (TeamsJS), and pick its colors from the answer.

How a Page Learns the Theme​

A page inside Teams runs in a frame, and Teams doesn't restyle what's inside it. To follow Teams' theme, the page asks for it. TeamsJS gives the page its context with microsoftTeams.app.getContext(), and context.app.theme holds the theme's name. Microsoft's Get context for your tab describes it as the current user interface (UI) theme such as default, dark, or glass, contrast:

ValueTheme
defaultLight. Microsoft's docs: When the app.theme property in getContext API returns the default value, Teams client is in light theme.
darkDark
contrastHigh contrast
glassTeams on Apple Vision Pro, according to the docs

The page writes the value on its <body> as an attribute, and CSS rules pick the colors for each value.

Syntax​

Read the theme and write it on the page, after app.initialize():

Syntax (JavaScript)
const context = await microsoftTeams.app.getContext();
document.body.dataset.theme = context.app.theme;

Then style each theme:

Syntax (CSS)
body[data-theme="<theme>"] { <declarations> }
  • context.app.theme is default, dark or contrast in Teams on the web.
  • document.body.dataset.theme = … sets the <body>'s data-theme attribute, so the page's HTML becomes <body data-theme="dark"> in the dark theme.
  • <theme> is the value the rule is for.
  • <declarations> are the styles for that theme, such as color: #ffffff;.

The Example​

The app Dialog Page Demo is a bot that opens a Teams dialog with a page it serves, theme.html. The page shows one line, Teams theme: and the value TeamsJS reports. You'll open it in each theme and make its text readable in all of them.

Prerequisites​

  • The page's domain in your app's validDomains. Teams loads a page in a dialog or tab only from a domain its manifest lists.

Step 1: Read the Theme​

Start with a page that loads TeamsJS, connects to Teams, reads the theme, shows it and writes it on the <body>. Its style sets only the font and the spacing:

wwwroot/theme.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Theme check</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; }
</style>
</head>
<body>
<p>Teams theme: <strong id="theme">...</strong></p>

<script src="https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js"
integrity="sha384-oM1pbPWSxyAWgEVI9GzHDR2xzdMMehbAdsl9cH5emcTxX9Qr/kE4IS/Ld5IdcLmy"
crossorigin="anonymous"></script>
<script>
async function start() {
await microsoftTeams.app.initialize();

const context = await microsoftTeams.app.getContext();
document.body.dataset.theme = context.app.theme;
document.getElementById("theme").textContent = context.app.theme;
}

start();
</script>
</body>
</html>
  • await microsoftTeams.app.initialize() connects the page to Teams. getContext() comes after it.
  • document.getElementById("theme").textContent = … shows the value on the page, so you can see what Teams reports.

How to Add the Teams JavaScript Library (TeamsJS) to a Web Page from the CDN explains the script tag and its integrity hash.

Open the dialog in Teams. With Teams following my computer's light theme, the page read default:

A Teams dialog from the app Dialog Page Demo, titled Theme check, on a white background, with the page's line Teams theme: default in dark text.

Step 2: Open the Page in the Dark Theme​

Switch Teams to the dark theme, as How to Switch Microsoft Teams to Dark Theme shows, and open the dialog again. The page read dark, but its text stayed black on the dark dialog:

The same Theme check dialog in Teams' dark theme: the app name and title are white, but the page's line Teams theme: dark is black on the dark grey dialog and barely readable.

The page sets no text color, so it gets the browser's black. It sets no background either, so Teams' dark dialog shows through behind it. The page knows the theme now; it just doesn't use it yet.

Step 3: Give Each Theme Its Colors​

Add a text color for the light theme, and a rule for the dark and high contrast themes, to the <style>:

wwwroot/theme.html (style)
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; color: #242424; }
body[data-theme="dark"],
body[data-theme="contrast"] { color: #ffffff; }
  • color: #242424 is a dark grey for the light theme. It also covers any value the page has no rule for.
  • body[data-theme="dark"], body[data-theme="contrast"] is one rule for two themes: the comma lists both.
  • color: #ffffff is white.

The whole page now:

A theme.html file in a code editor window: a style with a dark grey text color for the body and a white text color for body[data-theme="dark"] and body[data-theme="contrast"], the Teams theme paragraph, the TeamsJS 2.60.0 script tag, and a start function that initializes TeamsJS, reads context.app.theme, writes it to document.body.dataset.theme and shows it in the page.

wwwroot/theme.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Theme check</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; color: #242424; }
body[data-theme="dark"],
body[data-theme="contrast"] { color: #ffffff; }
</style>
</head>
<body>
<p>Teams theme: <strong id="theme">...</strong></p>

<script src="https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js"
integrity="sha384-oM1pbPWSxyAWgEVI9GzHDR2xzdMMehbAdsl9cH5emcTxX9Qr/kE4IS/Ld5IdcLmy"
crossorigin="anonymous"></script>
<script>
async function start() {
await microsoftTeams.app.initialize();

const context = await microsoftTeams.app.getContext();
document.body.dataset.theme = context.app.theme;
document.getElementById("theme").textContent = context.app.theme;
}

start();
</script>
</body>
</html>

If your app serves the page, restart it so it serves the new version, then open the dialog again. In the dark theme, the text is white:

The Theme check dialog in Teams' dark theme, now with the page's line Teams theme: dark in white, easy to read on the dark grey dialog.

Step 4: Check the High Contrast Theme​

Switch Teams to High contrast in the same menu, and open the dialog once more. The page read contrast, and the same rule made its text white on Teams' black dialog:

The Theme check dialog in Teams' high contrast theme: a black dialog with a white border, the title in white, and the page's line Teams theme: contrast in white.

Switch Teams back to the theme you use. In the light theme, the page reads default, neither rule matches, and the text is dark grey.

Common Mistakes​

MistakeWhat happensDo this instead
Styling the page for one theme onlyIts text is unreadable in the othersGive every theme a rule, or a default that fits
Forgetting contrastHigh contrast has a black dialog, and dark text disappears on itList contrast with dark
Checking only in the theme you useThe other themes are never seenOpen the page in each theme

Troubleshooting​

SymptomLikely causeFix
The page shows ... instead of a themeThe script stopped before it wrote the valueOpen the browser console and read the first error
Black text on a dark dialogThe page has no color for the dark themeAdd the body[data-theme="dark"] rule
The new colors don't showThe old page is still servedRestart the app that serves it

FAQ​

What if the theme changes while the page is open?​

This page reads the theme when it loads, and every time the dialog opened, it got the current one. Microsoft's docs also describe microsoftTeams.app.registerOnThemeChangeHandler(), which a page that stays open, such as a tab, can use to hear about a change.

Why does Teams' dialog color show through the page?​

The page sets no background color, and a page without one is transparent. Teams shows the page in a frame on top of the dialog, so the dialog's color, which follows the theme, shows through.

Can I read the theme without TeamsJS?​

A tab's address in the manifest can include the placeholder {app.theme}, which Teams fills in when it loads the tab, as Microsoft's Get context for your tab explains. This example uses TeamsJS, which works the same in a dialog.

Can a bot pass the theme to its dialog page?​

A bot gets a context.theme in the task/fetch request that opens the dialog, but in Teams on the web it said default even in Dark and High contrast. Read the theme in the page with TeamsJS instead, as Fixed: "theme": "default" in a Teams task/fetch Request While Teams Is in Dark Theme shows.

Conclusion​

To match Teams' theme, read context.app.theme with microsoftTeams.app.getContext() after app.initialize(), write it on the <body> as data-theme, and give dark and contrast their own colors in CSS. Then open the page in each theme to check it.

Additional Resources​