As title, how to change certain style in css class?
For example,
html (direction may be rtl. We don't know)
.container {
display: block;
width: 400px;
height: 200px;
border: 1px solid black;
}
.first {
display: inline-block;
width: 100px;
height: 100px;
padding: 20px;
background-color: red;
}
.second {
display: inline-block;
width: 100px;
padding: 20px;
background-color: yellow;
}
<html dir="ltr">
<body>
<div class="container">
<div class="first">1</div>
<div class="second">2</div>
</div>
</body>
</html>
If dir="rtl", I want to set background-color: cyan in .first. How to do that with javascript?
I have found 2 solutions.
One is using getElementsByClassName and style.backgroundColor = "cyan". It will create inline style to overwrite .first.
The other one is creating a new css class (e.g. .rtlFirst) to replace old one.
Can we just change background-color: red in .first to background-color: cyan without creating new class or inline style?
Edit: Clarify my question
If we use dev tools, we can see style = "..." in html in first solution (inline style).
If we don't want the inline style appears in html, I have found 2 solutions.
- Append a new class to overwrite the property in
.first - Replace
.firstwithrtlFirst(getElementsByClassName("first")and useclassList.replace)
Can we just replace background-color in .first?