Capitalizing first letter in string with ::first-letter. bug? - html

I stumbled across this weird behaviour recently while trying to find a generic front-end way of capitalizing the first letter in a string. Normally, i would just call it quits, but i kept on trying to find a solution. However, that's when i came across this strange behaviour.
snippet:
.capitalizeWord::first-letter {
text-transform: uppercase;
}
<!-- example 1 -->
<p class="capitalizeWord">
<span>
<i></i>
test1></span>hello
</p>
<!-- example 2 -->
<p class="capitalizeWord">
<span style="float:left;"> <!--float causes it to work -->
<i></i>
test2></span>hello
</p>
According to caniuse ::first-letter chrome is supporting this.
So, my first question is: What is causing this behaviour? This has been "somewhat" answered in the comments
My initial thought was that the compiler interpreted the <i>-tags as chars. But no, the issue is still prevalent when they're removed.
Can anyone else verify that they are experiencing the same results?
Posting image for reference if your browser isn't seeing what mine is:
Anyhow, my second question: Is there a frontend way to accomplish the result of test2 but with test1's structure?
Off-topic but still relevant to answer my first question. See below
Update:
After some discussion in the comments, the problem has been narrowed down to be about the <i>-tag after all.
However, I would still classify this as a bug. Here's why:
An empty <i></i> should not trigger ::first-letter. Like, that should be self-evident by just looking at the name of the selector.
Why is ::first-letter triggered by a non letter character (an EMPTY html-tag in this case), and then stops looking within that scope when nothing has been found in the EMPTY tags, which isn't where the root-elements (p/div) scope ends... Like, whyyyyyyyyyy?
I can break the entire selector by just doing:
.box::first-letter {
text-transform: uppercase;
}
<div class="box">
some text
<!-- wooohoo, works! -->
</div>
<div class="box">
<i></i>some text
<!-- doesn't work now... I broke it, without adding ANY characters/letters to the dom -->
</div>
<span class="box">
some text
<!-- doesn't work now... I broke it again, just changed to a span-tag -->
</span>
<span class="box" style="display:block;">
some text
<!-- wooohoo, works! -->
</span>
Maybe this is me being stubborn, but, i really hate this approach they've taken with this selector. It really could have been used as "the way to go" for capitalizing strings on the web, instead of all these javascript workarounds has had to be created in its place.

You are asking two specific distinct questions, so I will answer them both.
my question is: what is causing this behaviour?
To answer your question: When you float the element, it is taken out of the document flow and no longer counts as part of the content of the block in regards to pseudoelement selectors. From the CSS visual formatting spec:
An element is called out of flow if it is floated, absolutely positioned, or is the root element.
Visual formatting model - W3
So in accordance with the spec, we can see the exact same result if we take the element out of the flow by using a different method, e.g. absolute positioning:
.capitalizeWord::first-letter {
text-transform: uppercase;
}
<!-- example 1 -->
<p class="capitalizeWord">
<span>
<i></i>
test1></span>hello
</p>
<!-- example 2 -->
<p class="capitalizeWord">
<span style="float:left;"> <!-- float causes it to work -->
<i></i>
test2></span>hello
</p>
<!-- example 3 -->
<p class="capitalizeWord">
<span style="position: absolute; left: 50px"> <!-- absolute positioning causes it to work -->
<i></i>
test3></span>hello
</p>
is there a frontend way to accomplish the result of test2 but with test1's structure?
As far as I know there is no way to do this in pure CSS, because you would necessarily need to take the offending element out of the flow to get the selector to find the desired pseudoelement. And by taking the element out of the flow, your elements would no longer have the same visual hierarchy.
I would still classify this as a bug
The behavior follows the spec, so it is not a bug. You might argue that it's a poor design choice.

Related

XHTML Validation

I am running my XHTML file through W3C validation and an error is coming up. However, I am unable to understand the error - it says, "No p element in scope but a p end tag seen".
Can some with fresh eyes see it?
Here is my code:
<div id="mainBody">
<div id="text">
<p>
<img src="images/billyGraham.png" alt="Billy Graham">
<blockquote>
Billy Graham:<br><br>
Is it not arrogance or narrow-mindedness to claim that there is only one way of salvation or that the way we follow is the right way? I think not. After all, do we fault a pilot for being narrow-minded when he follows the instrument panel [while] landing in a rainstorm? No, we want him to remain narrowly focused!
</blockquote>
</p>
</div>
</div>
A blockquote element can't be inside a P element. It's invalid HTML. Although if you've tested in your target browsers and there's no issues I wouldn't worry too much, validation is just a guideline and shouldn't be taken too literally (IMO)

How to name nested elements using BEM and SMACCS

I just started out using BEM and SMACCS for my stylesheets but have run into some trouble as far as naming deeply nested elements in the DOM. Say for instance I have a div called .main-container. Nested inside the first level of the main-container is an additional div which by convention would be named .main-container__article.
<div class="main-container>
<div class="main-container__article></div>
</div>
This is where things get confusing. Inside that article div let's say I have a header followed by a paragraph that has a nested span tags. Do I continue prepending classes with main-container__article as so?
<div class="main-container>
<div class="main-container__article>
<h1 class="main-container__article__header">Heading</h1>
<p class="main-container__article__copy">
<span class="main-container__article__copy__intro-text>Example text.</span>
</p>
</div>
</div>
How far down does the rabbit hole go when it comes to naming parent/child elements? Is there a point where you reset at the second-level element and go from there?
<div class="main-container>
<div class="article>
<h1 class="article__header">Heading</h1>
<p class="article__text">
<span class="article__text__intro-text>This is example text.</span> for a paragraph
</p>
</div>
</div>
BEM naming shouldn't resemble DOM structure because otherwise you won't be able to change markup without changes in CSS.
So for your example I'd make it like this:
<div class="main-container">
<div class="article">
<h1 class="article__header">Heading</h1>
<p class="article__copy">
<span class="article__intro-text">Example text.</span>
</p>
</div>
</div>
There's also a quite powerful thing called mixes, which gives possibility to mix different BEM entities on the same DOM node:
Heading
Example text.
So now you may apply CSS to article block and main-container__article element separately which is very useful when you need to reuse article outside main-container.
.main-container__article__copy__intro-text
definitely doesn't help the readability and maintainability of your stylesheets.
I suggest to break such giant blocks into several smaller blocks. If you do this, you can reuse your styles - in your example you couldn't use the article-block somewhere else.
I would "reset" everytime you can encapsulate a block which can potentially be used in several places in your app/website.

Confusion about HTML5 section, article and aside element compared to div element

Let's say I want to create a simple responsive one page homepage. I find several alternatives to do this, but what is the best option? I have read several articles on the net including the ones fron W3C, but I don't get a clear answer!
I'm going to have two column layout with text to the left and an image to the right. On a desktop computer they will be besides each other, styled left and right. But in smaller devices like a mobile, the right column will be changed to left and be placed below the text column.
Is alternative 1 bad in a HTML5 point of view? My thought was to devide the page with several parts of alternative 1 or 2. There is also a third alternative(I guess there almost endless with other options aswell) to use two article elements inside the section element and use a article element for the image instead of the aside element.
I guess some of you might also suggest me to use article element instead of section elements and use nested article. It's confusing with all this options!
Should I also use article and header element in alternative 1?
Preciate some feedback and guidelines! Sorry for all my questions, I just want to improve my coding skills!
Alternative 1:
<div id="intro">
<div class="content-left">
<h2>Headline</h2>
<p>Text</p>
</div><!-- end class content-left -->
<div class="content-right">
<img src="...."/>
</div><!-- end class content-right -->
</div><!-- end id intro -->
Alternative 2 with HTML5 elements:
<section id="intro">
<article>
<header>
<h1>Headline</h1>
</header>
<p>Text</p>
</article>
<aside>
<img src="...."/>
</aside>
</section>
The answer is: it doesn't really matter much, apart from code readability. Please see Why use HTML5 tags? for more on that.
You could have a <section class="articles"> that contains all <article> elements. You could have a <div class="articles"> that contains all <div class="article"> elements. I think it's safe to say there's no doubt the first one is easier to read for developers. Your pick.
There is, however, one issue: you self-close <img> -- no need for that in html5 anymore. See Are (non-void) self-closing tags valid in HTML5?.
In HTML 5, <foo /> means <foo>, the start tag. It is not a "self-closing tag". Instead, certain elements are designated as having no end tag, for example <br>. These are collectively called void elements. The slash is just syntactic sugar for people who are addicted to XML. Using the slash in a non-void element tag is invalid, but browsers parse it as the start tag anyway, leading to a mismatch in end tags.

Semantic HTML5 for UI elements

With HTML5, there were many additional elements added for structuring documents like blog posts or long texts. But what I have problems coming up with is a semantic way of structuring UI components.
On a typical webapp, you have many different components such as modals, button elements, interacitve forms, containers, and so on. Often, I see those things being constructed using div and span only or by misusing header, footerand nav elements and I get the feeling I missed something out.
Is it really semantic to create all structural, not content-related elements using the div element only? Will there be a more diverse element choice in the future?
EDIT: Here's a short example of what I mean:
<div class="modal foo">
<div class="inner wrapper">
<div class="upper bar">
<div class="inner">
<div class="window-name">
<span class="upper heading">
<h1>Foo</h1>
</span>
<span class="lower heading">
<h3>Extra Baz</h3>
</span>
</div>
<div class="buttons">
<div class="button close"><span class="icon"><i>×<i></span></div>
<div class="button maximize"><span class="icon"><i class="fa fa-maximize"><i></span></div>
</div>
</div>
</div>
<div class="content well">
<!--
Whatever happens inside the modal window named foo.
Pretty sure it needs many divs as well, though.
-->
</div>
<div class="lower bar">
<div class="buttons">
<div class="button help"><span class="icon"><i>?<i></span></div>
</div>
<span class="info">
<p>Enter your barbaz.</p>
</span>
</div>
</div>
</div>
The last W3C working draft for HTML 5.1 was released two days ago, on April, 13, and it is "semantic-centered": see
http://www.w3.org/TR/html51/Overview.html
It is an interesting reading, while waiting to have all those fancy things implemented by the most common browsers.
Is it really semantic to create all structural, not content-related elements using the div element only?
Not in my opinion. Even without to cite "the media is the message", everything has something to do with the content, even "open" and "close" buttons allowing users to see the content.
Will there be a more diverse element choice in the future?
Of course! And with a lot of proprietary prefixes, as usual, just to keep our life busier.
Ignoring div and span elements (which are meaningless, except for the case of specifying some meaningful attributes), your snippet consists of this:
<h1>Foo</h1>
<h3>Extra Baz</h3>
<i>×</i>
<i></i>
<!-- content -->
<i>?</i>
<p>Enter your barbaz.</p>
This is what your content looks like from the semantic perspective. Not very clear what gets represented here.
Using a heading element for a subtitle (h3 in your case) is not appropriate. (Or, if it’s not a subheading but really a new/own section, don’t skip a heading level; but I’m assuming the former.) Use one heading element, and use p for the subheading, and group them in header.
Using i elements for adding icons via CSS is not appropriate. Either use CSS only (with the help of existing elements), or, if you have to add an empty element, use span.
Using span/div elements for buttons is not appropriate. Use button instead.
As you are already using a heading element, it’s recommended to explicitly specify a sectioning content element. Depending on the context of this content, it may be article or aside (or nav if it’s for navigation), but in all other cases section.
Following this, you’d get:
<section>
<header>
<h1>Foo</h1>
<p>Extra Baz</p>
</header>
<button>Close</button>
<button>Maximize</button>
<!-- content -->
<button>Help</button>
<p>Enter your barbaz.</p>
</section>
Now you may add header/footer elements for those parts that are not part of this section’s (not this document’s, it’s only about this section!) main content.
You may, for example, enclose the maximize/close buttons in a header (however, opinions if this would be appropriate differ).
HTML 5.1 will probably have a menu element and a dialog element, which might be useful in this case.

HTML5 - header and the outline algorithm - confusion

I was just reading the HTML5 spec again to find out if I can nest a nav element inside a header. The paragraph for the header element is a bit strange if you ask me, but probably only because I don't understand it.
In this example, the page has a page heading given by the h1 element,
and two subsections whose headings are given by h2 elements. The
content after the header element is still part of the last subsection
started in the header element, because the header element doesn't take
part in the outline algorithm.
<body>
<header>
<h1>Little Green Guys With Guns</h1>
<nav>
<ul>
<li>Games
<li>Forum
<li>Download
</ul>
</nav>
<h2>Important News</h2> <!-- this starts a second subsection -->
<!-- this is part of the subsection entitled "Important News" -->
<p>To play today's games you will need to update your client.</p>
<h2>Games</h2> <!-- this starts a third subsection -->
</header>
<p>You have three active games:</p>
<!-- this is still part of the subsection entitled "Games" -->
...
Source: http://www.w3.org/TR/html5/sections.html#the-header-element
So, according to the comment, <p>You have three active games:</p> is still part of the subsection that started inside the header because the header by itself does not introduce a new section.
But doesn't this break with the nesting structure that HTML is build on? For example, this is invalid markup because the nesting is wrong:
<div>
<p>
</div>
</p>
But If I would define - theoretically - that the div does not do anything, then it would be fine?
Please don't get me wrong, this is not a rant or anything, I just try to understand whats the sense behind this? Also, why is the header element listed under sections, if its not a sectioning element?
By the way, is HTML5 outlining still as broken as it was about a year ago? Or is there a tool or something that handles it correct by now?
So, according to the comment, You have three active games: is
still part of the subsection.
Keyword here is according to the comment, which is not the code. Technically the paragraph is a child of the body. I'ld agree the coding and comments do not match, but the code is technically not incorrect. And there are no restrictions that comments or content can only exist in one block.
This
<div>
<p>
</div>
</p>
is technically incorrect which you can see more clearly if I add some CSS.
div { height:100px; width:auto; }
p {display:block; height:50px; width:300px}
Given that CSS what is the above code suppose to look like?
What compatible browsers may do is rewrite the above code into
<div>
<p></p>
</div>
<p></p>
After the browser engine rewrite the HTML and CSS can be rendered.