Should we avoid styling HTML tags?

Would it be considered poor practice, for instance, to apply the following CSS rules to all images on my webpage in order to make them responsive:

img {
  display: inline-block;
  height: auto;
  max-width: 100%;
}

Instead of using img-responsive on each image individually, or styling my h1 to h6 tags separately?

Answer №1

No, it is not considered a negative practice. Understanding the distinction between concepts is crucial in this context.

Typically, tags' styling serves the purpose of defining or resetting styles at a fundamental level for your website. This is commonly done to reset styles for cross-browser compatibility. Tools like Normalize.CSS are often used for this task.

When working with frameworks such as Bootstrap or Foundation, they come with their own set of styles. At this point, classes are usually utilized for better organization and reusability.

From here, you have the option to either adopt the framework's styling or create your own unique style.

It is still advisable to use classes for styling purposes. Consider the following HTML structure:

<div class="profile">
  <img class="avatar-url" src="..." />
  <p>John</p>
</div>

and

.profile p {
  font-size: 16px;
}

While this works fine when "name" is the only element within the profile block, if you need to add "title" in the future, you may encounter issues.

<div class="profile">
  <img class="avatar-url" src="..." />
  <span class="title">Mr. </span><span class="name">John</span>
</div>

This change could lead to problems such as:

  • Your CSS will be affected since there is no longer a <p> tag present.
  • You would need to adjust your CSS to accommodate the new structure which could cause issues in terms of scalability and control.
  • By forcing the name to use a <p> tag, you risk breaking your layout if properties defined for one tag need to be applied to another, especially concerning display attributes like inline or block.

In addition, using styles with classes enhances code readability. It is much clearer to read something like .list .item compared to .list div.

Regarding CSS specificity calculation, tags have the lowest priority. Therefore, to override a defined class like .btn, it is necessary to use .btn at minimum.

Answer №2

Utilizing classes is a beneficial strategy, especially when needing diverse styles for images across a website. For instance, you might require specific images to have a fixed width while others may need display: block and additional features like box-shadow.

Answer №3

It's absolutely acceptable to customize basic HTML elements. A common approach is using a CSS reset file or a set of rules specifically designed to reset all default styles. This is necessary because browser defaults can vary. Here's an effective workflow:

  1. Begin by resetting all browser defaults. There are many well-crafted CSS reset rule sets available online. I personally recommend the html5 doctor resets, a modern option for a CSS reset.

  2. Establish your own default styles for HTML elements on your website. These are the base styles that apply to all <img>, <h1> to <h6>, <a>, and so on. This helps maintain clean HTML without the need for excessive classes on every element.

  3. For specific elements requiring different styles than the default or sections needing complex design patterns beyond the default styles, utilize classes and IDs as necessary.

While there are various other methodologies that are equally valid, consider exploring popular CSS methodologies like OOCS, SMACSS, or BEM. These formalized conventions aim to enhance the organization and reusability of your CSS code.

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

Guide on displaying search results in separate boxes using HTML

I am struggling with organizing my search engine results in separate boxes on my website. Being new to HTML, I am unsure of how to achieve this layout. Currently, all the results are overlapping each other, and it's creating a messy display. Can anyon ...

Guide on showcasing jQuery variable values in PHP on a single page

I have some JavaScript and PHP code that I need help with. I want to assign a jQuery variable value to a PHP variable called $nicks, and then echo the value of $nicks on the same page. <script type="text/javascript"> var axel = 131.5678466; < ...

What is the best way to hide the jQuery modal I created?

Hello everyone! Currently, I am working on coding a simple JS modal that can be opened and closed smoothly. The issue I am facing is related to adding a click function to (.black-overlay) in order to fade out everything and close the modal. <div class ...

Django causing issues with displaying Bootstrap modal on website

I am having issues with implementing a Bootstrap modal in my Django template. I followed an example from the Bootstrap website to create a button that triggers a modal, but when I click the button, nothing happens. There are no errors displayed in the co ...

AngularJS is receiving an array of null inputs

I have a form where data from my DB is displayed through WebApi. One of the controls will contain an array of values. There is a scenario where the user can edit and save it using the PUT function. Controller : $scope.Put= function () { $scope.i ...

Switch between Fixed and Relative positions as you scroll

Just a small adjustment is needed to get it working... I need to toggle between fixed and relative positioning. JSFiddle Link $(window).on('scroll', function() { ($(window).scrollTop() > 50) ? ($('.me').addClass('fix ...

Link clicking causing webpage navigation issues

My web page features a table of contents, but unfortunately the page navigation is not functioning properly. Below is the HTML code: <a name="top_of_page">Create Account</a> <a href="#top_of_page">top</a> Even when I click on the ...

obtain the text content from HTML response in Node.js

In my current situation, I am facing a challenge in extracting the values from the given HTML text and storing them in separate variables. I have experimented with Cheerio library, but unfortunately, it did not yield the desired results. The provided HTML ...

The table refuses to load

I've been troubleshooting this issue for the past two days. The array is visible in the console, but it refuses to show up on the table. I've tried multiple approaches, but none seem to work. I suspect that "tobodyHtml" is not defined properly, a ...

Toggling between different divisions independently

I'm trying to create a navigation bar similar to Twitter. I want one specific div within the bar to scroll with notifications or news, while keeping the rest of the bar content unchanged. Any ideas on how to achieve this? Check out my code here. #nav ...

Executing Cascading Style Sheets (CSS) within JQuery/Javascript

I've been encountering a problem with my website. I have implemented grayscale filters on four different images using CSS by creating an .svg file. Now, I'm looking to disable the grayscale filter and show the original colors whenever a user clic ...

Tapping on content within SPAN elements with Selenium Webdriver in Java

Here is an example of what HTML code might look like: <td id="id26a" class="doclisting-name link" style="width: 319px; min-width: 319px;"> <span id="id26b" title="Document"> <span class="ie-fallback-marker"> sample text </span> ...

Learn the process of creating a unique item layout to suit your preferences

Exploring the use of div to create the desired layout but struggling to achieve my goal .skills-container { height:50px; /* aiming for around 50px */ padding:5px; } .skill-pic { width:48px; height:48px; } .skill-content { } .skill-content p { } .progres ...

Why aren't NavBar Image Links Functional, even though the code seems flawless? What could be the issue?

Looking at the current HTML code snippet I have within the <head> section, it includes: <ul class="nav"> <li> <a href="http://www.greg-holmes.com/#/title" target="_blank"> <img src="img/home.png"> ...

The PHP code fails to run and instead displays as raw PHP code on the webpage

As someone new to web development, I am diving into the world of PHP for the first time. So please excuse my ignorance as I navigate through this process. The task at hand involves creating a website with an HTML form that sends emails. After doing some r ...

Illuminate the rows in a table generated from a parsed text file using PHP

I am facing an issue with my PHP logic for selecting and highlighting rows in a table based on dropdown selection. I have a group of text files that are parsed into a table, and I need to highlight rows that match selections from 5 dropdowns created from t ...

What is the best way to retrieve the value of a div using AutoIt?

For this particular scenario, my objective is to extract a specific value from the last div element in the following list: <div id="past"> <div class="ball ball-8" data-rollid="242539">11</div> <div class="ball ball-8" data-ro ...

Achieving effective targeting of the second segment within a string enclosed in a span tag through increased specificity

When it comes to styling elements, the easiest way is to use classes and assign the properties needed. However, 1. I'm not a huge fan of creating classes all over the place. That's what specificity and CSS mapping are for. 2. Going thro ...

Instructions on how to add an ExternalLink to a webpage linking back to the previous page

I'm in the process of creating an error page for my app, and I'm trying to figure out how to add a Wicket ExternalLink that takes users back to the previous page. I believe I need to store the parameters or entire URL of the last visited page, b ...

Identify a specific HTML template using jQuery

I am currently working on implementing datepickers in various HTML templates. The issue I am facing is that the functionality only seems to work in my index.html file. When I try to replicate the same setup in another template, it does not function proper ...