2

I am new to Nuxt.

I have a page

pages/page1.vue

<template>
   <h1 class="title">Page</h1>
</template>
<style>
  .title {
     font-size: 16px;
  }
</style>

Then, under layouts I have an error.vue

layouts/error.vue

<template>
  <h1 class="title">Error Page</h1>
</template>
<style>
  .title {
    color: red;
    font-size: 18px;
  }
</style>

What I found is that when page1 is rendered, the title appears in Red. I checked the inspect elements and found that the CSS of error as well as of page 1 is applied.

I do not have a default.vue in the layouts directory.

As mentioned this is my first project in Nuxt (or vue) and want to understand how to ensure that CSS of a page are applied on that page only. This is in development mode (npm run dev). Thanks

1 Answer 1

1

The issue is CSS without scoped attribute renders at application level. You should use scoped attribute in styles tag as

layouts/error.vue

<template>
  <h1 class="title">Error Page</h1>
</template>
<style scoped>
  .title {
    color: red;
    font-size: 18px;
  }
</style>

**When a <style> tag has the scoped attribute, its CSS will apply to elements of the current component only otherwise consider global style.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops. This is indeed embarrasing. The auto suggest even puts it and I removed it. :-). Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.