search
HomeWeb Front-endVue.jsHow to integrate CSS framework in Vue? Method introduction

The following Vue.js Tutorial column will introduce to you how to integrate the CSS framework in Vue.js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to integrate CSS framework in Vue? Method introduction

#CSS frameworks are great for many reasons: for example, the code is easier to understand, web applications are easier to maintain, and it simplifies the steps when implementing prototypes. Generally speaking, the method of integrating CSS frameworks in VUE is the same. This article takes the widely used Bootstrap 4 as an example.

Preparation

Before downloading the CSS framework, first create a new project with Vue CLI:

npm install vue-cli
vue init webpack project-name

Install and integrate Bootstrap 4

After creating and initializing the new Vue project, use npm to download Bootstrap 4. Since Bootstrap 4's JavaScript depends on jQuery, jQuery also needs to be installed.

npm install bootstrap jquery --save

You need to add the Bootstrap dependency in your Vue's main.js file so that it can be used globally throughout the entire program.

import './../node_modules/jquery/dist/jquery.min.js';
import './../node_modules/bootstrap/dist/css/bootstrap.min.css';
import './../node_modules/bootstrap/dist/js/bootstrap.min.js';

If your application fails to build, you should install the popper.js dependency. There should be no errors after that.

npm install --save popper.js

In this way, Bootstrap 4 is integrated into Vue.

Install and integrate Bulma

Bulma is a lightweight and flexible CSS framework based on Flexbox. It has more than 25K stars on GitHub.

Unlike Bootstrap, Bulma only contains CSS and has no dependencies on jQuery or JavaScript.

Installing Bulma:

npm install bulma

After downloading Bulma, open your main.js and import it.

/* main.js */
import './../node_modules/bulma/css/bulma.css';

In this way, Bulma is integrated into your Vue.js program.

Install and integrate Foundation

Foundation is an excellent CSS framework. It has two frames: one for email and one for website. What we need is the Foundation Sites framework, since we are only interested in using Foundation in the web.

Install Foundation Sites and import it into your main.js file:

$ npm install foundation-sites --save

Import it into your main.js In the document:

/* main.js */
import './../node_modules/foundation-sites/dist/css/foundation.min.css';
import './../node_modules/foundation-sites/dist/js/foundation.min.js';

Best Practices in Vue

The above three frameworks are very similar: they are all based on rows and columns. Creates a "grid" that you can use to create user interfaces. Grids make it easy to change the width of a column based on the device width simply by adding or changing the class attached to the element.

Note: The following example uses Bootstrap4. But this approach based on row and column frameworks applies to all CSS frameworks.

It is best to use framework classes whenever possible. For ease of use, these frameworks are carefully designed to be extendable and customizable. Instead of creating your own button with your own class, you can use Bootstrap, Bulma, or Foundation to do the same thing.

<!-- Bootstrap -->
<button class="btn btn-primary btn-lg">Bootstrap 大按钮</button>

<!-- Bulma -->
<button class="button is-primary is-large">Bulma 大按钮</button>

You can configure each color so that btn-primary (Bootstrap) or is-primary (Bulma) refer to the color of your own style instead of using Default colors shipped with Bootstrap and Bulma.

If you need to create your own theme with your own styles, you can create a global style sheet that overrides the framework's global styles; because we don't want to modify the framework directly.

Create your own style

If you want to create your own CSS theme, you need to create a new CSS file first and put it in in the assets directory, and then import it into the App.vue file.

/* App.vue */
import &#39;@/assets/styles.css&#39;;
...

Try to map some default styles that match your own to Bootstrap components:

/* styles.css */
/* Buttons
--------------------------- */
.btn-primary   { background: #00462e; color: #fff; } /* dark green */
.btn-secondary { background: #a1b11a; color: #fff; } /* light green */
.btn-tertiary  { background: #00b2e2; color: #fff; } /* blue */
.btn-cta       { background: #f7931d; color: #fff; } /* orange */

/* Forms
--------------------------- */
.form-control {
  border-radius: 2px;
  border: 1px solid #ccc;
}

.form-control:focus,
.form-control:active {
  outline: none;
  box-shadow: none;
  background: #ccc;
  border: 1px solid #000;
}

Pay attention to the reusability of components

When using the CSS framework in Vue.js, be sure to keep the reusability of components in mind. We cannot mix layout CSS with the component itself. Sometimes you may just need to reuse this component, and other times you may need a different layout.

Bad example

<!--Navigation.vue-->
<template>
  <p class="row">
    <p class="col">
      <nav>
        <ul>
          <li><a href="#">Navigation Item #1</a></li>
          <li><a href="#">Navigation Item #2</a></li>
          <li><a href="#">Navigation Item #3</a></li>
        </ul>
      </nav>
    </p>
  </p>
</template/>
<!--App.vue-->
<template>
  <p>
    ...
    <Navigation/>
  </p>
</template/>

This component may be used in both the header and footer. The two should look different, but they will contain the same information. Let's delete the layout HTML and move it to its parent or base component.

Good example

<!--Navigation.vue-->
<template>
  <nav>
    <ul>
      <li><a href="#">Navigation Item #1</a></li>
      <li><a href="#">Navigation Item #2</a></li>
      <li><a href="#">Navigation Item #3</a></li>
    </ul>
  </nav>
</template/>
<!--App.vue-->
<template>
  ...
  <p class="row">
    <p class="col">
      <Navigation/>
    </p>
  </p>
</template/>

Summary

CSS framework makes our development work easier . They make your template code consistent and easy to maintain and write. You can focus on the functionality and overall design of the program instead of wasting time on common tasks, such as creating buttons from scratch.

Bootstrap, Bulma and Foundation are just three commonly used frameworks, but they are not limited to these. There are also many frameworks for you to explore, such as Semantic UI and UI Kit, etc.

English original address: https://www.digitalocean.com/community/tutorials/vuejs-css-frameworks-vuejs

Author: Dave Berning

Related recommendations:

2020 front-end vue interview questions summary (with answers)

##vue tutorial recommendations: 2020 latest 5 A selection of vue.js video tutorials

For more programming-related knowledge, please visit:

Programming Courses! !

The above is the detailed content of How to integrate CSS framework in Vue? Method introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Vue.js's Function: Enhancing User Experience on the FrontendVue.js's Function: Enhancing User Experience on the FrontendApr 19, 2025 am 12:13 AM

Vue.js improves user experience through multiple functions: 1. Responsive system realizes real-time data feedback; 2. Component development improves code reusability; 3. VueRouter provides smooth navigation; 4. Dynamic data binding and transition animation enhance interaction effect; 5. Error processing mechanism ensures user feedback; 6. Performance optimization and best practices improve application performance.

Vue.js: Defining Its Role in Web DevelopmentVue.js: Defining Its Role in Web DevelopmentApr 18, 2025 am 12:07 AM

Vue.js' role in web development is to act as a progressive JavaScript framework that simplifies the development process and improves efficiency. 1) It enables developers to focus on business logic through responsive data binding and component development. 2) The working principle of Vue.js relies on responsive systems and virtual DOM to optimize performance. 3) In actual projects, it is common practice to use Vuex to manage global state and optimize data responsiveness.

Understanding Vue.js: Primarily a Frontend FrameworkUnderstanding Vue.js: Primarily a Frontend FrameworkApr 17, 2025 am 12:20 AM

Vue.js is a progressive JavaScript framework released by You Yuxi in 2014 to build a user interface. Its core advantages include: 1. Responsive data binding, automatic update view of data changes; 2. Component development, the UI can be split into independent and reusable components.

Netflix's Frontend: Examples and Applications of React (or Vue)Netflix's Frontend: Examples and Applications of React (or Vue)Apr 16, 2025 am 12:08 AM

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

The Frontend Landscape: How Netflix Approached its ChoicesThe Frontend Landscape: How Netflix Approached its ChoicesApr 15, 2025 am 12:13 AM

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.

React vs. Vue: Which Framework Does Netflix Use?React vs. Vue: Which Framework Does Netflix Use?Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

The Choice of Frameworks: What Drives Netflix's Decisions?The Choice of Frameworks: What Drives Netflix's Decisions?Apr 13, 2025 am 12:05 AM

Netflix mainly considers performance, scalability, development efficiency, ecosystem, technical debt and maintenance costs in framework selection. 1. Performance and scalability: Java and SpringBoot are selected to efficiently process massive data and high concurrent requests. 2. Development efficiency and ecosystem: Use React to improve front-end development efficiency and utilize its rich ecosystem. 3. Technical debt and maintenance costs: Choose Node.js to build microservices to reduce maintenance costs and technical debt.

React, Vue, and the Future of Netflix's FrontendReact, Vue, and the Future of Netflix's FrontendApr 12, 2025 am 12:12 AM

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.