


Ruby on Rails Fast Frontend Using CSS-Zero as a Classless CSS Frameworks
Jan 19, 2025 am 10:05 AMThis article is very similar to the previous articles in this series, but this time we will use the Tailwind framework as a classless CSS framework. The article is inspired by the article "Classless CSS based on Tailwind".
Create a new Rails app
-
rails serve
Thetime
before the command is used to display the total time of command execution. The following example took 47 seconds.
$ rails -v Rails 8.0.0 $ time rails new classless-css-tailwind ... real 0m47.500s user 0m33.052s sys 0m4.249s
Rails 8, based on its "no build" philosophy, uses Propshaft as the resource pipeline library and Importmap as the JavaScript library by default. Importmap does not do anything with JavaScript.
Open the project using VSCode or your favorite editor
$ cd classless-css-tailwind && code .
Create some pages to preview the styles of HTML elements
The page is in the "Common Steps" section of the first article in the series.
Modify Tailwind file app/assets/stylesheets/application.tailwind.css
Expand…
Modify the above file to include a reference to the Tailwind CSS style file. Note that only option 1 is uncommented./* 在頂部插入自定義的 Tailwind CSS */ /* 如果“@tailwind base”、“@tailwind components”和“@tailwind utilities”沒(méi)有被注釋 */ /* 選項(xiàng) 1:綠色 */ @import "./custom_tailwind/custom1.css"; /* 選項(xiàng) 2:藍(lán)色 */ /* @import "./custom_tailwind/custom2.css"; */ /* 選項(xiàng) 3:來(lái)自文章“基于 Tailwind 的無(wú)類(lèi)名 CSS” */ /* http://ipnx.cn/link/9220e33481b237d9d5d19112688f6dd4 */ /* @import "./custom_tailwind/custom3.css"; */ /* @tailwind base; @tailwind components; @tailwind utilities; */
Create a app/assets/stylesheets/
folder under the custom_tailwind
directory to add custom Tailwind files.
Add content to the first custom Tailwind file custom1.css
Expand…
Create the file `app/assets/stylesheets/custom_tailwind/custom1.css` and copy the following content:/* 概述: 統(tǒng)一主題變量(我們只使用 --background、--text 和 --accent 等,而不是 --background-light 和 --background-dark)。 減少 @media (prefers-color-scheme: dark) 的重復(fù)。大部分深色主題都集中在 :root 中。 我們使用變量代替直接顏色,并在某些地方利用 Tailwind 的命名。 如果您使用類(lèi)(class="dark")而不是 prefers-color-scheme 來(lái)使用深色模式, 邏輯會(huì)略有不同(使用 dark:bg-*、dark:text-* 等)。 但是,根據(jù)建議,我們保留了 @media (prefers-color-scheme: dark) 以尊重用戶(hù)的偏好。 1. 統(tǒng)一的主題變量 現(xiàn)在我們使用 --background、--text 和 --accent(以及其他)代替 --background-light、--background-dark 等。 這減少了重復(fù),使代碼更易于維護(hù)。 2. 減少 @media (prefers-color-scheme: dark) 的重復(fù) 幾乎所有深色主題的內(nèi)容都集中在一個(gè)塊中,位于 :root 內(nèi)。 因此,每當(dāng)用戶(hù)偏好深色模式時(shí),所有變量都會(huì)被重新定義。 3. 使用補(bǔ)充變量 我們添加了 --background-code、--border、--form-border 和 --focus-ring,以確保所有可能根據(jù)主題變化的顏色都易于修改。 4. 優(yōu)化的表單樣式 我們統(tǒng)一了大多數(shù)表單元素,而不是將每種輸入類(lèi)型分成多個(gè)塊。 這避免了重復(fù),并保持了設(shè)計(jì)的一致性。 --- 最終說(shuō)明 如果您想進(jìn)一步遵循 Tailwind 的標(biāo)準(zhǔn),減少變量的使用,您可以使用標(biāo)準(zhǔn)的實(shí)用程序類(lèi) (bg-gray-50、text-gray-900、dark:bg-gray-800、dark:text-gray-100 等)。 對(duì)于那些更喜歡使用 .dark 類(lèi)來(lái)實(shí)現(xiàn)深色模式的用戶(hù),只需將 @media (prefers-color-scheme: dark) 替換為文件中的 .dark & { ... } 選擇器,并使用 JavaScript 或在 HTML 中手動(dòng)控制主題即可。 */
Add content to the second custom Tailwind file custom2.css
Expand…
Create the file `app/assets/stylesheets/custom_tailwind/custom2.css` and copy the following content:/* ================================================================= CSS 變量配置 集中定義項(xiàng)目的所有變量 ================================================================= */ :root { /* 顏色 - 淺色主題 */ --color-primary: #2563eb; /* Tailwind 的 blue-600 */ --color-primary-hover: #1d4ed8; /* Tailwind 的 blue-700 */ --color-background: #ffffff; --color-text: #1f2937; /* Tailwind 的 gray-800 */ --color-text-muted: #4b5563; /* Tailwind 的 gray-600 */ --color-border: #d1d5db; /* Tailwind 的 gray-300 */ --color-input-bg: #f9fafb; /* Tailwind 的 gray-50 */ --color-code-bg: #f3f4f6; /* Tailwind 的 gray-100 */ --color-code-text: #273e65; /* Tailwind 的 blue-800 */ /* 間距 */ --spacing-base: 1rem; --spacing-lg: 1.5rem; --spacing-xl: 2rem; /* 圓角 */ --radius-base: 0.375rem; --radius-lg: 0.5rem; /* 最大寬度 */ --max-width-content: 48rem; /* 768px */ } /* 使用 prefers-color-scheme 配置深色主題 */ @media (prefers-color-scheme: dark) { :root { /* 顏色 - 深色主題 */ --color-primary: #0284c7; /* Tailwind 的 sky-600 */ --color-primary-hover: #6990c7; /* Tailwind 的 blue-400 */ --color-background: #111827; /* Tailwind 的 gray-900 */ --color-text: #f3f4f6; /* Tailwind 的 gray-100 */ --color-text-muted: #9ca3af; /* Tailwind 的 gray-400 */ --color-border: #374151; /* Tailwind 的 gray-700 */ --color-input-bg: #1f2937; /* Tailwind 的 gray-800 */ --color-code-bg: #1f2937; /* Tailwind 的 gray-800 */ --color-code-text: #e8ecf6; /* Tailwind 的 blue-100 */ } } /* Tailwind 導(dǎo)入 */ @tailwind base; @tailwind components; @tailwind utilities; // ... (其余樣式代碼,與原文相同) ...
Add content to third custom Tailwind file custom3.css
Expand…
Create the file `app/assets/stylesheets/custom_tailwind/custom3.css` and copy the following content:// ... (其余樣式代碼,與原文相同) ...
Remove Tailwind class name from app/views/layouts/application.html.erb
file
Expand…
In the `application.html.erb` file, remove or comment out the `<!-- ... --> </main>
More steps to make custom Tailwind styles take effect
Expand…
If you followed the steps above, the `app/assets/stylesheets/application.tailwind.css` file should contain only one uncommented line `@import "./custom_tailwind/custom1.css";`.There should be only one style that is uncommented. To test other styles, first comment out the style you are currently using, then uncomment the other style you want to test.
After selecting an available custom style, execute the following command:
$ rails -v Rails 8.0.0 $ time rails new classless-css-tailwind ... real 0m47.500s user 0m33.052s sys 0m4.249s
If the above command cannot make the HTML element effective, please clear the previous file first, and then re-precompile:
$ cd classless-css-tailwind && code .
Now, a styled HTML?
After configuring Tailwind with the above customizations and starting the Rails server, you will see styled HTML.
Dark Mode
Some styles have dark mode options. To confirm, change the theme in your computer's color personalization settings. Search Windows for "enable dark mode for apps" and toggle between dark mode and light mode. After changing the operating system settings, the HTML page should automatically change to indicate that it supports light and dark modes.
Next Steps
[x] Organize styles according to your preferences; [x] Use CSS files in the project for styling, without using CDN; [x] Use Tailwind to copy the functionality of classless CSS frameworks; [-] Use Rails Live Reload to dynamically update project changes in the browser; [-] If you want to spend more time on the front end, check out the customization options for your favorite styles;
Reference materials
- http://ipnx.cn/link/9220e33481b237d9d5d19112688f6dd4
- http://ipnx.cn/link/cc0a521e0f695aa06ed11384fb616ac3
- http://ipnx.cn/link/dfae769c739093f5225cecaf4d5a612f
- http://ipnx.cn/link/930473a02d035f62b3c3c2628a284416
- http://ipnx.cn/link/c42c101f89ec57e54230d611f74d5ae1
- http://ipnx.cn/link/3f37c010783748f8e8577f732d74054c
- http://ipnx.cn/link/480167897cc43b2fb914238f45d7dbbf
- http://ipnx.cn/link/c48eb27d5b0a288f5bbf1545c218e001
The above is the detailed content of Ruby on Rails Fast Frontend Using CSS-Zero as a Classless CSS Frameworks. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

The choice of CSS units depends on design requirements and responsive requirements. 1.px is used for fixed size, suitable for precise control but lack of elasticity; 2.em is a relative unit, which is easily caused by the influence of the parent element, while rem is more stable based on the root element and is suitable for global scaling; 3.vw/vh is based on the viewport size, suitable for responsive design, but attention should be paid to the performance under extreme screens; 4. When choosing, it should be determined based on whether responsive adjustments, element hierarchy relationships and viewport dependence. Reasonable use can improve layout flexibility and maintenance.
