How can you automatically number cross-references in HTML+CSS, without JavaScript? - html

The usual method for creating cross-references in LaTeX is to put a \label inside something you want to refer to later, and then use \ref. So for example, writing as we saw in Section~\ref{intro} in the LaTeX source might produce "as we saw in Section 1" in the final output. Is it possible to get the same effect using just HTML and CSS? Newer features of CSS allow sections and so on to be numbered automatically, but I haven't found anything that lets you reference these counter values from elsewhere in the document.
Here is a concrete example:
<!DOCTYPE html>
<html>
<head>
<title>Cross references</title>
<meta charset="utf-8">
<style type="text/css">
body {
counter-reset: section;
}
section > h1::before {
counter-increment: section;
content: counter(section) ". ";
}
</style>
</head>
<body>
<section>
<h1 id="intro">Introduction</h1>
<p>Pineapples contain essential vitamins.</p>
</section>
<section>
<h1>Further discourse on pineapples</h1>
<p>As we saw in Section ??, pineapples contain essential vitamins.</p>
</section>
</body>
</html>
In this example, all the section headings are prepended with an automatically generated number. I would like to know if there is any HTML markup or CSS styling I could use in place of ?? that will insert the number corresponding to #intro.
I realize I could use <a href="#intro"> to create a cross-hyperlink to the Introduction, but I'd like to include the section number in the link text as well.
If this is not possible with HTML and CSS alone, are there any JavaScript libraries for adding cross-references within a document?

A better way for me to have phrased this question (in hindsight) would have been "How can you access content generated automatically by CSS without using JavaScript?", and that these questions are relevant:
Is it possible to access the content generated by a css :before rule?
How to access CSS generated content with JavaScript
It looks like accessing the automatically generated counter values is not easy even with JavaScript, and not possible without it, so that answers my question.
There are a number of documents from the W3C that discuss counters and generated content, but none as far as I could tell discuss referencing generated content from other parts of a document:
CSS Counter Styles Level 3
CSS Lists and Counters Module Level 3
CSS Generated and Replaced Content Module
There is a note in the W3C's List of suggested extensions to CSS that mentions exactly the cross-reference problem - it comes from an email written in 1998, and I haven't found any follow-up work, so I guess this wasn't a high priority.
Update
The CSS Generated Content for Paged Media Module discusses cross references explicitly, and describes the target-counter and target-text functions to implement them with CSS. However, this is a working draft dated 29 November 2011; these functions don't appear to be implemented in any major browser. An article from A List Apart talks about using these CSS features to produce PDF eBooks from HTML using some random propriety tools.

Related

What are these HTML <c- g> tags? Undefined custom element?

When looking at the source code of the HTML standard there were some tags that I didn't recognise..
For example in this snippet:
<pre><code class='idl'>[<c- g>Exposed</c->=<c- n>Window</c->]
<c- b>interface</c-> <dfn id='htmlparagraphelement' data-dfn-type='interface'><c- g>HTMLParagraphElement</c-></dfn> : <a id='the-p-element:htmlelement' href='dom.html#htmlelement'><c- n>HTMLElement</c-></a> {
[<a id='the-p-element:htmlconstructor' href='dom.html#htmlconstructor'><c- g>HTMLConstructor</c-></a>] <c- g>constructor</c->();
// <a href='obsolete.html#HTMLParagraphElement-partial'>also has obsolete members</a>
};</code></pre>
From https://html.spec.whatwg.org/multipage/grouping-content.html
I thought these may be custom elements, but it doesn't look like they are defined via the custom element registry.. This is the result of interrogating the customElements object.
>>> customElements.get('c')
undefined
>>> customElements.get('c-')
undefined
Is this allowed? (I'd guess so since it's from the HTML standard, but it's still surprising to me). How would the browser know how these elements are supposed to be displayed? For example display: block vs. display: inline.
These are custom-elements (and valid HTML), generated by bikeshed's highlighter.
There is no need to define these as customElements because they don't bring any particular behavior, all they do is to ... save bandwidth.
Here is the commit excerpt:
🚨 TERRIBLE-HACK-ALERT 🚨 Switch to using <c- kt> instead of <span clas…
…s='kt'> to cut the weight of highlighting in half. Still valid HTML!
So apparently by switching from <span class="kt"> to <c- kt> (and span.kt { to c-[kt]{) they saved half of the weight induced by their highlighting.
Though as they say, it's a "terrible-hack", which still can make sense when building a tool that generates the majority of Web Standards pages, which can get very lengthy.
Regarding the default display of such custom-element, I'll quote Alohci's comment which did put it nicely:
All elements take the initial, or inherited for inherited properties, value of each CSS property until specified otherwise. So they would be display:inline
And regarding your expectation to see only best practices in the specs sources, it's better not assume so. Read the content of these pages, don't look at how they're built.
Most HTML editors don't look at the tools that will generate the pages, they write the specs in a pseudo-HTML language full of templates.
Or as it's put in the source:
<!-- Note: This file is NOT HTML, it's a proprietary language that is then post-processed into HTML. -->

Is it more efficient to have multiple HTML files or just one big HTML file when making a presentation website

I am working on a presentation website that has 7 HTML pages. Is it more efficient to have only one big HTML file than 7 smaller ones?
The reason I'm asking this is because the header, footer and 20% of the body are the same for all pages. The parts that are different have only lists, p and h tags.
Thanks!
The performance difference is almost certainly negligible, but by having one html file instead of the seven, you get a smoother ux by changing only what's different with javascript (no page refresh, no flicker).
If you want to stick to vanilla js or jquery, put all the html in one file and toggle the elements' css display properties.
A cleaner and easier way, though, is to use a framework like Angular. That way you can break the dynamic elements into partials, and you'll end up with eight clean, concise html pages (total html = one page option), and need no javascript at all. The best implementation depends on your specifics, a simple one might look something like:
<!DOCTYPE html>
<html ng-app>
...
<body ng-init="partial = 'initial.html'>
<!-- header, whatever part of that 20% -->
<!-- put dynamic elements in separate html files (no html tags or anything, just the divs or whatever -->
<a ng-click="partial = '/path/something.html'">something</a>
<a ng-click="partial = '/path/whatever.html'">whatever</a>
...
<div ng-include="partial">
<!-- rest of that 20%, footer -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</body>
</html>
I like Angular a lot. It's incredibly powerful, yet you can use it for super small things like this with just one script tag. (If you want the back/forward buttons to work, look into ng-route.) Either way, your presentation will be a lot more impressive if you don't use seven static html files. Cheers
The answer is no, it is not efficient to have only one big HTML file compared to 7 smaller files. For if you have many smaller files, let's say for example you have 1 file for header, 1 file for footer, 1 file for body and others. By that you could just easily modify and trace your code by looking on the file on what you want to modify and not by looking on the whole HTML file.

How to define HTML symbol for special char like Natural join: β‹ˆ

I use Markdown and HTML for my lecture notes, and when I need an unusual character like Natural join I have to use the unmemorable code β‹ˆ (β‹ˆ). Is there any way I can define a symbol, like &MYNATJOIN; in a CSS file (or wherever) that would be replaced with the β‹ˆ at HTML rendering time?
ccp
You can use the character β€œβ‹ˆβ€ as such in HTML, provided that you use UTF-8 and declare it properly, as you should anyway; see my Guide to using special characters in HTML.
Alternatively, much less reliably, you can use the HTML5 character reference &bowtie;. It belongs to the added named references that are completely unnecessary and are not supported by any browser version older than 2011.
In order to define your own entitiy that you could use as &MYNATJOIN;, you would need to serve your document with an XML content type, which means that old versions of IE will choke on it and that it will be processed in Draconian mode (i.e., any violation of XML well-formedness constraints will cause just an error message to be shown to users, no document content). Under these conditions, you can use XML entity declarations.
CSS is for optional presentational suggestions and should not be used to add significant content, due to the CSS caveats. If you would use β€œβ‹ˆβ€ for decorative purposes or to visually highlight something that is already duly emphasized verbally or in markup, you can add it to the rendering using generated content, e.g.
.funny:after { content: " β‹ˆ" }
in order to append a space and the β€œβ‹ˆβ€ character to the content of every element in class funny.
You can add a small javascript to the top of your document to do a global replace on your "user defined entity with the entity you want it to refer to. This function runs when the document is loaded.
JS (In <head> tag)
window.onload=function () {
document.body.innerHTML=document.body.innerHTML
.replace(/&MYNATJOIN;/gi,"β‹ˆ");
};
HTML (In <body> tag)
these are some notes. <br />
the entity &MYNATJOIN; should now be a bowtie
You can define more entites by adding more replace statements
See the code snippet below:
window.onload=function () {
console.log(document.body.innerHTML);
document.body.innerHTML=document.body.innerHTML.replace(/&MYNATJOIN;/gi,"β‹ˆ");
console.log(document.body.innerHTML);
document.body.innerHTML=document.body.innerHTML.replace(/&PLUSMINUS;/gi,"βˆ“");
console.log(document.body.innerHTML);
document.body.innerHTML=document.body.innerHTML.replace(/&SINEWAVE;/gi,"∿");
};
<body>
these are some notes.<br />
the entity &MYNATJOIN; should now be a bowtie <br />
a plus or minus looks like this &PLUSMINUS; <br />and how about a sine wave? &SINEWAVE;
</body>
Note that:
There are a litany of ways to trigger javascript to run when a document has loaded, but window.onload is simple and gets the job done.
The replacement uses a regular expression as that is a requirement for doing a global string replace in javascript.
Any & in an HTML document are implicitly converted to & by the HTML parser.
HTML
<span class='mynatjoin'><span/>
CSS
.mynatjoin:before{
content: "\22C8";
}
Result
β‹ˆ
JSfiddle
If you want it to be even simpler, and your willing to break your HTML validity, you could use tags, instead of classes like this:
HTML
<mynatjoin />
CSS
mynatjoin:before{
content: "\22C8";
}
Result
β‹ˆ
JSfiddle
I dont know if this will cause problems in some browsers, but I tested this in the latest, Chrome, FF an IE. It worked. Probably wont work in older browsers.
If you want to do it the way you specified i.e &MYNATJOIN;, then you will need to use some sort of javascript which scans the document and replaces &MYNATJOIN; with β‹ˆ. I don't think it is possible with pure html and css
Based on the example above, you can have multiple css classes to support your symbols. You can use this to find the css code for your corresponding symbol.

How to apply css to html so that it shows as the style property

I want to write an application that sends html formatted email. I have the css and html files as I want them. I'm trying to send the email with the embedded css using the style element like so:
<style type="text/css">
h1 {border-width: 1; border: solid; text-align: center}
</style>
<h1>Title</h1>
<p>Content of the email</p>
It works in some clients (e.g. it works on Mac OSX mail app) and not others (e.g. it doesn't work when reading the email in gmail). When I translate the above to:
<h1 style="border-width: 1; border: solid; text-align: center">Title</h1>
<p>Content of the email</p>
Then it works everywhere. What I'm looking for is a way to place the css as style properties on their corresponding dom elements according the css rules I defined. So for a given file.css and file.html I want to create a new file result.html which displays correctly but in which all the css is embedded as style properties in the dom elements. Any ideas?
This is what you're looking for:
http://www.mailchimp.com/labs/inlinecss.php
Hope this helps!
Drop the style tag, use inline styles.
I have the same issue - I have a php app that sends out a confirmation email once a customer has placed an order. In various email clients it's fine, but web based clients tend to strip out the HEAD tag, which includes the STYLE tag - so any style is lost.
While it's still a good idea, as #Zack mentions, to include a plain text version of what you wanted to say, nobody likes to read plain text. I doubt that Zack is reading Stack Overflow on Lynx, for example.
A quick Google search for 'CSS inliner php' brings up: http://classes.verkoyen.eu/css_to_inline_styles
Also it seems that this question has been asked before on stackoverflow (at least once), at least for php, and there was a Ruby answer given in php class to inline css styles?
I want to write an application that sends html formatted email
Never do this. Email MUST be plain text. You cannot even rely on attachments.

Is there a way to create your own html tag in HTML5?

I want to create something like
<menu>
<lunch>
<dish>aaa</dish>
<dish>bbb</dish>
</lunch>
<dinner>
<dish>ccc</dish>
</dinner>
</menu>
Can it be done in HTML5?
I know I can do it with
<ul id="menu">
<li>
<ul id="lunch">
<li class="dish">aaa</li>
<li class="dish">bbb</li>
</ul>
</li>
<li>
<ul id="dinner">
<li class="dish">ccc</li>
</ul>
</li>
</ul>
but it is so much less readable :(
You can use custom tags in browsers, although they won’t be HTML5 (see Are custom elements valid HTML5? and the HTML5 spec).
Let's assume you want to use a custom tag element called <stack>. Here's what you should do...
STEP 1
Normalize its attributes in your CSS Stylesheet (think css reset) -
Example:
stack{display:block;margin:0;padding:0;border:0; ... }
STEP 2
To get it to work in old versions of Internet Explorer, you need to append this script to the head (Important if you need it to work in older versions of IE!):
<!--[if lt IE 9]>
<script> document.createElement("stack"); </script>
<![endif]-->
Then you can use your custom tag freely.
<stack>Overflow</stack>
Feel free to set attributes as well...
<stack id="st2" class="nice"> hello </stack>
I'm not so sure about these answers. As I've just read:
"CUSTOM TAGS HAVE ALWAYS BEEN ALLOWED IN HTML."
http://www.crockford.com/html/
The point here being, that HTML was based on SGML. Unlike XML with its doctypes and schemas, HTML does not become invalid if a browser doesn't know a tag or two. Think of <marquee>. This has not been in the official standard. So while using it made your HTML page "officially unapproved", it didn't break the page either.
Then there is <keygen>, which was Netscape-specific, forgotten in HTML4 and rediscovered and now specified in HTML5.
And also we have custom tag attributes now, like data-XyZzz="..." allowed on all HTML5 tags.
So, while you shouldn't invent a whole custom unspecified markup salad of your own, it's not exactly forbidden to have custom tags in HTML. That is however, unless you want to send it with an +xml Content-Type or embed other XML namespaces, like SVG or MathML. This applies only to SGML-confined HTML.
I just want to add to the previous answers that there is a meaning to use only two-words tags for custom elements.
They should never be standardised.
For example, you want to use the tag <icon>, because you don't like <img>, and you don't like <i> neither...
Well, keep in mind that you're not the only one. Maybe in the future, w3c and/or browsers will specify/implement this tag.
At this time, browsers will probably implements native style for this tag and your website's design may break.
So I'm suggesting to use (according to this example) <img-icon>.
As a matter of fact, the tag <menu> is well defined ie not so used, but defined. It should contain <menuitem> which behave like <li>.
As Michael suggested in the comments, what you want to do is quite possible, but your nomenclature is wrong. You aren't "adding tags to HTML 5," you are creating a new XML document type with your own tags.
I did this for some projects at my last job. Some practical advice:
When you say you want to "add these to HTML 5," I assume what you really mean is that you want the pages to display correctly in a modern browser, without having to do a lot of work on the server side. This can be accomplished by inserting a "stylesheet processing instruction" at the top of the xml file, like <?xml-stylesheet type="text/xsl" href="menu.xsl"?>. Replace "menu.xsl" with the path to the XSL stylesheet that you create to convert your custom tags into HTML.
Caveats: Your file must be a well-formed XML document, complete with XML header <xml version="1.0">. XML is pickier than HTML about things like mismatched tags. Also, unlike HTML, tags are case-sensitive. You must also make sure that the web server is sending the files with the appropriate mime type "application/xml". Often the web server will be configured to do this automatically if the file extension is ".xml", but check.
Big Caveat: Finally, using the browsers' automatic XSL transformation, as I've described, is really best only for debugging and for limited applications where you have a lot of control. I used it successfully in setting up a simple intranet at my last employer, that was accessed only by a few dozen people at most. Not all browsers support XSL, and those that do don't have completely compatible implementations. So if your pages are to be released into the "wild," it's best to transform them all into HTML on the server side, which can be done with a command line tool, or with a button in many XML editors.
Creating your own tag names in HTML is not possible / not valid. That's what XML, SGML and other general markup languages are for.
What you probably want is
<div id="menu">
<div id="lunch">
<span class="dish">aaa</span>
<span class="dish">bbb</span>
</div>
<div id="dinner">
<span class="dish">ccc</span>
</div>
</div>
Or instead of <div/> and <span/> something like <ul/> and <li/>.
In order to make it look and function right, just hook up some CSS and Javascript.
Custom tags can be used in Safari, Chrome, Opera, and Firefox, at least as far as using them in place of "class=..." goes.
green {color: green} in css works for
<green>This is some text.</green>
<head>
<lunch>
<style type="text/css">
lunch{
color:blue;
font-size:32px;
}
</style>
</lunch>
</head>
<body>
<lunch>
This is how you create custom tags like what he is asking for its very simple just do what i wrote it works yeah no js or convoluted work arounds needed this lets you do exactly what he wrote.
</lunch>
</body>
For embedding metadata, you could try using HTML microdata, but it's even more verbose than using class names.
<div itemscope>
<p>My name is <span itemprop="name">Elizabeth</span>.</p>
</div>
<div itemscope>
<p>My name is <span itemprop="name">Daniel</span>.</p>
</div>
Besides writing an XSL stylesheet, as I described earlier, there is another approach, at least if you are certain that Firefox or another full-fledged XML browser will be used (i.e., NOT Internet Explorer). Skip the XSL transform, and write a complete CSS stylesheet that tells the browser how to format the XML directly. The upside here is that you wouldn't have to learn XSL, which many people find to be a difficult and counterintuitive language. The downside is that your CSS will have to specify the styling very completely, including what are block nodes, what are inlines, etc. Usually, when writing CSS, you can assume that the browser "knows" that <em>, for instance, is an inline node, but it won't have any idea what to do with <dish>.
Finally, its been a few years since I tried this, but my recollection is that IE (at least a few versions back) refused to apply CSS stylesheets directly to XML documents.
The point of HTML is that the tags included in the language have an agreed meaning, that everyone in the world can use and base decisions on - like default styling, or making links clickable, or submitting a form when you click on an <input type="submit">.
Made-up tags like yours are great for humans (because we can learn English and thus know, or at least guess, what your tags mean), but not so good for machines.
Polymer or X-tags allow you to build your own html tags. It is based on native browser's "shadow DOM".
In some circumstances, it may look like creating your own tag names just works fine.
However, this is just your browser's error handling routines at work. And the problem is, different browsers have different error handling routines!
See this example.
The first line contains two made-up elements, what and ever, and they get treated differently by different browsers. The text comes out red in IE11 and Edge, but black in other browsers.
For comparison, the second line is similar, except it contains only valid HTML elements, and it will therefore look the same in all browsers.
body {color:black; background:white;} /* reset */
what, ever:nth-of-type(2) {color:red}
code, span:nth-of-type(2) {color:red}
<p><what></what> <ever>test</ever></p>
<p><code></code> <span>test</span></p>
Another problem with made-up elements is that you won't know what the future holds. If you created a website a couple of years ago with tag names like picture, dialog, details, slot, template etc, expecting them to behave like spans, are you in trouble now!
This is not an option in any HTML specification :)
You can probably do what you want with <div> elements and classes, from the question I'm not sure exactly what you're after, but no, creating your own tags is not an option.
As Nick said, custom tags are not supported by any version of HTML.
But, it won't give any error if you use such markup in your HTML.
It seems like you want to create a list. You can use unordered list <ul> to create the rool elements, and use the <li> tag for the items underneath.
If that's not what you want to achieve, please specify exactly what you want. We can come up with an answer then.
You can add custom attribute through HTML 5 data- Attributes.
For example: Message
That is valid for HTML 5. See http://ejohn.org/blog/html-5-data-attributes/ to get details.
You can just do some custom css styling, this will create a tag that will make the background color red:
redback {background-color:red;}
<redback>This is red</redback>
you can use this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>MyExample</title>
<style>
bloodred {color: red;}
</style>
</head>
<body>
<bloodred>
this is BLOODRED (not to scare you)
</bloodred>
</body>
<script>
var btn = document.createElement("BLOODRED")
</script>
</html>
I found this article on creating custom HTML tags and instantiating them. It simplifies the process and breaks it down into terms anyone can understand and utilize immediately -- but I'm not entirely sure the code samples it contains are valid in all browsers, so caveat emptor and test thoroughly. Nevertheless, it's a great introduction to the subject to get started.
Custom Elements : Defining new elements in HTML