Styling Components
When using Vue components, the webpack compiler allows you to use almost any frontend templating language style you prefer. For example, there are several ways to compose CSS, either directly or with pre-processing. The easiest way to enable these expressive languages in your Vue templates is to install them when you set up your project ahead of time using the Vue CLI.
When using the style
tag inside of a Vue component, you have the option to specify a language, provided you have installed the appropriate webpack loader. In Exercise 1.01
, if you chose to install the SCSS preprocessor, you can add the lang="scss"
attribute to the style
tag to begin using SCSS.
For example, if you chose to install the Stylus preprocessor, you can add the lang="stylus"
attribute to the style
tag to begin using Stylus:
<style lang="stylus"> ul   color: #2c3e50;   > h2   color: #22cc33; </style>
Vue scoping is a handy way to stop individual components from inheriting styles from the virtual DOM head. Add the scoped attribute to your style
tag and write some component-specific styles that will override any other CSS rules from the global sheet. The general rule is to not scope global styles. A common method for defining global styling is to separate these styles into another style sheet and import them into your App.vue
.
Exercise 1.03: Importing SCSS into a Scoped Component
In this exercise, we will be utilizing the style
tag to add SCSS preprocessed styles to a component and importing external stylesheets.
To access the code files for this exercise, refer to https://packt.live/3nBBZyl.
- Open a command-line terminal and navigate into the
Exercise1.03
folder and run the following commands in order:> cd Exercise1.03/ > code . > yarn > yarn serve
Go to
https://localhost:8080
. - Inside of the exercise file, let's write some HTML that can be styled using SCSS. Let's keep practicing the interpolation method:
// src/components/Exercise1-03.vue <template> Â Â <div> Â Â Â Â <h1>{{ title }}</h1> Â Â Â Â <h2>{{ subtitle }}</h2> Â Â Â Â <ul> Â Â Â Â Â Â <li>{{ items[0] }}</li> Â Â Â Â Â Â <li>{{ items[1] }}</li> Â Â Â Â Â Â <li>{{ items[2] }}</li> Â Â Â Â </ul> Â Â </div> </template> <script> export default { Â Â data() { Â Â Â Â return { Â Â Â Â Â Â title: 'My list component!', Â Â Â Â Â Â subtitle: 'Vue JS basics', Â Â Â Â Â Â items: ['Item 1', 'Item 2', 'Item 3'] Â Â Â Â } Â Â }, } </script>
- Add the
lang
property to thestyle
tag and add the valuescss
to enable SCSS syntax inside thestyle
block:<style lang="scss"></style>
- Create a folder inside the
src/
directory calledstyles
. Inside this new folder create a file calledtypography.scss
:src/styles/typography.scss
- Inside
typography.scss
, add some styling for the template you composed in your component:/* typography.scss */ $color-green: #4fc08d; $color-grey: #2c3e50; $color-blue: #003366; h1 { Â Â margin-top: 60px; Â Â text-align: center; Â Â color: $color-grey; Â Â + h2 { Â Â Â Â text-align: center; Â Â Â Â color: $color-green; Â Â } } ul { Â Â display: block; Â Â margin: 0 auto; Â Â max-width: 400px; Â Â padding: 30px; Â Â border: 1px solid rgba(0,0,0,0.25); Â Â > li { Â Â Â Â color: $color-grey; Â Â Â Â margin-bottom: 4px; Â Â } }
Note
In SCSS, you can use standard CSS selectors to select elements in your component.
ul > li
will select every<li>
element inside of a<ul>
element for styling. Similarly, using the addition symbol+
means the elements placed after the first element will be styled if they match the condition. For example,h1 + h2
will dictate that allH2
elements afterH1
will be styled in a way, butH3
will not. You can understand this better through the following example.In CSS, you would present this code as follows:
h1 + h2 { Â Â Â /* Add styling */ } ul > li { Â Â Â /* Add styling */ }
In SCSS, the same code can be represented as follows:
h1 {    + h2 {       // Add styling    } } ul {    > li {       // Add styling    } }
- In your component, import these styles by using the SCSS
@import
method:<style lang="scss"> @import '../styles/typography'; </style>
This will generate an output as follows:
- Add the
scoped
attribute to your<style>
tag to only apply these styles to this component instance. Use the variable from the imported stylesheet$color-blue
:<style lang="scss" scoped> @import '../styles/typography'; h1 { Â Â font-size: 50px; Â Â color: $color-blue; // Use variables from imported stylesheets } </style>
The output of the preceding code is as follows:
Inspect the DOM and you will notice that at run-time, that scoping has applied
v-data-*
attributes to your DOM elements specifying these specific rules. Ourtypography.scss
, which we are scoping to our component, references an HTML tag that does not live within the scope of our component. When Vue adds data attributes to the scoped component, it generates the style if the<body>
tag exists within the component. In our case, it does not.The
Elements
tab of your browser dev tools will show the following after expanding the<head>
and<style>
tags: - Create a new style sheet called
global.scss
in thestyles
folder:/* /src/styles/global.scss */ body { Â Â Â Â font-family: 'Avenir', Helvetica, Arial, sans-serif; Â Â Â Â margin: 0; }
- Import this stylesheet into your
App.vue
:<style lang="scss"> @import './styles/global'; </style>
Our app should now be back to normal, with a mixture of globally defined styling and properly scoped styles for this component, as follows:
In this exercise, we interpolated data that originated from an array, then styled our component using forms of scoped SCSS, which can both exist inside the <style>
tag or be imported from another directory in our project.