skip to Main Content

I would like to style the ‘TextWithNoStyle’, but as it has no element, how can this be possible?

<div class=“MyClass”>
  <a href=“#Something”>Some Text</a>
  TextWithNoStyle
</div>

The style needs to affect only the ‘TextWithNoStyle’ text.

3

Answers


  1. Something like this?

    .MyClass {
      font: normal normal 16px/24px sans-serif;
      color: #F33;
    }
    <div class="MyClass">
      <a href="#Something"></a>
      TextWithNoStyle
    </div>
    Login or Signup to reply.
  2. You could add some default styles to MyClass then override them into the a tag or others.
    Also you have css pseudo elements selectors like :first-line or :first-letter
    I leave a link that answers this

    Styling Text without HTML Tag?

    Login or Signup to reply.
  3. Here’s a possible way: Do the styling with a css rule for MyClass, then create another rule for .MyClass > a where you set the previously styled parameters to initial (i.e. their default value):

    .MyClass {
      color: green;
      font-size: 20px;
      font-weight: bold;
    }
    
    .MyClass > a {
      font-size: initial;
      font-weight: initial;
    }
    <div class="MyClass">
      <a href="#Something">some link</a>
      TextWithNoStyle
    </div>

    Note: a has its own color definition by default, you you don’t have to / shouldn’t reset it using initial.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search