OO Design for CSS Style Elements

I'm curious about the most effective way to implement objective-oriented CSS for quick and clean reuse of components.


Scenario 1:

HTML:

<button class="button button-submit">Submit</button>

CSS:

.button {
  /* basic styles */
}
.button-submit {
  /* special styles for submit button */
}

Scenario 2:

HTML

<button class="button submit">Submit</button>

CSS

.button {
  /* basic styles */
}
.button.submit {
  /* special styles */
}

There are two drawbacks I can identify, one for each scenario.

In scenario 1, you may end up with excessively long class names.
In scenario 2, if you define .submit as its own element/component, you may encounter the issue of not being able to easily reuse code across different sections while maintaining consistency.

Answer №1

Perhaps Scenario 1 would be the more object-oriented approach, but when it comes to choosing technologies and methodologies, it's best to go with what suits your needs and what you enjoy working with.

Recently, I've been rebuilding an ecommerce platform from scratch and in my research, I was looking into adopting similar methods. However, I found myself steering away from options like your first example because of the cumbersome class names mentioned.

The main goal is to make coding easier, faster, and more reusable. If one of these factors is compromised in trying to achieve the others, then the whole purpose of implementing these methods becomes pointless.

Keep in mind that this response is purely subjective, as everyone has their own preferences.

If you're interested, here are some informative articles on methodologies and practices that helped me in making a decision:

I hope this information proves useful to you!

Answer №2

Although CSS is not inherently object-oriented, there are frameworks available that enable you to write CSS in a more efficient and DRY manner.

  • Stylus
  • PostCSS

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with HTML produced by a loop

I am encountering difficulties when trying to create a collapsible list of elements. You can view my code on JSFiddle This is what my code looks like: <div class="projectscontainer"> <span class="item destproject" title="ID: 384">Kaoweuza ...

Top Method for Reloading Element-Specific JavaScript When Replacing Elements

Is there a better way to re-initialize JavaScript without needing a page refresh? I'm currently struggling with appending an MDBootstrap <select> tag, which involves more than just adding a child element. Instead, I have to remove the element an ...

Adjusting webpage zoom based on different screen sizes

Hey there, I ran into an issue with my web page design. It looks great on my desktop monitors, but when I checked it on my laptop, things went haywire. Strangely, adjusting the zoom to 67% seemed to fix the problem. Both screens have a resolution of 1920 ...

Using the power of jQuery and Ajax to smoothly transition to a different page after editing the value of

Displaying a MySQL table named demo with the values Peter and John along with edit links Example Peter EDIT John EDIT When the edit link is clicked, it will open the page edit_name.php. In edit_name.php, you can update the clicked value using the updat ...

Improving an HTML list with JavaScript

My current project involves a JavaScript game similar to Scrabble. I want users to be able to create new words in the game, and have those words added to a list that is displayed on the page within a div element. However, I'm struggling to understand ...

Using HTML5 to play video from a stored binary string

I have been attempting to extract the data from a video file in binary format using the FileReader.readAsBinaryString(Blob|File) method, as demonstrated in this example http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files. My intention i ...

What is the best way to arrange 4 resizable divs in a horizontal layout?

I need to create a search feature with a specific layout: [Search Input (visible when search icon is clicked)] SearchIcon ClearIcon RefreshIcon All arranged in a horizontal layout. I have created a directive (called my-search) to combine the search inpu ...

Error in thread : TagNameUnexpectedException

I encountered an issue while trying to find a dropdown using Select: An error occurred: Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input" Even when attempting to use ...

The outcome of the Javascript Calculator is showing as "Undefined"

I've been attempting to create a calculator using JavaScript, but I'm facing an issue where the 'operate' function keeps returning Undefined, and I can't seem to figure out why. I suspect that the problem lies with the switch state ...

How can I make a child of Object3d in Three.js face the camera?

One challenge I am facing involves an Object3d composed of 6 planes arranged to form a cube. After applying quaternion rotation based on mouse input and bringing the cube to a stop, my goal is to have the cube align itself directly with the camera at its c ...

Using touch-action to enable movement from the last item to the first item in HTML/C

Currently, I am utilizing the touch-action property in my carousel which auto slides without any issues. However, I am facing a problem where when I scroll using touch-action, it stops at the last slide instead of looping back to the first slide. My goal i ...

Unable to Trigger Click Event on Div Element in Angular 9

Take a look at my HTML code snippet <mat-grid-list cols="3" rowHeight="150px"> <mat-grid-tile *ngFor="let tile of tiles;index as j;" [colspan]="tile.cols" [rowspan]="tile.rows" ...

Having difficulty animating the height transition of the NextJS Image component

On my webpage, I have a headerbar that changes size (from 120px to 70px) when the user scrolls down. I successfully implemented this transition using Tailwind classes and a height transition. Now, I am trying to make the site logo resize along with the hea ...

How can I position an element to the bottom right using CSS?

Is there a way to position this element to the bottom right corner? HTML <div class="info player"> <div id="job" style="display:none;"><span>Jobname</span></div> <div i ...

Displaying the contents of all files within a directory on a webpage by utilizing AJAX and JQuery

Scenario: Within my server, there exists a directory housing various log files. My objective is to exhibit all these files on a single webpage in a sequential list, with the most recent log positioned at the bottom. These logs are generated externally (ou ...

browsing through a timeline created using HTML and CSS

I am currently working on a web project that involves creating a timeline with rows of information, similar to a Gantt chart. Here are the key features I need: The ability for users to scroll horizontally through time. Vertical scrolling capability to na ...

The console is not displaying the data from the useForm

I am working on a project to create a Gmail clone. I have implemented the useForm library for data validation. However, when I input data into the form and try to print it to the console, nothing is being displayed. const onSubmit = (data) => { ...

Replicate the cursor to make it show up twice

Is there a way to create a duplicate cursor effect on my website, making it appear twice and perhaps look like it's drunk? I want the cursor to still interact with images and change accordingly, but always have two visible. Can this be achieved easily ...

Utilize jQuery to dynamically load and assign unique ids to elements within an array

I am seeking assistance with dynamically assigning unique IDs to elements in an array using JavaScript and jQuery. I am new to these languages and need some guidance. function assignIds() { var elementIds = ['name', 'lname', ' ...

Display the picture for a few moments, then conceal it. A button will appear to reveal the following image after a short period

For my project, I want to create a webpage for school where I display one image that disappears after 5 seconds, followed by a button. The user must click the button before the next image appears and stays for another 5 seconds. This sequence repeats each ...