Phil
Phil

Reputation: 14651

What should be the following tag to a span nested within an anchor?

I am trying to learn fundamentals of html and markings.

I want to create an anchor which containes two lines of information.

first line: the name of the link second line: short explanation

e.g.

<a href='#'>
<span>Studies</span>
<span class="alt">-Information about studies</span>
</a>

Is this correct?

How should the following (2nd span) be modified if necessary?

Thank you

PS. Both lines need to be surrounded with span for css-styling.

Upvotes: 1

Views: 134

Answers (2)

Matt Coughlin
Matt Coughlin

Reputation: 18906

First off, don't rule out using a br tag. This is a semantically-appropriate place for a br tag (forcing a hard break inside a line or paragraph of text). Plus, if you use a br tag, it may no longer be necessary to put either of the two lines of text in separate tags, unless you want to style them differently.

<a href='#'>
    Studies<br/>
    -Information about studies
</a>

Second, try viewing the HTML with stylesheets disabled (I do this in Firefox by pressing ctrl-shift-S, with a little help from the Web Developer extension). Is the browser able to render the content in an easy-to-read way based solely on the HTML provided? To some extent, the more readable the "unstyled" content appears, the more semantically-correct the HTML is.

Given that the second line of text seems to be secondary to the first line (a subtitle, not as important, possibly redundant or not entirely essential), putting the first line in a strong tag or putting the second line in a small tag are a couple ways to establish the relative importance of the two lines, if you wish to do so.

<a href='#'>
    <strong>Studies</strong><br/>
    -Information about studies
</a>

<a href='#'>
    Studies<br/>
    <small>-Information about studies</small>
</a>

There's some room for personal preference here. These are just two approaches.

It may be a little bit of a stretch using a small tag in a case like this, but it's not entirely inappropriate. A small tag is typically used for "fine print", attribution, disclaimers, or side comments. It doesn't semantically mean the text is small, but it does tend to be used for content that's secondary to something else (that clarifies something else). It should though only be used for text that's short in length.

And a strong tag doesn't have to be styled bold. In fact, that's the whole point of semantic markup: It doesn't specify or imply how the content will be styled; all it does is offer a hint to the meaning or context of the content. A strong tag can reasonably be given a style of font-weight:normal.

Upvotes: 3

Chris
Chris

Reputation: 26888

In order to achieve that those are in separate lines, try using the <div> tag instead. You can still specify a class for styling, the only difference is that <div>s are block-elements; each of them is rendered on a separate line. Here's the modified version of your code:

<a href='#'>
Studies
<div class="alt">-Information about studies</div>
</a>

Another, slightly more preferable way of doing that is by styling the elements to be block-elements. That can be used by setting the CSS display property to block. Something like:

<a href='#'>
Studies
<span class = "alt block">-Information about studies</span>
</a>

(Note that class = "alt block" means the element has both classes alt and block, and note also that the first <span> is removed because there's no need to style that node with anything).

CSS:

.block {
    display: block;
}

Hope that helped you!

Upvotes: 0

Related Questions