Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override link style inside an html div

I have a div in which I'd like to override my global link style. I have two link styles, one global, one specific. Here the code:

A:link {text-decoration: none; color: #FF0000;}
A:visited {text-decoration: none; color: #FF0000;}
A:hover {text-decoration: none; color: #FF0000;}
A:active {text-decoration: none; color: #FF0000;}

#macrosectiontext
{
    position:relative;
    font:Arial, sans-serif;
    text-align:center;
    font-size:50px;
    font-style: bold;
    margin-top:245px;
    opacity: 0.6;
    background-color:transparent;
}

#macrosectiontext A:link {text-decoration: none; color: #000000;}
#macrosectiontext A:visited {text-decoration: none; color: #FFFFFF;}
#macrosectiontext A:hover {text-decoration: none; color: #FFFFFF;}
#macrosectiontext A:active {text-decoration: none; color: #FFFFFF;}

and I use the div like this:

<div id="macrosectiontext"><a href="www.google.it">bla bla bla</a></div>

however it seems that it doesn't work. The div still inherits the global link style.

like image 605
Gianluca Ghettini Avatar asked Oct 14 '13 15:10

Gianluca Ghettini


People also ask

How do you override a link color in HTML?

To change the color of links in HTML, use the CSS property color. Use it with the style attribute. The style attribute specifies an inline style for an element. Use the style attribute with the CSS property color to change the link color.

Can HTML override CSS?

Using HTML Code in this way creates an internal stylesheet (on the page) that overrides any same-specificity CSS defined in the external stylesheets of your themes and modules. This is handy when you want to test changes of your existing module and frontend theme styles, without having to recompile .

How do you override a div in CSS?

To override an attribute that a CSS class defines, simply append a new inline style after the DIV's class definition.

How do you overwrite a style in CSS?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.


2 Answers

CSS work on inheritance, so you should only override the properties you want to change.

Try always to write HTML & CSS lowercase, still your HTML and CSS are correct

a:link,
a:visited,
a:hover,
a:active {
  text-decoration: none; 
  color: #f00;
}

#macrosectiontext {
  position: relative;
  font:Arial, sans-serif;
  text-align: center;
  font-size: 50px;
  font-style: bold;
  margin-top: 245px;
  opacity: 0.6;
  background-color: transparent;
}

#macrosectiontext a:link {
  color: #000;
}
#macrosectiontext a:visited,
#macrosectiontext a:hover,
#macrosectiontext a:active {
  color: #fff;
}

I made a fiddle for you to show your code is working (changed the hover color, just for demo)

like image 118
Mark Avatar answered Oct 21 '22 06:10

Mark


  1. In The css I would not use the id "#macrosectiontext a:link..." for the link code I would use a class ".macrosectiontext"

  2. use a lower case "a" instead of a Cap "A" in the link style

  3. If you using the style only a few times you can use a span tag around the link and then call to your style from the span tag in stead of the div.

like image 21
JFelton Avatar answered Oct 21 '22 07:10

JFelton