Why can't I nest these headings in HTML? (beginner level) - html

I'm doing some of these basic lessons for HTML on Codeacademy, and I've hit my first round-block. Why doesn't this construct work properly?
<h1>
First text.
<h4>
I'm a different size than the first text.
</h4>
I'm not the same size as the first text. Why?
</h1>
I don't really know what headings are. Code Academy hasn't really explained much.
Thanks to anyone who takes the time to look at my n00b question.

I can't find a good reference for this, but it looks like nesting these tags is simply not allowed.
Browsers are pretty lenient about what they'll attempt to display. In this case, I believe the browser closes the h1 tag for you when it encounters the h4 tag. Then it quietly ignores the spurious </h1> it finds later—remember that it already closed it for you.
So the actual elements I see in Chrome are roughly this:
<h1>First text.</h1>
<h4>I'm a different size than the first text.</h4>
I'm not the same size as the first text. Why?
You can see this yourself in Chrome's developer tools (or the equivalent in your browser of choice).
UPDATE
The HTML 5 spec is a good place to learn about this. If I'm understanding it correctly, h1, h2, etc. are "flow content" and expect "phrasing content" to be nested inside (not other flow content). I'm new to reading this spec, so I may be misunderstanding the structure of the document. Someone please correct me if I'm wrong!

If you want to achieve this used span tag instated of nesting head tag.
Like this :
<h1>
First text.
<span>
I'm a different size than the first text.
</span>
I'm not the same size as the first text. Why?
</h1>
CSS:
h1 span{
font-size:20px;
display:block;
}

Heading tags are block level elements like <p> tags. You can think of them taking up a whole line. Thus with your example you're trying to squeeze a whole line inside of a full line. It just won't fit. Your browser probably assumes you meant to have a </h1> right before the <h4> tag in your example. Thus the remaining text after the </h4> tag is just seen as plain text and the closing </h1> tag is simply ignored.
If you want switch between h1 text and h4 text. you need something like this:
<h1>first text.</h1>
<h4>I'm a different size than the first text.</h4>
<h1>I'm now the same size as the first text</h1>
If for some inexplicable reason, you want the h1,h4,h1 tags in the same line. You'll need to use css. You could add a class, then set them as inline block. However, if you just want to change the styling of text in a line, you should use span tags instead.

Related

Would making up an HTML tag break anything?

I'm currently working on a piece of interactive fiction, and part of it requires the reader to look at the HTML document itself. I want to make it as readable as possible, especially for people who aren't used to looking at HTML documents. I already am using very specific class names to style certain elements (i.e. <span class="game_mechanic_A">) but there are parts of the text that I want to "highlight"/draw attention to in a more streamlined manner.
I was thinking I could do This text is <foo>important</foo> for the story instead of This text is <span class="foo">important</span> for the story. In my own testing it seems to work fine, but I am absolutely not an expert on HTML so I don't know if there's something I'm missing. I have noticed that in the inspect pane/panel it formats it like so:
This text is
<foo>important</foo>
for the story
...which feels like a nice bonus, since it brings even more attention to what's in <foo>. Would I be making a huge mistake if I do this? I don't actually want the tag to do anything, just be "formatting" in the HTML document.
Mostly, no.
What's the difference between the two snippets below?
<invalidtag>some tag</invalidtag>
<span>some tag</span>
You can't see any difference in the rendering output. But this doesn't mean it is always no.
The browser renders it as an inline element, like <span>. So, if you replace <span> with <invalidtag>, it will still work.
This is really bad practice though, don't do it!
It triggers a markup validation error: Unknown tag <invalidtag>.
Screen readers do not know how to handle it. The blind people are cut off from enjoying your site.
So, invalid tags are rendered as spans, but don't do it.
To make it more clear:
<p>This is a p tag with a <span style="color:red;">red span</span> inside.</p>
<p>This is a p tag with a <invalidtag style="color:red;">red invalid tag</invalidtag> inside.</p>
<small><em>No difference, eh? Try using a screen reader or looking below!</em></small>
<p>This is a <div style="background-color:cyan; border:1px solid black;">div</div> inside a p tag.</p>
<p>This is a <badtag style="background-color:cyan; border:1px solid black;">bad tag</badtag> inside a p tag.</p>

<h1> inside or outside of <a>?

I want to use h1 in a element. Which one is more correct?
<h1>Home</h1>
OR
<h1>Home</h1>
As w3 describes the markup A:
An anchor is a piece of text which marks the beginning and/or the end
of a hypertext link.
The text between the opening tag and the closing tag is either the
start or destination (or both) of a link. Attributes of the anchor tag
are as follows.
This means both are correct, and they don't have the exact same effect. So it depends on what you want to make a hyperlink, in case you surround the entire <h1> you need to put it as a parent. This will make the entire block clickable, including the space to the right:
<h1 style="background-color: yellow">Anchor for entire title</h1>
In case you want to get only a specific part of the <h1> you only surround that part like this:
<h1 style="background-color: yellow">Anchor only for this</h1>
According to HTML5 specs both are correct:
The a element may be wrapped around entire paragraphs, lists, tables,
and so forth, even entire sections, so long as there is no interactive
content within (e.g. buttons or other links).
Both are fine.
It really depends if you want the whole heading to be a link or just the text in it.
(It's a bit odd to make the main heading for your page a link to the top of the page (or anywhere else for that matter) though.)

Correct tag for one line of text?

What's the correct HTML markup for a single line of text?
The text is not a title so I've ruled out h1, etc. The text is not part of copy so I've ruled out p. The text is just an instruction to the user, click below for more info.
What's the correct tag to use?
<span> is usually the go-to for everything not containers or headings. They typically display inline on default, so they won't break text line endings.
You can use the <p> tag in this instance. You may also consider putting in a span to make the tag semantic like:
<p><span class="user-directive">click below for more info</span></p>
You can also use the span tag in isolation but that isn't necessarily always going to work as you'd expect.
I don't understand your question so clear.
But, if what you're trying to do is "Click this sentence to see more info" <span> should work well.
http://jsbin.com/xeqaqu/1/edit?output

How Do I create two CSS Classes and prevent line break

I have the following 2 HTML statements. I want the "p" tag to NOT wrap to the next line. But I do want it to wrap in subsequent lines. (so I can't use the nowrap style). I want to create two CSS classes to treat each class differently.
<b class= "mscFlapSumBold" id='flapSum0'>This is the Flap Summary</b>
<p class= "mscFlapText" id='flap0'>This is the Flap text </p>
EDIT:
OK. I'm using James suggestion and it's working except that I can't seem to change the line spacing between my lines. When I use margin or line-height, they get ignored. So, James' suggested code is working mostly....
<p>
<strong class="mscFlapSumBold" id="flapSum0">...</strong>
<span class="mscFlapText" id="flap0">...</span>
</p>
Furthur edit: My issue surrounds the fact that I am using jQuery Mobile. So, depending on the viewport, your solution works only sometimes (with certain viewports). Driving me CRAZY. If you have any ideas, I would sure appreciate them.
Simply wrap both in the same p element and place .mscFlapText within a span instead:
<p>
<strong class="mscFlapSumBold" id="flapSum0">...</strong>
<span class="mscFlapText" id="flap0">...</span>
</p>
JSFiddle demo.
It's worth noting that the specification defines p as Grouping Content and b is Text-level Semantics; they aren't designed to go inline with each other.
You can use:
p.mscFlapText {
display: inline;
}
But actually, you should use <span> instead, better not mess up with default behaviour of HTML element.
Besides that, a good habit and small tip is to use <strong> rather than <b> since it's can improve your SEO ranking.

No page breaks between paragraphs in print style sheet

I have a HTML fragment, a list item of a long ordered list
<li>
<p class="nw">Abɩlɩsa ba tɔwɛ asɩn mʋ.</p>
<p class="English">The elders discussed the matter.</p>
</li>
How do the CSS rules look like to keep the two paragraphs in the list item together when printing the document? This means that they either appear together at the end of a page or then are moved together to the next page.
How do I keep the paragraph <p class="nw"> and the paragraph <p class="English"> together so that no page breaks occurs?
I use
.nw {page-break-after:avoid;}
but does not work. There are in fact page breaks between the nw and English paragraphs. This should not be the case as far as I understand the rule. To check it I use the print preview function of Firefox.
The answer How do I avoid a page break immediately after a header was helpful to find a solution. It refers to this bug in the Mozilla bug database.
A solution is the following.
li {
page-break-inside: avoid;
}
It works fine in Firefox.
There are multiple factors in play, first in importance: The user's printer.
There is no bullet-proof way of achieving what you want (Notice how a printer will even cut images in two if it decides to).
You could use a query indicating that if it is on print, that particular piece of text moves somewhere safe on your page, but this could cause other problems, like breaking the normal flow of your layout, etc.
I suggest you read this: http://davidwalsh.name/css-page-breaks
And this :
http://www.w3schools.com/cssref/pr_print_pageba.asp
Do you mean to have no break between the p class?
You can try grouping everything in one <p> element, and then identifying each class with a <span> element. For example,
<li>
<p>
<span class="nw">Abɩlɩsa ba tɔwɛ asɩn mʋ.</span>
<span class="English">The elders discussed the matter.</span>
</p>
</li>
Or if you are trying to just remove the space between the two <p> elements, you can look here - remove spaces between paragraphs
Is this what you meant?
According to your edit, you mean in terms of printing. This removes the paragraph space in a web document, but not while printing - Just a note to anyone searching this question in the future. R Lacorne seems to know the answer to the edited question.