Add styles to IMG elements only if there are no existing styles already applied

Feeling a bit embarrassed asking such a basic question, but I seem to be struggling with it.

I want to use CSS to style all IMG elements on my page that do not have any other class applied to them.

For instance, I want all images to have a red border by default, unless a different class is specified. It sounds simple enough, right? However, I'm running into issues where my other classes are being overridden by the base IMG class.

In this example, I aim to have a red border on the first image and a green border on the rest:

CSS:

<style>
  IMG {  
     /* This should apply to all images except those with another style */
     border: solid 12px red;
     width:50%;
   }

  IMG.nicepic {
     border: solid 12px green;
   }
</style>

EDIT: The problem was just a typo in my code. IMG:nicepic should have been written as IMG.nicepic (using "." instead of ":"). This entire question was based on a simple mistake in my CSS file. I tried to delete the question, but stackoverflow denied me permission to do so.

Answer №1

The dot (.) is used to indicate a class selector in CSS:

div.highlighted { /*not div:highlighted*/
   background-color: yellow;
}

Answer №2

Welcome! Here's a minor update:

CSS CODE

IMG {  
     /* This style will be applied to all images except those with other styles */
     border: solid 12px red;
     width:50%;
   }

  IMG.nicepic {/*replace (:) with (.)*/
     border:solid 12px green;
   }

Check out the Demo http://jsfiddle.net/JentiDabhi/ygco1ahf/

Answer №3

Have you ever considered making a unique class for the initial image instead of relying on the generic "img" class?

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

JavaScript function that reveals a block of text when clicked for multiple Div Class elements

I am attempting to create an Onclick event that will trigger when any of my div elements with the class "Clicker" is clicked. I am not proficient in JavaScript and need assistance on how to achieve this. I was able to make it work using document.getElement ...

What is the best way to utilize Django forms with a <select> value that is frequently used?

I am currently developing an application that enables users to interact with a database of information, including searching and saving entries. Within the search feature of the application, there are multiple fields where users should specify comparison o ...

Unable to use document.write when extracting code from the internet

I am currently developing a game using emulatorjs and ruffle. The goal is to have it all contained in a single downloadable file that can be run locally. I attempted to create another file for auto-updating purposes, but encountered issues with some of the ...

Why must I handle Access-Control-Allow-Origin issues with XMLHttpRequest() but not with forms?

When attempting to make a request using XMLHttpRequest(), I encounter the Access-Control-Allow-Origin error. However, when I use a form like the one below, no issues arise. Is there something about forms that allows access? What could be the reason for t ...

JavaScript Equivalent of jQuery's removeClass and addClass Functions

I am faced with the challenge of rewriting the following code without using jQuery: document.querySelector('.loading-overlay').classList.remove('hidden'); Can anyone provide guidance on how this can be achieved? ...

What could be the reason for the lack of a border below the navigation bar in this code?

I am attempting to create a navigation bar with a simple thin border that becomes thicker when hovered over. Although I have written the code below, it seems to not be working as intended. How can I fix this issue? #top-menu ul li a { display: block ...

Why does my HTML page keep triggering a redirect loop when embedded?

Trying to create an HTML document that auto-refreshes every 5 seconds while embedding a web page poses a challenge. Various attempts with embed tags, object tags, and iframe tags led to automatic redirection to the embedded page upon each refresh. The curr ...

"Maximizing battery life: Efficient video playback on iOS in low power

Summary: I am currently working on a website that features a video set as the background which autoplays. However, I have encountered an issue on iOS devices when "Low Power Mode" is activated - the video fails to play and instead displays a large "Play" i ...

javascript basic image carousel

How can I modify my slider so that "slide 1" appears after "slide 3" without needing to use the return or previous button each time? I'm not very well-versed in javascript, so I thought I'd reach out for some assistance ...

Toggling javascript functionality based on media queries

On one of my slides, I want to display images that are specific to different devices like iPhone, iPad, and desktop. I am looking for a way to filter these images based on the device. Using display:none won't work as it's JavaScript, but here is ...

Creating a collapsible accordion feature with jQuery using a specific HTML layout: wanna learn how?

I am looking to create an accordion effect with the structure provided below. The goal is to have the corresponding article toggle when clicking on .booklist>li>a, allowing only one article to be open at a time. Can someone assist me with writing thi ...

Scoped Styles rarely stand a chance against more dominant styles

I'm facing a scope issue while learning Vue. Question: I am trying to make sure that the styles in profile.vue do not override the ones in sidebar.vue. I want the sidebar to have a red background and the profile section to be blue. Shouldn't usi ...

What is the best way to align text with my CSS number and circle on the same line?

Here are two questions for you: First off, I've successfully created a circle with a number inside it using CSS. How can I go about filling the circle with color? Secondly, I'd like to know how I can align the words "Opportunity #1" on the same ...

Slide inside a div to move content

Check out the script I've been experimenting with for bringing in a menu from the left: Demo Fiddle To make this work, the sliding DIV needs to not only appear on the left but also push the 'left panel' and 'right panel' accordin ...

What is the best way to turn off autocorrect in a textarea on IE11 without turning off spellcheck?

In my experience, disabling the spellcheck attribute seems to solve the auto-correct issue, but it also eliminates the underlining of misspelled words. <textarea id="TextArea1" spellcheck="false"></textarea> I prefer to keep spellcheck enabl ...

1 in every 3 divs in jQuery, chosen randomly

Hey there! I have a fun project in mind - programming a "Rock Paper Scissors" game with the computer. The only issue is figuring out how to make the computer choose a random object after I pick one of the 3 options. Here's what I've come up with ...

Transferring content from a div class to an input class

I am seeking help to extract text from a div class and transfer it to an input class. Here is the code I have written: import os import time from selenium import webdriver from pyvirtualdisplay import Display from selenium.webdriver.common.by import By fr ...

Countdown Timer App using Flask

I'm currently working on a Flask-based game that involves countdown timers for each round. My goal is to have the timer decrease by 1 second every round without the need to reload the page. I've tried using time.sleep in my Python code to update ...

How can one effectively locate a specific element in an HTML document?

Using Beautiful Soup 4, I have successfully written code that scrapes online data from a webpage. My current challenge involves extracting data from a table, specifically targeting the 4th row. Is there a way to pass an argument to the .find() method to sk ...

What are some ways to enhance the opacity of a Material UI backdrop?

I'm looking to enhance the darkness of the material UI backdrop as its default appearance is not very dark. My goal is to make it dimmer and closer to black in color. ...