Is it better to overwrite past values in CSS or to duplicate rules?

Which one of these CSS code practices is considered better?

A - concise and rewritten:

@media only screen and (min-width: 480px) and (max-width: 960px) {
  h1, h2, .header, .widget, .copyright {
    position: static;
    width: 100%;
    height: auto;
    /* additional styles may be added here */
  }
  .copyright {position: relative;}
}

B - repetitive:

@media only screen and (min-width: 480px) and (max-width: 960px) {
  h1, h2, .header, .widget {
    /* CLASS .copyright is missing to prevent redundancy */
    position: static;
    width: 100%;
    height: auto;
    /* additional styles may be added here */
  }
  .copyright {position: relative; width: 100%; height: auto; /* more of the SAME styles could be added */}
}

Could rewriting the code have any negative implications (as shown in Exhibit A)? Could it lead to slower rendering or compatibility issues with older browsers? Despite being an easier approach, something about it doesn't feel quite right.

Answer №1

While speed may pose a slight concern, the primary focus should be on ensuring the readability of your code. It is important to prioritize making edits that are easily transferable from A to B for future modifications by yourself or others.

Answer №2

If I had to choose, I'd lean towards option A. However, my decision may vary depending on the context and practicality.

In terms of performance, the slight distinction between the two options is minimal. What truly matters is choosing the option that aligns with logic. For instance, if you later find yourself needing to adjust from 100% width to 95%, opting for the initial choice will prove advantageous.

Answer №3

Isn't it curious that in scenario A) you are specifically defining the position attribute for .copyright, only to promptly override it. While it may seem like a minor detail, it would be more elegant to instead:

h1, h2, .header, .widget, .copyright {
  width: 100%;
  height: auto;
}
h1, h2, .header, .widget {
  position: static;
}
.copyright {
  position: relative;
}

Alternatively, you could think about consolidating the width: 100%; height: auto; aspect into a distinct class with a meaningful name such as full-width. This way, you prevent contaminating h1 universally. Here's how you would implement it:

.full-width {
  width: 100%;
  height: auto;
}
h1, h2, .header, .widget {
  position: static;
}
.copyright {
  position: relative;
}

and then

<h1 class="full-width">My Header<h1>
<div class="copyright full-width">(c) 2013 Joudicek</div>

As pointed out by others, performance should not be your primary concern at this stage. Focus on getting the foundation right first, and later on, if necessary, you can conduct CSS performance analysis and make any needed adjustments.

Answer №4

I suggest opting for the initial option (A) instead of the subsequent one (B) as a best practice in coding.

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

Attempting to incorporate a new CSS sub menu alongside the current one

Having trouble with menu styling, I'm new to this and trying to add a sub menu to an existing drop down menu. Check out my JSFiddle This is the CSS code: .menu_main { float: left; width: 60%; } #access .menu { list-style: none; font-weight: normal ...

Utilize Bootstrap 4 to seamlessly switch between nav-pills for larger screens and navbar-nav for smaller screens

On larger screens, I prefer the appearance of the nav pills. However, once the collapse button appears, I would like the menu to stack and resemble the image provided instead of the pills. Do I require custom CSS to achieve this? <div id="menu" class=" ...

Issue with jQuery select box (roblaplaca) and its custom scroll bar functionality not functioning as expected

Recently, I implemented the custom select box script from . This script allows for two select boxes, where the second one updates dynamically using AJAX when there is a change in the first select box. Below is a sample of the HTML code: <option>One& ...

Month and year selection feature in ExtJS 4 catalog

I recently came across an interesting Fiddle that featured a Month and Year picker for apps. After finding this Fiddle here, I noticed that it was built using Extjs 5.0, and it worked perfectly. However, when attempting to switch it to version 4.2.1, the l ...

Is it possible to deactivate an anchor tag based on the result of a conditional statement that returns a string?

I've created an anchor tag (not a button) with text that redirects me to another page <StyledTableCell align="center"> <Link href={`/races/results/${race.id}`}>{race.race_name}</Link> </StyledTableCell> There is a ...

ReactJs: difficulty in resetting input field to empty string

I have an application using React v 0.13.1 (Old version). I am struggling to update my input field to "" after retrieving the updated value from the database. Scenario: I am updating the input fields by clicking on the button named "Pull&qu ...

Tips for aligning Picture boxes using CSS

As a newbie, I recently received training in HTML, CSS, and a bit of JavaScript. Currently, I am involved in a project where I display the latest news from an API. The technical aspects of fetching data from the API using JavaScript are handled by a senior ...

What is the best way to use CSS to ensure that dynamic, data-driven characters can be properly displayed within a div

I'm in the process of updating a data-centric website that relies on information from an automated database engine. In the HTML, there's a fixed-size button containing text pulled from the database. I'm looking to incorporate some CSS styles ...

What is the best way to adjust the width of a div based on the remaining space in the viewport?

I've created a Wireframe to showcase the concept I'm aiming for. There are 2 screens included to illustrate how the layout should adapt to different screen sizes. Check out the Wireframe here The goal is to have a center-aligned container named ...

When the cursor hovers over an item, I would like to enlarge its width while simultaneously shrinking the width of two additional items

Here is a section that I have created, you can view the mockup here. The section is divided into 3 parts, each taking up around 33.3% of the width and floated. My goal is to make it so that when a specific element, like .col-one in my example, is hovered ...

Aligning the Twitter and Facebook buttons

Issue with Social Media Button Alignment I'm encountering a problem with aligning Twitter and Facebook buttons on my website. The alignment seems to be off and I need some help fixing it. Code Snippet html: <span id="social"> ...

Concealing inactive select choices on Internet Explorer versions greater than 11 with the help of AngularJS

I am having trouble hiding the disabled options in AngularJS specifically for IE. Check out this fiddle for a better idea: https://jsfiddle.net/s723gqo1/1/ While I have CSS code that works for hiding disabled options in Chrome/Firefox, it doesn't see ...

The element within the flexbox does not extend across the entire width, despite being designated as 100% full width

I'm currently attempting to center certain elements to ensure they are visually aligned. The upper div seems to have some odd space on the right, causing all the content within that div to be shifted and not accurately centered. On the other hand, th ...

What is the best way to arrange a bootstrap .container?

I've been struggling to position a content DIV on a bootstrap carousel, but so far I haven't been successful in figuring it out. Below is a screenshot of the current output from my code, which is not what I am aiming for. What I'm trying t ...

Activate SVG graphics upon entering the window (scroll)

Can anyone assist me with a challenging issue? I have numerous SVG graphics on certain pages of my website that start playing when the page loads. However, since many of them are located below the fold, I would like them to only begin playing (and play onc ...

How can I make my navbar stay fixed in place and also activate the transform scale functionality?

After fixing the position of my navbar with the class "top," I noticed that the transform property scale does not work on the div element I applied. The hover effect on the box only works when I remove the position from the navbar. This is the HTML code: ...

Tips for incorporating Action Links into your CSS workflow

<li class="rtsLI" id="Summary"><a href="javascript:void(0);" onclick="javascript:rtsXXX.OnClientTabSelected(this‌​, 0);" class="rtsLink"><span class="rtsTxt">Test</span></a></li> I have made a change by replacing th ...

What could be causing the issue with absolute positioning not functioning correctly?

I'm having trouble keeping my footer at the bottom of the page: body, html{position:relative;} footer{position:absolute;} Even when I use bottom: 0;, the footer doesn't seem to go all the way to the bottom. Is there anyone who can help me troub ...

Instructions on displaying the filename as the input for width and height dimensions

Running into an issue with Bootstrap 4 - whenever I try to upload a file with a long name, it ends up overflowing beyond the input field. ...

Prevent scrolling while popup is open

I found a helpful tutorial online for creating a jQuery popup (). However, due to my limited understanding of jQuery, I'm having trouble getting it to meet my needs. The issues I'm facing are: I need to prevent the webpage from scrolling when ...