Notes on modern CSS and HTML

...for want of a better place to put and organize them.

Links

Other stuff...

Once you've analyzed the structure of your pages, it's time to take a look under the proverbial hood and analyze your existing markup for presentational HTML that can be replaced with structural markup.

At the begining, give each div a border. For example, div {border: 1px dotted gray; padding: .5em} This will help you see where they begin and end, and also whether or not you have any nesting going on.

Write your CSS for element selectors first (<html>, <body>, <p>, <h1>, <h2>, <ul>, <li>, etc.)

Use contextual or descendant selectors as much as possible. This will keep your markup much cleaner. For example, #subnav li {border: 1px solid black; padding: .5em; display: inline} will only affect list items that occur within your subnav div.


Mason's tutorial on CSS styles

CSS styles

#ids are generally for unique, one-shot cases whereas,
.classes are rules expected to apply here and there when needed (like object classes).

 <div id="test">
  <p>
  Text here...
  </p>
 </div>

 #test { font-style: italic; }

will make the paragraph text above always italic.

 <p id="test">
 Text here...
 </p>

will also be in italics no matter where, but here the example cries for a class (<p class="...">) because it's not within the <div> and it's likely going to be useful elsewhere.

In the example above, <td> text will also be italic. If that's not the intent, then maybe do <td class="not-italic"> (or that effect) to prevent. You can also use !important (but don't do it).

For margins, padding, etc.

Remember that margins are on the outside, padding is on the inside of the <div> or other element. The CSS box model is:

    margins

      +-- border -------------------------
      |
      |     padding
      |
      |     +-----------------------------
      |     |
      |     |    content
      |     |

Links to great sites with CSS suggestions

Useful and ready-to-use HTML 5 snippets

I ripped this collection off from a post on DZone; the author ripped them off from elsewhere.

HTML5 Starter Template

When starting a new project, you need a starter template. Here is a concise and clean template to serve as a basis for your HTML5 projects.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled</title>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
</body>
</html>

Source: http://snipplr.com/view/68539/plain-html5-starter-template/

Get Directions Form (Google Maps)

Here is a simple yet powerful code to create a form where the user can enter his location to get directions to a specific place. Very useful for contact pages.

<form action="http://maps.google.com/maps" method="get" target="_blank">
   <label for="saddr">Enter your location</label>
   <input type="text" name="saddr" />
   <input type="hidden" name="daddr" value="350 5th Ave New York, NY 10018 (Empire State Building)" />
   <input type="submit" value="Get directions" />
</form>

Source: Source: http://css-tricks.com/snippets/html/get-directions-form-google-maps/

Base64 Encode of a 1*1px “spacer” GIF

I don’t recommend using transparent “spacer” gifs, but I know that even in 2013, many people are still using them from time to time. If you’re one of them, you’ll probably enjoy this Base64 encode of a 1*1px “spacer” gif. Way better than using an image.

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">

Source: Source: http://css-tricks.com/snippets/html/base64-encode-of-1x1px-transparent-gif/

Regexp Pattern for Email Validation

HTML5 allows, among other things, validating emails using a regular expression pattern. Here is a ready to use input field with the regexp pattern to validate an email address.

<input type="text" title="email" required pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" />

Source: Source: http://blog.staffannoteberg.com/2012/03/01/html5-form-validation-with-regex/

Valid Flash Embed

Are you often embedding Flash files into your html pages? If yes, you’d better save the valid embedded code below for future use.

<object type="application/x-shockwave-flash" 
  data="your-flash-file.swf" 
  width="0" height="0">
  <param name="movie" value="your-flash-file.swf" />
  <param name="quality" value="high"/>
</object>

Source: Source: http://yoast.com/articles/valid-flash-embedding/

HTML5 Video with Flash Fallback

Another great feature of the new HTML5 specification is definitely the video tag which allows you to easily embed video files. But unfortunately, some browsers can’t deal with embedded HTML5 video. So, here is a complete code snippet with a flash fallback for older browsers.

<video width="640" height="360" controls>
  <source src="__VIDEO__.MP4"  type="video/mp4" />
  <source src="__VIDEO__.OGV"  type="video/ogg" />
  <object width="640" height="360" type="application/x-shockwave-flash" data="__FLASH__.SWF">
    <param name="movie" value="__FLASH__.SWF" />
    <param name="flashvars" value="controlbar=over&image=__POSTER__.JPG&file=__VIDEO__.MP4" />
    <img src="__VIDEO__.JPG" width="640" height="360" alt="__TITLE__"
         title="No video playback capabilities, please download the video below" />
  </object>
</video>

Source: Source: http://camendesign.com/code/video_for_everybody

iPhone Call & SMS Links

With the release of the iPhone, Apple introduced a quick way to create call and SMS links. Here is a code snippet to keep in your library.

<a href="tel:1-408-555-5555">1-408-555-5555</a>
<a href="sms:1-408-555-1212">New SMS Message</a>

Source: Source: https://developer.apple.com/library/ios/featuredarticles…

Autocompletion with HTML5 Datalists

Using the datalist element, HTML5 allows you to create a list of data to autocomplete an input field. Super useful! Here is a code snippet to re-use in your own projects.

<input name="frameworks" list="frameworks" />

<datalist id="frameworks">
  <option value="MooTools">
  <option value="Moobile">
  <option value="Dojo Toolkit">
  <option value="jQuery">
  <option value="YUI">
</datalist>

Source: Source: http://davidwalsh.name/datalist

Country Drop-down List for Web Forms

Here’s another time saver: A ready-to-use dropdown list with all countries. Due to the size of the code, please see the source directly.

Source: Source: http://snipplr.com/view/4792/country-drop-down-list-for-web-forms/

Downloadable Files

HTML5 allows you to force download of files using the download attribute. Here is a standard link to a downloadable file.

<!-- will download as "expenses.pdf" -->
<a href="/files/adlafjlxjewfasd89asd8f.pdf" download="expenses.pdf">Download Your Expense Report</a>

Useful line-drawing characters

The initial use of this table was for converting Linux tree representations to display in HTML.

horizontal bar &#9472;
vertical bar &#9474;
last indent &#9492;
indent &#9500;

HTML character reference, diacritics, special characters, etc.

https://dev.w3.org/html5/html-author/charref


How to scale a div section

Normally, I scale the individual contents of a <div>, but that's cumbersome. Today, I looked for something better. (Even though I know HTML very well and almost never have to look stuff up, it doesn't mean I don't like to learn something new.) It's called zoom:

<div style="zoom: 0.85; -moz-transform: scale( 0.85 )">
    (content including images, tables, lists, mere text, etc.)
</div>

—scales everything in the <div> to 85%. Now, the Mozilla setting is because Firefox doesn't support this, but all other browsers do (reputedly). I've included it because Firefox is a browser I accept to use occasionally.


Toggle open/closed (click to hide/click to unhide)
<!DOCTYPE html>
<html lang="en">
<head>
	<title> Click to hide/show </title>
	<meta charset="UTF-8">
	<style>
		.example
		{
  			position: relative;
  			width: 200px;
  			height: 200px;
  			border: solid 4px #4b4b4b;
  			border-radius: 4px;
  			margin-bottom: 20px;
  			padding: 10px;
  			display: none;
  			font-family: sans-serif;
  			font-size: 32px;
		}

		label
		{
  			padding: 10px;
  			background: #4b4b4b;
  			color: white;
  			border-radius: 4px;
  			font-family: sans-serif;
  			font-size: 32px;
  			display: inline-block;
  			margin-bottom: 20px;
  			cursor: pointer;
		}

		input[ type=checkbox ] { display: none; }
		input[ type=checkbox ]:checked + .example { display: block; }

		* { box-sizing: border-box; }

		body { color: #4b4b4b; }
	</style>
</head>
<body translate="no">

<div class="example-container">
	<label for='checkbox'> Click to show/hide </label>
	<input id='checkbox' type='checkbox' />
	<div class="example"> Hello 👋 </div>
</div>

</body>
</html>

Keeping legend inside fieldset box despite hyperlinks

The key to keeping a <legend> inside the box line of a <fieldset> (instead of seeing it rendered inside the fieldset box) is not to use

<a name="name" />

...as the index/landing place, but, instead, this form:

<a name="name"></a>

Hiding and expanding lines in HTML

Here's how I succeeded in creating some documentation on a class in an API while cleverly offering an example. The page (in the illustration below) starts out with only the class definition. Clicking on the triangle (near the word Pojo) causes the XML corresponding to the class definition to appear (as shown).

Note: I display the XML using Gorbachev's Syntax Highlighter, but that's not an important part of the equation. (The highlighted lines removed from the HTML below obviate the use of Gorbachev.)1

Imagine the following HTML page, contained in a single file. (For your information and delight, I'm am displaying the contents of this example using Gorbachev's Syntax Highlighter too.)

<html>
<head>
<title> Experiment </title>
  <link rel="stylesheet" type="text/css" href="styles/shCore.css"/>
  <link rel="stylesheet" type="text/css" href="styles/shThemeDefault.css"/>
  <script type="text/javascript" src="scripts/shCore.js"></script>
  <script type="text/javascript" src="scripts/shBrushXml.js"></script>
  <script type="text/javascript">
    SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
    SyntaxHighlighter.defaults['auto-links'] = false;
    SyntaxHighlighter.all();
  </script>
  <style>
    html          { font-family: Candara; }
    table.example { padding: 5px 0px 5px 5px }
    td.example    { vertical-align: top }
    div.example   { margin-left: 20px; margin-top: -40px; font-size: small }
  </style>
</head>
<body>
<h2> This is a test </h2>


<a name="CustodianInfo" />
<table class="example"><tr><td class="example">
<pre>
class <b>CustodianInfo</b> extends <i>Pojo</i> 
{
  String  oid;
  String  name;
  <b>Telecom</b> telecom;
  <b>Address</b> address;
}
</pre>
</td><td class="example">
<details><summary></summary>
<div class="example" style="width: 350px">
<pre class="brush: xml; gutter: false">
<custodianinfo>
  <oid>2.16.840.1.113883.3.371</oid>
  <name>Annette Funicello</name>
  <telecom use="HP">385 861-3336</telecom>
  <address use="WP">
    <line>5800 Hollywood Boulevard</line>
    <line>#1638</line>
    <city>Berverly Hills</city>
    <state>CA</state>
    <zipcode>90210</zipcode>
    <country>US</country>
  </address>
</custodianinfo>
</pre>
</div>
</details>
</td></tr></table>

</body>
</html>

Note on writing the HTML including the example

Removing the <table> from the HTML above will return the effect of the <details> mechanism to its usual verticality. Also, the preformatted XML text that is the example (beginning with <custodianinfo> and ending with </custodianinfo> (lines 39 through 51), must be crafted with XML/HTML-legal angle brackets for them to display as you would expect. I.e.: <oid> must be typed as &lt;oid&gt;. (I was too tired to go to the trouble of including this in my example. If you're doing this, you probably already understand the problem anyway.)

Note in Gorbachev1

Underneath the subdirectory in which resides the HTML file above, for our purposes let's name it example.html, must be placed the following, minimal installation of Gorbachev, which only provides a display "brush" for XML (HTML) syntax highlighting (and none for other languages):

.
├── example.html
├── scripts
│    ├── shAutoloader.js
│    ├── shBrushXml.js
│    └── shCore.js
└── styles
     ├── shCore.css
     ├── shCoreDefault.css
     └── shThemeDefault.css

Highlighting list elements on hover

To make it so entire contents of an item in a list (<ol>, <ul>, etc.) get a changed backgroud color as you hover over them, do this:

  <style>
    .list-item-green:hover  { background-color: #DEFDE0; }
  </style>
</head>
<body>
.
.
.
<ul>
<li class="list-item-green"> List element 1 </ul>
<li class="list-item-green"> List element 2 </ul>
<li class="list-item-green"> List element 3 </ul>
</ul>

Here's the example: