Sunday, 22 December 2019

Web Tutorial: Christmas Tree SVG Animation

Merry Christmas, readers!

For this month's web tutorial, let's do something light and fun. We'll be playing with Scalable Vector Graphics, SVG for short! SVG has been around a long time, but it is only with HTML5 that we get to embed them in HTML pages. They're different from CSS-rendered graphics, but totally compatible with CSS styling. In fact, there are many parallels.

It's really easy, if you're prepared to do a bit of simple arithmetic. Really simple arithmetic.

Without further ado, here's some HTML.
<!DOCTYPE html>
<html>
    <head>
        <title>Xmas SVG</title>

        <style>
       
        </style>
    </head>

    <body>

    </body>
</html>


Add in an svg tag. Yep, it's that simple! Then add some text in there. This should not show up on your browser, but in the event you come across a browser that's so backward it doesn't support SVG (yes, Internet Explorer, I'm looking at you), the text will be displayed.
<!DOCTYPE html>
<html>
    <head>
        <title>Xmas SVG</title>

        <style>
       
        </style>
    </head>

    <body>
            <svg>
                SVG not supported
            </svg>
    </body>
</html>


Let's do some styling. Set the body background to a dark blue, then give the svg element a height, width and a red outline.
        <style>
            body
            {
                background-color: rgba(0, 0, 50, 1);
            }

            svg
            {
                width: 500px;
                height: 500px;
                outline: 0px solid #FF0000;
            }
        </style>


Here's your SVG. We'll start drawing next.


OK, this may be counter-intuitive, but we will jump right into drawing a path - the shape of a Christmas tree. The cool thing about SVG is that unlike CSS, you don't have to divide complex shapes into rectangles and circles and figure out clever ways to make it work. Nope, you just draw a path. Like so. Give it a class of tree while you're at it.
            <svg>
                <path class="tree" />
                SVG not supported
            </svg>


Now remember you're drawing an SVG, so the usual CSS attributes don't apply. Here, we define the thickness of the lines using the stroke-width property, and set the line color to a basic green. You could define stroke and stroke-width as attributes within the path tag itself, but that's just messy.
            svg
            {
                width: 500px;
                height: 500px;
                outline: 1px solid #FF0000;
            }

            .tree
            {
                stroke: rgba(0, 255, 0, 1);
                stroke-width: 1;
            }


In the SVG, the d attribute is a string of commands we'll use to define the path. I broke up the line into several lines for clarity. It's about to get a lot longer. First, we use "M 250 100" which means "move to x-coordinate 250 and y-coordinate 100". For reference, the top left corner of the SVG is (0, 0). Since the SVG is 250 pixels wide and tall, (250, 100) is horizontally right in the middle and vertically near the top. This point is the top of the tree.
            <svg>
                <path d="
                M 250 100"
                class="tree"/>
                SVG not supported
            </svg>


Next, draw a line to the next point, somewhere left to the starting point and lower. For this, you use "l". It stands for "line", as in, "line to x minus 30, y plus 30 from the last specified point". The lower case "l" allows us to specify the instructions relative to the last point. You can use an uppercase "L", but then you'd have to specify the absolute coordinates (220, 70) which doesn't sound too bad right now, but can be a huge pain in the butt if you need to move the tree later on.
            <svg>
                <path d="
                M 250 100
                l -30 30
                "
                class="tree"/>
                SVG not supported
            </svg>


So yeah, you see what you've done? You've created a green line from (250, 100) to (220, 70).


Let's specify another line. The next one doesn't move vertically, but is 10 pixels right from the last point.
            <svg>
                <path d="
                M 250 100
                l -30 30 l 10 0
                "
                class="tree"/>
                SVG not supported
            </svg>


Now you see you've drawn a horizontal line from the last point. Do you see the black background there? That's because we never specified a background color for the path, so it's defaulting to that color. Leave it alone for now.


Here's one side of the tree drawn for you.
                <path d="
                M 250 100
                l -30 30 l 10 0
                l -30 30 l 10 0
                l -30 40 l 10 0
                l -40 50 l 10 0
                l -40 70 l 10 0
                l -40 80 l 320 0
                " class="tree"/>


See it taking shape?


Let's complete the drawing.
                <path d="
                M 250 100
                l -30 30 l 10 0
                l -30 30 l 10 0
                l -30 40 l 10 0
                l -40 50 l 10 0
                l -40 70 l 10 0
                l -40 80 l 320 0
                l -40 -80 l 10 0
                l -40 -70 l 10 0 
                l -40 -50 l 10 0
                l -30 -40 l 10 0
                l -30 -30 l 10 0
                l -30 -30
                " class="tree"/>


Ah. Spectacular!


To color the tree, we use the fill property. We'll set it to a muted green.
            .tree
            {
                stroke: rgba(0, 255, 0, 1);
                stroke-width: 1;
                fill: rgb(0, 100, 0);
            }


The tree's been drawn! But it looks a bit stiff...


What this tree needs is curves. Instead of "l", use "q". That's "q" as in "Quadratic Bézier curve". It still draws a line to the point specified, but two new numbers, namely, 0 and 10, form the arguments. The first number is how much the line will curve towards the x-axis, while the second number is how much the line will curve towards the y-axis. In this case, "0" means that no action is taken along the x-axis while the line dips slightly about "10" pixels vertically.
                <path d="
                M 250 100
                q 0 10 -30 30 l 10 0
                l -30 30 l 10 0
                l -30 40 l 10 0
                l -40 50 l 10 0
                l -40 70 l 10 0
                l -40 80 l 320 0
                l -40 -80 l 10 0
                l -40 -70 l 10 0 
                l -40 -50 l 10 0
                l -30 -40 l 10 0
                l -30 -30 l 10 0
                l -30 -30
                " class="tree"/>


See the curve at the top?


The rest of it has been done for you. Experiment! Quadratic Bézier curves aren't the point of this tutorial today, and they aren't that important right now. But they sure do make things prettier.
                <path d="
                M 250 100
                q 0 10 -30 30 l 10 0
                q 0 10 -30 30 l 10 0
                q 0 20 -30 40 l 10 0
                q 0 20 -40 50 l 10 0
                q 0 30 -40 70 l 10 0
                q 0 40 -40 80 l 320 0
                q -40 -40 -40 -80 l 10 0
                q -40 -30 -40 -70 l 10 0 
                q -30 -20 -40 -50 l 10 0
                q -20 -10 -30 -40 l 10 0
                q -20 -10 -30 -30 l 10 0
                q -20 -10 -30 -30
                " class="tree"/>


Nice curves!


The Star

So now you've drawn a tree-shaped path and colored it. Let's draw a circle at the top of the tree. The center of the circle is defined by the cx and cy attributes. They've been set to the coordinates of the topmost point of the tree. The radius is defined by the attribute r. In this case, it's 5 pixels. We'll set the rest of the circle using the toplight CSS class.
                <circle cx="250" cy="100" r="5" class="toplight">

                </circle>

                <path d="
                M 250 100
                q 0 10 -30 30 l 10 0
                q 0 10 -30 30 l 10 0
                q 0 20 -30 40 l 10 0
                q 0 20 -40 50 l 10 0
                q 0 30 -40 70 l 10 0
                q 0 40 -40 80 l 320 0
                q -40 -40 -40 -80 l 10 0
                q -40 -30 -40 -70 l 10 0 
                q -30 -20 -40 -50 l 10 0
                q -20 -10 -30 -40 l 10 0
                q -20 -10 -30 -30 l 10 0
                q -20 -10 -30 -30
                " class="tree"/>


Here, we define a bright yellow (almost white) border at a very low opacity, and the fill is the same color but at higher opacity. The border width is 15 pixels.
            .tree
            {
                stroke: rgba(0, 255, 0, 1);
                stroke-width: 1;
                fill: rgb(0, 100, 0);
            }

            .toplight
            {
                stroke: rgba(255, 255, 200, 0.3);
                stroke-width: 15;
                fill: rgba(255, 255, 200, 0.8);
            }


Note that there are no z-index properties for SVGs. Whatever element comes first is covered by anything that is drawn over it. Which is pretty damn convenient under certain conditions!


Let's animate that!

Use the animate tag within the circle tag. The attributeName attribute defines the attribute that will be animated. It's "r". The from and to attributes determines what values the animation will change "r" to, And the dur attribute defines how long the entire animation takes. Let's set it to half a second. The repeatCount attribute, set to "indefinite", means that the animation will repeat forever.

Refresh your browser. The circle at the top will appear to be blinking rapidly! That's because it's growing from 4 pixels radius to 6 pixels radius every half a second.
                <circle cx="250" cy="100" r="5" class="toplight">
                    <animate attributeName="r" from="5" to="6" dur="0.1s" repeatCount="indefinite" />
                </circle>


Time for some decorations...

We'll draw another circle. Place it anywhere within the Christmas Tree, make it as large as you want, and style it using the CSS class ball.
                <path d="
                M 250 100
                q 0 10 -30 30 l 10 0
                q 0 10 -30 30 l 10 0
                q 0 20 -30 40 l 10 0
                q 0 20 -40 50 l 10 0
                q 0 30 -40 70 l 10 0
                q 0 40 -40 80 l 320 0
                q -40 -40 -40 -80 l 10 0
                q -40 -30 -40 -70 l 10 0 
                q -30 -20 -40 -50 l 10 0
                q -20 -10 -30 -40 l 10 0
                q -20 -10 -30 -30 l 10 0
                q -20 -10 -30 -30
                " class="tree"/>

                <circle cx="270" cy="150" r="5" class="ball">

                </circle>


Here, we will give ball an orange outline, 1 pixel thick. For the fill property, we will use an SVG-defined value. It's represented with a hash sign and the value name, which is "ballGradient".
            .tree
            {
                stroke: rgba(0, 255, 0, 1);
                stroke-width: 1;
                fill: rgb(0, 100, 0);
            }

            .ball
            {
                stroke: rgba(255, 200, 100, 0.3);
                stroke-width: 1;
                fill: url(#ballGradient);
            }

            .toplight
            {
                stroke: rgba(255, 255, 200, 0.3);
                stroke-width: 15;
                fill: rgba(255, 255, 200, 0.8);
            }


You should now see an orange circular outline near the top of the tree.


Now let's define the fill, ballGradient. We first create a defs tag.
            <svg>
                <defs>

                </defs>

                <circle cx="250" cy="100" r="5" class="toplight">
                    <animate attributeName="r" from="5" to="6" dur="0.1s" repeatCount="indefinite" />
                </circle>


Within this tag, create a radialGradient tag with an id of ballGradient.
                <defs>
                    <radialGradient id="ballGradient">

                    </radialGradient>
                </defs>


Let's fill in these attributes. fx and fy define the center point of the innermost circle. In our case, we want it horizontally to the left, and vertically middle. cx and cy define the center point of the outermost circle. I'm gonna place this right in the middle of the ball. r defines the radius of the gradient.
                <defs>
                    <radialGradient id="ballGradient" cx="50%" cy="50%" r="80%" fx="10%" fy="50%">

                    </radialGradient>
                </defs>


Now, radial gradients need stops. Stops are the points in the gradient where the color r opacity changes. For the first stop, let's define the offset attribute at 10%, meaning that when the gradient reaches 10% of its entire range, the color is bright orange.
                <defs>
                    <radialGradient id="ballGradient" cx="50%" cy="50%" r="80%" fx="10%" fy="50%">
                        <stop offset="10%" style="stop-color:rgb(255, 200, 100); stop-opacity:1" />
                    </radialGradient>
                </defs>


Then at 100%, the color should have changed to a darker shade of orange.
                <defs>
                    <radialGradient id="ballGradient" cx="50%" cy="50%" r="80%" fx="10%" fy="50%">
                        <stop offset="10%" style="stop-color:rgb(255, 200, 100); stop-opacity:1" />
                        <stop offset="100%" style="stop-color:rgb(255, 50, 0); stop-opacity:1" />
                    </radialGradient>
                </defs>


All good? Fantastic.


Let's add an animation to this decorative ball. Here, we'll animate the width of the outline. Fill in whatever values you like. Get creative!
                <circle cx="270" cy="150" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>


You'll see the decorative orange ball animate with a radiant glow effect.


Nice, right? So just place several more of these babies all  over the tree, with random cx and cy attributes, radii and animation values. In particular, try varying the animation durations.
                <circle cx="270" cy="150" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="240" cy="290" r="3" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="150" cy="330" r="7" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="290" cy="250" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="220" cy="180" r="6" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="1.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="170" cy="260" r="3" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="1.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="280" cy="320" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="230" cy="380" r="4" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="350" cy="360" r="7" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="220" cy="230" r="8" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="330" cy="290" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="240" cy="130" r="3" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>
                SVG not supported


This is great, ain't it?


Some text

Let's put up a text greeting in your SVG. It can be something boring, like "Merry Christmas". Here's two lines of text using the text tag. We position the first line on top of the tree, and the second line below.
                <defs>
                    <radialGradient id="ballGradient" cx="50%" cy="50%" r="80%" fx="10%" fy="50%">
                        <stop offset="10%" style="stop-color:rgb(255, 200, 100); stop-opacity:1" />
                        <stop offset="100%" style="stop-color:rgb(255, 50, 0); stop-opacity:1" />
                    </radialGradient>
                </defs>

                <text x="150" y="75">MERRY</text>
                <text x="80" y="455">CHRISTMAS</text>

                <circle cx="250" cy="100" r="5" class="toplight">
                    <animate attributeName="r" from="5" to="6" dur="0.1s" repeatCount="indefinite" />
                </circle>


Then let's style this. I'm going to set the font style and size, and give it a red fill and white outline. Pretty Christmassy, eh?
            .toplight
            {
                stroke: rgba(255, 255, 200, 0.3);
                stroke-width: 15;
                fill: rgba(255, 255, 200, 0.8);
            }

            text
            {
                stroke: rgba(255, 255, 255, 1);
                stroke-width: 1;
                fill: rgba(255, 0, 0, 1);
                font: bold 50px georgia;
            }


Bueno!


Next, we'll try to position this SVG in the middle of the screen. Remember, SVGs can't be directly manipulated to be relatively positioned, so let's encase this entire thing within a div. Style it using the svgContainer class.
        <div id="svgContainer">
            <svg>
                <defs>
                    <radialGradient id="ballGradient" cx="50%" cy="50%" r="80%" fx="10%" fy="50%">
                        <stop offset="10%" style="stop-color:rgb(255, 200, 100); stop-opacity:1" />
                        <stop offset="100%" style="stop-color:rgb(255, 50, 0); stop-opacity:1" />
                    </radialGradient>
                </defs>

                <text x="150" y="75">MERRY</text>
                <text x="80" y="455">CHRISTMAS</text>

                <circle cx="250" cy="100" r="5" class="toplight">
                    <animate attributeName="r" from="5" to="6" dur="0.1s" repeatCount="indefinite" />
                </circle>

                <path d="
                M 250 100
                q 0 10 -30 30 l 10 0
                q 0 10 -30 30 l 10 0
                q 0 20 -30 40 l 10 0
                q 0 20 -40 50 l 10 0
                q 0 30 -40 70 l 10 0
                q 0 40 -40 80 l 320 0
                q -40 -40 -40 -80 l 10 0
                q -40 -30 -40 -70 l 10 0 
                q -30 -20 -40 -50 l 10 0
                q -20 -10 -30 -40 l 10 0
                q -20 -10 -30 -30 l 10 0
                q -20 -10 -30 -30
                " class="tree"/>

                <circle cx="270" cy="150" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="240" cy="290" r="3" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="150" cy="330" r="7" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="290" cy="250" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="220" cy="180" r="6" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="1.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="170" cy="260" r="3" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="1.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="280" cy="320" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="230" cy="380" r="4" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="350" cy="360" r="7" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="220" cy="230" r="8" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2.5s" repeatCount="indefinite" />
                </circle>

                <circle cx="330" cy="290" r="5" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>

                <circle cx="240" cy="130" r="3" class="ball">
                    <animate attributeName="stroke-width" from="1" to="10" dur="2s" repeatCount="indefinite" />
                </circle>
                SVG not supported
            </svg>
        </div>


Then write the CSS style. While you're at it, remove the red outline of the SVG.
            body
            {
                background-color: rgba(0, 0, 50, 1);
            }

            #svgContainer
            {
                width: 500px;
                margin: 5em auto 0 auto;
            }

            svg
            {
                width: 500px;
                height: 500px;
                outline: 0px solid #FF0000;
            }


And here your SVG is nicely positioned in the middle of your screen!


Here's a preview of the actual thing...

MERRY CHRISTMAS SVG not supported


Final thoughts

SVGs are fun to write, though it would be foolhardy to totally do without CSS. Maybe some day we'll take a more in-depth look at the different approaches, but for now, it's time to enjoy your holiday season!

Here's to a well-lit Christmas!
T___T

Sunday, 15 December 2019

Ways of Using Cascading Style Sheets (Part 3/3)

This is where it gets hairy. What I'm about to show you is frowned on by most professionals... in public, anyway. But it's important that you know this implementation because it does have its uses despite its several disadvantages. Just don't go crazy with it.

Inline Styling

This is where the style is implemented within the HTML tag itself, as opposed to being in a style tag in the head tag of a page.


In code, it looks like this.

index.html
<!DOCTYPE html>
<html>
    <head>
       
    </head>

    <body>
        <p style="color: #FFAA00;">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere elementum ex. Duis id dolor ex. Praesent augue nulla, consectetur quis lectus eget, luctus pulvinar risus. Nunc ipsum lorem, gravida non purus ac, dapibus facilisis urna. Curabitur vel tempor erat. Maecenas egestas sapien nulla, a lobortis felis posuere ut. Ut ornare laoreet tristique.
        </p>
    </body>
</html>


Pros

It's really quick, and it overrides both External Style Sheets and Internal Style Sheets. When you want to be guaranteed that a style will be applied regardless of what other rules may have been implemented, this is your guy.

Cons

When overused, it bloats the HTML, causing it to be hard to maintain and load slower.

The example below shows multiple tags styled this way in one page. That's painful enough on its own. When you have to maintain multiple pages of this, the pain increases exponentially.

index_nightmare.html
<!DOCTYPE html>
<html>
    <head>
       
    </head>

    <body>
        <p style="color: #FFAA00;">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere elementum ex. Duis id dolor ex. Praesent augue nulla, consectetur quis lectus eget, luctus pulvinar risus. Nunc ipsum lorem, gravida non purus ac, dapibus facilisis urna. Curabitur vel tempor erat. Maecenas egestas sapien nulla, a lobortis felis posuere ut. Ut ornare laoreet tristique.
        </p>

        <p style="color: #FFAA00;">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere elementum ex. Duis id dolor ex. Praesent augue nulla, consectetur quis lectus eget, luctus pulvinar risus. Nunc ipsum lorem, gravida non purus ac, dapibus facilisis urna. Curabitur vel tempor erat. Maecenas egestas sapien nulla, a lobortis felis posuere ut. Ut ornare laoreet tristique.
        </p>

        <p style="color: #FFAA00;">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere elementum ex. Duis id dolor ex. Praesent augue nulla, consectetur quis lectus eget, luctus pulvinar risus. Nunc ipsum lorem, gravida non purus ac, dapibus facilisis urna. Curabitur vel tempor erat. Maecenas egestas sapien nulla, a lobortis felis posuere ut. Ut ornare laoreet tristique.
        </p>
    </body>
</html>


Use Cases

Useful when you don't have access to the existing External Style Sheet.

When the page you're writing is a quick, throwaway hack which you will never touch again or acknowledge in any way.

If you want things done quickly without thought to future maintenance, i.e, under some kind of insane deadline.

If you're some kind of masochist.

If you're not the poor bastard who is going to be maintaining these pages.

Final Notes

CSS is an indelible part of front-end web development today, and while its inclusion is something of a given, its implementation is the next most important factor. Properly implemented CSS will require a bit more effort up-front, but the maintenance benefits are worth it.

It's also a valuable way to separate style from markup (unless for some inexplicable reason you insist on using Inline Styling) and in a tech world where Separation of Concerns is something of an industry standard, any developer would do well to take heed.

Signing off in style,
T___T

Friday, 13 December 2019

Ways of Using Cascading Style Sheets (Part 2/3)

There are times you want to tailor a certain look and feel for a certain HTML page only, and exclude the other pages in the site. Clever use of External Style Sheets can still accomplish that, though this may take more effort, and both the External Style Sheet and the HTML file may need to be modified.

Internal Style Sheets

These is represented by the style tag nested within the head tag of a HTML page. It can override whatever styling is dictated by an External Style Sheet provided it's declared after the reference link to that stylesheet.


In code, it looks like this.

index.html
<!DOCTYPE html>
<html>
    <head>
        <style>
            p
            {
                color: #FFAA00;
            }
        </style>
    </head>

    <body>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere elementum ex. Duis id dolor ex. Praesent augue nulla, consectetur quis lectus eget, luctus pulvinar risus. Nunc ipsum lorem, gravida non purus ac, dapibus facilisis urna. Curabitur vel tempor erat. Maecenas egestas sapien nulla, a lobortis felis posuere ut. Ut ornare laoreet tristique.
        </p>
    </body>
</html>


If you want to use an Internal Style Sheet to override the External Style Sheet, you'd do this. In this example, you want to turn the paragraphs grey instead of orange.



styles.css
p
{
    color: #FFAA00;
}


index_grey.html
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="styles.css">

        <style>
            p
            {
                color: #DDDDDD;
            }
        </style>
    </head>

    <body>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere elementum ex. Duis id dolor ex. Praesent augue nulla, consectetur quis lectus eget, luctus pulvinar risus. Nunc ipsum lorem, gravida non purus ac, dapibus facilisis urna. Curabitur vel tempor erat. Maecenas egestas sapien nulla, a lobortis felis posuere ut. Ut ornare laoreet tristique.
        </p>
    </body>
</html>


Pros

It's a quick fix, and still separates HTML from the CSS. You can screw up the CSS or the HTML without affecting the other... mostly. A good middle ground between External Style Sheets and Inline Styling (which we will examine in the next part).

Cons

Since the CSS is bundled in with the HTML in a single file, causing that file to be larger, loading time would be affected.

If you have a bunch of these pages that you want to affect using the same change, maintenance increasingly becomes a chore as more pages are added.

Use Cases

Useful when you don't have access to the existing External Style Sheet.

Best for projects where only one HTML file is required otherwise. And also if the HTML and/or styling is light.

Used in conjunction with an External Style Sheet when there are multiple pages and some pages need their own unique styling.


Incidentally, this is the implementation I use the most when I'm making really small projects, after which I'm confident that I will never or rarely need to revisit it.

Next

Inline Styling

Tuesday, 10 December 2019

Ways of Using Cascading Style Sheets (Part 1/3)

When implementing Cascading Style Sheets (CSS), there are three main ways to go about it. Each of these ways has its pros and cons (some more than others) and their own set of use cases.

Let's say, for example, that we want to turn all paragraphs orange (actually no, I really can't think of a reason why you'd want to do that unless you're color-blind, but this is just an example.) and we're going to implement that in CSS.

External Style Sheets

This is the de facto standard; if in doubt as to whether you should use an external style sheet or not, just do it. You'll probably thank yourself later.

In this implementation, your HTML and CSS are in two different files, separate and distinct. Your HTML contains a reference link to the CSS file, either over the web or in your own server.


In code, it looks like this.

styles.css
p
{
    color: #FFAA00;
}


index.html
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="styles.css">
    </head>

    <body>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere elementum ex. Duis id dolor ex. Praesent augue nulla, consectetur quis lectus eget, luctus pulvinar risus. Nunc ipsum lorem, gravida non purus ac, dapibus facilisis urna. Curabitur vel tempor erat. Maecenas egestas sapien nulla, a lobortis felis posuere ut. Ut ornare laoreet tristique.
        </p>
    </body>
</html>


Pros

Website expansion is easier. All you realistically have to do is add more pages with the same CSS reference link.

Generally, the more pages you have using one stylesheet, the greater the benefit of the External Style Sheets implementation. If you wanted to turn the paragraphs to a less glaring deep grey, all you would have to do is amend one stylesheet, and all pages referencing that stylesheet would have their paragraphs turned grey.

Great for maintenance. Because the stylesheet is in a separate file from the HTML, it's less probable that you will need to edit the HTML file itself, which can be messy. Also, this lends itself to better collaboration. Imagine there's a team maintaining the site. One is the copywriter who needs to update the text, and the other is the designer. If the style and content are in separate files, the designer and the copywriter can edit the stylesheet and the HTML respectively, concurrently.

Due to caching, the CSS file only needs to be loaded once for it to work. The HTML pages, without CSS, will load faster on their own.

Cons

Could be overkill if you're definitely only going to make one small page or two.

Use Cases

Using one stylesheet to style multiple pages. In fact, as long as you have multiple pages you want to maintain, chances are that even if you end up utilizing other CSS implementations, you will use External Style Sheets as a base implementation.


When either or both the styles and the HTML content are extremely long, it's also a good idea to keep them separate. Because the annoying thing about styles is that they can break if you miss out an opening or closing symbol. Same for HTML. You really don't want to risk screwing up your HTML or CSS due to constant (and unnecessary) edits. Sure, it's easily fixed. It's also a pain in the ass.

Next

Let's explore Internal Style Sheets.

Monday, 2 December 2019

Food Delivery hits a PMD-shaped roadblock!

The 5th of November 2019 was when I turned forty-two, but it's not for that reason that the date was significant last month. You see, on that date, the Land Transport Authority (LTA) implemented a ban of all Personal Mobility Devices (PMDs) on footpaths, ruling that users of such devices would legally only be allowed to use Park Connector Networks, Cycling Paths and Shared Paths.

This caused quite a large outcry among Food Delivery riders. Many of them had been using PMDs to make deliveries. These people are part of the gig economy pertaining to Food Delivery apps such as GrabFood, FoodPanda, WhyQ and Deliveroo. A user uses the app to place an order, and someone in a painfully bright uniform (seriously, have you seen some of those uniforms? They're hideous.) will bring that food to their doorstep. Some of these workers used bicycles, some used motorcycles, and some unfortunate ones used the now-restricted PMDs.

Benefits of the PMD in Food Delivery

PMDs are a fun activity for hobbyists. But where they really shine is as vehicles for Food Delivery.

Sleek and sexy.

Apparently, it's easier on the knees and back as opposed to cycling, and obviously cheaper than a motor vehicle. Some enterprising mothers were also seen bringing their very young children along on deliveries, in effect babysitting and making deliveries simultaneously. And, of course, it's faster than walking.

Back when PMDs first hit the market, Food Delivery riders found a new, less physically-taxing way to make fast deliveries. However, there was a snag - there were reports of fire safety issues and one man even died from a PMD-related fire. Owners of PMDs now had to adhere to safety regulations (the UL2272 certification), paying the cost out of pocket as they upgraded to the new model.

However, due to an increasing number of injuries stemming from accidents due to reckless riding, the LTA soon declared a ban of PMDs from roads and footpaths. Those who had ponied up cash for the new PMD models were now owners of an expensive machine that was severely hamstrung by the ban. Vendors of PMDs ended up with an untenable amount of immovable inventory. Deliveries slowed and income suffered as riders found themselves unable to make deliveries with the same dizzying speed as before. Food vendors, previously being able to sell more food via delivery orders as opposed to being limited by seating spaces in food establishments, found their incomes affected as well. There were even recent cases of e-scooter tourism being affected.

The people happy with the ban (or, at the very least, relieved) were those who feared for their safety as pedestrians, or those who had already been affected by reckless riders.

The Gig Economy

Food Delivery apps are one manifestation of a growing gig economy here in Singapore; indeed, worldwide. It's a new mode of working, made possible by mobile technology and the wonders of the Internet in an increasingly-connected world.

Food Delivery Apps

The Food Delivery industry, in particular, experienced a minor boom as citizens took to it. Not only is the pay pretty decent, it does not require paper qualifications or hard-to-learn skills. The barrier to entry is almost non-existent. Ergo, one could be a school dropout and still make a good living as a Food Delivery rider. And as part of the gig economy, workers enjoy a flexible working schedule and determine just how much work they want to do each day. Kind of like being a taxi driver... without the requisite license and cab rental.

A tech professional's take on the situation

Reactions have been mixed throughout the Nation. Some have stated that the riders' right to earn a living does not trump the right of the public to be safe on footpaths. Conversely, some of the riders have expressed sympathy for those injured or killed by reckless riders, but insist that it is unfair for their living to be affected by the actions of a few.

Some have even accused the upset food delivery riders of being lazy and entitled, and wanting easy, well-paid jobs, and told to take up actual jobs. 

Now, before I weigh in here, I just want to state that I have very little skin in the game. I do not use food delivery apps, choosing to either walk to buy my own, or cook, like some kind of psychopath. I don't own a PMD, though those e-unicycles do look pretty cool. I have not had any bad encounters with reckless riders, though as a pedestrian and someone who has aging parents and a very young nephew, I can certainly see why people would be concerned.

More importantly, I have never been in a situation where riding a PMD to deliver food looked like a viable career choice. I am a tech professional in an industry that will probably never see a sunset, because the only thing that makes software obsolete is other software. So I'm quite aware that me dispensing career advice to food delivery riders to suck it up and "get an actual, stable job" would come off as more than a little sanctimonious, and rightfully so. I have no aversion to sounding like a condescending prick - it can be fun and therapeutic - but if and when I do, I'd prefer it to be intentional.

Here's a stable job.

Besides, there's no such thing as a "stable" job (unless you're a horse jockey. Stable, geddit? Hur hur). Years of being affected by changes to the economy have taught me that valuable lesson.

No, as a tech professional, the point I want to make is that the Internet-enabled gig economy is, by its very nature, volatile. Those food delivery riders who adopted that trend so handily should realize that as quickly as mobile technology disrupted traditional services such as taxis with ride-sharing, advances in technology could just as rapidly disrupt the newly-minted Food Delivery industry. Ban or no ban, that too-good-to-be-true job was built on top of an unsustainable foundation.

Food Delivery companies are basically driven by investor money, trying to strike while the iron is hot and anticipating their investment will eventually come good. Much like bicycle-sharing companies (of which several ceased operations in Singapore since the last time I wrote about them) and the ride-sharing company Uber which also ceased operations here last year.

Riders complaining about their affected income is reminiscent of passengers griping about increased cab fares after Uber's exit. They had a good thing while it lasted, but they should also be aware that it was never going to last.

It's not about what you can get. It's about what you can keep. Whether the affected Food Delivery riders continue on their chosen track or seek career opportunities elsewhere, they should bear this in mind.

That's not to say Food Delivery riders are wrong to be upset. I've lost my job before. I get it. This situation sucks to be in. When I lost my jobs, on each occasion, I upskilled, came back stronger and landed another one. It's not my place to suggest that Food Delivery riders should do the same, but like it or not, the ban is staying until our infrastructure expands enough to safely incorporate PMDs. There's a limit to how productive railing at the Singapore Government is going to be. Get it out of your system if you must, but let it go.

Remember, one can always ask the Government to help take care of you. But no one takes care of you better than you.

Finally...

I've avoided expounding on whether the Singapore Government was right or wrong in issuing the ban. It's a moot point. They did what they felt they had to do.

For wannabe politicians hoping to cash in on some political points by politicizing this issue ahead of the next General Elections, you're wasting your time. The math doesn't add up. You could gain some support from Food Delivery riders who are upset at how the Singapore Government handled this, but for every disgruntled rider, there are at least eight citizens who support the ban.

This is not a hill you want to die on. Move on.

Some food for thought!
T___T