It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site.
  2. Sign up if you don't have an account yet.
  3. Or, disable your ad blocker by doing this:
    • Click on the ad blocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

LCC: Web Apps Development Essentials

Course by zooboole,

Last Updated on 2024-09-05 14:50:08

Day 9: CSS Box Model and Layout

Quiz ahead

Welcome to Day 9! In the previous lesson, we introduced CSS and learned how to connect it to our HTML documents and write some basic CSS rules. Today, we will dive into fundamental CSS concepts that are essential for styling web pages: the CSS Box Model, display properties, and positioning.

1. Understanding the Browser's Rendering: Inline and Block Elements

By default, the browser renders HTML elements in two main ways:

- Block Elements: These occupy the full width of their container, causing any following elements to appear below them. Examples include <div>, <h1>, and <p>.

- Inline Elements: These take up only the space required for their content and can sit alongside other inline elements. Examples include <span>, <a>, and <img>.

Knowing how elements behave in their default state helps you understand how to manipulate their layout using CSS.

2. The CSS Box Model

The CSS Box Model is a foundational concept for controlling the space and layout of HTML elements. Every HTML element is treated as a box, and understanding how these boxes work allows you to create more advanced designs.

Each box has four key areas:

- Content: The innermost part of the box, where the text, images, or other content are displayed.

- Padding: The space between the content and the box’s border. Increasing padding will enlarge the box without affecting its position relative to other elements.

- Border: Surrounds the padding and content. You can adjust the border’s thickness, style (solid, dashed, etc.), and color.

- Margin: The space between the border of the box and the outside world (other elements or the edge of the container). Margins do not affect the box’s size but push elements away from each other.

Here’s an example of a box model in action:

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  margin: 10px;
}

In this case, the total width of the box is the width (200px) plus padding (20px on both sides), border (5px on both sides), and margin (10px on both sides).

3. Changing the Display of Elements

CSS allows you to change the way elements are displayed beyond the default inline and block behaviors. Here are some common display values:

* Inline-Block:** Similar to inline, but you can set width and height, which is not possible with purely inline elements. It’s useful when you want elements to sit next to each other but still behave like blocks.

* Grid:** The grid display allows you to create complex, two-dimensional layouts by dividing the space into rows and columns. Grid is perfect for creating structured layouts with precise control over the size and placement of elements.

* Flex:** Flexbox is a layout model that enables responsive design by distributing space dynamically among elements within a container. It’s particularly useful for creating flexible, one-dimensional layouts (either horizontal or vertical).

You can transform an element’s display property easily:

.element {
  display: flex;
}

4. Changing the Flow with CSS Positioning

By default, HTML elements are laid out in a natural vertical flow, where each new element appears below the previous one. This flow can be controlled using CSS positioning. There are several types of positioning:

- Static: The default position; elements are positioned according to the normal document flow.

- Relative: The element is positioned relative to its normal position. You can use top, right, bottom, and left to adjust it from this original spot.

- Absolute: The element is removed from the document flow and positioned relative to its nearest positioned ancestor (not necessarily the parent).

- Fixed: The element is positioned relative to the browser window, and it stays in place even when the page is scrolled.

- Sticky: This is a hybrid between relative and fixed positioning. The element behaves like a relatively positioned element until a certain scroll point, after which it becomes fixed.

Example of setting an absolute position:

.box {
  position: absolute;
  top: 50px;
  left: 100px;
}

Conclusion

Today’s lesson gave you the tools to start manipulating how HTML elements are displayed and positioned on the page. The CSS Box Model gives you control over margins, padding, borders, and content. By changing the display and positioning properties of elements, you can break free from the default flow of the page and create complex, responsive designs.

These concepts are essential for laying the foundation for modern web layouts. Stay tuned for our next lesson, where we’ll explore how to work with flexbox and grid in more detail!

Assignment 9

Objective:
Create a webpage that displays three custom cards side by side, demonstrating your understanding of the CSS Box Model, display properties, and positioning.

Instructions:

1. HTML Structure:

  • Create an HTML document with a <div> container that holds three individual <div> elements representing cards.

  • Each card should contain:

    • An image
    • A heading (<h2>)
    • A paragraph describing the card content
    • A link (<a>) as a "Read More" button

2. CSS Styling:

  • Apply the CSS Box Model (margins, padding, borders) to the cards:

    • Add margin between each card.
    • Add padding inside the cards between the content and the borders.
    • Add a visible border around each card.
  • Use inline-block or flex to display the three cards side by side.

  • Set a background color or gradient for the cards to distinguish them.

  • Adjust the size of the images to fit within the cards using width and height properties.

3. Positioning:

  • Use relative positioning to create an overlapping "NEW" label on one of the cards.
  • Make the "Read More" button stay fixed at the bottom of each card using absolute positioning.
Take Quiz