# Build a Twitter Clone From Scratch: The Design

This article represents the first in a new group effort by the Nettuts+ staff, which covers the process of designing and building a web app from scratch - in multiple languages! We'll use a fictional Twitter-clone, called Ribbit, as the basis for this series.

In this tutorial, we need to focus on the UI. We'll leverage the popular LESS Preprocessor to make our CSS as manageable as possible.

## Introduction

Be sure to download the assets for this tutorial, if working along.

This tutorial is divided into five major parts, which explain how to style various pages of Ribbit's layout. I will reference HTML elements using CSS selectors to make it easier to understand. But before diving into the layout, let's briefly discuss nesting.

### Nesting

In CSS, referencing a nested element can result in lengthy selectors. For example:

 1 2 someId {  3  /* ... */  4 }  5 6 someId div.someClass {  7  /* ... */  8 }  9 10 someId div.someClass p.someOtherClass {  11  /* ... */  12 }  13 14 someId div.someClass p.someOtherClass target {  15  /* ... */  16 } 

And it can grow even bigger! With LESS, you can nest one element in another, making it easier to read:

 1 2 someId {  3  /* ... */  4  div.someClass {  5  /* ... */  6  p.someOtherClass {  7  /* ... */  8  target {  9  /* ... */  10  }  11  }  12  }  13 } 

## Variables and Mixins

Create a new file and name it, style.less. When using any style preprocessor, it's a good idea to store important colors and sizes within variables; you can easily adjust their values without searching the file, looking for property values that you need to change. We will use a handful of variables for the text color, border color, and content width:

 1 2 @text-color: #3F3E3D;  3 @border-color: #D2D2D2;  4 @content-width: 860px; 

Now, let's create two mixins. The first will create the illusion of anti-aliased text, and the second will allow for cross-browser gradients. The former is rather simple:

 1 2 .antialiased (@color) {  3  color: @color;  4  text-shadow: @color 0 0 1px;  5 } 

The trick is to create a shadow underneath the text with the same color and a one-pixel spread, making the browser display a nice shade around the text.

Now for the gradient; this is more complicated than the anti-aliased text because every browser implements gradients differently. Once we've compensated for the various vendor prefixes, here is the code:

 1 2 .gradient4f (@p1, @c1, @p2, @c2, @p3, @c3, @p4, @c4) {  3  background: @c1;  4  background: -moz-linear-gradient(top, @c1 @p1, @c2 @p2, @c3 @p3, @c4 @p4);  5  background: -webkit-gradient(linear, left top, left bottom, color-stop(@p1, @c1), color-stop(@p2, @c2), color-stop(@p3, @c3), color-stop(@p4, @c4));  6  background: -webkit-linear-gradient(top, @c1 @p1, @c2 @p2, @c3 @p3, @c4 @p4);  7  background: -o-linear-gradient(top, @c1 @p1, @c2 @p2, @c3 @p3, @c4 @p4);  8  background: -ms-linear-gradient(top, @c1 @p1, @c2 @p2, @c3 @p3, @c4 @p4);  9  background: linear-gradient(to bottom, @c1 @p1, @c2 @p2, @c3 @p3, @c4 @p4);  10 } 

Every browser has a prefix: -moz- for Firefox, -webkit- for Chrome, etc. The last line uses the W3C recommended version for gradients. If a browser supports it, it will override the previous properties because it's the last background property declaration in the rule. The linear-gradient function accepts eight parameters: four pairs of percent-color values. It creates the gradient with four color steps.

## Global Styles

Let's next style some global elements, such as for buttons and links. We want all elements to use the Helvetica or Arial fonts with the text color defined earlier:

 1 2 * {  3  font-family: sans-serif;  4  color: @text-color;  5 } 

### Body

The body is pretty easy; we need a white background with an image-based pattern. There are no margins and padding:

 1 2 body {  3  background: white url(gfx/bg.png);  4  margin: 0;  5  padding: 0;  6 } 

### Inputs

We'll also provide a default style for all <input/> elements in the page:

 1 2 input {  3  width: 236px;  4  height: 38px;  5  border: 1px solid @border-color;  6  padding: 0 10px;  7  outline: none;  8  font-size: 17px;  9 10  &:focus {  11  background: #FFFDF2;  12  }  13 } 

We set the default size and padding, and we use the @border-color variable to remove the annoying blue outline when the element is focused. You should notice another bit of LESS sugar: we can add CSS pseudo-classes (and normal classes too) using the & character (parent reference), as shown here:

 1 2 &:focus {  3  background: #FFFDF2;  4 } 

This causes the input to have a light yellow background, when focused.

### Submits

Submit buttons will use both the previously defined mixin and the border-radius to create nice effect:

 1 2 input[type="submit"] {  3  height: 36px;  4  border: 1px solid #7BC574;  5  border-radius: 2px;  6  color: white;  7  font-size: 12px;  8  font-weight: bold;  9  padding: 0 20px;  10  cursor: pointer;  11  .gradient4f(0%, #8CD585, 23%, #82CD7A, 86%, #55AD4C, 100%, #4FA945);  12 } 

The links should have a different color than normal text. We'll also underline them on hover:

 1 2 a {  3  text-decoration: none;  4  .antialiased(#58B84E);  5  &:hover {  6  text-decoration: underline;  7  }  8 } 

## Basic Template

We will begin with the portion of the layout that remains the same in every page. Here is the HTML code, which I will explain below:

 1 2   3   4   5   6   7   8   9 
 10 
 11   12  Twitter Clone

 13 14 
 15 
 16 
 17 
 18 19 
 20 
 21 
 22 
 23  Ribbit - A Twitter Clone Tutorial  24 
 25 
 26   27  

We start with a normal doctype definition and document head. You can use the less.js library and include the style.less in the development stage (as I did in this code). Later, you can compile the LESS file into CSS, if you don't wish to use less.js. As you've probably noticed by now, the layout is divided into three parts: header, #content, and footer. You should save this HTML to see if you are styling everything correctly.

Let's tackle the header. It contains Ribbit's logo and the two words: 'Twitter Clone'. It's wrapped in a wrapper, the width of which is controlled by the @content-width variable. There are several wrappers in the layout, and all are @content-width wide with auto margin:

 1 2 .wrapper {  3  width: @content-width;  4  margin: auto;  5 } 

The header itself is 85px tall and page wide:

 1 2 header {  3  background: url(gfx/bg-header.png);  4  height: 85px;  5  width: 100%;  6 } 

After the width, add div.wrapper's style with vertical padding:

 1 2 div.wrapper {  3  padding: 11px 0;  4 } 

So the header should look like:

 1 2 header {  3  background: url(gfx/bg-header.png);  4  height: 85px;  5  width: 100%;  6 7  div.wrapper {  8  padding: 11px 0;  9  } 

Images in the wrapper need to be 10px lower, in order to be nicely centered:

 1 2 img {  3  position: relative;  4  top: 10px;  5  margin: 0 15px 0 0;  6 } 

Also, the font in <span/> elements must be larger than the default size:

 1 2 span {  3  font-size: 18px;  4  margin: 0 42px 0 0;  5 } 

Here's how our design should look at this point.

### Content

There's not much we can do with #content at this time. We'll add some margin to the bottom and a minimum height; the layout will look funky if it's not tall enough:

 1 2 #content {  3  margin-bottom: 15px;  4  min-height: 560px;  5 } 

Inside, the wrapper needs to have some vertical margin with an automatic horizontal margin:

 1 2 div.wrapper {  3  margin: 38px auto;  4 } 

### Footer

Like the header, the footer is the same for all pages. We'll use a background image and a smaller font size. We'll also need to clear: both, because we'll use floats in the content. Without clearing, the footer will not adjust in accordance with the content:

 1 2 footer {  3  background: url(gfx/bg-footer.png);  4  height: 251px;  5  font-size: 14px;  6  clear: both;  7 } 

Let's now add some padding to the wrapper, and images within it should float to the right:

 1 2 div.wrapper {  3  padding: 15px;  4 5  img {  6  float: right;  7  }  8 } 

Here's our footer:

This page displays for users not logged in to Ribbit. Therefore, it will need to present the login form in the header and a register form, with a big frog image in the content. Let's start with a basic template.

Add this login form to the div.wrapper of the header, after the <span/> element:

 1 2 
 3   4   5  

These inputs are already styled, but we do need to add the margins and make the form display as inline. Append this after span in div.wrapper of header:

 1 2 form {  3  display: inline;  4 5  input {  6  margin: 0 0 0 14px;  7  }  8 } 

### Register Form

Here is the HTML for the registration form:

 1 2   3 
 4 

New to Ribbit?

 5 
 6   7   8   9   10   11 


Add this HTML within div.wrapper of #content. We want the image to have rounded corners and to be floated to the left (add this after margin in div.wrapper of #content):

 1 2 img {  3  border-radius: 6px;  4  float: left;  5 } 

Now, we can style the registration form. It will also be a panel that we'll use later; that's why we will style the .panel:

 1 2 div.panel {  3  border: 1px solid @border-color;  4  background: white;  5  margin: 0;  6  margin-bottom: 29px;  7  border-radius: 6px;  8  font-size: 14px;  9 } 

For now, though, we will only style the right panel. It's narrower and sticks to the right side of the panel. Naturally, insert the following into div.panel:

 1 2 &.right {  3  width: 303px;  4  height: 313px;  5  float: right;  6 } 

Also, we need to take care of the header and content of the panel. We use <h1/> elements for the header and <p/> elements for content. Notice that you can use the * wildcard inside of another element:

 1 2 * {  3  margin: 6px 0;  4 }  5 form {  6  padding: 0 23px;  7 }  8 h1 {  9  border-bottom: 1px solid @border-color;  10  margin: 5px 0;  11  font-weight: normal;  12  font-size: 18px;  13  padding: 13px 23px;  14  height: 23px;  15 }  16 p {  17  padding: 0 24px;  18  margin: 18px 0;  19 } 

Here is how div.panel's style should look:

 1 2 div.panel {  3  border: 1px solid @border-color;  4  background: white;  5  margin: 0;  6  margin-bottom: 29px;  7  border-radius: 6px;  8  font-size: 14px;  9 10  &.right {  11  width: 303px;  12  height: 313px;  13  float: right;  14  }  15   16  * {  17  margin: 6px 0;  18  }  19  h1 {  20  border-bottom: 1px solid @border-color;  21  margin: 5px 0;  22  font-weight: normal;  23  font-size: 18px;  24  padding: 13px 23px;  25  height: 23px;  26  }  27  p {  28  padding: 0 24px;  29  margin: 18px 0;  30  }  31 } 

And here is a screenshot of how this page should look, thus far (click to see full size):

## Buddies Page

The Buddies page should be displayed when a user logs in. It will display a list of the last "Ribbits," along with some statistics of your account. Once again, start with the basic template. This page, along with other pages, will display a logout button in place of the login form in the header:

 1 2 
 3   4  

The buttons have already been styled, so we only need to pin it to the right side of container and add some margins:

 1 2 #btnLogOut {  3  float: right;  4  margin: 14px 0 0 0;  5 } 

Because this rule's selector is an element's ID, you can place it either outside of any element or within the header's div.wrapper. It's your choice, but remember that, if you choose to place it inside of another element, the compiled CSS will have a longer selector (header div.wrapper #btnLogOut).

### "Create a Ribbit" Box

First, add this panel's code to div.wrapper of #content:

 1 2 
 3 

Create a Ribbit

 4 

 5 
 6   7   8   9 

 10 


The .right class was styled earlier, but we need to add some styling for the <textarea/> element. We'll give it a proper size and border:

 1 2 textarea.ribbitText {  3  width: 249px;  4  height: 160px;  5  border: 1px solid @border-color;  6 } 

Add this in the style definition of the right panel.

### User Information

Now, let's focus on the panel, which contains the user's account information. We'll temporarily populate it with some random content to see the styling:

 1 2 
 3 

 4 
 5   6  Frogger @username  7 

 8  567 Ribbits45 Followers32 Following
 9  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  10 

 11 
 12 


It may look complex, but the structure is fairly simple, when you strip out the content:

 1 2 
 3 

 4 
 5   6   7 

 8 
 9 

 10 
 11 


Regardless, we have another panel; so we need to style it first:

 1 2 &.left {  3  width: @content-width - 327;  4  float: left;  5 } 

You probably know where to place this code (notice how easily you can perform arithmetical operations in LESS). This panel contains div.ribbitWrapper. So, add the following code:

 1 2 div.ribbitWrapper {  3  padding: 15px 0;  4 } 

There are two <span/> elements inside this element, each with a different color and font size. They have classes of .name and .time:

 1 2 span {  3  &.name {  4  font-size: 18px;  5  color: #58B84E;  6  }  7 8  &.time {  9  font-size: 12px;  10  color: #CCC;  11  }  12 } 

We should also position the avatar image near the left border. Add the following code:

 1 2 img.avatar {  3  margin: 0 19px 0 20px;  4  float: left;  5 } 

Also, Ribbit's text needs to be anti-aliased, justified and moved to the right. This code will place the text next to the avatar, as opposed to beneath it:

 1 2 p {  3  margin: 5px 50px 0 90px;  4  padding: 0;  5  text-align: justify;  6  line-height: 1.5;  7  .antialiased(@text-color);  8 } 

In this paragraph, there are <span/> elements with vertical lines, visually separating them. This effect is achieved by using border, padding, and margin:

 1 2 span.spacing {  3  padding-left: 9px;  4  margin-left: 9px;  5  height: 10px;  6  border-left: 1px solid @border-color;  7 } 

### Buddies' Ribbits

This panel lists the latest ribbits from the people to whom the user follows. Insert the following after the user's information panel:

 1 2 
 3 

 4 
 5   6  Kermit @username 15m  7 

 8  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  9 

 10 
 11 
 12   13  Frogger @username 15m  14 

 15  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  16 

 17 
 18 
 19   20  Kermit @username 15m  21 

 22  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  23 

 24 
 25 
 26   27  Hypnotoad @username 15m  28 

 29  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  30 

 31 
 32 
 33   34  Kermit @username 15m  35 

 36  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  37 

 38 
 39 
 40   41  Hypnotoad @username 15m  42 

 43  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  44 

 45 
 46 


There are few example ribbits to see how it looks. We'll add some borders to visually separate them. Add this code in div.ribbitWrapper:

 1 2 border-bottom: 1px solid @border-color;  3 &:last-child {  4  border: none;  5 } 

This styling adds the bottom border, while removing the border on the last div.ribbitWrapper; a border already exists on the panel.

## Public Ribbits Page

The "Public Ribbits" page will list the latest ribbits of profiles not marked as private, so that users can view the ribbits of those who they don't have in their buddy list. Surprisingly, there is nothing else to style, but we do need to add a touch of HTML. The only difference between this and the previous page is that this one doesn't have the user's information panel, but it will have other content in the final site. So feel free to copy the code of the buddies page, but remove this panel:

 1 2 
 3 

 4 
 5   6  Frogger @username  7 

 8  567 Ribbits45 Followers32 Following
 9  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  10 

 11 
 12 


Also change the header of the panel to "Public Ribbits." Here is the preview of this page:

## Public Profiles Page

On this page, users can see a list of profiles that are not marked as private. There is also a search box to find other profiles. We'll start with the basic template.

### Profile Search

The search box will use the .right panel with an <input/> element inside:

 1 2 
 3 

Search for profiles

 4 

 5 
 6   7   8   9 

 10 


### Profiles List

Here are a handful of example profiles for the profile list, so that you can see how it looks in the browser. In a future lesson, we'll of course replace this, accordingly.

 1 2 
 3 

Public Profiles

 4 
 5   6  Kermit @username 625 followers follow  7 

 8  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  9 

 10 
 11 
 12   13  Frogger @username 329 followers follow  14 

 15  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  16 

 17 
 18 
 19   20  Hypnotoad @username 129 followers follow  21 

 22  Cras justo odio, dapibus ac facilisis in, egestas Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. https://code.tutsplus.com ...  23 

 24 
 25 


## Compiling The CSS

Referencing a nested element can result in lengthy selectors.

As I noted earlier, for production, you can compile your LESS to CSS (and I recommend you do so for performance reasons). There are a few available online compilers:

Along with some stand-alone compilers:

• Crunch! (which is a full-blown LESS editor)
• WinLess (features auto compilation when the less file changes)
• SimpLESS (includes CSS minification)

If any of these compilers do not work for you, Google for more; there are plenty! You can also use lessc from LESS's site, but it's much easier to compile your LESS files with other compilers. Of course, there is nothing wrong with using the less.js library to dynamically compile the layout; new browsers cache the resulting CSS.

## Conclusion

As you can see, LESS is a powerful tool that makes it much easier and faster than plain CSS to style beautiful layouts.

But this is just the beginning. Be prepared to implement Ribbit's back-end in a plethora of languages and platforms in the next set of tutorials!