Why is HAML swapping tag positions? - html

This HAML (with chained helpers):
- haml_tag :a, helper_method_1(local1, local2).merge( helper_method_2 local3, local4 ) do
%div{ style: helper_method_3(local5, local6) }
%span= local7
%h3{style: "color: black"}= local8
Is generating this (inverted) HTML:
<div style="prop1: val:1; prop2: val2;"></div>
<span>Span Text</span>
<h3 style="prop3: val3;">H3 Text</h3>
<a class="class-value" id="id-value" href="href-value"></a>
But if we remove the chained method, like:
(...)
- haml_tag :a, helper_method_1(local1, local2) do
(...)
Than HTML renders as expected:
<a class="class-value" id="id-value" href="href-value">
<div style="prop1: val:1; prop2: val2;"></div>
<span>Span Text</span>
<h3 style="prop3: val3;">H3 Text</h3>
</a>
What's wrong with HAML?

Problem solved by changing this line:
- haml_tag :a, helper_method_1(local1, local2).merge( helper_method_2 local3, local4 ) do
Into this line (added parenthesis on helper_method_2):
- haml_tag :a, helper_method_1(local1, local2).merge( helper_method_2(local3, local4) ) do
This is an incomplete answer.
Please feel free to extend/edit it.

I'd guess that HAML is producing the HTML you're expecting but the browser is rewriting it to conform to an HTML standard.
Let us look at what HTML4 has to say about <a>:
<!ELEMENT A - - (%inline;)* -(A) -- anchor -->
and inline means:
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
If you look at fontstyle, phrase, special, and formctrl, you won't find <div> or <h3> in any of those lists so neither <a><div></div></a> nor <a><h3></h3></a> are valid HTML4.
HTML5 has this to say about what an <a> can contain:
Permitted contents
Transparent (either phrasing content or flow content)
Transparent content means:
If the content model of a particular element foo is described as
transparent, it means:
when element foo is a child of a parent element bar whose content model is allowed to contain flow elements, then the contents
of element foo may also contain flow elements
when element foo is a child of a parent element baz whose content model restricts its child elements to only being phrasing
elements, then any child elements of element foo are also restricted
to only being phrasing elements
A <div> is a flow element but not a phrasing element, <h3> is also a flow element rather than a phrasing element. So, if you're in HTML5 mode, your <a> would need a parent element that can contain flow elements. I don't think this applies though; I'd expect the browser to restructure things even more if HTML5 was being used and the <a>'s parent didn't allow flow elements, it would have to move the <div> and <h3> up one more level to get past the restriction to phrasing elements.
I'd guess that your browser thinks it is rendering HTML4 or it isn't quite up to date on what HTML5 says.
You have some options:
Look at the HTML that HAML produces, don't ask the browser, look at the raw output before the browser can do anything to it. This will tell you who is changing things.
Make sure your HTML is being rendered as HTML5.
Restructure your HTML to match the relevant standard, you might have to compensate for outdated but overzealous browsers here. This may require you to use something other than <div> and <h3> inside your <a>.

Related

Wrapping a H2 tag inside another H2 tag?

Is it bad practice in terms of SEO to wrap a H2 tag inside another H2 tag like so or is this still a valid method of displaying h2 tags :
<div class="content-caption light-content">
<h2 class="open-project-link">
<a class="open-project" href="work01.html">
<h2 class="section-info">Branding</h2>
</a>
</h2>
</div>
Or will you be disciplined by google and will your SEO take a hit?
It is impossible to nest H2 elements. The HTML specification forbids it. Error recovery will either give you sequential headings or ignore one entirely (triggering it is foolishing though).
Content model: Phrasing content.
Phrasing content does not include headings.
It doesn't make sense to try to do so in the first place though.

Browsers rendering multiple nested a tags

Imagine this block of HTML:
<a href="/somewhere/">
<div class="nested">
<div class="sub-nested">
<div class="sub-sub-nested">
button
</div>
</div>
</div>
</a>
This gets rendered in my browsers like this:
<div class="nested">
<div class="sub-nested">
<div class="sub-sub-nested">
button
</div>
</div>
</div>
This happens only if there is another a tag inside the outer a tag.
I totally don't understand why this is happening. How this could even be. And it's driving me insane.
The problem looks so basic, that i wonder what it was about the HTML standard that i have misunderstood? After all, shouldn't as of HTML5 any tags be allowed within a tags?
What am i missing here?
You can't next anchor tags. As the W3 says:
12.2.2 Nested links are illegal
Links and anchors defined by the A element must not be nested; an A
element must not contain any other A elements.
If you try to validate your code, you will get
Document type does not allow element "div" here; (...)
One possible cause for this message is that you have attempted to put
a block-level element (such as "<p>" or "<table>") inside an inline
element (such as "<a>", "<span>", or "<font>").
So you can't put a <div> inside an <a>.
To expand a bit on why you can't nest A tags, the browser would not know where to direct the user, since the multiple A tags would have multiple HREF attributes. This is why it is illegal to nest A tags.

Opening and closing tags in HTML

I am just beginner with HTML. So, sorry if its very basic question.
So far I know that for each tag we should open and make sure to close it for example:
<span id="xxx" class="yyy">Hello</span>
Today I saw something like this:
<span id="xxx" class="yyy">X</a>
can someone explain please
<span id="xxx" class="yyy">X</a>
This is an error and browsers will attempt to recover from it so that users don't get a broken experience.
Some elements have optional start tags. Some elements have optional end tags. span and a are not among those elements.
The markup <span id="xxx" class="yyy">X</a> is invalid in any context by HTML syntax rules: even if there is a an opening <a> tag before this, with no matching end tag so far, the a element cannot be closed this way, since there would be an open span element inside it, and end tag omission is not allowed for span.
What actually happens depends on the context. If there was an <a> start tag, the end tag </a> would close it, and, as error recovery, the browser would implicitly close the span tag as well.
If there were no <a> start tag, the end tag </a> would be skipped, as not matching anything, and the browser would keep looking for an </span> tag, until it encounters a closing tag for an outer element or a start tag for an element that cannot be contained in a span element, such as <p>.
A simple demo:
<style>span { background: green }</style>
<a><span id="xxx" class="yyy">X</a> Hello world
<hr>
<span id="xxx" class="yyy">Y</a> Hi again
<p>Done.
Here “X” will have green background, as it is taken as the sole content of the first span. There is also green background for “Y Hi again”, as that will parsed as the content of the second span.
So it gets dirty and messy. And although the error recovery is rather consistent across browsers, and being standardized in HTML5, it’s best to avoid it, by using valid markup. You can use validators such as http://validator.w3.org to check your markup.

Coloring a single word with CSS

I want to set the color of individual words within a <pre> block (roughly speaking, for displaying code with syntax highlighting). The <font> tag is deprecated in favor of using CSS, fair enough; what's the required syntax? In particular, in what element should the words be wrapped? I've previously used <div> to mark chunks of text for CSS styling, but that would seem only suitable for marking full paragraphs.
You should use the simplest, most generic inline element: <span>. For each type of token, give one or more appropriate classes to the span. For example:
<span class="type">int</span>
<span class="name">foo</span>
<span class="op">=</span>
<span class="number literal">42</span>
See it in action.
Update: StackOverflow also does code highlighting -- the code just above is highlighted! What does the HTML for that look like? Viewing the source HTML shows that the first line is highlighted using
<span class="tag"><span</span>
<span class="pln"> </span>
<span class="atn">class</span>
<span class="pun">=</span>
<span class="atv">"type"</span>
<span class="tag">></span>
<span class="pln">int</span>
<span class="tag"></span></span>
// and it goes on
Use span with a style attribute on it. Like:
This is a <span style="color:#f00;">sentence</span>.
<span>
This HTML element is a generic inline container for phrasing content,
which does not inherently represent anything. It can be used to group
elements for styling purposes (using the class or id attributes), or
because they share attribute values, such as lang. It should be used
only when no other semantic element is appropriate. <span> is very
much like a <div> element, but <div> is a block-level element whereas
a <span> is an inline element.
Use <span class="red">text</span> and some basic CSS like .red { color: red; }
Edit : notice class name "red" isn't a good practice
There’s no markup magic here: you can use any inline markup, and you do the magic (coloring or other formatting) in CSS. Technically, not all inline markup is valid inside pre, but browsers don’t really care. It’s more important that some inline markup has some default rendering or functionality.
If you don’t want any default rendering, you can use a or font or span markup, which have no impact on anything when they don’t have attributes and they are not styled. If you want some default rendering, you can use corresponding markup, if it exists, such as b for bold, or u for underline. This means that some special presentation is applied even if your stylesheet is not used.
Most people decide to use just span, as suggested in other answers. It’s simple, and nobody can claim that it has “wrong semantics”, because it has none. But the magic is really in CSS, and you use markup just to distinguish some sequence of characters as an element, so that it can be styled as a unit.
Contrary to what you probably hear most people saying, there is nothing inherently wrong with using font when you are really doing some font settings. But there is a practical problem in the old-style usage like <font color=red>. If you have gazillion tags like that and your boss or customer or wife tells you to use a different shade of red, you will have to change myriads of tags, perhaps in dozens of files. But if you have written <font class=keyword> or <a class=keyword> or, if you prefer, <span class=keyword>, and you use a CSS file referred to in all of your HTML files, you will need to change just one value in that CSS file.

Can we add a <span> inside an <h1> tag?

Is it a proper method to use a <span> tag inside an <h1> tag?
<h1>
<span class="boardit">Portfolio</span>
</h1>
I know we can write it in this way...and am also following the below syntax in my own website..
<h1 class="boardit">
<span>Portfolio</span>
</h1>
However, I Just wanted to know the cleaner form of html..
Yes you can.
HTML4 has this to say:
<!ENTITY % heading "H1|H2|H3|H4|H5|H6">
<!--
There are six levels of headings from H1 (the most important)
to H6 (the least important).
-->
<!ELEMENT (%heading;) - - (%inline;)* -- heading -->
And %inline; is:
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
And %special; includes <span>.
The current HTML has this to say:
Content contents
Phrasing content
And Phrasing content includes <span>.
Yes you can. It can be used to format a part of a h1 block:
<h1>Page <span class="highlight">Title</span></h1>
If the style applies to the entire h1 block, I do this:
<h1 class="highlight">Page Title</h1>
Yes, it's typically fine to use a span inside of an h1. span is an inline element, so it's typically okay to use it inside anything (that allows elements inside it!)
And there's not really a cleaner way to do it sometimes, say if you want to style only part of the h1.
On the other hand... don't do it if it's not necessary, as it is a little ugly : )
Yes that's fine, but why not
<h1 class="boardit">
Portfolio
</h1>
If thats all you're doing?
Yes, you can. The span displays inline, so it should not affect the styling of the H1.
Yes, we can use span tag with header tags and there is nothing wrong in it. Indeed this is widely used for styling header tags, specially for coloring a particular word or letter.
Yes, we can use span tag with header tags and there is nothing wrong in it. Indeed this is widely used for styling header tags, specially for coloring a particular word or letter.
<h1 style="display:inline;">Bold text goes here</h1>
<span style="display:inline;">normal text goes here</span>
Think in above lines - Worked for me - use display:inline prop