History: Style with CSS

Revision made 8 years ago by Francisco Presencia. Go to the last revision.

CSS is a lan­guage for giv­ing color and shape to your web­site. As you've seen in the HTML les­son, we have al­ready in­cluded Pic­nic CSS as a solid foun­da­tion to build the rest of the style. There are many li­bra­ries like this, in­clud­ing the fam­ous but com­plex Bootstrap.

The CSS al­ways modif­ies one or sever­al of the HTML ele­ments. Let's see how it works togeth­er:

Selec­tors and pro­pert­ies

To know which HTML ele­ment we are modify­ing, we'll use what is cal­led a selec­tor. Every one of the style de­fini­tion that we make on this selec­tor is cal­led a pro­per­ty. For ex­am­ple, to give some mar­gin to all of the para­graphs of a web­site:

p {
  margin: 5px;
}

The selec­tor is the "p" that we find in the first line. What it does is select­ing all the para­graph ele­ments from the HTML.

After the selec­tor we will al­ways find braces "{}" that sur­round all the pro­pert­ies that we want to de­fine for the ele­ments selec­ted.

Fin­al­ly, the pro­pert­ies are pairs of name: value;. In this ex­am­ple we only have the mar­gin as a name and 5px as a value. So we are say­ing that al of the para­graphs in the site have a mar­gin of 5px.

We can add sever­al pro­pert­ies to a single selec­tor, for in­stan­ce to add mar­gin AND to chan­ge the background of all the para­graphs:

p {
  margin: 5px;
  background: rgb(200, 100, 100);
}

Clas­ses

There are several kind of selectors. As you might remember from the previous lesson, classes have a special meaning. For instance, for this HTML:

<section class="row">
  <div class="hello">Hello</div>
  <div class="hello world">Hello world</div>
  <div class="bye">Bye</div>
</section>

We can style it with CSS (Cas­cade Style Sheet):

/* Style for all of the <div> */
div {
  background: red;
}

/* Style for all elements with the class "hello" */
.hello {
  background: green;
}

/* Style for all elements with the classes "hello" and "world" */
.hello.world {
  background: blue;
}

Layouts

Most websites can and should be designed from the more general items to the more specific. First we program the layout of the website. This is a layout:

When we want to make it in HTML+CSS, we first need to take into account the top-most elements:

Now let's program it. Since it's really generic, layout elements we will be using <div> and classes for it. First, the circle that surrounds them all:

<div class="gallery">

  <!-- Everything else goes here -->

</div>

As we see, we assign the class gallery for the container. We will need to remember this class for giving it style later on. Then, each of the components will have the class element:

<div class="gallery">
  <div class="element">A</div>
  <div class="element">B</div>
  <div class="element">C</div>
</div>

Right now our elements don't have any style or content. Let's give it to them. While we could do it with CSS, we'll be using Picnic CSS to make it easier. For this, we will need a class called flex on our gallery and div elements inside of it:

<div class="gallery flex three">
  <div>
    <div class="element">A</div>
  </div>
  <div>
    <div class="element">B</div>
  </div>
  <div>
    <div class="element">C</div>
  </div>
</div>

Check it out in this jsfiddle. There we can see the three letters spaced horizontally. However we cannot see it clearly. Let's give some basic style to the elements to see them better:

.element {
  background: #eee;
  padding: 10px 15px;
  height: 120px;
}

You can see how it looks in this jsfiddle. It looks a lot more like our general layout: