Building Reusable UI Components with HTML and CSS

Creating a consistent, polished user interface is an aspect of web development. However, crafting UI components that are both functional and visually appealing can be a time-consuming and tedious process.

In this post, we'll explore how to build reusable UI components using HTML and CSS. We'll cover best practices for structuring and styling these components, as well as techniques for making them flexible and adaptable to different contexts. By the end of the post, you'll have a solid understanding of how to design and implement UI components that can be easily reused across projects.

Structuring HTML for Reusability When building a UI component, it's important to think about how the component will be used and what its purpose is. This will help inform the structure of the HTML. For example, if the component is a button, the HTML might look something like this:

  • PGJ1dHRvbiBjbGFzcz0iYnV0dG9uIj5CdXR0b248L2J1dHRvbj4K

The class "button" can be used as a hook for styling.

It's also a good idea to use semantic HTML elements whenever possible. For example, instead of using a <div> for a button, use a <button> element. This not only improves accessibility but also provides more context for the component's purpose.

Styling Components with CSS When styling a UI component, it's important to use class and id selectors rather than element selectors. This will prevent the styles from unintentionally affecting other elements on the page.

For example, instead of using the following CSS:

  • YnV0dG9uIHsKICBiYWNrZ3JvdW5kLWNvbG9yOiBibHVlOwogIGNvbG9yOiB3aGl0ZTsKICBwYWRkaW5nOiAxMHB4OwogIGJvcmRlci1yYWRpdXM6IDVweDsKfQo=

Use this:

  • LmJ1dHRvbiB7CiAgYmFja2dyb3VuZC1jb2xvcjogYmx1ZTsKICBjb2xvcjogd2hpdGU7CiAgcGFkZGluZzogMTBweDsKICBib3JkZXItcmFkaXVzOiA1cHg7Cn0K

Making Components Flexible and Adaptable It's important to design UI components that can be easily adapted to different contexts. One way to achieve this is by using variables, such as CSS custom properties, in the styles. This allows the component to be easily customized without having to rewrite the CSS.

For example, instead of hard-coding the background color in the CSS, you can use a variable:

LmJ1dHRvbiB7CiAgLS1idXR0b24tYmFja2dyb3VuZC1jb2xvcjogYmx1ZTsKICBiYWNrZ3JvdW5kLWNvbG9yOiB2YXIoLS1idXR0b24tYmFja2dyb3VuZC1jb2xvcik7CiAgY29sb3I6IHdoaXRlOwogIHBhZGRpbmc6IDEwcHg7CiAgYm9yZGVyLXJhZGl1czogNXB4Owp9Cg==

Now, if you need to change the background color of the button, you can simply update the value of the variable

LmJ1dHRvbiB7CiAgLS1idXR0b24tYmFja2dyb3VuZC1jb2xvcjogcmVkOwp9Cg==

Another way to make components adaptable is by using utility classes. For example, if you have a button component that needs to be able to handle different sizes, you can create utility classes for small, medium, and large buttons:

LmJ1dHRvbiB7CiAgYmFja2dyb3VuZC1jb2xvcjogYmx1ZTsKICBjb2xvcjogd2hpdGU7CiAgcGFkZGluZzogMTBweDsKICBib3JkZXItcmFkaXVzOiA1cHg7Cn0KCi5idXR0b24tc21hbGwgewogIGZvbnQtc2l6ZTogMTJweDsKICBwYWRkaW5nOiA1cHg7Cn0KCi5idXR0b24tbWVkaXVtIHsKICBmb250LXNpemU6IDE2CjsKcGFkZGluZzogOHB4Owp9CgouYnV0dG9uLWxhcmdlIHsKZm9udC1zaXplOiAxOHB4OwpwYWRkaW5nOiAxMHB4Owp9

With these utility classes, you can easily apply the appropriate size to a button by applying the class. For example:

  • ```html

  • <button class="button button-medium">Button</button>

It's also good practice to make the base button class as minimal as possible and to only include styles that are shared among all variations like padding, border radius etc and use the utility class to make variations.

In this way you have created a reusable button component that can be adapted to different sizes without having to rewrite any CSS.

In this post, you've learned how to create reusable UI components using HTML and CSS. Remember to structure your HTML semantically and use class and id selectors in your CSS. Make your components adaptable by using variables, such as CSS custom properties, and utility classes.

It's also worth noting that you could use a CSS preprocessor like Sass or LESS which will allow you to use variables, mixins and nested styling rules that can make your CSS more organized and maintainable which will aid in creating reusable components.

By keeping a consistent approach when building your components, you will be able to create a library of reusable UI components that can be used across different projects, saving time, and increasing the consistency and quality of your UI.

Want us to build yours instead?

On a project, every milestone ends with a link to the working build, and the code, the accounts and every password are yours at the handover.

What we’d build for you is on Services.

Send us the brief, or start with the free call.

Page updated .

Privacy preferences

Choose whether this browser sends visit data to our analytics tools. The site works the same either way.

Google Analytics, Amplitude and Microsoft Clarity show us which pages get read. Off means none of the three loads.

Always on

Sentry error reports, with personal data removed, and the Cloudflare spam check on the brief form.

Each cookie is listed in our Cookie Policy.