0

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.

  1. Append a new class to overwrite the property in .first
  2. Replace .first with rtlFirst (getElementsByClassName("first") and use classList.replace)

Can we just replace background-color in .first?

3
  • No, because those are your options. Either set the style, inline, with js or however you want, OR overwrite the .first styleclass with new properties, OR write a new class and change it.. Commented May 23, 2020 at 8:07
  • Clarify my question a little bit. If I use dev tools, inline style will appear in html. If I don't want it appears in html, can we do that? Commented May 23, 2020 at 23:51
  • Then both answers below are correct, i would opt for the css class with html[dir="rtl"] .first {..}, it's cleaner and no js needed Commented May 24, 2020 at 17:50

2 Answers 2

2

You could try to use a CSS attribute selector specifying that whenever the text must be displayed, more styles should be aplyed.

html[dir="rtl"] .first {
    background-color: cyan;
}

These styles will only be aplied if the direction is rtl, and they will overwrite the styles you had specified before.

Sign up to request clarification or add additional context in comments.

Comments

1

By using html[dir="rtl"] you can add css to the given class.

or you can also use the given js code to add the background color when needed

Solution :

if(document.getElementsByTagName("HTML")[0].dir=="ltr")
{
document.getElementsByClassName("first")[0].style.backgroundColor="cyan";
}
.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>

2 Comments

But we have used the existing class named first right ? @Rmaxx
Well now its edited and inline through js, but indeed still correct. My point is you will always need to overwrite either inline or via a class. The original question is faulty or it cannot be done ,"without inline style or a new class"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.