How to change the style of elements with same class name
document.getElementsByClassName
: returns a set of elements which have all the given class names.
You may have multiple elements with this class name. so you need to provide index like
document.getElementsByClassName('s')[0].style.display='block';
Inorder to apply for style for all elements with same class name:
var ele = document.getElementsByClassName('s');
for (var i = 0; i < ele.length; i++ ) {
ele[i].style.display = "block";
}
Change the style of multiple elements with the same class
var el = document.querySelectorAll('#site-nav__link-id');
for (var i = 0; i < el.length; i++) {
var currentEl = el[i];
currentEl.style.color = 'white';
}
You need to use querySelectorAll for getting multiple elements with same id.
How to style nested divs with same class name?
The trick is that you cannot style the container, but you need to style the folders within them. Otherwise you will put background on background, and this way you can only make them more opague. With
the way I wrote it down here you can make them more transparent.
.Folder-container > .folder
{
background-color: rgba(0,0,0,1);
}
.Folder-container > .Folder-container > .folder
{
background-color: rgba(0,0,0,.8);
}
.Folder-container > .Folder-container > .Folder-container > .folder
{
background-color: rgba(255,255,255,.6);
}
.Folder-container > .Folder-container > .Folder-container > .Folder_Container > .folder
{
background-color: rgba(255,255,255,.4);
}
Changing CSS of elements of the same class onmouseover, depending on each element's id
The easiest way would be to avoid using an id
, and instead use a CSS custom-property, for example --hoverColor
:
/*
The default styling for the element(s):
*/
.p-class {
color: #000;
background-color: silver;
}
/*
The styles for the :hover pseudo-class/interaction:
*/
.p-class:hover {
/* the var() retrieves the named custom-property from
the closest ancestor element upon which it's defined.
Here this is specified in the inline 'style'
attribute for the .p-class elements: */
color: var(--hoverColor);
}
<p class="p-class" style="--hoverColor: #000">Hi</p>
<p class="p-class" style="--hoverColor: #FFF">Hi</p>
<p class="p-class" style="--hoverColor: #FFFF66">Hi</p>
<p class="p-class" style="--hoverColor: #498F83">Hi</p>
Changing multiple elements' styles with a same class name at once with JavaScript?
The DOM methods that getElementsBySomething
(plural), as opposed to ones which getElementBySomething
return a NodeList, not an element. NodeLists are Array-like objects.
You have to loop over it and change each element in turn if you take that approach. Most libraries have shortcut methods to do that for you.
e.g. for YUI 3:
Y.all('.foo').setStyle('background', '#f00');
or jQuery
jQuery('.foo').css('background', '#f00');
An alternative approach would be to modify the stylesheet itself with JavaScript.
A third option would be to set up your changed styles in advance, using a descendent selector such as:
.foo { /* something */ }
body.bar .foo { /* something else */ }
And then
document.body.className += " bar";
to activate the alternative styling.
styling multiple divs with same class name differently
.right:nth-child(1) {
background: red;
}
.right:nth-child(2) {
background: yellow;
}
...
https://www.w3schools.com/CSSref/sel_nth-child.asp
Related Topics
Disable Spaces in Input, and Allow Back Arrow
Find Selected Item in Datalist in HTML
React-Leaflet Map Not Correctly Displayed
Make the on Scroll Growing <Path> to Dashed Line
How to Get Display:Table-Cell Support in Ie? Any Pure JavaScript or Jquery Workaround
Controlling Svg Colors with CSS
How to Position a React Component Relative to Its Parent
-Webkit-Tap-Highlight-Color: Rgba(0,0,0,0); on a Div
How to Make Div Slide from Right to Left
Angular 2 - How to Dynamically Change an Entire CSS Stylesheet Based on Url Queryparams
Can Multiple HTML Elements Receive Focus at the Same Time
Center Flex Item in Container, When Surrounded by Other Flex Items
Chrome Extension Inject Sidebar into Page
Export SASS/Scss Variables to JavaScript Without Exporting Them to CSS
Something Similar to Treegrid in Jqgrid
How to Have Select Filter in Ngtable
How to Make the Facebook Like Button's Width Automatically Resize