Chris Chris -4 years ago 87
CSS Question

a tag inside div-class

Can I place

<a href="#">test</a>
inside
<div class="test">


I am well aware of
<div class="test"> <a href="#">test</a> </div>

my problem with that code is that the a tag leaves some styling for me to do in the css and i would be happy to shrink down my code as much as possible.

Answer Source

Absolutely! There is no reason why an anchor can't reside within a div:

<div class="test">
  <a href="#">test</a>
</div>

Is perfectly valid syntax.

<body>
  <a class="test2" href="#">test</a>
</body>

Is also perfectly valid syntax.

It really just comes down to how you want to apply your styling. You can use either:

.test a {
  // Style the whole DIV, which can contain multiple <a> tags
}

Or:

.test2 {
  // Style the <a> tags that match the class `test2`.
}

Hope this helps! :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download