Div Unique CSS Style Links

I want to create unique styles for my links in one specific div (for example, I want all links to be bold and red in the main body, but in sidebardiv I want them to be blue and italic) How do I do this?

I have:

a:link{ color:#666666; } a:visited{ color:#003300; } a:hover{ color:#006600; } a:active{ color:#006600; } 

however, if I put this in a div section in the sidebar, this will ruin my} 's

+4
source share
5 answers

Use the descendant selector :

 #sidebar a:link{ color:#134896; } #sidebar a:visited{ color:#330033; } #sidebar a:hover{ color:#942A5F; } #sidebar a:active{ color:#6FB25C} 

This is a basic css type selector, and you can link as many descendant selectors as you want, i.e.:

 #content .navigation .header h1.red { /* Properties */ } 

This will match any <h1 class="red"> that is a descendant of an element with a header class, which is a descendant of an element with a navigation class, which is a descendant of an element with id content .

Descendant selectors are one of the few selector types that really works in browsers, so you can rely on them. It should be noted that you should have as few selectors as possible to achieve the goal, as this will increase productivity. Also, try not to specify the type of the element if you can avoid it (this contradicts the recommendations for JavaScript selectors), as it will bind your css to what html looks like now. The developer may decide to change a <span class="highlight"> to <em class="highlight"> later, which will break span.highlight -selector, while .highlight -selector will continue to work.

+13
source
 a:link { font-weight: bold; color: #F00 } #sidebar a { color: #00F; font-style: italic;} #sidebar a:visited { color: #003300; } #sidebar a:hover { color: #006600 } #sidebar a:active { color: #006600 } 
+2
source
 #divId a:link{ color:#666666; } 
+1
source
 div#div_id a:link {style} 

Repeat this as many times as you need for each div, and: visit ,: active ,: hover states.

+1
source
 a { font-weight: bold; color: red; } #sidebardiv a { color: blue; font-weight: normal; font-style: italic; } 
+1
source

Source: https://habr.com/ru/post/1304899/


All Articles