r/HTML • u/Zestyclose_Set3040 • Jul 09 '26
Question Assist me
What is the purpose of <span? here, like why did we write that?
11
Upvotes
r/HTML • u/Zestyclose_Set3040 • Jul 09 '26
What is the purpose of <span? here, like why did we write that?
9
u/davep1970 Jul 09 '26
possibly using to attach a style to with css (altough a class would be better unless you *always* want every span tag to be styled that way
OR they copied and pasted it from somewhere or made a mistake. as it is, it's not doing anything, at least at the moment.
8
u/AshleyJSheridan Jul 09 '26
There are two tags in HTML that have absolutely no semantics, meaning that there is no intent behind the tags, and no contextual information that can be inferred and passed onto accessibility tools.
Those two tags are
<div>and<span>. They are the same in all but one aspect: a<div>is a block level, and<span>is inline. That just means how the browser treats it with regards to the flow of the surrounding content.Your example there is wrapping your specific hobbies, I guess with the intent to style them differently later, but you're not using any CSS yet. You could do something like this:
That makes your hobbies render in italics. Now, if you really wanted to use italics there to emphasise your hobbies, you're better off using the
<em>tag, as it implies a semantic emphasis, which can be surfaced to things like screen readers.Basically, use semantic tags as much as you can, and reach for
<div>and<span>when you just need to style something and add no meaning to the content. If you're still learning what those semantic tags are, I made a tag wizard to help pick the most semantic markup about 4½ years ago: https://www.ashleysheridan.co.uk/blog/Picking+The+Right+HTML+Tag#flowchart-tag-selector4
u/frownonline Jul 09 '26
H2 in a list as a direct descendent is invalid markup.
2
u/RevolutionaryBeat301 Jul 09 '26
Where did that code come from? Those span tags have no purpose. Some gui editors will insert span tags if you change something. If it gets changed back, the span tags remain but have no attributes making them meaningless.
2
u/Original-Ant-9197 Jul 09 '26
well spans used like this is mostly with the objective of giving some styles to a particular text instead of the entire paragraph as with spans everything by default remains inline (doesn't move to next line) a style for it can either be given directly/inline using style="" or given a class which can then be used to apply that style at various places
1
u/brandinobowman Jul 13 '26
A span is an inline element, so you won't actually see that there is a nested span in your paragraph element unless you actually give it some styling. In a lot of cases, a span is simply used to separate and target a portion of text from a larger portion of text.
1
u/fischhut1234 Jul 09 '26
If you want to implement css, then start by creating a .css file and connecting it trough <link href="..."> in the header section, copy the link of the css file into the link-element. For example <link href = "css.css">