Scripting With Style

In this lab we shall cover the fundamentals of CSS

Task 1

Follow the link below to open the repl. Fork the project then sign in.

Open Lab 3

Every HTML element has the following properties which determine their sizing and layout.

Together these properties make up the box model

Padding, margin and border can have specific styles applied to its four directions, top, right, bottom and left.

Task 2

Update index.html the following and view the result

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
        <style>
                div{
                        background-color: lightgrey;
                        width: 300px;
                        border-bottom: solid;
                        padding: 50px;
                        margin:20px 10px 40px 20px;
                }
        </style>
  </head>
  <body>
        <div>
                This the div content
        </div>
    <script src="script.js"></script>
  </body>
</html>

Open the preview in a new tab by clicking the "Open in new tab" () button.

In the new tab right click the element and select inspect (chrome). This will open the chrome dev tools debugger and allow you to visualize the box model of the element.

If you want the same value for all directions simply use the generic property and provide the single value.
Example: padding : 50px

However, if you want the same value specified for all directions then simply provide the single value to the generic property.
Example: border-bottom: solid

If you want to style all directions with different values then the generic property can be used but with multiple values in the order top, right, bottom and left.

Example: margin: 20px 10px 40px 20px;

Most elements have an inherent display property which has the value block or inline. There can be 4 possible values for display.

Task 3

Use the :hover pseudo class to hide elements of an ordered list when the mouse hovers over them.

Solution:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
        <style>
        li:hover {
           display: none
        }
        </style>
  </head>
  <body>
        <ol>
           <li>Item 1</li>
           <li>Item 2</li>
           <li>Item 3</li>
           <li>Item 4</li>
        </ol>
    <script src="script.js"></script>
  </body>
</html>

Text elements can be styled with several properties some of them are as follows:

Task 4

Edit index.html to complete the following

  1. Delete the styles and the unordered list from the previous task.
  2. Add a section element to the page with a height of 80vh which should contain the following elements
  3. Add a heading 1 element with the content "Sample Poem"
  4. Add a heading 2 element with the title of a poem of your choice
  5. Add the poem below to the page placing each stanza in a paragraph tag.
  6. Center align the heading 2 element
  7. Justify the content of the paragraphs
  8. Add a solid black over and underline to the heading 2

You can use the following content for the poem

We use the following properties to affect the display of fonts of the text in an element.

Task 5

Edit index.html to complete the following on the poem from Task 4

  1. Using span elements and an appropriate class style the first word of every paragraph to be bold.
  2. Style all paragraphs to have a width of 50vw and set the left and right margins to auto

Your result should look something like.

We can style the background of your elements to be an image, solid color or gradient.

Replace the entire contents of index.html with the following

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <style>
          .bold{
                 font-weight: bold;
           }
            
           h2{
                 text-align: center;
                 text-decoration: underline overline solid black;
           }

           p{
                 text-align: justify;
                 width:50vw;
                 margin-left: auto;
                 margin-right: auto;
           }

           section{
                 border-bottom: 5px solid black;
           }

           #section1{
                 height: 80vh;
           }

           #section1::before{
             background-image: url("https://images.unsplash.com/reserve/LJIZlzHgQ7WPSh5KVTCB_Typewriter.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=80");
             background-size: stretch;
             background-position: center;
             content: "";
             position: absolute;
             width:100%;
             height: 100%;
             z-index: -2;
             opacity: 0.3;
           }

          #section2{
             padding: 2em;
             color: white;
             height: 60vh;
             background: rgb(131,58,180);
             background: linear-gradient(90deg, rgba(131,58,180,1) 0%, rgba(29,211,253,1) 50%, rgba(252,176,69,1) 100%);
           }

    </style>
  </head>
  <body>
    <section id="section1">
              <p>
                    Why won't students come to labs? This is something I always ponder. 
                    After unprepared students write exams, They watch their marks in wonder. 
              </p>
               <p>
                   Why won't students come to labs? What is more important than class? 
                   How can you be a student if you don't even try to pass?
               </p> 
                <p>
                   Why won't students come to labs? It happens all the time. 
                   If you came to labs, you'd know what to do. But you would rather duck and go lime.
               </p>
               <p>
                  Why won't students come to labs? I see them all now stressing. 
                  If you only learned this basic programming, You would surely now be zessing.
                </p>
      <!-- This is how html comments are done btw -->
    </section>
    <section id="section2">
         <h1>Section 2</h1>
    </section>
    <script src="script.js"></script>
  </body>
</html>

This should apply an image background to section 1 and a gradient background to section 2.

The selector ‘::' is used to specify pseudo elements. We use the pseudo element "before" to write a background image to the dom before the section 1 element. The styles within the selector is just a recipe we follow for displaying background images while keeping the text readable.

Just like margin, borders of all 4 directions of an element can be set in one line or individually. Some properties for styling borders are as follows:

Task 7

For each section add a solid black bottom border of 5px thick.

You should now have a border separating your sections.

Outlines are drawn outside of an element's border and are used to highlight an element.

Outlines are styled similarly to borders with the following properties and values:

Task 8

Use the :hover pseudo class to add a red double outline 2px thick around any paragraph which is hovered by the mouse.

Solution

p:hover{
        outline: 2px double red;
}

This should create the following effect.

Overflow is used to make content scrollable on a page when it cannot fit into the space allocated for it. Typically we want to avoid nested scrollbars in our application so if elements on a page are scrollable in a direction then the page itself should not be scrollable in the same direction. The overflow properties are as follows:

Overflow properties can have the following values:

Let's resize our sections so that they both fit on the page.

Update the styles in index.html to the following.

<style>
.bold{
  font-weight: bold;
}

h2{
  text-align: center;
  text-decoration: underline overline solid black;
}

p{
  text-align: justify;
  width:50vw;
  margin-left: auto;
  margin-right: auto;
}

section{
  border-bottom: 5px solid black;
  height: 40vh;
  overflow-y:scroll
}

#section1::before{
  background-image: url("https://images.unsplash.com/reserve/LJIZlzHgQ7WPSh5KVTCB_Typewriter.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=80");
  background-size: stretch;
  background-position: center;
  content: "";
  position: absolute;
  width:100%;
  height: 40vh;
  z-index: -2;
  opacity: 0.3;
}

#section2{
  padding: 2em;
  color: white;
  background: rgb(131,58,180);
  background: linear-gradient(90deg, rgba(131,58,180,1) 0%, rgba(29,211,253,1) 50%, rgba(252,176,69,1) 100%);
}

p:hover{
  outline: 2px double red;
}
</style>

Now both sections fit on the page with their individual scrollbars. Notice there is no nested scrolling.

Shadows add depth to the elements on a webpage and come in two properties.

We can use tools like a box shadow generator to generate the styles needed for our desired shadow effect.

Task 10

Add a 400px by 300px div in the center of the second section with a white background colour, 4px border radius, a 5px padding and a box shadow of your choice using the generator. The div should have a heading2 with the text "My Div" in black and centered and no text decoration.

div{
  width: 400px;
  height: 300px;
  margin-left: auto;
  margin-right: auto;
  margin-top: auto;
  margin-bottom: auto;
  background-color: white;
  color: black;
  text-align: center;
  box-shadow:  0 8px 16px 0 rgba(0,0,0,0.2);
  border-radius:4px;
  padding: 5px;
}

div> h2{
  text-decoration: none;
}

Notice we must select the h2 child element and apply a text decoration of none so that it overrides the other h2 style written in task 4.

This should produce the following result.

That's it for your first real dive into css, the next lab will go into things like animations and layout.

Exercise

You are required to add styles to a page to achieve the design below. You are not allowed to change any markup on the page.

How to read the Specifications

The specifications are categorised by test suites and test cases. A test suite specifies a css selector, while a test case specifies a css rule.

Example The follow spec:

Test Suite

Test Cases

All tables

  1. must have a blue background
  2. must have white text

Is satisfied by the following CSS

table {
    color: white;
    background-color: blue;
}


Add styles to the following workspace according to the specification below.

Test Suite

Test Cases

The Body

  1. must be full width
  2. must have no spacing outside of its border in any direction
  3. must have no spacing between its content and border in any direction
  4. must be coloured #fafafa

The Stories Section

  1. Must be 90px tall
  2. Must be as long as 70% of the Viewport
  3. Must have a space of -5px outside the top of the element's border
  4. Must be spaced automatically outside the left of the element's border
  5. Must be spaced automatically outside the right of the element's border
  6. Must have a 1px solid border coloured #b5b5b5;
  7. Must have the corner of its borders curved by 2px
  8. Must have a space of 30px between the element's content and left border.

The User class

  1. Should be as long as 18% the of its parent's width
  2. Should be displayed as an inline block

The paragraph descendants of the user class

  1. Should be centre aligned
  2. Should have no spacing outside of its borders
  3. Should be in "Lucida Sans Unicode" font
  4. Should have a weight of 100
  5. Should have be in smaller font

The post-header class

  1. Should be coloured white
  2. Should be 50px tall
  3. Should be sized according to its border
  4. Should inherit its parents width
  5. Should have a spacing of 5px between its content and border
  6. Should have a 1px solid border on the bottom coloured #b5b5b5

The Navbar

  1. should be displayed as an inline-block
  2. should be coloured white
  3. must be full width
  4. must be 54 pixels tall
  5. must have a solid border below it that is 1 pixel thick with the colour #e5e5e5

The instagran Image

  1. must be 40px tall
  2. must positioned relative to its parent

The search box

  1. must be 25px tall
  2. must be positioned relative to is parent
  3. must be have a width that spans 30% of the viewport width
  4. must be positioned 35vw to the left
  5. must be positioned 35vw to the right
  6. must be coloured #fafafa
  7. must have its text-centred
  8. must have a solid #b5b5b5 coloured 1px thick border rounded with a radius of 3px

The actions image

  1. must be 40px tall
  2. must be positioned relative to is parent
  3. must be positioned 0px to the right of its parent

Test Suite 1 The Image Descendants of the User Class

  1. Should have a 5px space on the top
  2. Should have a rounded border that is 50% of its parent in size
  3. Should have a double border 4px thick and coloured #b5b5b5
  4. Should be 56px wide
  5. Should be 56px tall
  6. Should be displayed as a block
  7. Should have an automatic spacing to the left of the element
  8. Should have an automatic spacing to the right of the element

Test Suite 2 the post class

  1. Should have a solid 1px border #b5b5b5 in colour
  2. Should have a rounded border with a curve of 2px
  3. Should have a height that is 60% of the viewport height
  4. Should have a width that is 560px wide
  5. Should have a 119px spacing to the left of the element
  6. Should have a 119px spacing to the right of the element
  7. Should have a 10px space above the element

Test Suite 3 the post-header class

  1. Should be coloured white
  2. Should be 50px tall
  3. Should be sized according to its border
  4. Should inherit its parents width
  5. Should have a spacing of 5px between its content and border
  6. Should have a 1px solid border on the bottom coloured #b5b5b5

Exercise Workspace

References