0

Is there a way to structure a css-file in a way like this?:

a:link {
  color: red;
  font-weight: bold;
  a:hover {
    text-decoration: none;
    font-weight: normal
  }
}

So I want a normal link to be underlined and bold, but when I hover over it, it shouldn't be underlined and bold, but it should still have the same color. (This is a simple example just for explanation)

EDIT: I am/was looking for a way without sass or less

3
  • 2
    Yup But U Must Use CSS Less Commented Aug 23, 2016 at 7:00
  • 1
    @Dinesh Anything that can be written in Less can also be written in plain CSS; Less compiles to plain CSS after all. You cannot write it exactly like this, but there's no need to anyway. Even in Less this means something different than what OP actually wants. Commented Aug 23, 2016 at 7:10
  • Possible duplicate of Can i use css like as javascript? Commented Aug 23, 2016 at 7:18

3 Answers 3

2
a {
  color: red;
  font-weight: bold;
}

a:hover {
  text-decoration: none;
  font-weight: normal
}

All a elements will be red and bold. Specifically a:hover elements will also have no text-decoration and the font-weight is overridden to normal. You're not trying to deal with "parents and children" where, just with more specific states of an element.

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

1 Comment

Thanks, I guess I will use this way. For the title thing, I did not know how to call it, so I thought it might be similar to the parent child association where the child has the same attributes as its parents. But I edited the title now.
0

There is no such thing as inheritance in CSS, but you can do :

a:link, a:hover {
    color: red;
    font-weight: bold;
}
a:hover {
    text-decoration: none;
    font-weight: normal;
}

Comments

0

This was partially mentioned in the comments but I believe it deserves its own post.

You'd use SASS (or LESS, but I find the former much easier) to write out your code and then a compiler like Koala to compile it into regular CSS.

This way your code becomes:

a:link {
  color: red;
  font-weight: bold;

  &:hover {
    text-decoration: none;
    font-weight: normal
  }
}

And it will work as intended.

2 Comments

&:hover will compile to a:link:hover, which is not what's intended…
It is a possibility, but I was looking for a way without sass or less

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.