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
浏览:519

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]删除
最新教程 更多>
  • 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
  • 在 Go 中使用 WebSocket 进行实时通信
    在 Go 中使用 WebSocket 进行实时通信
    构建需要实时更新的应用程序(例如聊天应用程序、实时通知或协作工具)需要一种比传统 HTTP 更快、更具交互性的通信方法。这就是 WebSockets 发挥作用的地方!今天,我们将探讨如何在 Go 中使用 WebSocket,以便您可以向应用程序添加实时功能。 在这篇文章中,我们将介绍: WebSoc...
    编程 发布于2024-11-15
  • 大批
    大批
    方法是可以在对象上调用的 fns 数组是对象,因此它们在 JS 中也有方法。 slice(begin):将数组的一部分提取到新数组中,而不改变原始数组。 let arr = ['a','b','c','d','e']; // Usecase: Extract till index p...
    编程 发布于2024-11-15
  • 如何在 PHP 中组合两个关联数组,同时保留唯一 ID 并处理重复名称?
    如何在 PHP 中组合两个关联数组,同时保留唯一 ID 并处理重复名称?
    在 PHP 中组合关联数组在 PHP 中,将两个关联数组组合成一个数组是一项常见任务。考虑以下请求:问题描述:提供的代码定义了两个关联数组,$array1和$array2。目标是创建一个新数组 $array3,它合并两个数组中的所有键值对。 此外,提供的数组具有唯一的 ID,而名称可能重合。要求是构...
    编程 发布于2024-11-15
  • 如何修复 macOS 上 Django 中的“配置不正确:加载 MySQLdb 模块时出错”?
    如何修复 macOS 上 Django 中的“配置不正确:加载 MySQLdb 模块时出错”?
    MySQL配置不正确:相对路径的问题在Django中运行python manage.py runserver时,可能会遇到以下错误:ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-...
    编程 发布于2024-11-15
  • 除了“if”语句之外:还有什么地方可以在不进行强制转换的情况下使用具有显式“bool”转换的类型?
    除了“if”语句之外:还有什么地方可以在不进行强制转换的情况下使用具有显式“bool”转换的类型?
    无需强制转换即可上下文转换为 bool您的类定义了对 bool 的显式转换,使您能够在条件语句中直接使用其实例“t”。然而,这种显式转换提出了一个问题:“t”在哪里可以在不进行强制转换的情况下用作 bool?上下文转换场景C 标准指定了四种值可以根据上下文转换为的主要场景bool:语句:if、whi...
    编程 发布于2024-11-15
  • 如何使用 MySQL 查找今天生日的用户?
    如何使用 MySQL 查找今天生日的用户?
    如何使用 MySQL 识别今天生日的用户使用 MySQL 确定今天是否是用户的生日涉及查找生日匹配的所有行今天的日期。这可以通过一个简单的 MySQL 查询来实现,该查询将存储为 UNIX 时间戳的生日与今天的日期进行比较。以下 SQL 查询将获取今天有生日的所有用户: FROM USERS ...
    编程 发布于2024-11-15
  • **如何在不禁用索引的情况下优化 InnoDB 中的批量插入?**
    **如何在不禁用索引的情况下优化 InnoDB 中的批量插入?**
    禁用 InnoDB 中优化批量插入的索引在尝试禁用 InnoDB 表中的索引以增强批量插入性能时,您可能会遇到由于 InnoDB 存储引擎中缺少此功能而发出警告。以下是实现目标的替代策略:MySQL 参考建议:根据 MySQL 文档的建议,考虑使用以下命令:SET 自动提交=0;SET unique...
    编程 发布于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,'...
    编程 发布于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

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3