diff --git a/docs/css/box-model/border.mdx b/docs/css/box-model/border.mdx index e345ed2..6607260 100644 --- a/docs/css/box-model/border.mdx +++ b/docs/css/box-model/border.mdx @@ -1 +1,119 @@ - \ No newline at end of file +--- +title: "The border Property" +description: "Learn how the CSS border property defines the style, width, and color of the line separating an element's padding and margin." +keywords: [CSS border, border property, border-style, border-width, border-color, box model, CodeHarborHub] +tags: [CSS border, border property, border-style, border-width, border-color, box model] +sidebar_label: border +--- + +The **`border`** property is the structural line that surrounds an element, situated between the `padding` and the `margin`. It is the visual frame of the element. + +A border requires three components to be visible: a **width**, a **style**, and a **color**. + + +
+ +## The `border` Shorthand + +The most common and efficient way to define a border is using the `border` shorthand property, which accepts the width, style, and color in any order. + +```css title="styles.css" +selector { + border: ; +} +``` + +### Example + +```css title="styles.css" +.highlight-box { + /* 2px wide, solid line, indigo color */ + border: 2px solid #5C6BC0; +} +``` + +## Longhand Properties + +For more granular control, especially when dealing with different styles or colors on specific sides, you can use the longhand properties. + +### 1. `border-width` + +Sets the thickness of the border. Values can be length units (`px`, `em`, `rem`) or keywords (`thin`, `medium`, `thick`). + +| Syntax | Description | +| :--- | :--- | +| `border-width: 5px;` | All four sides are $5\text{px}$. | +| `border-width: 1px 3px;` | Top/Bottom $1\text{px}$, Left/Right $3\text{px}$. | +| `border-top-width: 10px;` | Only the top border is $10\text{px}$. | + +### 2. `border-style` (Mandatory) + +This is the most critical property—if the style is omitted or set to `none`, the border will not be displayed, regardless of its width or color. + +| Style | Description | +| :--- | :--- | +| **`solid`** | A single, continuous line (most common). | +| **`dotted`** | A series of dots. | +| **`dashed`** | A series of short lines. | +| **`double`** | Two parallel solid lines. | +| **`groove`** | Creates a 3D grooved effect. | +| **`ridge`** | Creates a 3D raised effect (opposite of groove). | +| **`none`** | No border (default). | + + +
+ +### 3. `border-color` + +Sets the color of the border, using color names, hex codes, or RGB/HSL values. + +```css title="styles.css" +.special-border { + border-style: double; + border-width: 4px; + border-color: #FFB300; /* Amber */ +} +``` + +## Side-Specific Borders + +You can define all three properties (width, style, and color) for a single side: + +| Property | Description | +| :--- | :--- | +| `border-top` | Shorthand for top width, style, and color. | +| `border-right` | Shorthand for right width, style, and color. | +| `border-bottom` | Shorthand for bottom width, style, and color. | +| `border-left` | Shorthand for left width, style, and color. | + +```css tiutle="styles.css" +/* Example: Border only on the left side */ +.sidebar-note { + border-left: 6px solid #4CAF50; /* Thick green line */ +} +``` + +## Shaping Corners with `border-radius` + +While technically not part of the Box Model layers (width, padding, margin), the **`border-radius`** property is essential for styling the border and corner shapes. It allows you to round the corners of the element's border. + +| Syntax | Description | +| :--- | :--- | +| `border-radius: 10px;` | Rounds all four corners equally. | +| `border-radius: 10px 0;` | Top-Left/Bottom-Right $10\text{px}$, Top-Right/Bottom-Left $0\text{px}$. | + +```css title="styles.css" +.rounded-button { + border: 1px solid #7986CB; + border-radius: 8px; /* Smooth corners */ +} +``` + +## Interactive `border` Demo + +Use the live editor to experiment with different border styles and colors. Note how the border sits between the inner content/padding and the outer margin. + + \ No newline at end of file diff --git a/docs/css/box-model/box-shadow.mdx b/docs/css/box-model/box-shadow.mdx index e345ed2..36dfea2 100644 --- a/docs/css/box-model/box-shadow.mdx +++ b/docs/css/box-model/box-shadow.mdx @@ -1 +1,94 @@ - \ No newline at end of file +--- +title: "The box-shadow Property" +description: "Learn how the CSS box-shadow property adds visual depth and dimension to elements, enhancing UI design without affecting the document layout." +keywords: [CSS box-shadow, shadow effects, UI depth, design effects, box model, CodeHarborHub] +tags: [CSS box-shadow, shadow effects, UI depth, design effects, box model] +sidebar_label: "box-shadow" +--- + +The **`box-shadow`** property allows you to cast one or more drop shadows from the frame of an element. This property is purely visual; it **does not affect the element's size, position, or the flow of surrounding content** in the document layout. + +It is a powerful tool for enhancing the user interface, creating a sense of depth, lifting elements off the page, and indicating interactivity. + + +
+ +## Syntax and Parameters + +The `box-shadow` property can take multiple values, separated by commas, to create complex, stacked shadows. A single shadow is defined by up to six components: + +```css title="styles.css" +box-shadow: [inset] ; +``` + +### 1. `` and `` (Required) + +These define where the shadow falls horizontally and vertically. + + * Positive `offset-x` moves the shadow to the right. + * Positive `offset-y` moves the shadow down. + +### 2. `` (Optional, Recommended) + +The higher the value, the larger and lighter the shadow edge becomes. A value of `0` means the shadow is a sharp, solid shape. + +### 3. `` (Optional) + +A positive value increases the size of the shadow uniformly in all directions, and a negative value shrinks it. + +### 4. `` (Optional, Recommended) + +The color of the shadow. It's common to use `rgba()` to include transparency, making the shadow softer and more realistic (e.g., `rgba(0, 0, 0, 0.3)`). + +### 5. `inset` (Optional) + +If included, the shadow is drawn **inside** the element's border, creating an embossed or depressed effect. + + +
+ +## Common Shadow Recipes + +### 1. Basic Drop Shadow + +A subtle, soft shadow that makes the element appear slightly raised. + +```css title="styles.css" +.card { + /* x: 0, y: 4px, blur: 8px, spread: 0, color: black with 20% opacity */ + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); +} +``` + +### 2. Multi-layered Shadow + +Stacking shadows to create a deeper, more realistic sense of elevation. + +```css title="styles.css" +.elevated { + box-shadow: + /* First, subtle layer for soft general shadow */ + 0 1px 3px rgba(0, 0, 0, 0.12), + /* Second, stronger layer for defined lift */ + 0 1px 2px rgba(0, 0, 0, 0.24); +} +``` + +### 3. Inset Shadow + +Creates a feeling that the element is pressed down or concave, often used for input fields or containers. + +```css title="styles.css" +.pressed-input { + box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15); +} +``` + +### Interactive `box-shadow` Demo + +Use the live editor to change the values for offset, blur, spread, and color, and toggle the `inset` keyword to see how the visual depth of the box is altered without changing its size. + + \ No newline at end of file diff --git a/docs/css/box-model/box-sizing.mdx b/docs/css/box-model/box-sizing.mdx index e345ed2..e1335a9 100644 --- a/docs/css/box-model/box-sizing.mdx +++ b/docs/css/box-model/box-sizing.mdx @@ -1 +1,67 @@ - \ No newline at end of file +--- +title: "The box-sizing Property" +description: "Understand the fundamental box-sizing property, which controls how an element's width and height are calculated in relation to its padding and border." +keywords: [CSS box-sizing, content-box, border-box, box model calculation, responsive design, CodeHarborHub] +tags: [CSS box-sizing, content-box, border-box, box model calculation, responsive design] +sidebar_label: "box-sizing" +--- + +The **`box-sizing`** property is the single most important tool for modern CSS layout. It fundamentally changes the way the browser calculates an element's total width and height based on the values you set. + +Without `box-sizing`, layout can be frustratingly unpredictable because adding padding or a border changes the total size of the element. `box-sizing` provides a solution to this. + +It accepts two main values: `content-box` (the default) and `border-box` (the recommended value). + + +
+ +## 1. `box-sizing: content-box;` (The Default) + +This is the standard, original CSS Box Model behavior. + +* **Calculation:** `width` and `height` properties apply **only** to the **Content Area**. +* **Result:** The element's total size on the page is the sum of its Content size, plus any Padding, plus any Border. + +$ +\text{Total Width} = \text{Content Width} + \text{Padding}_L + \text{Padding}_R + \text{Border}_L + \text{Border}_R +$ + +### Drawbacks + +If you set a container to `width: 50%;` and then add `padding: 10px;`, the element will actually be wider than $50\%$ of its parent, often causing layout breakage or horizontal scrolling. + +## 2. `box-sizing: border-box;` (The Modern Standard) + +This behavior is far more intuitive for layout work. + +* **Calculation:** `width` and `height` properties apply to the **Content Area + Padding + Border**. +* **Result:** Padding and Border are absorbed *into* the declared `width` and `height`. The Content Area shrinks to accommodate them. The element's final size remains exactly the size you declared. + +$$ +\text{Total Width} = \text{Declared Width} +$$ + +### Recommendation + +For virtually all modern web development, it is recommended to set `box-sizing: border-box;` globally using the universal selector: + +```css title="styles.css" +/* This is often the first CSS rule in a modern stylesheet */ +*, *::before, *::after { + box-sizing: border-box; +} +``` + +This ensures that $100\text{px}$ is always $100\text{px}$—including the padding and border—making responsive grid systems much easier to manage. + + +
+ +## Interactive `box-sizing` Demo + +In the demo below, both boxes have the same CSS dimensions (`width: 300px`, `padding: 20px`, `border: 10px`). Observe how the final visual size differs based on the `box-sizing` value. + + \ No newline at end of file diff --git a/docs/css/box-model/css-units/absolute-vs-relative.mdx b/docs/css/box-model/css-units/absolute-vs-relative.mdx index e345ed2..146913a 100644 --- a/docs/css/box-model/css-units/absolute-vs-relative.mdx +++ b/docs/css/box-model/css-units/absolute-vs-relative.mdx @@ -1 +1,78 @@ - \ No newline at end of file +--- +title: "Absolute vs. Relative Units" +description: "Understand the fundamental difference between absolute and relative CSS units and when to use each for optimal responsiveness and accessibility." +keywords: [CSS units, absolute units, relative units, px, em, rem, responsiveness, accessibility, web design] +tags: [CSS units, absolute units, relative units, px, em, rem, responsiveness, accessibility, web design] +sidebar_label: "Absolute vs. Relative" +--- + +When defining the size of any Box Model property (width, margin, padding, font-size, etc.), you need to use a unit of measurement. CSS units are fundamentally categorized into two types: **Absolute** and **Relative**. + +Choosing the right unit is critical for creating responsive, scalable, and accessible web experiences. + + +
+ +## 1. Absolute Units (Fixed) + +Absolute length units resolve to a fixed size, regardless of the size of the viewport, the element's parent, or the user's settings. Once declared, the physical size of the element on the screen is fixed (assuming no zooming). + +The most common absolute unit is the pixel. + +| Unit | Description | Best for... | +| :--- | :--- | :--- | +| **`px`** | **Pixels**. The number of dots on a screen. The most commonly used absolute unit. | Small borders, shadows, or when you need guaranteed pixel-perfect alignment. | +| **`pt`** | **Points**. $1\text{pt}$ is $1/72$ of an inch. Mainly used in print styles. | Print stylesheets (not generally used for screen). | + +### When to use `px`: + +* When the unit must not scale (e.g., a $1\text{px}$ border). +* When a specific size is required and responsiveness is managed by other elements (like `max-width`). + +### Drawbacks of `px`: + +* **Poor Accessibility:** When a user increases the base font size in their browser settings (for better readability), any text sized with `px` will not scale, leading to a poor user experience. + +## 2. Relative Units (Scalable) + +Relative length units define a size that is relative to another length property. This could be the size of the parent element, the root element, or the viewport dimensions. + +Relative units are the foundation of **responsive and accessible design** because they automatically scale when the context changes. + +### Common Relative Units + +| Unit | Description | Relative To... | Best for... | +| :--- | :--- | :--- | :--- | +| **`em`** | **Element M.** Based on the font size of the **parent element**. | Creating component-specific scaling (e.g., button padding relative to its own font size). | +| **`rem`** | **Root EM.** Based on the font size of the **root `` element**. | Typography, margins, and padding. The preferred unit for large-scale font sizing. | +| **`%`** | **Percentage.** Calculated as a percentage of the parent element's size. | Width and height of layout containers. | +| **`vw`** | **Viewport Width.** $1\text{vw}$ is $1\%$ of the viewport width. | Full-screen hero sections, fluid typography, or elements that must scale with the window. | +| **`vh`** | **Viewport Height.** $1\text{vh}$ is $1\%$ of the viewport height. | Elements that must fill the vertical screen space (e.g., `height: 100vh`). | + + +
+ +## Key Concept: `em` vs. `rem` + +The difference between `em` and `rem` is crucial for typography and spacing consistency. + +### `em` (The Cascading Unit) + +If you use `em` for the font size of a child element, and that child is nested several levels deep, the size can become unpredictable because it compounds based on every parent's font size. + +### `rem` (The Stable Unit) + +The `rem` unit solves the cascading problem by *always* basing its value on the font size of the root `` element. If the user changes their browser's base font size, all `rem`-sized elements scale uniformly, maintaining the intended spatial relationships. + +:::tip Accessibility Recommendation +For typography, margins, and padding, **`rem`** is generally the recommended unit because it ensures that all elements scale correctly with user-defined browser font settings, fulfilling WCAG accessibility guidelines. +::: + +### Interactive Unit Demo + +Observe how the size of the "Content Box" changes when you adjust the unit type. Note how `rem` is fixed relative to the root, while `em` scales based on the parent container's font size. + + \ No newline at end of file diff --git a/docs/css/box-model/css-units/units-with-functions.mdx b/docs/css/box-model/css-units/units-with-functions.mdx index e345ed2..ccd2777 100644 --- a/docs/css/box-model/css-units/units-with-functions.mdx +++ b/docs/css/box-model/css-units/units-with-functions.mdx @@ -1 +1,153 @@ - \ No newline at end of file +--- +title: "Units with CSS Functions (calc, min, max, clamp)" +description: "Learn how to use CSS mathematical and comparison functions like calc(), min(), max(), and clamp() to create dynamic, highly flexible dimensions by combining different unit types." +keywords: [CSS calc, CSS min, CSS max, CSS clamp, fluid sizing, hybrid units, responsive functions, CSS math] +tags: [CSS calc, CSS min, CSS max, CSS clamp, fluid sizing, hybrid units, responsive functions, CSS math] +sidebar_label: "Units with Functions" +--- + +While setting dimensions with a single unit (like `200px` or `2rem`) works for fixed layouts, modern responsive design often requires dimensions that are based on a calculation or a comparison between different units (e.g., "be $80\%$ of the screen, but never less than $300\text{px}$"). + +CSS provides several powerful functions to achieve this fluid, hybrid sizing: `calc()`, `min()`, `max()`, and `clamp()`. + + +
+ +## 1. The `calc()` Function + +The `calc()` function allows you to perform basic mathematical operations (addition, subtraction, multiplication, and division) directly in CSS value declarations. + +The primary power of `calc()` is its ability to combine **different unit types** within a single property. + +### Syntax and Operators + +```css title="styles.css" +selector { + property: calc( ); +} +``` + + * **Operators:** `+`, `-`, `*`, `/` + * **Crucial Rule:** Always include **spaces** around the `+` and `-` operators. Failure to do so will cause the expression to be invalid. Spaces are optional for `*` and `/`. + +### Use Cases + +1. **Hybrid Layouts (Fixed + Percentage)** + +This is commonly used to create space for a fixed sidebar while the main content takes up the remaining width. + +```css title="styles.css" +.main-content { + /* Take up the full width minus a 250px sidebar */ + width: calc(100% - 250px); +} +``` + +2. **Viewport-based Sizing with Offset** + +Creating a container that is $100\%$ of the viewport height minus the height of a fixed header. + +```css title="styles.css" +.scrollable-area { + /* Full viewport height minus a 60px header */ + height: calc(100vh - 60px); +} +``` + + +
+ +## 2. The `min()` Function + +The `min()` function accepts two or more comma-separated values and uses the **smallest** (minimum) calculated value as the final dimension. + +### Syntax and Logic + +```css title="styles.css" +selector { + property: min(, , ...); +} +``` + +The browser evaluates all values and picks the one that results in the shortest dimension. + +### Use Cases + +1. **Maximum Fluidity** + +Use `min()` to make an element fluid up to a certain maximum size. + +```css title="styles.css" +.heading { + /* The element will use 80% of the parent width, + BUT will never exceed 1000px wide. */ + width: min(80%, 1000px); +} +``` + +## 3. The `max()` Function + +The `max()` function is the opposite of `min()`. It accepts two or more comma-separated values and uses the **largest** (maximum) calculated value as the final dimension. + +### Syntax and Logic + +```css title="styles.css" +selector { + property: max(, , ...); +} +``` + + +
+ +### Use Cases + +1. **Minimum Fluidity** + +Use `max()` to ensure an element is always large enough for readability, even if a parent container is tiny. + +```css title="styles.css" +.card-title { + /* The font size will be 2.5rem, + BUT will always be AT LEAST 1.5rem, even if the parent font size shrinks. */ + font-size: max(1.5rem, 2.5vw); +} +``` + +## 4. The `clamp()` Function (Best for Fluid Typography) + +The `clamp()` function is a combination of `min()` and `max()`, allowing you to set a flexible middle value constrained by a minimum and maximum limit. It is the perfect tool for **fluid typography**. + +### Syntax + +```css title="styles.css" +property: clamp(, , ); +``` + +1. **``:** The smallest size the property will ever be. +2. **``:** The ideal, fluid size (often a `vw` value). +3. **``:** The largest size the property will ever be. + +### Use Cases + +1. **Fluid Typography with Control** + +This ensures that the text size scales with the viewport width (`4vw`), but it will never shrink below $1\text{rem}$ (readable minimum) and never grow beyond $3\text{rem}$ (design maximum). + +```css title="styles.css" +.fluid-header { + font-size: clamp(1rem, 4vw, 3rem); +} +``` + + +
+ +## Interactive Functions Demo + +Try resizing the browser window to see how the width of the box changes based on the constraints and calculations. + + \ No newline at end of file diff --git a/docs/css/box-model/css-units/viewport-units.mdx b/docs/css/box-model/css-units/viewport-units.mdx index e345ed2..e9a0784 100644 --- a/docs/css/box-model/css-units/viewport-units.mdx +++ b/docs/css/box-model/css-units/viewport-units.mdx @@ -1 +1,69 @@ - \ No newline at end of file +--- +title: "Viewport Units (vw, vh, vmin, vmax)" +description: "Explore CSS viewport units (vw, vh, vmin, vmax) and how they enable elements to scale dynamically with the size of the browser window, forming the basis of fluid layouts." +keywords: [CSS viewport units, vw, vh, vmin, vmax, fluid layout, responsive design, typography scaling] +tags: [CSS viewport units, vw, vh, vmin, vmax, fluid layout, responsive design, typography scaling] +sidebar_label: "Viewport Units" +--- + +**Viewport Units** are a set of relative units that base their measurement on the dimensions of the browser window (the viewport). These units are essential for creating truly **fluid layouts** where elements scale dynamically as the user resizes their screen. + +There are four primary viewport units: `vw`, `vh`, `vmin`, and `vmax`. + + +
+ +## 1. `vw` (Viewport Width) + +The `vw` unit is $1\%$ of the width of the viewport. + +* `1vw` = $1\%$ of the browser window's width. +* `100vw` = $100\%$ of the browser window's width. + +### Use Cases: +* **Full-width Sections:** Ensuring a banner spans the entire width: `width: 100vw;`. +* **Fluid Typography:** Creating font sizes that scale with the browser window, common in hero sections: `font-size: 5vw;`. + +:::warning Horizontal Scroll Alert +Be careful when combining `width: 100vw;` with fixed horizontal padding or margin, as this can cause the total width to exceed $100\%$ of the viewport, leading to unwanted horizontal scrolling. +::: + +## 2. `vh` (Viewport Height) + +The `vh` unit is $1\%$ of the height of the viewport. + +* `1vh` = $1\%$ of the browser window's height. +* `100vh` = $100\%$ of the browser window's height. + +### Use Cases: + +* **Full-height Sections:** Making a hero section or landing page cover the entire screen vertically: `height: 100vh;`. +* **Fixed Scroll Areas:** Creating a sidebar or log area that always uses a specific percentage of the screen height: `max-height: 80vh;`. + +### Note on Mobile Devices: + +On modern mobile browsers, the `100vh` unit sometimes includes the dynamic toolbars (like the address bar). When these toolbars retract, the visible area increases, which can cause layout shifts. To combat this, CSS introduced `svh` (small viewport height) and `lvh` (large viewport height) units for more predictable mobile sizing, though `vh` remains the most widely used unit. + + +
+ +## 3. `vmin` and `vmax` + +These units are based on the smaller or larger dimension of the viewport, respectively. + +| Unit | Description | Calculation | Use Case | +| :--- | :--- | :--- | :--- | +| **`vmin`** | $1\%$ of the **smaller** dimension (width or height). | If width is $1000\text{px}$ and height is $800\text{px}$, $1\text{vmin} = 8\text{px}$. | Ensures an element (like a logo) is always visible, regardless of screen orientation. | +| **`vmax`** | $1\%$ of the **larger** dimension (width or height). | If width is $1000\text{px}$ and height is $800\text{px}$, $1\text{vmax} = 10\text{px}$. | Ensures an element is always large enough to fill the entire screen on either axis. | + +## Interactive Viewport Units Demo + +Resize your browser window (both horizontally and vertically) to observe how the two boxes scale. + +* The **`vw` Box** scales only with the horizontal width. +* The **`vh` Box** scales only with the vertical height. + + \ No newline at end of file diff --git a/docs/css/box-model/height.mdx b/docs/css/box-model/height.mdx index e345ed2..ad8aab9 100644 --- a/docs/css/box-model/height.mdx +++ b/docs/css/box-model/height.mdx @@ -1 +1,83 @@ - \ No newline at end of file +--- +title: "The height Property" +description: "Explore the CSS height property, how it defines the vertical size of an element's content box, and how to use min-height and max-height effectively." +keywords: [CSS height, min-height, max-height, vertical size, viewport units, box model, CodeHarborHub] +tags: [CSS height, min-height, max-height, vertical size, viewport units, box model] +sidebar_label: height +--- + +The **`height`** property controls the vertical dimension of the innermost layer of the Box Model: the **Content Area**. Setting `height` defines the vertical space reserved for the element's content, *before* vertical padding and borders are added (under `box-sizing: content-box`). + + +
+ +## Default Vertical Behavior + +Unlike `width`, which defaults to $100\%$ for block elements, `height` has different default behavior: + +### 1. Default `height: auto` + +By default, the `height` of a block-level element is set to `auto`. This means the element will only be as tall as necessary to contain its content (including any internal padding and border, but excluding margin). + +### 2. The Percentage Problem + +When you set `height` using a percentage value (e.g., `height: 50%;`), this value is calculated relative to the height of its **parent element**. + +* **The Dependency:** For `height: 50%;` to work, the parent element **must** have an explicitly defined `height`. If the parent's height is also `auto`, the percentage will fail to calculate, and the element's height will revert to `auto`. +* This dependency often creates a chain, requiring you to define `height: 100%;` on every ancestor up to the `` and `` tags. + +## Viewport Units for Height + +To solve the percentage dependency problem, especially when you want an element to fill the entire screen vertically, you should use **Viewport Units**. + +| Unit | Description | Usage Example | +| :--- | :--- | :--- | +| **`vh`** | Viewport Height: $1\%$ of the browser window's height. | `height: 100vh;` (Fills the entire screen height). | +| **`svh`** | Small Viewport Height: $1\%$ of the smallest possible viewport height. | Better for mobile, accounts for dynamic browser toolbars. | + +```css title="styles.css" +/* Recommended for a full-screen landing section or hero banner */ +.hero-section { + height: 100vh; +} +``` + + +
+ +## Vertical Constraints: `min-height` and `max-height` + +Using vertical constraints is essential for maintaining design stability and preventing content from spilling outside its container. + +### 1. `min-height` + +This property sets the minimum vertical size an element's content box is allowed to be. + + * **Primary Use:** It is critical for defining the initial size of containers, like a main content area (`min-height: 80vh;`), ensuring the page doesn't look empty even with minimal content. The element can still grow taller if content requires it. + +### 2. `max-height` + +Sets the maximum vertical size of the content box. + + * **Primary Use:** Often used to limit the size of elements that contain variable content, such as modals, dropdown menus, or scrollable logs. If the content exceeds `max-height`, the browser typically adds a vertical scrollbar (if `overflow-y: auto` is set). + +```css title="styles.css" +.content-area { + /* Ensure the area is at least 500px tall */ + min-height: 500px; + /* Allows it to grow taller if necessary */ + + /* Prevent content from growing beyond 800px and set it to scroll */ + max-height: 800px; + overflow-y: auto; +} +``` + +## Interactive `min-height` and `max-height` Demo + +Use the live editor to adjust the `min-height` and `max-height` properties. Then, try adding or deleting text in the HTML panel to see how the blue box reacts to its content constraints. + + \ No newline at end of file diff --git a/docs/css/box-model/introduction.mdx b/docs/css/box-model/introduction.mdx new file mode 100644 index 0000000..938f528 --- /dev/null +++ b/docs/css/box-model/introduction.mdx @@ -0,0 +1,103 @@ +--- +title: "Introduction to the CSS Box Model" +description: "Understand the fundamental CSS Box Model, which describes how every HTML element is represented as a rectangular box composed of content, padding, border, and margin." +keywords: [CSS Box Model, content, padding, border, margin, box-model introduction, web layout, CodeHarborHub] +tags: [CSS Box Model, content, padding, border, margin, box-model introduction, web layout] +sidebar_label: Introduction +--- + +The **CSS Box Model** is the most fundamental concept in web layout and design. It is a set of rules that defines how every HTML element is rendered by the browser as a rectangular box. Understanding the Box Model is crucial because it dictates the total space an element occupies on the page. + +Every element you see on a webpage a paragraph, an image, a button, or a container is drawn as a box with four distinct, concentric layers. + + +
+ +## The Four Layers of the Box Model + +The Box Model consists of four main layers, built outward from the center (the content): + +```mermaid +flowchart TD + subgraph margin_box["Margin"] + subgraph border_box["Border"] + subgraph padding_box["Padding"] + content_box("Content (Text, images, etc.)") + end + end + end + + style margin_box fill:#fee2e2,stroke:#f00,stroke-width:2px,color:#333 + style border_box fill:#ffedd5,stroke:#f97316,stroke-width:2px,color:#333 + style padding_box fill:#dbeafe,stroke:#3b82f6,stroke-width:2px,color:#333 + style content_box fill:#ecfdf5,stroke:#10b981,stroke-width:2px,shape:rectangle,color:#333 + +``` + +### 1. Content + +The innermost area. This is where the **actual content** of the element resides, such as text, images, or child elements. Its size is controlled by the `width` and `height` properties. + +### 2. Padding + +The space between the **Content** and the **Border**. +* It clears an area around the content. +* The background color or image of the element extends into the padding area. +* Its size is controlled by the `padding` property. + +### 3. Border + +A structural line that wraps the **Padding** and **Content**. +* It sits directly around the padding. +* Its appearance (style, width, and color) is controlled by the `border` properties. + +### 4. Margin + +The outermost layer. The space **outside** the Border. +* It clears an area around the element, separating it from other elements. +* The margin area is always transparent. +* Its size is controlled by the `margin` property. + + +
+ +## Calculating Total Element Space (Default) + +The single most important takeaway from the Box Model is how to calculate the **total space** an element consumes on the page. + +By default (`box-sizing: content-box;`), the `width` and `height` CSS properties only control the size of the **Content** area. + +The total space occupied by an element is calculated as follows: + +$ +\text{Total Width} = \text{Margin Left} + \text{Border Left} + \text{Padding Left} + \text{Width} + \text{Padding Right} + \text{Border Right} + \text{Margin Right} +$ + +:::info Total Width Example +If you set an element to `width: 300px;` and then add `padding: 20px;` and `border: 5px solid black;`, the element's total visible width will actually be **$350\text{px}$** ($20\text{px}$ padding left + $5\text{px}$ border left + $300\text{px}$ content + $20\text{px}$ padding right + $5\text{px}$ border right). This is a common source of layout frustration! +::: + +## The `box-sizing` Property + +To solve the frustration of having padding and border increase the total size, CSS introduced the **`box-sizing`** property to change the Box Model's behavior. + +### `box-sizing: content-box;` (Default) + +This is the standard model described above: **Width** only includes the **Content** area. + +### `box-sizing: border-box;` (Recommended) + +This widely-used alternative changes the calculation: **Width** and **Height** now include the **Content, Padding, and Border**. +* If you set `width: 300px;` and `padding: 20px;`, the browser automatically shrinks the content area to $260\text{px}$ to keep the total width exactly $300\text{px}$. + + +
+ +## Interactive Box Model Demo (Default Behavior) + +The demo below illustrates the default `content-box` behavior. The CSS sets the content width to $200\text{px}$. Change the `padding` and `margin` values below and see how the box's size and position change relative to the total space. + + \ No newline at end of file diff --git a/docs/css/box-model/margin.mdx b/docs/css/box-model/margin.mdx index e345ed2..db26ab9 100644 --- a/docs/css/box-model/margin.mdx +++ b/docs/css/box-model/margin.mdx @@ -1 +1,89 @@ - \ No newline at end of file +--- +title: "The margin Property" +description: "Learn how the CSS margin property controls the space outside an element's border, separating it from surrounding elements, and how margin collapse works." +keywords: [CSS margin, margin property, margin collapse, margin auto, spacing, box model, CodeHarborHub] +tags: [CSS margin, margin property, margin collapse, margin auto, spacing, box model] +sidebar_label: margin +--- + +The **`margin`** property is the outermost layer of the CSS Box Model. It controls the transparent space surrounding an element, serving to separate the element from its neighbors. + +Unlike `padding`, margins do **not** extend the element's background color or image; the margin area is always transparent, revealing the parent's or page's background. + + +
+ +## Syntax and Shorthands + +The `margin` property is a shorthand that sets the margin on all four sides (top, right, bottom, left). You can define the margins using 1, 2, 3, or 4 values, or use the individual longhand properties. + +### 1. The `margin` Shorthand + +| Syntax | Description | +| :--- | :--- | +| `margin: 10px;` | Sets all four margins to $10\text{px}$. | +| `margin: 10px 20px;` | Sets top/bottom to $10\text{px}$, and left/right to $20\text{px}$. | +| `margin: 10px 20px 5px;` | Sets top to $10\text{px}$, left/right to $20\text{px}$, and bottom to $5\text{px}$. | +| `margin: 10px 20px 5px 30px;` | Sets margins in **T**op, **R**ight, **B**ottom, **L**eft order (clockwise). | + +### 2. Longhand Properties + +For setting individual sides: + +* `margin-top` +* `margin-right` +* `margin-bottom` +* `margin-left` + +```css title="styles.css" +/* Example using longhand properties */ +.card { + margin-top: 2rem; + margin-left: auto; /* Used for centering (see below) */ + margin-bottom: 0.5rem; +} +``` + +## Key Concept: Margin Collapse + +Margin collapse is a unique behavior in CSS that affects **vertical margins** (top and bottom). When two vertical margins meet, they do not add up; instead, they "collapse," and the resulting space is the **larger** of the two margins. + +Margin collapse occurs in three main scenarios: + +1. **Adjacent Siblings:** The bottom margin of one element collapses with the top margin of the next element. + + * Example: If Element A has `margin-bottom: 20px;` and Element B has `margin-top: 30px;`, the space between them will be $30\text{px}$, not $50\text{px}$. + +2. **Parent and First/Last Child:** If a parent element has no border or padding between its top/bottom edge and its first/last child's margin, the child's margin collapses with the parent's margin. + +3. **Empty Block:** A block element with no content, padding, or border will have its own top and bottom margins collapse. + +:::tip Horizontal Margins Never Collapse +Horizontal margins (left and right) **never** collapse. They are always added together. +::: + + +
+ +## Centering Block Elements with `margin: auto` + +You can horizontally center a block-level element (like a `
`) that has a defined `width` using the `auto` value for the left and right margins. + +When you set `margin-left` and `margin-right` to `auto`, the browser divides the remaining horizontal space equally between them, pushing the element to the center. + +```css title="styles.css" +.center-box { + width: 600px; + /* Top and Bottom margin set to 30px, Left and Right margin set to auto */ + margin: 30px auto; +} +``` + +## Interactive `margin` Demo + +Use the live editor to adjust the margins on the blue box. Note that the total space it occupies expands outward, pushing the surrounding elements away. + + \ No newline at end of file diff --git a/docs/css/box-model/outline.mdx b/docs/css/box-model/outline.mdx index e345ed2..de8f03b 100644 --- a/docs/css/box-model/outline.mdx +++ b/docs/css/box-model/outline.mdx @@ -1 +1,109 @@ - \ No newline at end of file +--- +title: "The outline Property" +description: "Learn about the CSS outline property, which draws a line outside the border of an element, often used for accessibility and visual focus indication." +keywords: [CSS outline, outline property, outline-style, outline-width, outline-color, accessibility, focus state, box model, CodeHarborHub] +tags: [CSS outline, outline property, outline-style, outline-width, outline-color, accessibility, focus state, box model] +sidebar_label: outline +--- + +The **`outline`** property is often confused with `border`, but it serves a fundamentally different purpose and behaves uniquely. An outline is a line drawn **outside** the element's border, designed primarily to help users identify the element that currently has keyboard focus. + +:::info Key Differences from `border`: + +1. **Placement:** The outline is drawn *outside* the `border`, and it does **not** take up space in the document layout. It is drawn on top of other content if necessary. +2. **Layout Impact:** Because the outline does not occupy layout space, it **does not affect the position** of other elements or the size of the Box Model. +3. **Default Use:** Outlines are critical for **accessibility**, as browsers automatically display them on elements like links and form fields when they receive keyboard focus (`:focus` state). + +::: + + +
+ +## The `outline` Shorthand + +Like `border`, the `outline` property is a shorthand that defines the width, style, and color of the outline in a single declaration. + +```css title="styles.css" +selector { + outline: ; +} +``` + +### Example + +```css title="styles.css" +.focus-button:focus { + /* 4px wide, dashed line, blue color */ + outline: 4px dashed #007bff; + /* Add some space between border and outline */ + outline-offset: 2px; +} +``` + +## Longhand Properties + +For more specific control, you can use the individual properties: + +### 1. `outline-width` + +Sets the thickness of the outline. Values are the same as `border-width`. + +### 2. `outline-style` (Mandatory) + +Defines the style of the line. It uses the same values as `border-style` (`solid`, `dotted`, `dashed`, etc.). If this is not set, no outline will appear. + +### 3. `outline-color` + +Sets the color of the outline. + +### 4. `outline-offset` (Unique to Outline) + +This property controls the amount of space between the element's **border** and the start of the **outline**. This gap is transparent. + +```css title="styles.css" +.input-field:focus { + outline-style: solid; + outline-width: 2px; + outline-color: #f44336; /* Red outline */ + outline-offset: 3px; /* Gap of 3px between the border and the outline */ +} +``` + + +
+ +## The Accessibility Warning (`outline: none;`) + +While setting `outline: none;` might seem tempting to remove the default, sometimes ugly, focus ring from buttons and links, **this is generally a bad practice for accessibility.** + +Keyboard users (who cannot use a mouse or trackpad) rely entirely on the focus outline to know which element they are currently interacting with. + +:::warning **Best Practice** +Never use `outline: none;` without providing an alternative visual focus indicator, such as styling the `:focus` state with a custom `border` or a better-styled `outline`. +::: + +```css title="styles.css" +/* BAD Practice - Removes focus indicator completely */ +a:focus { + outline: none; +} + +/* GOOD Practice - Replaces the default outline with a better-looking one */ +a.custom-link:focus { + outline: 2px solid #673ab7; /* Custom purple outline */ + outline-offset: 2px; + border-radius: 4px; /* Optional: adds a nice touch */ +} +``` + + +
+ +## Interactive `outline` Demo + +Click inside the text input box below, then press the Tab key to shift focus to the button. Observe how the outline appears **outside** the border without changing the layout. + + \ No newline at end of file diff --git a/docs/css/box-model/padding.mdx b/docs/css/box-model/padding.mdx index e345ed2..9dc4b3b 100644 --- a/docs/css/box-model/padding.mdx +++ b/docs/css/box-model/padding.mdx @@ -1 +1,82 @@ - \ No newline at end of file +--- +title: "The padding Property" +description: "Learn how the CSS padding property controls the inner space between an element's content and its border, and how it affects the element's background." +keywords: [CSS padding, padding property, inner spacing, content-to-border space, box model, CodeHarborHub] +tags: [CSS padding, padding property, inner spacing, content-to-border space, box model] +sidebar_label: padding +--- + +The **`padding`** property defines the inner space between an element's content and its border. It is the third layer of the CSS Box Model, sitting directly inside the border and surrounding the content area. + +The key distinction from `margin` is that the element's **background color or background image extends into the padding area**, making padding part of the element's visible space. + + +
+ +## Syntax and Shorthands + +Similar to `margin`, `padding` is a shorthand property that allows you to set the padding on all four sides (top, right, bottom, left) using 1, 2, 3, or 4 values, or by using individual longhand properties. + +### 1. The `padding` Shorthand + +| Syntax | Description | +| :--- | :--- | +| `padding: 15px;` | Sets all four sides (top, right, bottom, left) to $15\text{px}$. | +| `padding: 10px 20px;` | Sets top/bottom to $10\text{px}$, and left/right to $20\text{px}$. | +| `padding: 10px 20px 5px;` | Sets top to $10\text{px}$, left/right to $20\text{px}$, and bottom to $5\text{px}$. | +| `padding: 10px 20px 5px 30px;` | Sets padding in **T**op, **R**ight, **B**ottom, **L**eft order (clockwise). | + +### 2. Longhand Properties + +For setting individual sides: + +* `padding-top` +* `padding-right` +* `padding-bottom` +* `padding-left` + +```css title="styles.css" +/* Example using longhand properties */ +.button { + padding-top: 0.5rem; + padding-bottom: 0.5rem; + padding-left: 1.5rem; + padding-right: 1.5rem; +} +``` + +## Padding and Total Size + +As discussed in the introduction to the Box Model, `padding` directly affects the element's total size, unless you use `box-sizing: border-box;`. + +### **Default Behavior (`content-box`)** + +If you use `box-sizing: content-box` (the default), adding padding will **increase the total width and height** of the element. + + * `width: 200px;` + `padding: 20px;` = Total visual width of $240\text{px}$. + +### **Recommended Behavior (`border-box`)** + +If you use `box-sizing: border-box`, adding padding will **reduce the size of the content area** but keep the total element size fixed. + + * `width: 200px;` + `padding: 20px;` = Total visual width remains $200\text{px}$. + +It is almost always recommended to use `border-box` for predictable layout management. + + +
+ +## Practical Use Cases for Padding + +1. **Readability:** Adding horizontal padding to text containers prevents lines of text from running right up against the edge of the screen or container, greatly improving readability. +2. **Touch Targets:** Increasing the padding around interactive elements (like buttons or links) makes the clickable area larger, improving accessibility and usability on touch devices. +3. **Visual Balance:** Padding is essential for creating visual balance and "white space" around content blocks, separating them visually from the border. + +## Interactive `padding` Demo + +Use the live editor to adjust the padding on the blue box. Observe how the content moves inward and the blue background area expands outward, without moving the red margin boundary. + + \ No newline at end of file diff --git a/docs/css/box-model/width.mdx b/docs/css/box-model/width.mdx index e345ed2..6a60b7e 100644 --- a/docs/css/box-model/width.mdx +++ b/docs/css/box-model/width.mdx @@ -1 +1,62 @@ - \ No newline at end of file +--- +title: "The width Property" +description: "Explore the CSS width property, how it defines the size of an element's content box, and the importance of min-width and max-width for responsive design." +keywords: [CSS width, min-width, max-width, content size, block level, responsive width, box model, CodeHarborHub] +tags: [CSS width, min-width, max-width, content size, block level, responsive width, box model] +sidebar_label: width +--- + +The **`width`** property controls the horizontal dimension of the innermost layer of the Box Model: the **Content Area**. By default, the value you set for `width` determines the horizontal space available for the element's content, *before* horizontal padding and borders are added (under `box-sizing: content-box`). + + +
+ +## Block vs. Inline Element Behavior + +The `width` property's behavior depends on the element's default `display` type: + +### 1. Block-Level Elements (`
`, `

`, `

`, etc.) +* By default, block elements are greedy, automatically consuming $100\%$ of the available width of their parent container. +* You can set a fixed `width` (e.g., $400\text{px}$) or a relative `width` (e.g., $50\%$) to constrain them horizontally. + +### 2. Inline Elements (``, ``, etc.) +* Inline elements (like ``) **ignore** explicitly set `width` properties. Their horizontal size is determined solely by the width of their content. +* To apply a specific `width` to an element that typically flows inline, you must change its display type using `display: block` or `display: inline-block`. + +## Responsive Constraints: `min-width` and `max-width` + +While setting a fixed `width` is sometimes necessary, using minimum and maximum constraints is crucial for creating robust, responsive layouts that adapt gracefully to different screen sizes. + +### 1. `max-width` (Essential for Responsiveness) + +This property ensures an element can shrink to fit smaller screens but will not grow beyond a certain size on very large screens. + +* **Syntax:** `max-width: 800px;` or `max-width: 95%;` +* **Benefit:** Setting `width: 100%;` combined with a reasonable `max-width` (e.g., `1200px`) ensures the element is full-width on mobile but remains contained and readable on desktop, preventing overly long lines of text. + +### 2. `min-width` + +Sets the smallest size an element's content box is allowed to be. + +* This is useful for preventing complex content, like tables or navigation items, from shrinking past a point where they become unreadable or cause horizontal overflow. + +```css title="styles.css" +.responsive-container { + width: 90%; /* Start at 90% of parent width */ + max-width: 650px; /* Never exceed 650px */ + min-width: 300px; /* Never shrink below 300px, regardless of viewport size */ + margin: 0 auto; /* Center the element */ +} +``` + + +
+ +## Interactive `width` and `max-width` Demo + +Use the live editor to adjust the `width` and `max-width` properties. Try resizing your browser window to see how these horizontal constraints affect the blue box, especially on a small screen versus a large screen. + + \ No newline at end of file diff --git a/sidebars.ts b/sidebars.ts index 0bf3397..df3024c 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -474,136 +474,6 @@ const sidebars: SidebarsConfig = { }, ], - // css: [ - // { - // type: "category", - // label: "Introduction to CSS", - // link: { - // type: "doc", - // id: "css/introduction/what-is-css", - // }, - // items: [ - // "css/introduction/what-is-css", - // "css/introduction/how-to-add-css-to-html", - // "css/introduction/comments-in-css", - // ], - // }, - - // { - // type: "category", - // label: "Selectors", - // link: { - // type: "doc", - // id: "css/selectors/attribute-selectors", - // }, - // items: [ - // { - // type: "category", - // label: "Simple Selectors", - // items: [ - // "css/selectors/simple-selectors/element-selector", - // "css/selectors/simple-selectors/class-selector", - // "css/selectors/simple-selectors/id-selector", - // "css/selectors/simple-selectors/grouping-selectors", - // "css/selectors/simple-selectors/universal-selector", - // ], - // }, - // { - // type: "category", - // label: "Combinator Selectors", - // items: [ - // "css/selectors/combinator-selectors/descendant-selector", - // "css/selectors/combinator-selectors/child-selector", - // "css/selectors/combinator-selectors/adjacent-sibling-selector", - // "css/selectors/combinator-selectors/general-sibling-selector", - // ], - // }, - // "css/selectors/attribute-selectors", - // "css/selectors/compound-selectors", - // "css/selectors/pseudo-class-selectors", - // "css/selectors/pseudo-elements-selectors", - // ], - // }, - - // { - // type: "category", - // label: "Colors", - // link: { - // type: "doc", - // id: "css/colors/color-names", - // }, - // items: [ - // "css/colors/color-names", - // "css/colors/rgb", - // "css/colors/rgba", - // "css/colors/hex", - // "css/colors/hsl", - // "css/colors/hsla", - // ], - // }, - - // { - // type: "category", - // label: "Box Model", - // link: { - // type: "doc", - // id: "css/box-model/intro", - // }, - // items: [ - // "css/box-model/intro", - // "css/box-model/width-height", - // "css/box-model/min-width-height", - // "css/box-model/max-width-height", - // "css/box-model/margin", - // "css/box-model/margin-callapse", - // "css/box-model/padding", - // "css/box-model/border", - // "css/box-model/border-radius", - // "css/box-model/box-sizing", - // ], - // }, - - // { - // type: "category", - // label: "Backgrounds", - // link: { - // type: "doc", - // id: "css/backgrounds/background-color", - // }, - // items: [ - // "css/backgrounds/background-color", - // { - // type: "category", - // label: "Background Image", - // items: [ - // "css/backgrounds/background-image/background-attachment", - // "css/backgrounds/background-image/background-position", - // "css/backgrounds/background-image/background-repeat", - // "css/backgrounds/background-image/background-size", - // ], - // }, - // ], - // }, - - // { - // type: "category", - // label: "Fonts and Text Properties", - // link: { - // type: "doc", - // id: "css/fonts-and-text-properties/font-size", - // }, - // items: [ - // "css/fonts-and-text-properties/font-size", - // "css/fonts-and-text-properties/font-style", - // "css/fonts-and-text-properties/font-weight", - // "css/fonts-and-text-properties/font-align", - // "css/fonts-and-text-properties/generic-vs-specific-font-families", - // ], - // }, - - // "css/opacity", - // ], - css: [ "css/introduction", { @@ -834,6 +704,7 @@ const sidebars: SidebarsConfig = { ], }, items: [ + "css/box-model/introduction", "css/box-model/margin", "css/box-model/padding", "css/box-model/border",