HTML/CSS/JS - Extended - Part II
15 Sep 2015##Overview
Last week we went over some important aspects of JavaScript and applied them in our Calculator. We also went over new HTML elements and explained the difference between in-line and block elements. This session, we will explain how CSS deals with HTML elements by providing their id
and/or class
attributes. We also see how JavaScript can minpulate CSS in a better sense.
##Previous Code
Everything we cover during the sessions are pushed onto Github. You can find the code here.
##CSS
Let’s go ahead and design our page a little bit. This is what we already have:
The tag p
targets all the <p></p>
elements in our HTML page. This is useful if you want to set a default style for certain HTML elements. But what if we want to specify what we want to style? What if we want to override current styling?
Something important to understand is that there are two HTML attributes that can be used to help select the elements we want. It is also important to name them in a meaningful way. These two attributes are id
and class
.
id
is usually used to identify individual elements, and class
is used to group elements together. Both attributes can be used on the same element, but you cannot have more than one of each on the same element.
example:
So what do we have now? In our index.html
, we have an id
called calculator-container
on our div
element. Let’s try and style it.
Nothing new, all we did was to add a border and add some spacing from out of the border using margin
property and space from the inside using the padding
property. One thing to notice here how we specified the element using the #
sign. This is used to capture an id
whereas the dot (.
) sign is used to capture a class.
Let us try to override one of our p
elements, calculator-heading
.
Notice how we used the same properties in here and only modified the border width and nothing else.
All good, let’s deal with classes now. We have calculator
and we will need to identify it using the dot notation.
But this doesn’t make sense since we are only applying the styling to one element. So let’s change something in our index.html
.
to this
Now we are applying our styling in two parts.
##JavaScript Manipulation
We know how to deal with id’s and classes. Let’s make our JavaScript take part with our display. That is, modifying the CSS of our page based on some events.
First, let’s modify our style.css
.
This will hide our HTML element. In our script.js
, we will display our html tag after we obtain our result.
In our executeCalculator method, add these lines in the very end:
That’s it!