Comprehensive Study Notes on CSS Box Model, Units, and Grids
Computer Science Student Society (CSSS)
Background and History
The society has been an integral part of the School of Computing and Mathematical Sciences since its establishment in .
It is a student-led organization, managed primarily by undergraduate students who handle all aspects of operations, including event management, organization, budgeting, and strategic decision-making.
Purpose and Membership
The club serves as a social and professional hub for students interested in computer science, data, design, mathematics, and related interdisciplinary fields.
Its primary goals include connecting students with one another and providing opportunities for professional development and industry engagement.
Membership is free, and most events are provided at no cost to students. Any exceptions are advertised in advance.
Activities and Events
Social gatherings such as barbecues, pizza nights, games nights, and quiz nights.
Professional events, most notably the annual Careers Fair.
Traditional events, such as the campus pub crawl, which has been a recurring activity for approximately years.
Competitive events including various design competitions.
Organizational Benefits for Committee Members
Involvement in the club committee requires an average time commitment of approximately per week.
Committee members gain experience in group work, industry networking, event planning, and personnel management.
Such extracurricular involvement is emphasized as a way to diversify a student's profile in an uncertain job market, demonstrating capabilities beyond academic knowledge.
Review of Web Development Fundamentals
CSS Selector Priority
When multiple CSS rules target the same HTML element, the browser determines priority based on the type of selector used.
Highest Priority: The ID selector.
Lowest Priority: The Type selector (in a hierarchy including ID, Attribute, Class, and State).
CSS and HTML Structure
A CSS file consists of a collection of CSS rules. These rules define the syntax and structure applied to the visual representation of a webpage.
An HTML document is composed of a series of tags and attributes.
Root Element: The
<html>element is the root of the HTML document.Child Elements of the Root: The
<html>element contains two primary child elements: the<head>and the<body>elements.
The CSS Box Model
Conceptual Layout
The CSS box model is the layout engine where every HTML element is treated as an invisible rectangular box.
Elements are categorized as either inline or block, and they react differently to CSS styling based on these properties.
Core Properties of the Box Model
Content: The innermost part of the box, containing text, images, forms, or other media.
Padding: The space immediately surrounding the content. It remains inside the boundaries of the element itself.
Border: The edge that surrounds the padding and content. It is also considered part of the element.
Margin: The space outside the border, used to create distance between different elements. It is not considered part of the element itself.
Styling Syntax
Universal setting: One value applied to all sides (e.g.,
padding: 20px;sets top, bottom, left, and right to ).Explicit setting: Targeting specific sides (e.g.,
padding-right: 15px;ormargin-top: 10px;).Visualization: If an element has a background color, it will be visible behind the content and padding, but not the margin.
CSS Units
Absolute Units
Fixed sizes that do not change relative to other elements or the viewport.
Examples include pixels (), points (), inches (), centimeters (), and millimeters ().
Note on Pixels: In modern browsers, a pixel acts as a logical unit. While generally absolute, the browser may scale these units when a user zooms in or out.
Relative Units
Sizes that adjust based on specific contexts such as font size, parent element size, or viewport dimensions.
Percentage (): Relative to the size of the parent element.
EM (): Relative to the font size of the parent element.
REM (): Relative to the font size of the root element (
<html>).Viewport Height (): Relative to the height of the browser window ( is of the height).
Viewport Width (): Relative to the width of the browser window ( is of the width).
Calculation Examples
If the root font size is set to , then a value of equals .
If a parent element has a font size of , a child element set to will have a font size of .
CSS Grids
Overview
CSS Grid is a layout system that allows developers to divide web pages into rows and columns without relying on external frameworks or old table-based layouts.
It enables the creation of complex, responsive designs by defining a parent container as a grid.
Grid Components
Grid Container: The parent element where
display: grid;is applied.Grid Items: The direct children of the grid container.
Grid Lines: The horizontal and vertical lines that divide the grid. They are numbered starting from at the top/left. Negative numbers can be used to count from the bottom/right (starting at ).
Grid Cell: The individual space enclosed by four grid lines.
Gaps: The space between rows or columns, often referred to as gutters.
Grid Implementation and Syntax
Activation: Use
display: grid;on the container.Columns:
grid-template-columnsdefines the width and number of columns (e.g.,400px 400pxcreates two columns of each).Rows:
grid-template-rowsdefines the height and number of rows.Gaps:
grid-gapsets a uniform space for both rows and columns. Specificity can be achieved usingrow-gaporcolumn-gap.
Positioning Items
Items can be moved within the grid using
grid-columnandgrid-rowproperties.Values are defined by start and end line numbers (e.g.,
grid-column: 1 / 3;specifies that an item should span từ grid line to grid line , covering two columns).Moving an item to a new cell will shift existing items within the layout flow automatically.
Questions & Discussion
Question: If an image is transparent, can you see the background color through it when using the box model?
Response: This is a possibility that should be tested practically, but background colors applied to an element typically show through transparent areas of the content.
Question: If there are more elements than defined columns and rows in a grid, what happens?
Response: The grid will automatically create additional rows or columns to accommodate the missing elements that were not explicitly accounted for in the template definitions.