Styling with CSS

What is the preferred method of CSS styling in the industry?

  1. Option 1: Create classes for each attribute (similar to bootstrap) and then attach the classes in HTML. For example:

    HTML:

    <p class="text-white text-bold"> John Doe </p>

    CSS:

    .text-white { 
        color: white; 
    }

    .text-bold{ 
        font-weight: bold;
    }

  2. Option 2: Add only one class per html tag. For example:

    HTML:

    <p class="description"> John Doe </p>

    CSS:

    .description { 
        color: white; 
        font-weight: bold; 
    }

Which approach is generally considered better?

Answer №1

In my experience, I typically opt for the first approach when it comes to styling. This is mainly because of the benefits it offers in terms of code reusability, allowing you to easily apply the same styles to multiple elements.

It's important to give your classes generic names whenever possible.

For example:

.hand { cursor:pointer; }
.padding-5 { padding:5px; }
.padding-left-5 { padding-left:5px; }
.border-1 { border:solid 1px #f00; } 
.corner-1 { corner-radius: 3px; }
.margin-auto { margin: auto; }
.bgcolor-1 { background-color: #b2e8ff; } /* light blue */
.bgcolor-2 { background-color: #FFF3CD; } /* light yellow */
.table-hover tr:hover { background-color: #eaf6ff !important} /* overide bootstrap's table hover */

Answer №2

Avoid using a single style in a class as it can lead to confusion. Instead, consider creating generic type group classes for different styles, such as .boxStyle1 and .fontStyle1.

.boxStyle1{
    border:solid 1px #333;
    color:#0f0;
}
.fontStyle1{
    font-family: Arial, ...;
    font-size:.9em;
}

By applying these group styles to various elements, like

<div class="boxStyle1 fontStyle1">
, you can easily maintain a consistent theme throughout your site without getting overwhelmed by individual style declarations. This approach simplifies the process of updating the look and feel of your website.

Answer №3

In my experience, I have adopted a similar approach to the second method mentioned. This involves specifying style class/id names in the HTML tag, such as

<p class="sample">...</p>
, and then detailing those styles in a separate CSS file:

.sample {
  font-family:Arial;
  font-variant:small-caps;
  color:#FF0;
  text-align:center;
}

One key advantage of this method is its efficiency. By consolidating all style details into one file, it only needs to be loaded once for site visitors, making it quicker to render each page they visit on your website. This not only enhances the user experience by reducing loading times but also streamlines the coding process for you as a developer. Once you establish the desired look for your website, you can create all element styles and reference them as needed, saving time during site creation. (Note: having more than one CSS file is still possible and valid.)

While there are instances where listing multiple class/id styles for an element may be necessary, separating each individual style into its own class/id can be excessive. Instead, consider grouping everything for your class="sample" under .sample in your CSS.

The adoption of this method by reputable organizations such as W3C, Mozilla, and edX underscores its status as the "industry standard":

W3C Starting with HTML + CSS
Mozilla's Introduction to CSS Layout
edX's CSS Courses

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

Using JavaScript import may encounter issues when trying to access the same file or a different file

When importing something and using it, there are certain scenarios where it may not work as expected. For example: <html> <body> <button onclick="foo()">Click Me</button> </body> <script type="module"> ...

Determination of the input parameters

Currently, I am developing an Angular application that includes a matInput field for user input. The issue I am encountering is that when the user enters a positive or negative value in the field (e.g. +5 or -5), the final output does not reflect the inten ...

designing various containers and adjusting their divisions

I have a pop-up window that contains the code snippet below, defining a template within a "container": <form method="post" class="signin" action="#"> <div id='container'> <div> <div id="divFeeTitle"&g ...

Having trouble integrating RGraph into my HTML page with AJAX

As I work on developing a simple webpage layout with both a static header and footer, I'm encountering some challenges in updating content dynamically. The left side menu triggers an ajax request when clicked, which should update the text on the right ...

The use of @font-face in CSS seems to be malfunctioning

I am having trouble changing the font in my CSS to what I want it to be. body{ background-color:#a8a7a7; color:#C50909; font-family: main, Calibri; text-align:center; } @font-face{ font-family: main; src: url('fonts/RegencieLight.ttf'); } ...

What is the best way to use JavaScript to conceal all div elements?

Can someone please provide a JavaScript solution to hide all divs on a web page without using jQuery? I am looking for a method that does not involve using the arrays that are associated with document.getElementByTag. If this is not possible, could you p ...

Encountered a problem while executing CSS in Shiny R

I am attempting to incorporate my css file into Shiny R. I have been able to run the application without any issues before adding the style.css file. Below are the steps I have taken: library(shiny) library(tidyverse) library(leaflet) library(leaflet.extra ...

Enhancing WordPress Menus with Icons in PHP

Is there a way to customize the HTML output of a menu created with wp_nav_menu()? I want to include < i > tags within the link < a > of each < li > item in the menu. I understand that I could achieve this using background-images in the C ...

How can an HTML form element identify which submit button has been clicked?

In my HTML form, I have included 2 buttons to meet the requirements of our customers. I am trying to determine how to identify which button was pressed on the backend. Below is a code snippet that demonstrates this: <form method="get"> < ...

Utilizing Piwik Analytics in jQuery Mobile Framework

Having an issue with tracking users on my mobile Web App using Piwik. Due to AJAX, only views on the first page are being tracked. I attempted to use the pageinit function to load the Piwik tracking script on every page, but it still only tracks the firs ...

Challenges arise with the height settings of ui-grid

Currently, I am facing an issue with using height: auto on an angularJS ui-grid. The problem arises from an inline style that specifies a fixed height on the div to which the ui-grid attribute is added. Additionally, the function getViewPortStyle() dynamic ...

Tips for Centering Text next to Logo on Mobile Display with Bootstrap 5

My website I've designed with Bootstrap 5 is facing responsiveness issues. While everything looks fine on desktop, the text and logo are misaligned in the mobile view. Here are screenshots demonstrating the problem: https://i.sstatic.net/jA5QTfFd.jp ...

Steps to remove a scrollbar from a fullscreen slider

Can anyone provide code examples on how to disable the scroll bar on a full screen slider? I want to have a scroll down button on each slider that, when clicked, will show the scrollbar only below the slider. I don't want the scrollbar to be visible ...

Apply a coating of white paint and add color to the border

I was struggling to create a form that has a white background color and a green border at the same time. I am using Bootstrap and it seems like I cannot apply both styles simultaneously. Here is the link to the form design I am trying to achieve: . Below ...

The JScolor Color Picker has a slight misalignment

I am having an issue with the jscolor color picker on my webpage. The rainbow part of it appears offset within the rest of the picker. You can see what it looks like on my site here: (https://ibb.co/9N8dHXs). On my website, I have a large canvas for three ...

When the audio on the device is in use, the video will not play and vice versa

Whenever my video starts playing, the audio from my device (such as iPod or Spotify) stops. If I try to play the audio manually while the video is playing, the video freezes. Interestingly, when I tested playing an audio file directly within the app, it wo ...

What is the best way to invoke an HTML file using a jQuery function in a JavaScript file?

When I call an HTML file from a jQuery function using require(), I am encountering an issue where I am unable to link the CSS with the HTML file. After running the file, only plain HTML is displayed without any styling. The structure of my HTML file is as ...

Implementing automatic activation of a tab upon page load using Angular

I am currently working with a set of Bootstrap nav pills in my navigation bar. The 'ng-init' code in my Angular script sets the 'dateState' to 'past' on page load. However, I have noticed that the 'Past Events' nav p ...

JavaScript validation function stopping after the first successful validation

Script: NewsletterValidation.js function formValidation() { var fname = document.getElementById('firstName').value; var lname = document.getElementById('lastName').value; var pnumber = document.getElementById('phoneNumb ...

Is there a way to properly direct to an internal page while utilizing [routerLink] and setting target="_blank" without triggering a full page reload?

I'm attempting to use [routerLink] along with target="_blank" to direct to an internal page without triggering a full app reload. Currently, the page opens in a new tab, but the whole application is refreshed. Here is the HTML: <a [routerLink]=" ...