Answer a question

I have a title:

<v-card-text style="font-size:5em">
    Some Heading Here
</v-card-text>

I would like to set font size to 3em when the view is XS. Right now I duplicated it as follows:

<v-card-text hidden-xs-only style="font-size:5em">
    Some Heading Here
</v-card-text>
<v-card-text visible-xs-only style="font-size:3em">
    Some Heading Here
</v-card-text>

However I would like to avoid this duplication and solve the issue with CSS alone, but without having to write my own @media queries in the local .vue file. Is that possible?

Alternatively, I'm ok with using predefined classes instead of specifying font size directly or even different elements completely, e.g. something like <h3> when it's XS but <h2> on other viewports.

Answers

I too have been trying to solve this riddle, as it feels gross to reach into javascript to handle simple style changes for different device sizes.

As it turns out, generating custom css rules for different breakpoints is quite easy because Vuetify is leveraging Stylus and assigning the breakpoints to a Stylus variable. Naturally, this variable is available in your custom vue components and style files (provided you have the proper pre-processor setup to compile stylus).

Here are some resources that helped me understand things better:

  1. Pre-processor setup:

    • https://vuetifyjs.com/en/framework/theme#setup-stylus-loader-with-webpack
    • In my case, by using Nuxt, the pre-processor setup for stylus was already set up for me. But for this approach to work, you'll need to be sure you can compile Stylus.
  2. Modifying Stylus Variables - Vuetify:

    • https://vuetifyjs.com/en/framework/theme#modifying-stylus-variables
    • On this page, you'll find a link to all the stylus variables that Vuetify is incorporating: https://github.com/vuetifyjs/vuetify/blob/master/packages/vuetify/src/stylus/settings/_variables.styl
  3. Stylus @media queries - http://stylus-lang.com/docs/media.html

As you'll see, the $display-breakpoints Stylus Object is your new best friend!

Boil it all down, and here's what you get in a Vue single file component:

<template>
  <v-layout column justify-center align-center>
    <v-flex xs12 sm8 md6>
      <v-card>
        <v-card-title class="custom-selector headline">
          Welcome to the Vuetify + Nuxt.js template
        </v-card-title>
      </v-card>
    </v-flex>
  </v-layout>
</template>

<script>
export default {
  // ...
}
</script>

<style lang="styl">
.custom-selector
  font-size 3em
  @media $display-breakpoints.xs-only
    font-size 2em
  @media $display-breakpoints.lg-and-up
    font-size 5em
</style>

Notice in the code above, we are changing the font-size of the <v-card-title> component by targeting it in our Stylus media queries and using the $display-breakpoints object to identify the desired breakpoint.

I think the benefit of not having a UI framework that generates every option at every breakpoint is a much smaller file to load. It seems like Vuetify+Stylus is a lighter approach that makes writing custom @media queries the simplest and most efficient approach.

Logo

Vue社区为您提供最前沿的新闻资讯和知识内容

更多推荐