Vertical alignment and 100% height with Flexbox & CSS3

Before flexbox, in old days of yore (i.e. 2007), getting something to vertically align within an element or have a particular element span the remaining percent of a layout was quite the task. It involved various hacks that may or may not have worked with specific versions of specific browsers of that time period. Using flexbox and CSS3, this can be achieved in just about 2 lines of CSS.

These days, there is a glorious savior: Flexbox!

Flexbox is mostly a fancy name for using the CSS display: flex. Once you slap this on one of your elements, a container div for example, the CSS3 gloves are off!

Let’s solve our vertical alignment issue first. In this sample, we will have a left menu that has a top, middle and bottom. We want the middle to take up the space of what the top and bottom do not.

.flex-container {
 display: flex;
 flex-direction: column;
 width: 100%;
 height: 100%;
}

.flex-grow {
 flex-grow: 1;
}

<div class="flex-container">
  <div class="flex-col">top</div>
  <div class="flex-col flex-grow">middle</div>
  <div class="flex-col">bottom</div>
</div>

The majority of the work is actually done by the flexbox container. Using flexbox allows the element to use the CSS flex-direction: column. By assigning the middle child div element to the have the CSS flex-grow: 1, it will automatically take up the remaining space!

Next, we conquer vertical alignment within elements. Using the same HTML from above, we can apply similar CSS. This time we are going for a flex-direction: row look.

.flex-container {
 display: flex;
 flex-direction: row;
 width: 100%;
 height: 25%;
}

.flex-grow {
 flex-grow: 1;
}

.flex-col {
  display: flex;
  align-items: center;
  justify-content: center;
}

<div class="flex-container">
  <div class="flex-col flex-grow">left</div>
  <div class="flex-col">middle</div>
  <div class="flex-col">right</div>
</div>

With this example, we assign display: flex to both the container and the child elements. Then we add the 1-2 punch of align-items: center and justify-content: center to align our hypothetical nav items both vertically and horizontally.

You’ll also notice that we snuck in some flex-grow goodness in there to not worry about the width of our middle and right items.

Google Web Toolkit (GWT) using Eclipse

GWT is Google’s open source set of tools to develop and debug ajax enabled web applications. GWT applications are written in Java and then compiled into Javascript.

The following article shows how to compile and deploy the sample GWT project in Eclipse 4.3/4.4 and can be downloaded here. For the sake of keeping this relevant to a broader group, I will be assuming basic level Java and Eclipse experience.

1. Setting Up Your Environment

There are a few Eclipse SDKs and browser plugins you’ll need to install in order to start developing GWT applications. To do this, navigate to Help > Install New Software. Depending on your version of Eclipse, you’ll need to enter the following into the “Work with:” dropdown:

4.3 – https://dl.google.com/eclipse/plugin/4.3
4.4 – https://dl.google.com/eclipse/plugin/4.4

You’ll want to check Google Plugin for Eclipse and SDKs to install the corresponding plugin/sdk for your Eclipse version.

2. Create Your Web Application

Once your environment is setup, you can birth your new GWT project. In Eclipse, navigate to File > New > Other. You’ll be prompted with a New Project Wizard. Expand the Google folder and select Web Application Project.

Enter a name for your project. For this example, I chose DemoApp.

Next, you’ll enter a Package. I chose com.demo.demoapp. This determines your folder structure in the src folder of your project. In this example, the folder structure would be /src/com/demo/demoapp.

Uncheck Generate project sample code. The sample code provides a very nice starting point, but does include some fairly advanced Java code (interfaces, implementations, etc). I would recommend checking out the sample code provided once you have a firm grasp of how GWT operates.

All other settings should be correct as default. Location determines which Eclipse workspace to use. Google Web Toolkit and Google App Engine under Google SDKs allows you to specify a particular version of each.

If you are deploying to Google App Engine and have already setup your Application ID, you can enter it here. Otherwise, you can leave this blank for now. It will be touched on later in this article.

3. Project Source

A GWT application can be separated into 2 parts, Server and Client. Technically, it could be separated into 2.5 parts if you count code that is used for the server and client (shared), but for the sake of brevity, we’ll focus on the 2 distinct parts.

Your server code can be found in your package directory. Using the settings from above, this example’s server code can be found at /src/com/demo/demoapp.

By the time you finish this section your project directory structure should look similar to this:

/src
--/com
----/demo
------/demoapp
--------DemoApp.gwt.xml
--------/client
----------DemoApp.java
--------/server
--------/shared
/war
--DemoApp.html

Create a folder for each area of your code. Within the /src/com/demo/demoapp folder, I created a server and client folder. For code that would be used by both, a shared folder would be useful, but we wont be putting anything in that in this example.

Create your Entry Point Class

The main entry point for the client side of things is within the file named after your project, DemoApp.java. In this example, I created this file in the client directory. This class implements the EntryPoint class. Within this Java class, we will create the method onModuleLoad(). Upon loading, this method is called automatically. In this example, we will use this entry point to create our user interface and event handlers.

Here is our entry point in full:

package com.demo.demoapp.client;

import com.google.gwt.core.client.EntryPoint;

public class DemoApp implements EntryPoint {
    public void onModuleLoad() {
    }
}

Create your Module XML

Next we will need to create a Module XML file detailing some things about our project. This will be named DemoApp.gwt.xml and it is placed in your package directory (/src/com/demo/demoapp). Obviously, DemoApp would be named after whatever your project name is. Here are the contents of our simple Module XML file. The parts relevant to your project have been marked in bold.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.6.0//EN"
 "http://google-web-toolkit.googlecode.com/svn/tags/2.6.0/distro-source/core/src/gwt-module.dtd">

<module rename-to='demoapp'>
 <!-- Inherit the core Web Toolkit stuff. -->
 <inherits name='com.google.gwt.user.User'/>

 <entry-point class='com.demo.demoapp.client.DemoApp'/>

 <!-- Specify the paths for translatable code -->
 <source path='client'/>
 <source path='shared'/>
</module>

Create your HTML Host Page

Our last step is to create a frontend to connect our fancy Java EntryPoint class to. These are called HTML Host Pages. These sound fancier than they really are. An HTML Host Page consists mostly of basic HTML. It can have references to your stylesheets, scripts, etc.

To start creating HTML Host Pages, you will need to create a war directory at the root of your project. Not in your package. This is where you would keep your static resources (scripts, css, etc). GWT also stores it’s compiled static resources here as well in a subfolder named after your project. For example, /war/demoapp.

For our very basic HTML Host Page, we will use the following:

<!doctype html>

<html>
  <head>
    <script type="text/javascript" language="javascript" src="demoapp/demoapp.nocache.js"></script>
  </head>

  <body>
    <div id="demoapp_main"></div>
  </body>
</html>

The most important thing to notice is that we are referencing the script myapp/myapp.nocache.js. This file will be created when we compile our project.

You could compile your project with just the entry point class and module XML before creating your host pages, but for the sake of keeping things in order, I explain the compile process after you master the ancient art of the HTML Host Page.

The next point of interest is the div with the id of demoapp_main. Going back to our entry point class, we can reference elements in our host page using their ID attribute.

Our new entry point class could look like this. New code has been highlighted in bold.

package com.demo.demoapp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class DemoApp implements EntryPoint {

	public void onModuleLoad() {
		final Button btn = new Button("Demo Button");
		RootPanel.get("demoapp_main").add(btn);
	}
}

All we are doing here is creating an instance of a button and adding it to our demoapp_main div from our HTML host page and attaching a click event to it.

We are now ready to compile and deploy.

4. Compile

To compile a GWT application in Eclipse, you right click on your project name and select Google > GWT Compile. You’ll be presented with the option to set a specific log level and the option to change the output style of the compiled Javascript. For now, leave these as default.

If you ever need to add compiler/VM arguments, you can expand the Advanced section and enter them there.

If any changes are made to any of your package code, you will need to compile your project again.

5. Deploy

Deploying locally to debug
To debug locally, you click on Run > Debug As > Web Application. If this is your first time viewing a GWT application locally, you will be prompted to download the GWT plugin.

At the time of writing, the plugin doesn’t seem to support Firefox versions higher than 26. Unfortunately, my Firefox was version 34 so I had to resort to Chrome.

Deploying to Google App Engine

If you are setup to use Google App Engine, you can deploy directly to it using an Application ID that has been setup in your Google App Engine console.

To set your project up to deploy to Google App Engine, right click on your project name and navigate to Google > App Engine Settings and enter your Application ID under Deployment. Hit Ok and right click on your project name and navigate to Google > Deploy to App Engine. Your project should then open up as the address app.appspot.com.

Upload to Host

If you want to deploy to your own server, simply upload the contents of the /war folder to your host of choice.

Tinkering with Dreamhost

So you have an old Dreamhost hosting account that’s been sitting around for a few years and you need to put some fancy new web tech on it via SSH. Perhaps you want to install Laravel! If only there was an article that could get you up and running with Laravel!

Laravel requires PHP 5.4. Once you are SSH’d into your account you should run a php -v to see what version you have. I was actually running 5.2. Not good.

For your Dreamhost account, your PHP installations are located at /usr/local/. For my particular server, I had /php5, /php53, and /php54. In order for the latest version of PHP to run when I use the php command, I will need to map the alias via alias php='/usr/local/php54/bin/php'.

For my scenario, I was actually trying to get Laravel 4 up and running and not only was my php alias out of date, but I was missing the phar extension required by Composer. To remedy this in Dreamhost, you need to create ~/.php/5.4/phprc file and put the line extension = phar.so. To verify what extensions/modules are loaded, you can do php -m to see a list.

Note: This post covers upgrading the php version that is used when you are logged into your Dreamhost account via SSH. To change the version that your server actually runs, you would go to Manage Account , scroll down to Domains Hosted and click the Edit button next to the domain you wish to upgrade. You’ll find your available PHP options under Web Options > PHP Mode.

Explaining the MEAN stack

Let’s check out the web’s latest darling buzzword. The MEAN stack in cynical, sub-layman’s terms. Whoever’s turn it was to be cutesy with the nerd acronyms really nailed it this time.

MongoDB – http://www.mongodb.org/

This is our method of storage. It is the 50’s hip greaser dude to mySQL’s Pauly Shore.

According to http://www.mongodb.org/, it is agile and scalable. If you’re playing hipster developer bingo at home, you’re off to a great start. Also, it has automatic sharding. If this doesn’t make you giggle, then you need to see yourself out.

MongoDB is a key player in the NoSQL movement. That is, not relying fully (or at all) on a database for data queries. Instead using XML, JSON, etc document based solutions. No more querying one table to query another table to query another table. You get delicious data objects that bend to your will at a fraction of the overhead.

ExpressJS – http://expressjs.com/

Bundles up a bunch of NodeJS goodness to be used at our leisure. Perhaps the most expendable letter in the MEAN acronym, this is mostly to make life easier until we want/need to get under the hood of NodeJS.

AngularJS

It’s tagline is Superheroic Javascript MVW Framework. MVW!? What magical, problem solving acronym is MVW? Well it’s Model-View-Whatever. At first glance, this caused a grimace, but upon further research, it is a peaceful gesture to calm the gang mentality of MVC vs MVVM and whatever else ego tripping developers argue over when it comes to frameworks.

What it actually does, is quite impressive. It turns what used to be lifeless HTML into something alive and dynamic.

Nodejs

This allows you to write server side applications via normal javascript. After being a supporting player in all of the web hoopla since animated gifs, it finally gets to be the star.

There you go, you can now bring up this spicy hot buzzword at interviews or company bbqs and ACTUALLY know what it means. If only this existed when I needed to flub my way through the last six months.