Skip to main content

CSS BASICS : CLASS 108

Hello and welcome.
In the previous post, we did a simple test.
The correct HTML code for that is shown below;

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="my_site.css">   
</head>
<body>
    <h2 class="reds">NASA | JPL</h2>
    <h1 class="blues">ON A MISSION</h1>
    <h4 class="reds">PODCAST</h4>

    <p id="underlined">A new podcast from NASA</p>

    <ul>
        <li>Arrival</li>
        <li>We Are the World</li>
        <li>Worlds of Wonder</li>
    </ul>

     <ol id="roman">
        <li>NASA is cool</li>
        <li>Coding comes second, arguably.</li>
        <li>What is third place?!</li>
    </ol>

    <table >
    <tr>    <th class="blueBordered">Name</th>          <th class="blueBordered">Title</th>         </tr>
    <tr>    <td >John Doe</td>    <td >Tutor</td>       </tr>
    <tr>    <td >Jane Doe</td>    <td >Student</td>    </tr>
    </table>

     <img src="images/apollo.jpg" width="450px" height="450px" class="blueBordered">


     <p id="greenItalic">Click <a href="https://relatableterms.blogspot.com/">me</a> to start learning code </p>


</body>
</html>


Be sure to compare your code with the one above and make any necessary corrections to your answer.

Let's move on to today's subject:  imaginary boxes, borders, margins and padding.

Consider the following two boxes:

The left box has a blue border while that on the right has a green border.
The space outside the border is called the margin.
The top, right, bottom and left margins of the blue box are indicated by the red lines.
Similarly, the margins (top, right, bottom and left) of the green box are indicated by the yellow lines.
Each box has some text inside it (content).
The space from the border of a box to the text content is called the padding of the box.
The top, right, bottom and left padding of each box are indicated by the black lines.

Understanding the above, is important when arranging elements in html.
Each element can be thought of as a box, with a border (invisible by default), margins and padding.
To understand this, let's practice:
  1. Open Sublime Text and create a new file and name it 'arrangements.html' . Save the file.
  2. Create another file and name it 'arrangements.css'. Save this in the same location as the arrangements.html file.
  3. Open your html file and type in the following code inside the body tag:                                     <p id="p1">Paragraph 1</p>
     <p id="p2">Paragraph 2</p> 
  4. Link this html to the css file and save it.
  5. Open up the css file (arrangements.css) in Sublime text proceed as follows: 
  • First, type the following code, then save and open arrangements.html in your browser;                #p1{
        border: 1px solid black;
    }

    #p2{
        border: 1px solid blue;
    }
  • Notice that each of the paragraphs now has a border. Now change your css code to;                     
    #p1{
        border: 1px solid black;
        padding-left: 12px;
    }

    #p2{
        border: 1px solid blue;
        padding-left: 12px;
    }
  • Save and open up arrangements.html in your browser. Notice that the text inside each paragraph has been moved to the right a bit (12px). Experiment with the padding-left value by increasing it and decreasing it as you see fit, each time refreshing the site in your browser to see the results.
  • We can move the text within the borders of each paragraph using text-align attribute. In your css, change the code to;                                                                                                                    #p1{
        border: 1px solid black;
        padding-left: 12px;
        text-align: right;
    }

    #p2{
        border: 1px solid blue;
        padding-left: 12px;
        text-align: center;
  • Save and refresh your site in the browser. Now the text on the first paragraph appears on the right and that of the second paragraph is centered. 
  • We can increase the space between the two paragraphs by changing their margins. First change your css to;                                                                                                                                    #p1{
        border: 1px solid black;
        padding-left: 12px;
        text-align: right;
        margin-bottom: 64px;
    }
  • Save and refresh your site in the browser. Notice that the second paragraph has been moved further down (by 64pixels) from the first paragraph than before. The above code has the same effect as changing the top margin of the second paragraph by 64px as;                                           #p2{
        border: 1px solid black;
        padding-left: 12px;
        text-align: right;
        margin-top: 64px;
    }
  • Experiment with margins by changing the margin-top, margin-left, margin-right and margin-bottom values of each paragraph. Each time, save and refresh your browser and take note of the changes.
  • Now that you have understood what borders, margins and padding are. Let's finish up by changing the border and padding of another html element. the body itself. Change your css code to;                                                                                                                                           body{
        border: 1px solid red;
        padding-top: 48px;
        padding-right: 64px;
        padding-left: 32px;
    }

    #p1{
        border: 1px solid black;
        padding-left: 12px;
        text-align: right;
        margin-bottom: 64px;
    }

    #p2{
        border: 1px solid blue;
        padding-left: 12px;
        text-align: center;
    }
  • Save and refresh the site in your browser. 
Great! I hope you are starting to see how useful the manipulation of margins and padding is when arranging content in your html.
Note that these apply to all html elements (except the <head></head> section and everything that goes inside it) .
As always, here is a video that you can use to follow up on this tutorial.
Till next time, happy coding.

Comments

Popular posts from this blog

CSS BASICS : CLASS 107

Hello and welcome back to this series of tutorials for beginners in web development. So far we have a simple html page and a css file linked to it. We begin this tutorial by creating new html and css files, in the same folder as where my_site.html is located. We shall call them index.html and index.css respectively. Done? Great! Link the two files together , by opening up index.html in your text editor (e.g. Sublime Text) and typing the following code below the closing title tag  </title>  and before the closing head tag </head> :               <link rel="stylesheet" type="text/css" href="index.css"> Inside the body of your html (i.e after <body> and before </body>),  type the following html code :              <p> This is a paragraph </p>          ...

HTML BASICS : CLASS 109 part I

Hello and welcome back to this tutorial series for beginners in web development. This class introduces the html 'div' tag and form elements. Let's begin by learning about the html 'div' tag. " div " stands for division . It allows you to group and separate content into different sections . Consider the image below. The image above shows a common layout (way of arranging elements)  for many web pages. It consists of;  a top area called the header, where most sites place a logo and site name as well as navigation links (links to other parts of the website)  a bottom area called the footer, where most sites place contact information and social media links. a side bar, which can be on the right side or left side. Some sites have two side bars, one on each side. and a main content area, where the main content of the page is displayed. For example. This blog site has a header at the top. It contains the name of the site 'In Relatable Te...

HTML BASICS : CLASS 103

Hello and welcome to the third in a series of beginner level classes for web developers. In the previous lesson (see here ), we learned about the paragraph and list tags. At this point the code in your my_site.html file should look like this: <!DOCTYPE html> <html> <head>     <title></title> </head> <body>     <h2>NASA | JPL</h2>     <h1>ON A MISSION</h1>     <h4>PODCAST</h4>     <p>A new podcast from NASA</p>     <ul>         <li>Arrival</li>         <li>We Are the World</li>         <li>Worlds of Wonder</li>     </ul>      <ol>         <li>NASA is cool...