I want to remove HTML elements classes using CSS. What is the method of removing classes from HTML using CSS?
CSS cannot modify the classes applied in the DOM and is used only for visual styling.
To change classes of an element you have to use JavaScript
You can't. CSS is Cascading Style Sheets and is used only for styling, read more about CSS in this w3chools article.
For changing element's class you need to use Javascript, there are a bunch of methods to do this and most famous of them are pureJS DOM methods which you can learn here and of course amazing JQuery, both are not very hard and convenient but I prefer JQuery myself
You can't remove classes with CSS because that's for styling only and it can't modify the DOM. JavaScript hover, is able to do that. You could use this snippet to achieve that:
const classesToRemove = ['class1', 'class2', 'class3'];
const removeClassesFromElements = () => {
classesToRemove.forEach((className) => {
let elements = document.getElementsByClassName(className);
for (let element of elements) {
element.classList.remove(className);
}
});
};
document.addEventListener('DOMContentLoaded', (event) => {
removeClassesFromElements();
});
<h1 class="class1"></h1>
<p class="class2"></p>
<hr class="class3" />
CSS can't modify any HTML elements. But You can use pseudo classes for certain triggers.
Use :focus or :hover. Also you can use checked state of a hidden checkbox like
checkbox:checked + .btn {
color: #fff;
background-color: #6b15ce;
}
Related
I need help on how to make CSS styles final so that they could not be overwritten by Javascript.
Problem: Sometimes, because of the img tag in the JavaScript code, everything, (all styles of all of the images) gets overwritten.
Here is my code:
<img src='logoImg' style='width:45px; height:30px;' >
Please tell me ways to do this in JavaScript or in CSS itself.
Desired output: I need the Logo Image to have final styling. So it is not affected by JavaScript through the DOM (document object model).
You can try adding !important keyword to your CSS properties to prevent them from getting changed.
Example
const test = document.getElementById("test");
// The color won't change to orange
// due to !important keyword in the CSS code
test.style.backgroundColor = "orange";
#test {
width: 50px;
height: 50px;
background: green !important;
}
<div id="test"></div>
Ideally though it would be better to work around this using a different solution like using a more specific method of selecting an HTML element and changing it's properties.
If you have some kind of a style applied by selecting a tag, you can override it by using a class or an id when it comes to CSS. If you're searching for elements in JavaScript code, you can look for them by a class name or an id value instead.
An alternative solution in your case would be to re-write/modify the JavaScript code that targets your img tags and either skip images that have a specific class like let's say logo, or make a custom class for all your standard images that do not include your logo element and look them up in your JavaScript code using document.getElementsByClassName method.
If you want to prevent JS to overwrite the style of an image, you need to style it in CSS and add important; to raise it specificty weight. !important has a higher specificty weight then inline-style and as such JS style (JS add the style as inline-style) wont apply.
function resize() {
document.querySelector("img").style.height = "200px";
document.querySelector("img").style.width = "200px";
}
img.test {
width: 45px !important;
height: 30px !important;
}
<img src='https://via.placeholder.com/200x200.jpg' class="test">
<button onclick="resize()">Test</button>
This question already has answers here:
How can I override inline styles with external CSS?
(7 answers)
Closed last year.
When I inspect the html with IE Developer tools, I see that there is one inline style for a button:
I don't want any width property for this input element. How can I disable or overwrite this with empty width?
If you want to override inline styles then you need to add styles in your stylesheet with !important
for e.g.
width: auto !important;
Reference - CSS Specificity
You can disable inline styles for any given element using JavaScript:
Locate the button in the HTML document
Remove the button's style attribute
var button = document.getElementById('myButton');
button.removeAttribute('style');
Update for 2022+
If anyone else stumbles upon this in the present, you can do much more than remove an inline HTML style tag with JavaScript. You can keep the tag around in case you want to return it later. There are two ways you can do it!
One way: "Disabled" HTML property
I've heard tell that using disabled on the HTML tag will stop your browser from processing it. But I had trouble getting this to work on Firefox. Maybe someone can enlighten me about that!
<style class='style-class' disabled>
body { color: blue; }
</style>
So you'd just
$('.style-class').prop('disabled', true)
Another way: Changing the tag itself
Your browser will only parse styles within a style tag. So if you change the tag to anything else, you'll still be able to inspect it in the DOM just fine, but it won't treat it as a stylesheet.
(jQuery used for the explanation here)
$(style.selector).replaceWith (function () {
var attributes = $(this).prop("attributes");
var $newEl = $('<nostyle>')
$.each(attributes, function() {
$newEl.attr(this.name, this.value);
});
return $newEl.html($(this).html())
});
Then when you're ready to return the style, use this:
$(notstyle.selector).replaceWith (function () {
var attributes = $(this).prop("attributes");
var $newEl = $('<style>')
$.each(attributes, function() {
$newEl.attr(this.name, this.value);
});
return $newEl.html($(this).html())
});
In CSS you can do this:
nav:hover a {
But is there a way of changing nav when a is hovered?
Use the javascript event onHover
In jquery, it's something like that:
$("a").hover(function () {
$('#nav').css("color","red");
});
Coming Soon, to CSS
Explicit subjects in a selector are coming in CSS, but we'll have to wait just a bit longer. Soon you will be able to explicitly declare which element is the subject, for instance with your code:
$nav a:hover {
background: red;
}
This would change a nav's background to read when any of its anchor descendants are hovered.
Source: Selectors Level 4 » Determining the Subject of a Selector
Until this is implemented, you'll have to use JavaScript (or one of the tools built with it, such as jQuery, Mootools, etc) to accomplish a task like this.
Doing it with jQuery
You can accomplish this with jQuery, by adding and removing a class when any of the elements nested anchors are hovered or exited:
$("nav").on("mouseenter mouseleave", "a", function(e){
$(e.delegateTarget).toggleClass("hovered", e.type === "mouseenter" );
});
Demo: http://jsfiddle.net/jonathansampson/EPRRy/1/
This it the most compatible way
$("a").hover(function () {
$(this).parent().css({color:"red"});
});
No there isn't a way to ascend elements with CSS. To do explicitly what you described, it would require some JS.
#ssx had it close, but not quite, to do what your are asking with JS (and I'm going to simplify and use jQuery).
$("nav a:hover").hover(function() {
$(this).parent().css({'color': 'red'});
}), function() {
$(this).parent().css({'color': 'black'})
});
This gives changes the color to read, then back to black when the hover loses focus.
There is no solution for this in CSS 2 (dont know about CSS 3).
Javascript solution is easy and answered by other members.
You can try LESS. Using LESS you can do some conditional styling on DOM.
It will soon be intoduced in CSS 4. This is 5 or 6th question of the day i have seen today. I think it should be soon implemented by browser vendors.
I am trying to add an inline element to my page. It should behave exactly like span i.e. not introduce any default styling of it's own..I want to just be able to refer this element in my JS (using id).
Please help me suggest some style(less) elements like span.
Why not just add some extra css as mentioned by #James Allardice that removes old css and or applies new styling.
Html:
<span>My Super Cool Span</span>
<span id="MySpan>My Super Cool Span With An ID </span>
css:
span {
width:100px;
}
span #MySpan{
width: auto; // resetting width
// Reset css or apply new styling
}
By doing this only spans with id MySpan will use your new styling or have their styles reset.
Look at css attributes on www.w3schools.com to find out their defaults values.
Then you can refer to MySpan in javascript by doing:
document.getElementById('MySpan')
Or in jquery by doing:
$("#MySpan")
Use createElement() in JavaScript
<script>
var newElement = document.createElement('newElement');
newElement.setAttribute('id', 'newElementId');
var oldElement = document.getElementById('element');
oldElement.appendChild(newElement);
</script>
<div id="element"></div>
That should add a new element type, "newElement"
How can I set the default style for a checkbox in jquery or javascript or even html code?
or in another way how to disable the styling for checkbox.
there is a external css file that set the style for all checkboxes, but I would like to override the style to default style for specific checkboxes.
thanks
I use css' !important whenever I want to override some values.
But most modern browsers allow specific css selector like
input[type="checkbox"] {
//insert style here
}
you can use this to manipulate any style specific to checkboxes.
good luck.
This can be done easiest by controlling the CSS that's styling your check box to begin with.
Instead of the CSS on your page laying styles for all constants (body img input). Instead assign classes to the individual items if you want them styled a special way.
So dont use:
input { background: #000; }
Use:
<style>
.mystyle { background: #000; }
</stlye>
</head>
<body>
<input type="checkbox" class="mystyle">
Check to make sure all CSS on your site is clear of constants, this will make sure everything is set to default on all your pages and only styled at your choosing.
You can use JQuery to reset a css value ... like
$(this).css("color","red");
------------samples------------
$(document).ready(function() {
/* see if anything is previously checked and reflect that in the view*/
$(".checklist input:checked").parent().addClass("selected");
/* handle the user selections */
$(".checklist .checkbox-select").click(
function(event) {
event.preventDefault();
$(this).parent().addClass("selected");
$(this).parent().find(":checkbox").attr("checked","checked");
}
);
$(".checklist .checkbox-deselect").click(
function(event) {
event.preventDefault();
$(this).parent().removeClass("selected");
$(this).parent().find(":checkbox").removeAttr("checked");
}
);
});
});