In the code above, matcha.css classes like flash default, italic, and active were used for basic layouts and styling, and this is what it looks like:
\\\"How

Let’s add the to-do functionality and make it work:

document.addEventListener(\\\"DOMContentLoaded\\\", function () {  const todoInput = document.getElementById(\\\"todo-input\\\");  const addBtn = document.getElementById(\\\"add-btn\\\");  const todoList = document.getElementById(\\\"todo-list\\\");  addBtn.addEventListener(\\\"click\\\", function () {    const taskText = todoInput.value.trim();    if (taskText) {      addTask(taskText);      todoInput.value = \\\"\\\";    }  });  function addTask(taskText) {    const li = document.createElement(\\\"li\\\");    const taskSpan = document.createElement(\\\"span\\\");    taskSpan.textContent = taskText;    li.appendChild(taskSpan);    const completeBtn = document.createElement(\\\"button\\\");    completeBtn.textContent = \\\"✔\\\";    completeBtn.classList.add(\\\"success\\\");    li.appendChild(completeBtn);    const editBtn = document.createElement(\\\"button\\\");    editBtn.textContent = \\\"✎\\\";    editBtn.classList.add(\\\"edit-btn\\\");    li.appendChild(editBtn);    const deleteBtn = document.createElement(\\\"button\\\");    deleteBtn.type = \\\"reset\\\";    deleteBtn.textContent = \\\"✘\\\";    deleteBtn.classList.add(\\\"delete-btn\\\");    li.appendChild(deleteBtn);    completeBtn.addEventListener(\\\"click\\\", function () {      li.classList.toggle(\\\"strikethrough\\\");    });    editBtn.addEventListener(\\\"click\\\", function () {      const newTaskText = prompt(\\\"Edit your task:\\\", taskText);      if (newTaskText) {        taskSpan.textContent = newTaskText.trim();      }    });    deleteBtn.addEventListener(\\\"click\\\", function () {      li.remove();    });    todoList.appendChild(li);  }});

The code above is programmed to enable users to create, edit, complete, and delete tasks. When the add button is clicked, the addTask function creates the complete, edit, and delete buttons. This is where Matcha plays a unique role.

First off, adding matcha.css in the HTML file enables every future HTML element to be automatically styled by default. Later on, when we create the edit or delete button during a task, matcha.css will automatically implement the styling. If anything, we may just need to add its matcha.css type or class.

In normal circumstances, we create a class for these buttons. Let\\'s say we want line-through text decoration on any task we have completed. We would have to create the class in JavaScript and style it in CSS:

completeBtn.addEventListener(\\\"click\\\", function () {  li.classList.toggle(\\\"strikethrough\\\");});

With matcha.css, we do not need to go through the whole styling stress, as it gives us a strikethrough class for such an effect, as seen above.

We also used match.css for more styling as seen in the code below:

const completeBtn = document.createElement(\\\"button\\\");completeBtn.textContent = \\\"✔\\\";completeBtn.classList.add(\\\"success\\\");li.appendChild(completeBtn);

The success class added above gives the button a green background whenever we hover over it. The delete button wasn\\'t left alone in the styles:

const deleteBtn = document.createElement(\\\"button\\\");deleteBtn.type = \\\"reset\\\";deleteBtn.textContent = \\\"✘\\\";deleteBtn.classList.add(\\\"delete-btn\\\");li.appendChild(deleteBtn);

In the delete button, we added a matcha.css button type called reset. All it does is style the button border red, and when we hover over it, it turns red too. At the end of it all, this is what our to-do application looks like:
\\\"How

The ✔ button toggles a strikethrough effect on the task, indicating it has been completed. The ✎ button prompts the user to edit the task\\'s text, while the ✘ button removes the task entirely from the list.

We have been able to see matcha.css in a real-world project, and despite the application being small, it is very effective. But how does it perform when going head-to-head with a similar library?

How to combine matcha.css with other CSS libraries

If you want to know if combining matcha.css with other libraries is possible, the straight answer is yes, but there are conflicts. While matcha.css is mostly intended to be used along with semantic styling, the Matcha team advises that it is not a full-utility CSS framework and asks users to opt or consider using (or switching to) a utility-first CSS framework if you find the provided utility classes lacking or too limited.

When we attach Tailwind CSS’s script to an HTML file with Matcha originally in use, it overwrites some designs, like our projects after attaching this file:

It looks like this:
\\\"How

The right way to be in the mix with these CSS frameworks is to use them for what they are for — matcha.css is not a CSS out-of-the-box utility class framework. Rather, it applies custom styles to individual elements, and this is what it should be used for.

Compare that to Tailwind which is a CSS out-of-the-box utility class framework, and therefore it should be used to personalize your design. For example, you may not be a fan of Matcha\\'s input styling, but then you prefer with the button styles. In this case, Tailwind should be used for the inputs and matcha.css should be use for the buttons.

Here\\'s an example where Tailwind ruins some matcha.css styles: \\\"How

With a few Tailwind edits like this:

document.addEventListener(\\\"DOMContentLoaded\\\", function () {  const todoInput = document.getElementById(\\\"todo-input\\\");  const addBtn = document.getElementById(\\\"add-btn\\\");  const todoList = document.getElementById(\\\"todo-list\\\");  addBtn.addEventListener(\\\"click\\\", function () {    const taskText = todoInput.value.trim();    if (taskText) {      addTask(taskText);      todoInput.value = \\\"\\\";    }  });  function addTask(taskText) {    const li = document.createElement(\\\"li\\\");    // Add light green border and padding to list items    li.classList.add(      \\\"border\\\",      \\\"border-green-500\\\",      \\\"p-4\\\",      \\\"rounded-md\\\",      \\\"mb-3\\\",      \\\"shadow-sm\\\",      \\\"mt-5\\\"    );    const taskSpan = document.createElement(\\\"span\\\");    taskSpan.textContent = taskText;    li.appendChild(taskSpan);    // Button container with flex and spacing    const buttonContainer = document.createElement(\\\"div\\\");    buttonContainer.classList.add(\\\"flex\\\", \\\"space-x-3\\\", \\\"mt-2\\\");    // Complete button with softer contrast    const completeBtn = document.createElement(\\\"button\\\");    completeBtn.textContent = \\\"✔\\\";    completeBtn.classList.add(      \\\"bg-green-400\\\",      \\\"text-white\\\",      \\\"px-2\\\",      \\\"py-1\\\",      \\\"rounded-md\\\",      \\\"hover:bg-green-500\\\",      \\\"focus:outline-none\\\",      \\\"shadow-sm\\\"    );    buttonContainer.appendChild(completeBtn);    // Edit button with less aggressive yellow    const editBtn = document.createElement(\\\"button\\\");    editBtn.textContent = \\\"✎\\\";    editBtn.classList.add(      \\\"bg-yellow-300\\\",      \\\"text-gray-800\\\",      \\\"px-2\\\",      \\\"py-1\\\",      \\\"rounded-md\\\",      \\\"hover:bg-yellow-400\\\",      \\\"focus:outline-none\\\",      \\\"shadow-sm\\\"    );    buttonContainer.appendChild(editBtn);    // Delete button with a softer red    const deleteBtn = document.createElement(\\\"button\\\");    deleteBtn.type = \\\"reset\\\";    deleteBtn.textContent = \\\"✘\\\";    deleteBtn.classList.add(      \\\"bg-red-200\\\",      \\\"text-white\\\",      \\\"px-2\\\",      \\\"py-1\\\",      \\\"rounded-md\\\",      \\\"hover:bg-red-500\\\",      \\\"focus:outline-none\\\",      \\\"shadow-sm\\\"    );    buttonContainer.appendChild(deleteBtn);    li.appendChild(buttonContainer);    completeBtn.addEventListener(\\\"click\\\", function () {      li.classList.toggle(\\\"strikethrough\\\");    });    editBtn.addEventListener(\\\"click\\\", function () {      const newTaskText = prompt(\\\"Edit your task:\\\", taskText);      if (newTaskText) {        taskSpan.textContent = newTaskText.trim();      }    });    deleteBtn.addEventListener(\\\"click\\\", function () {      li.remove();    });    todoList.appendChild(li);  }});

Then we can have this:
\\\"How

Now we have seen how we can easily use matcha.css with Tailwind. Let\\'s look at how it performs against its peers.

Matcha CSS vs. Pico CSS

We can see how matcha.css performs against Pico CSS:

ComparisonMatcha CSSPico CSS
Semantic
Customizable
Open source
Theming✘ (Not inbuilt)
Classes for styling
Responsiveness
Available on CDN and NPM
LicenseMIT LicenseMIT License
Utility CSS frameworkLess support, as its original intent goes against it.

Conclusion

matcha.css holds its ground when it comes to styling HTML and is a solid option even after evaluation.

We\\'ve explored its use cases and how it can effectively fit into our project. It\\'s also important to note that matcha.css isn’t trying to compete as a full-utility CSS framework. If its built-in utilities feel too limited for your needs, the creators recommend considering a utility-first CSS framework as an alternative.


Is your frontend hogging your users\\' CPU?

As web frontends get increasingly complex, resource-greedy features demand more and more from the browser. If you’re interested in monitoring and tracking client-side CPU usage, memory usage, and more for all of your users in production, try LogRocket.

\\\"How

LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app, mobile app, or website. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.

Modernize how you debug web and mobile apps — start monitoring for free.

","image":"http://www.luping.net/uploads/20241114/1731594610673609723b127.png","datePublished":"2024-11-14T22:41:21+08:00","dateModified":"2024-11-14T22:41:21+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何使用 matcha.css 設定 HTML 樣式

如何使用 matcha.css 設定 HTML 樣式

發佈於2024-11-14
瀏覽:406

Written by Emmanuel Odioko✏️

Matcha, a famous green tea, is known for its stress-reducing benefits. I wouldn't claim that this tea necessarily inspired the library name I’ll be discussing with you, but the similar naming is a little coincidental as the library also saves us developers from some unnecessary stress.

So let’s shine a light on the benefits, concepts, and components of matcha.css to see how developers can introduce it to their projects.

If you want HTML elements to go from this:
How to style HTML with matcha.css

To this:
How to style HTML with matcha.css

Without having to write a line of CSS, then stick around.

A brief overview of matcha.css

matcha.css is a pure CSS library designed to style HTML elements similarly to a default browser stylesheet. This means it applies basic styling to elements, so instead of having just this:
How to style HTML with matcha.css

You can save yourself the stress of styling something so easy as a button by just linking Matcha’s stylesheet and having this:
How to style HTML with matcha.css

(Not to mention a bonus hover effect): How to style HTML with matcha.css

matcha.css doesn't take away your freedom to uniquely style anything. For example, I can simply add this line of code:

button{
    border-radius: 0;
    background-color: #fff;
}

And get this: How to style HTML with matcha.css

Now that we have seen the simplicity it brings, let's explore when exactly we should use this time-saving library.

Use cases

According to the library creators, the following are good times to use matcha.css:

  • Fast prototyping
  • Static HTML pages
  • Markdown-generated documents

Let’s move on to learn more about other matcha.css benefits:

Technical benefits

We know matcha.css saves time and effort, but how exactly does it do this?

Here’s a list of how the library works:

  • matcha.css helps developers streamline their workflow without having to dive into CSS intricacies
  • matcha.css has huge support for a full range of HTML elements, so there is a chance every element you will use in your project is already styled
  • The library has no build steps
  • It has no dependencies
  • It does not need JavaScript
  • It needs no extra configuration or refactoring
  • It is lightweight and only takes up 8.8kB of your project size; there are plans to reduce this size even further

Key features of matcha.css

In addition to matcha.css being open source under the MIT License, it has several other key features.

Responsiveness

Elements are styled to be responsive no matter the screen size.

Agnostic

The library works well with any type of document and covers more HTML elements than similar libraries. It stays out of the way of the existing structure of your HTML elements by using CSS pseudo-elements.

Reversible

You can start using it just by adding a simple tag, and it can easily be done away with at any time without having to change or clean up your document.

Semantic

The library styles elements based on how they are structured, offering helpful behaviors like showing submenus when

elements are nested or adding a required field indicator (*) when a
版本聲明 本文轉載於:https://dev.to/logrocket/how-to-style-html-with-matchacss-4ne7?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 在 Go 中使用 WebSocket 進行即時通信
    在 Go 中使用 WebSocket 進行即時通信
    构建需要实时更新的应用程序(例如聊天应用程序、实时通知或协作工具)需要一种比传统 HTTP 更快、更具交互性的通信方法。这就是 WebSockets 发挥作用的地方!今天,我们将探讨如何在 Go 中使用 WebSocket,以便您可以向应用程序添加实时功能。 在这篇文章中,我们将介绍: WebSoc...
    程式設計 發佈於2024-11-15
  • Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta:列偏移的刪除和恢復Bootstrap 4 在其Beta 1 版本中引入了重大更改柱子偏移了。然而,隨著 Beta 2 的後續發布,這些變化已經逆轉。 從 offset-md-* 到 ml-auto在 Bootstrap 4 Beta 1 中, offset-md-*...
    程式設計 發佈於2024-11-15
  • 大批
    大批
    方法是可以在物件上呼叫的 fns 數組是對象,因此它們在 JS 中也有方法。 slice(begin):將陣列的一部分提取到新數組中,而不改變原始數組。 let arr = ['a','b','c','d','e']; // Usecase: Extract till index ...
    程式設計 發佈於2024-11-15
  • 如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    在 PHP 中組合關聯數組在 PHP 中,將兩個關聯數組組合成一個數組是常見任務。考慮以下請求:問題描述:提供的代碼定義了兩個關聯數組,$array1和$array2。目標是建立一個新陣列 $array3,它合併兩個陣列中的所有鍵值對。 此外,提供的陣列具有唯一的 ID,而名稱可能重疊。要求是建構一...
    程式設計 發佈於2024-11-15
  • **如何在不停用索引的情況下最佳化 InnoDB 中的批次插入? **
    **如何在不停用索引的情況下最佳化 InnoDB 中的批次插入? **
    在InnoDB 中停用InnoDB 中最佳化批次插入的索引在嘗試停用InnoDB 表中的索引以增強批次插入效能時,您可能會遇到由於InnoDB 儲存引擎中缺少此功能而發出警告。以下是實現目標的替代策略:MySQL 參考建議:根據MySQL 文件的建議,考慮使用以下命令: SET 自動提交=0;SET...
    程式設計 發佈於2024-11-15
  • 如何使用命令式方法更新 React 中的巢狀狀態?
    如何使用命令式方法更新 React 中的巢狀狀態?
    更新嵌套狀態的命令式方法在 React 中,狀態更新是不可變的。這意味著要更新嵌套物件或數組,您不能簡單地修改其屬性並期望變更反映在 UI 中。相反,您需要建立包含更新值的新物件或數組,然後將其傳遞給 setState。 考慮以下範例,其中我們要更新物件中索引 1 處的物件的 name 屬性儲存在狀...
    程式設計 發佈於2024-11-14
  • 原子變數有哪些不同的記憶體排序模型?
    原子變數有哪些不同的記憶體排序模型?
    瞭解記憶體排序的意思原子變數提供安全的記憶體存取和跨執行緒同步。了解不同的記憶體順序對於有效利用它們至關重要。 輕鬆:無記憶體同步。 可能重新排序的最佳化操作讀取和寫入。 順序一致 (seq_cst):最嚴格的排序。 防止對周圍的任何記憶體操作進行重新排序原子操作。 確保所有執行緒的記憶體存取一致。...
    程式設計 發佈於2024-11-14
  • 為什麼在 For 迴圈中放錯 Return 語句會影響輸入迴圈?
    為什麼在 For 迴圈中放錯 Return 語句會影響輸入迴圈?
    For循環中返回語句錯位在你的作業中,你遇到了一個問題,程式只允許輸入一隻寵物,儘管瞄準三個。這個問題源自於 make_list 函數中 return 語句的定位。 在 for 迴圈中,return 語句在到達函數時立即終止函數的執行。在提供的程式碼中,return 語句放置在循環內部,導致函數在第...
    程式設計 發佈於2024-11-14
  • 如何防止使用者關閉 Bootstrap 模式?
    如何防止使用者關閉 Bootstrap 模式?
    停用使用者啟動的 Bootstrap 模態退出您可以透過點選 Bootstrap 模態區域外部來阻止使用者關閉 Bootstrap 模態。這在您想要強制使用者在繼續操作之前與特定模態內容互動的情況下非常有用。 停用模態背景點擊關閉預設情況下,使用者可以透過點選模式視窗之外的任何位置來關閉模式。若要停...
    程式設計 發佈於2024-11-14
  • 如何在 Python 中將巢狀清單匯出到 CSV 檔案?
    如何在 Python 中將巢狀清單匯出到 CSV 檔案?
    在Python中將嵌套列表匯出到CSV檔案將嵌套列表(其中每個內部列表包含不同類型的元素)寫入CSV檔案可以在Python 中處理資料時這是一項常見任務。以下是應對這項挑戰的方法:Python 的 csv 模組提供了處理 CSV 作業的便利方法。若要將清單的清單(例如a = [[1.2,'a...
    程式設計 發佈於2024-11-14
  • 如何有效率地提取Go Slice的最後一個元素?
    如何有效率地提取Go Slice的最後一個元素?
    Go 提取切片最後一個元素的最佳方法在 Go 中使用切片時,有效操作元素至關重要。一個常見的任務是提取最後一個元素,這可以透過多種方法來實現。 現有解決方案的缺點使用切片提供的解決方案[len(slice)-1 :][0] 看起來很麻煩並且涉及不必要的複雜性。它會傳回一個僅包含最後一個元素的切片,然...
    程式設計 發佈於2024-11-14
  • 為什麼我的 JavaScript 事件在動態元素追加後沒有被觸發?
    為什麼我的 JavaScript 事件在動態元素追加後沒有被觸發?
    動態元素追加後 JavaScript 事件未觸發您遇到了向 DOM 追加新元素後 JavaScript 事件未觸發的問題。這是因為 jQuery 僅識別頁面載入期間最初執行時存在的元素。 要解決此問題,您需要使用事件委託來擷取動態元素中的事件。事件委託涉及擷取頁面載入期間已經存在的 DOM 中較高層...
    程式設計 發佈於2024-11-14
  • `unshift()` 是在 JavaScript 中將元素新增到陣列的最有效方法嗎?
    `unshift()` 是在 JavaScript 中將元素新增到陣列的最有效方法嗎?
    JavaScript 中的最佳數組前置將元素前置到數組的開頭是 JavaScript 中的常見要求。在這裡,我們探索一種比問題中建議的傳統方法更好的方法。 Unshift 方法:本機解決方案JavaScript 提供了一個名為 unshift 的內建方法有效地將元素新增至陣列的開頭。與涉及建立新陣列...
    程式設計 發佈於2024-11-14
  • 在 JavaScript 中透過建構函數定義方法是否會建立重複的函數副本?
    在 JavaScript 中透過建構函數定義方法是否會建立重複的函數副本?
    JavaScript 中透過原型與建構函數定義方法的效能影響在JavaScript 中,存在兩種建立具有公用函數的「類”的方法:使用原型或建構函數。方法 1 透過建構函式將函數指派給實例,而方法 2 利用原型在所有實例之間共用函數。 雖然方法 2 通常被認為更有效率,但剝奪實例的私有實例變數是一個顯...
    程式設計 發佈於2024-11-14
  • 如何使用 matcha.css 設定 HTML 樣式
    如何使用 matcha.css 設定 HTML 樣式
    Written by Emmanuel Odioko✏️ Matcha, a famous green tea, is known for its stress-reducing benefits. I wouldn't claim that this tea necessarily inspire...
    程式設計 發佈於2024-11-14

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3