How to use External CSS Frameworks In CSS

Integrating external CSS frameworks like Bootstrap or Foundation can expedite styling and provide a consistent UI across your application.

  1. Install Framework: Install the desired framework via npm or include its CDN link in your application.
  2. Follow Framework Guidelines: Refer to the documentation of the chosen framework for instructions on usage and customization.
  3. Apply Framework Classes: Utilize pre-defined classes provided by the framework to style your elements.

You can install Bootstrap using npm or yarn

npm install bootstrap

Alternatively, you can include the Bootstrap CDN link directly in your application layout

<link href=”https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css” rel=”stylesheet”>

Below example shows Bootstrap integration:

HTML
<!DOCTYPE html>
<html>
<head>
  <title>Rails App with Bootstrap</title>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container d-flex justify-content-center align-items-center min-vh-100">
      <button class="btn-success btn-lg">Welcome to GFG</button>
     
    </div>
  </body>
</html>

Output:

Output

How to Add CSS in Ruby on Rails?

This article focuses on discussing how to add Cascading Style Sheets (CSS) in Ruby on Rails.

Table of Content

  • Using SCSS
  • Using Plain CSS
  • Using Inline Styles
  • Using External CSS Frameworks
  • Comparison of Different Approaches
  • Additional Considerations
  • Conclusion

Similar Reads

Using SCSS

SCSS (Sass) is a preprocessor that extends CSS with features like variables, mixins, and nesting. These features make your stylesheets more maintainable, reusable, and easier to read....

Using Plain CSS

While SCSS is generally preferred for its advanced features, you can also use plain CSS files....

Using Inline Styles

Alternatively, you can also apply styles directly within your HTML views using inline styles....

Using External CSS Frameworks

Integrating external CSS frameworks like Bootstrap or Foundation can expedite styling and provide a consistent UI across your application....

Comparison of Different Approaches

Feature SCSS Plain CSS Inline Styles External CSS Frameworks Variables Yes (Improve maintainability) No No Yes (Framework-specific) Mixins Yes (Reusable code blocks) No No No Nesting Yes (Organize styles hierarchically) No No No Preprocessing Requires compilation step (Rails Asset Pipeline) No compilation needed No compilation needed No compilation needed Learning Curve Slightly steeper learning curve Easier to learn for beginners Easy to learn for beginners Varies based on framework...

Additional Considerations

Asset Pipeline: Rails uses the Asset Pipeline to manage and compile SCSS/CSS files. The Asset Pipeline concatenates and minifies CSS files for production, improving performance.Turbolinks: Use data: { turbolinks: false } in the stylesheet_link_tag to prevent Turbolinks from interfering with CSS reloading during development....

Conclusion

By considering the differences between SCSS, plain CSS, inline styles, and external CSS frameworks, along with the specific needs of your project, you can make an informed decision on which method to use for styling your Ruby on Rails application....