53

Using the file nuxt.config.js file, head contents can be customized to add some meta, or other things:

module.exports = {
  /*
  ** Headers of the page
  */
  head: {
    title: 'awesome title',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: 'Nuxt.js project' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },
  ...
}

But I can't find anything in the documentation to set attributes on the html element -- I want to set the lang attribute. Is there a way to do that?

rocambille
  • 15,398
  • 12
  • 50
  • 68

3 Answers3

98

Source: Declaring language in HTML tag · Issue #388 · nuxt/nuxt.js

head supports a htmlAttrs property. It will map each key:value of the object as attribute:value

module.exports = {
  head: {
    htmlAttrs: {
      lang: 'en'
    }
  }
}

How to do this when using i18n: Setting the language attribute when using i18n and Nuxt?

yuriy636
  • 11,171
  • 5
  • 37
  • 42
17

In Nuxt 3 type in the component

<script setup>
useHead({
  htmlAttrs: {
    lang: 'en',
    style: 'font-size: 13px'
  }
})
</script>

https://v3.nuxtjs.org/getting-started/seo-meta/

Adarsh Madrecha
  • 6,364
  • 11
  • 69
  • 117
Daniel
  • 7,684
  • 7
  • 52
  • 76
  • Thanks! This isn't documented in https://v3.nuxtjs.org/getting-started/seo-meta/ – caiohamamura Nov 18 '22 at 16:21
  • It is mentioned, but now displying is bugged https://github.com/nuxt/framework/issues/9178 – Daniel Nov 18 '22 at 19:34
  • I see, they changed link to docs and now there are two different methods (in component and in nuxt config), in april there was no option to add it by globally like today. – Daniel Nov 18 '22 at 19:39
3

Add htmlAttrs to nuxt.config.js

export default defineNuxtConfig({
    app: {
        head: {
            htmlAttrs: {
                lang: 'en',
            },
            title: 'title',
            charset: 'utf-8',
            meta: [],
            link: [],
        }
    },
})
bilal
  • 176
  • 5