diff --git a/docs/css/typography/fonts/font-family.mdx b/docs/css/typography/fonts/font-family.mdx index e345ed2..7d59714 100644 --- a/docs/css/typography/fonts/font-family.mdx +++ b/docs/css/typography/fonts/font-family.mdx @@ -1 +1,84 @@ - \ No newline at end of file +--- +title: "The font-family Property" +description: "Learn how to use the CSS font-family property to select fonts for text, create reliable font stacks, and understand the role of generic font families." +keywords: [CSS font-family, font stack, generic font family, typography, web safe fonts, CodeHarborHub] +tags: [CSS font-family, font stack, generic font family, typography, web safe fonts, CodeHarborHub] +sidebar_label: "font-family" +--- + +The CSS **`font-family`** property is the most essential tool for controlling the typeface of your text. It specifies a **priority list** of font names for the browser to use when rendering text on your webpage. + + +
+ +## Defining a Font Stack + +The browser can only display a font if it is installed on the user's computer or if you provide it (we'll cover that in a later lesson). Because you can't guarantee a user has a specific font, you must define a **font stack**. + +A **font stack** is an ordered list of font families separated by commas. The browser checks the list from left to right, using the **first font it finds** on the user's system. + +### The Syntax + +```css title="styles.css" +selector { + font-family: "Font Name 1", "Font Name 2", generic-family; +} +``` + + * **`"Font Name 1"`:** The primary, desired font (e.g., `"Helvetica Neue"`). + * **`"Font Name 2"`:** The fallback font (e.g., `"Arial"`). + * **`generic-family`:** The required final fallback (e.g., `sans-serif`). + +### Quotation Marks + + * Use quotation marks (`""` or `''`) around font names that contain **spaces** (e.g., `"Times New Roman"`). + * Font names that are single words (e.g., `Arial`, `Verdana`) do not strictly need quotes, but using them is harmless. + +## Understanding Generic Font Families + +The **generic font family** is the last and most critical part of your font stack. It's a required safety net that tells the browser what *kind* of font to pick if none of the specific named fonts are found. + +There are five basic generic families: + +| Generic Family | Description | Characteristics | Common Examples | +| :--- | :--- | :--- | :--- | +| **`serif`** | Fonts with small decorative strokes (serifs) at the end of letter stems. | Formal, traditional, high readability for long text. | Times New Roman, Georgia | +| **`sans-serif`** | Fonts *without* serifs (clean lines). | Modern, clean, highly readable on screens. | Arial, Verdana, Helvetica | +| **`monospace`** | All characters have the same width. | Technical, used for code blocks and data tables. | Courier New, Consolas | +| **`cursive`** | Fonts that mimic handwriting or script. | Informal, decorative, low readability. | Comic Sans MS, Brush Script MT | +| **`fantasy`** | Highly decorative or stylized fonts. | Novelty display only, not for body text. | Impact, Papyrus | + +:::tip Always Use a Generic Fallback\! +Your font stack **must** end with a generic font family. This ensures that the user's text will always display using a legible default font, preventing a visual disaster if all your custom fonts fail. +::: + + +
+ +### Example Font Stacks + +```css title="styles.css" +/* Ideal for body text (clean and modern) */ +body { + font-family: "Helvetica Neue", Arial, sans-serif; +} + +/* Ideal for headings (strong and decorative) */ +h1 { + font-family: "Georgia", "Times New Roman", serif; +} + +/* Ideal for code blocks and data displays */ +code { + font-family: "Consolas", "Courier New", monospace; +} +``` + +## Interactive `font-family` Demo + +Use the live editor to test different font stacks. See how the browser applies the first font from the list that is available on your computer, falling back to `sans-serif` if the others are missing. + + \ No newline at end of file diff --git a/docs/css/typography/fonts/font-shorthand.mdx b/docs/css/typography/fonts/font-shorthand.mdx index e345ed2..578714b 100644 --- a/docs/css/typography/fonts/font-shorthand.mdx +++ b/docs/css/typography/fonts/font-shorthand.mdx @@ -1 +1,99 @@ - \ No newline at end of file +--- +title: The font Shorthand Property +description: Learn how to use the font shorthand property to set multiple typography properties (style, weight, size, height, and family) in a single, concise CSS declaration. +keywords: [CSS font shorthand, typography shorthand, concise CSS, font property order, font-weight, font-size, line-height, CodeHarborHub] +tags: [CSS font shorthand, typography shorthand, concise CSS, font property order, font-weight, font-size, line-height, CodeHarborHub] +sidebar_label: font Shorthand +--- + +The **`font`** property is the ultimate shorthand for typography. It allows you to set up to six related font properties in a single, efficient CSS declaration. Using the shorthand dramatically reduces the length of your stylesheet and improves readability. + + +
+ +## The Full `font` Syntax + +When using the `font` shorthand, you must follow a very strict order for the values, and some properties are optional while others are **required**. + +### Required and Optional Order + +The simplified, most common syntax looks like this: + +```css title="font Shorthand Syntax" +font: / ; +``` + +| Property | Required / Optional | Notes | +| :--- | :--- | :--- | +| **`font-style`** | Optional | E.g., `italic`, `normal`. Must come before `font-weight`. | +| **`font-weight`** | Optional | E.g., `bold`, `400`. Must come before `font-size`. | +| **`font-size`** | **Required** | Must be the third value, followed immediately by the line height. | +| **`/ line-height`** | Optional | The `/` is required if you include `line-height`. | +| **`font-family`** | **Required** | Must be the final value. | + +### The Critical Requirement + +The two **required** values, **`font-size`** and **`font-family`**, must always be the last two items in the list (with `font-family` being the absolute last). + +## Practical Examples + +### 1. Simple Body Text + +This example sets the size and the font stack. All other properties default to `normal`. + +```css title="styles.css" +/* Sets size to 1rem and uses the sans-serif font stack */ +body { + font: 1rem sans-serif; +} +``` + +**This is shorthand for:** +`font-size: 1rem;` +`font-family: sans-serif;` +`font-style: normal;` +`font-weight: normal;` + +### 2. Heading with Style and Weight + +This example includes `font-weight` and `font-style` before the required properties. + +```css title="style.css" +/* Italic, Bold, 2.5rem size, using a serif font */ +h1 { + font: italic bold 2.5rem serif; +} +``` + +### 3. Including `line-height` + +To specify `line-height`, you must insert a **forward slash (`/`)** immediately after the `font-size` value. + +```css title="styles.css" +/* Bold, 18px size, line height of 1.6, using a sans-serif stack */ +.article-text { + font: bold 18px / 1.6 "Roboto", Arial, sans-serif; +} +``` + + +
+ +## A Note on Unspecified Values + +When you use the `font` shorthand, any omitted optional properties (like `font-style` or `font-weight`) are **reset to their initial default values** (`normal` or `400`). + +This is important because if you had set a custom `font-weight: 700;` earlier in your stylesheet, and then later used `font: 1rem sans-serif;` (omitting `bold`), the weight would be **reset to `normal` (400)**. + +This is a key difference between shorthand and individual properties. + +## Interactive `font` Shorthand Demo + +Use the live editor to test different combinations. See how omitting `line-height` or `font-style` affects the text, and observe the strict order requirement. + + + +**Challenge:** Remove the `/ 1.8` from the CSS line. The line spacing will instantly snap back to the browser's default, demonstrating that the `line-height` property was successfully reset. \ No newline at end of file diff --git a/docs/css/typography/fonts/font-size.mdx b/docs/css/typography/fonts/font-size.mdx index e345ed2..665c93d 100644 --- a/docs/css/typography/fonts/font-size.mdx +++ b/docs/css/typography/fonts/font-size.mdx @@ -1 +1,67 @@ - \ No newline at end of file +--- +title: "The font-size Property" +description: "Learn how to use the CSS font-size property to control the size of text and understand the critical difference between absolute (px) and relative (em, rem, %) units for creating accessible and responsive layouts." +keywords: [CSS font-size, font units, px vs em vs rem, responsive typography, web accessibility, CodeHarborHub] +tags: [CSS font-size, font units, px vs em vs rem, responsive typography, web accessibility, CodeHarborHub] +sidebar_label: "font-size" +--- + +The CSS **`font-size`** property is used to specify the height of the characters in a font. Choosing the right font size and unit is one of the most critical decisions in web design, directly impacting **readability** and **accessibility**. + + +
+ +## Units for `font-size` + +Font sizes can be set using several different types of units, which fall into two main categories: **absolute** and **relative**. + +### 1. Absolute Units (Fixed) + +Absolute units are fixed measurements that do not change based on any other element or user setting. + +| Unit | Description | Best Use Case | +| :--- | :--- | :--- | +| **`px`** (Pixels) | A fixed number of dots (pixels) on the screen. | Small elements that must be pixel-perfect, such as thin borders or box shadows. **Avoid for font size.** | + +:::danger Accessibility Warning +Avoid using `px` for font sizes on body text. If a user tries to zoom their browser text using system settings, `px`-based fonts will often fail to resize, creating an accessibility barrier. +::: + +### 2. Relative Units (Scalable) + +Relative units are the **best practice** for typography because they allow the text to scale up or down relative to another size, respecting user settings and promoting responsiveness. + +| Unit | Description | Calculation | Best Use Case | +| :--- | :--- | :--- | :--- | +| **`em`** | Relative to the **font size of the parent element**. | $1\text{em} = 1 \times (\text{Parent's Font Size})$ | Component-level scaling (e.g., a button inside a card). | +| **`rem`** | Relative to the **font size of the root element** (``). | $1\text{rem} = 1 \times (\text{Browser's Default Font Size})$ | Global sizing consistency, main typography. **The recommended unit.** | +| **`%`** (Percent) | Relative to the **font size of the parent element**. | $100\% = \text{Parent's Font Size}$ | Similar to `em`, but `%` is often used for explicit scaling. | + + +
+ +## Why `rem` is Recommended + +Using `rem` (Root EM) is the modern standard because it prevents sizing issues caused by complex nesting. + +1. **Browser Control:** By default, $16\text{px}$ is the browser's root font size (i.e., $1\text{rem} = 16\text{px}$). If a user changes their browser's default font size to $20\text{px}$, then $1\text{rem}$ automatically becomes $20\text{px}$ across your entire site. +2. **Predictability:** Unlike `em`, where the size multiplies on every nested parent (the "compounding problem"), `rem` always refers back to the single `` root size, making calculations simple and predictable. + +### Example Scaling with `rem` + +If your site's root size is set to the default $16\text{px}$: + +| Desired Size | `rem` Value | Calculation | +| :--- | :--- | :--- | +| Standard Body Text | `1rem` | $1 \times 16\text{px} = 16\text{px}$ | +| Large Heading | `2.5rem` | $2.5 \times 16\text{px} = 40\text{px}$ | +| Small Caption | `0.875rem` | $0.875 \times 16\text{px} = 14\text{px}$ | + +### Interactive `font-size` Demo + +Use the live editor to change the `font-size` values for both the parent (`.parent-box`) and the child (`.child-text`). Observe how the `em` value scales relative to its parent, while the `rem` value remains constant relative to the root ($16\text{px}$). + + \ No newline at end of file diff --git a/docs/css/typography/fonts/font-style.mdx b/docs/css/typography/fonts/font-style.mdx index e345ed2..07f37c1 100644 --- a/docs/css/typography/fonts/font-style.mdx +++ b/docs/css/typography/fonts/font-style.mdx @@ -1 +1,85 @@ - \ No newline at end of file +--- +title: "The font-style Property" +description: "Learn how to use the CSS font-style property to control the slant and style of text, primarily setting it to normal, italic, or oblique." +keywords: [CSS font-style, italic text, oblique font, typography, font styling, CodeHarborHub] +tags: [CSS font-style, italic text, oblique font, typography, font styling, CodeHarborHub] +sidebar_label: "font-style" +--- + +The CSS **`font-style`** property is used to control the visual slant of the text. It determines whether the text is displayed in a **normal** (upright) style, or in an **italic** or **oblique** style. + + +
+ +## `font-style` Values + +The property accepts three main keywords, which cover virtually all use cases. + +### 1. `normal` + +This is the **default value** for almost all HTML elements. It displays the font face upright, without any slant. + +```css +p { + font-style: normal; /* Ensures paragraphs are not italic, overriding any inherited style. */ +} +``` + +### 2. `italic` + +The `italic` value is used to select the font's true **italic typeface**. + + * True italic fonts are designed specifically with different letter shapes (e.g., the lowercase 'a' often changes to a single-story form). + * If the font family you chose does **not** have an actual italic typeface file available, the browser will often synthesize it by slanting the normal face, but this is less visually appealing than the true italic version. + +```css title="styles.css" +/* Makes text inside elements with the class 'emphasis' use the italic font face */ +.emphasis { + font-style: italic; +} +``` + +:::info HTML Default +Note that the HTML tags `` (emphasis) and `` (idiomatic text) automatically have the browser's default style of `font-style: italic;`. +::: + +### 3. `oblique` + +The `oblique` value is primarily used to tell the browser to simply **slant the normal font face** programmatically. + + * `oblique` does not look for a separate, designed italic typeface; it just artificially skews the upright letters. + * In most modern browsers, if the font family has a true italic face, the browser will often use it even if you specify `oblique`. If no italic face is available, `oblique` is the best instruction for a synthesized slant. + + +```css title="styles.css" +.note { + font-style: oblique; /* Applies a programmatic slant */ +} +``` + + +
+ +## Italic vs. Oblique (The Design Difference) + +While the visual difference can be subtle, the distinction is based on design intent: + +| Style | Design Intent | Implementation | Result | +| :--- | :--- | :--- | :--- | +| **`italic`** | A **separate typeface** created by the font designer. | The browser looks for a font file named something like `MyFont-Italic.woff`. | Superior, authentic typography. | +| **`oblique`** | The **same upright typeface** is digitally skewed by the browser. | The browser calculates a slant (e.g., 10-14 degrees) and applies it to the `MyFont-Regular.woff` file. | Functional, but often less polished. | + +**Best Practice:** Always use **`italic`** unless you specifically know your desired font does not offer a true italic face, or if you need to specify an angle (e.g., `oblique 14deg;` is supported by some browsers). + +:::tip Browser Behavior +In most cases, if you specify `italic` and the font doesn't have a true italic file, the browser will automatically substitute it with the **oblique** version (a simple slant). For practical web design, specifying `italic` is generally the best choice, as it defaults to the best available slanted version. +::: + +### Interactive `font-style` Demo + +In the live editor, try setting the `font-style` property to `normal`, `italic`, and `oblique` for the `.text-box` element to observe the slight variations. + + \ No newline at end of file diff --git a/docs/css/typography/fonts/font-variant.mdx b/docs/css/typography/fonts/font-variant.mdx index e345ed2..a8e661a 100644 --- a/docs/css/typography/fonts/font-variant.mdx +++ b/docs/css/typography/fonts/font-variant.mdx @@ -1 +1,76 @@ - \ No newline at end of file +--- +title: "The font-variant Property" +description: "Learn how to use the CSS font-variant property, particularly for applying small-caps formatting, and understand its role as a shorthand for several advanced font features." +keywords: [CSS font-variant, small-caps, typography, font features, CodeHarborHub] +tags: [CSS font-variant, small-caps, typography, font features, CodeHarborHub] +sidebar_label: font-variant +--- + +The CSS **`font-variant`** property controls how the browser displays the specialized glyphs (character shapes) defined within a font. While it can control many advanced typographical features, its most common use is to set text to **small-caps**. + + +
+ +## The Primary Use: Small-Caps + +The `small-caps` value is the classic application of `font-variant`. It renders text using capital letters, but the lowercase letters are converted to capital letters that are slightly smaller than the original uppercase letters. + +### The Syntax + +```css +selector { + font-variant: small-caps; +} +``` + +### True Small-Caps vs. Faux Small-Caps + +It is important to know the difference between the two types of small caps: + +1. **True Small-Caps:** The font file itself contains special, custom-designed small capital letters that maintain the weight and clarity of the original font. This is the ideal rendering. +2. **Faux Small-Caps:** If the font doesn't have true small-caps, the browser will mechanically shrink the standard capital letters. These often look visually thin and inconsistent compared to true small-caps. + +:::tip Font Requirement +To ensure the best visual quality, your chosen font must specifically contain the small-caps glyphs. If the font does not support true small-caps, the browser will create a faux version. +::: + +## Advanced `font-variant` Shorthand + +The `font-variant` property is actually a **shorthand** for a group of more specific, advanced properties known as **`font-variant-*`** properties (e.g., `font-variant-caps`, `font-variant-numeric`). + +For standard web development, setting the property to **`normal`** or **`small-caps`** is usually sufficient. + +### `font-variant` Values + +| Value | Description | Notes | +| :--- | :--- | :--- | +| **`normal`** | The default. Disables any special font features (like small-caps). | Recommended when overriding a previous rule. | +| **`small-caps`** | Enables the small-caps feature. | The most common and visible effect. | +| **`inherit`** | Inherits the `font-variant` setting from its parent element. | | + +### Example using `normal` + +If a parent element sets `small-caps`, you can easily reset it for a child element: + +```css +.caption { + font-variant: small-caps; +} + +.caption a { + /* Resets the small-caps back to standard text for links inside the caption */ + font-variant: normal; +} +``` + + +
+ +### Interactive `font-variant` Demo + +Use the live editor to switch the `font-variant` value between `normal` and `small-caps`. + + \ No newline at end of file diff --git a/docs/css/typography/fonts/google-fonts.mdx b/docs/css/typography/fonts/google-fonts.mdx index e345ed2..018abd4 100644 --- a/docs/css/typography/fonts/google-fonts.mdx +++ b/docs/css/typography/fonts/google-fonts.mdx @@ -1 +1,98 @@ - \ No newline at end of file +--- +title: "Integrating Google Fonts" +description: "Learn the three-step process for using Google Fonts to enhance typography on your website selecting the font, linking it in HTML, and applying it with CSS." +keywords: [Google Fonts, web fonts, font linking, web typography, custom fonts, CodeHarborHub] +tags: [Google Fonts, web fonts, font linking, web typography, custom fonts, CodeHarborHub] +sidebar_label: "Google Fonts" +--- + +Up until now, we've focused on **web-safe fonts** (fonts assumed to be installed on the user's computer). However, most modern designs require unique, custom typography. + +**Google Fonts** is the easiest and most popular service for integrating thousands of high-quality, free, custom fonts onto your website. These are called **Web Fonts**. + +The process requires three simple steps: **Select, Link/Import, and Apply.** + + +
+ +## Step 1: Selecting the Font + +Navigate to the Google Fonts website and choose the desired font (e.g., **Roboto** or **Poppins**). + +1. **Select Styles:** Find the font you want to use. You must select every weight and style you intend to use (e.g., Regular 400, Bold 700, Italic 400). +2. **Generate Embed Code:** Google will generate the code snippet necessary to load these styles. + +## Step 2: Linking or Importing the Font + +There are two primary ways to load the font files from Google's servers into your project. + +### Method A: Linking via HTML (Recommended) + +This is the fastest and most efficient method. You place the generated `` tags inside the `` section of your HTML document. + +```html title="index.html" showLineNumbers + + + Google Fonts Demo + + + + + + + +``` + + * **`wght@400;700`**: This URL parameter ensures only the Regular (400) and Bold (700) font weights are downloaded, keeping the file size small. + +### Method B: Importing via CSS (`@import`) + +You can place the `@import` rule at the very top of your primary CSS file, **before** any other styles. + +```css title="styles.css" + +/* Must be the very first line of code! */ +@import url('[https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap](https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap)'); + +/* All your CSS rules follow */ +body { + margin: 0; +} +``` + +:::info Linking vs. Importing +**Linking (HTML)** is generally preferred because it allows the browser to download the font file concurrently with your main CSS, improving initial page load time. +::: + + +
+ +## Step 3: Applying the Font with `font-family` + +Once the font is linked/imported, you can use the specified font name in your CSS `font-family` declaration, just as you would any other font. + +You **must** still include a fallback stack ending in a **generic font family**. + +```css +/* Apply Roboto to the entire body */ +body { + /* Roboto is the primary choice */ + font-family: 'Roboto', Arial, sans-serif; +} + +/* Apply a different font (e.g., Poppins) to headings */ +h1, h2 { + /* Poppins is the primary choice */ + font-family: 'Poppins', serif; + font-weight: 700; /* Must use the weight you specifically linked/imported */ +} +``` + +## Interactive Google Fonts Demo + +Since we cannot link to external files in a live MDX block, this demo simulates the final step (Step 3) where the font is applied using the name provided by the Google Fonts service. + + \ No newline at end of file diff --git a/docs/css/typography/text-style/color.mdx b/docs/css/typography/text-style/color.mdx index e345ed2..3af8aa7 100644 --- a/docs/css/typography/text-style/color.mdx +++ b/docs/css/typography/text-style/color.mdx @@ -1 +1,147 @@ - \ No newline at end of file +--- +title: The color Property +description: "Learn how to use the CSS color property to set the foreground color of text, and explore the four primary ways to define color values in CSS: keywords, HEX codes, RGB/RGBA, and HSL/HSLA." +keywords: [CSS color property, color values, HEX code, RGB, RGBA, HSL, HSLA, web design colors, CodeHarborHub] +tags: [CSS color property, color values, HEX code, RGB, RGBA, HSL, HSLA, web design colors, CodeHarborHub] +sidebar_label: color +--- + +The CSS **`color`** property is one of the most fundamental properties in web design. It specifies the **foreground color** of an element's content, primarily the color of the text and text decorations (like underlines). + +It is important to remember that `color` controls the **text color**, while `background-color` controls the color of the element's box area. + + +
+ +## The Syntax + +The `color` property can accept a wide variety of color values. + +```css title="styles.css" +selector { + color: value; /* e.g., color: blue; */ +} +``` + +## Four Ways to Define Color Values + +CSS supports four primary methods for defining a color, giving you flexibility from simple keywords to precise numerical definitions. + +### 1. Color Keywords (Easiest) + +Color keywords are plain English words that represent a specific color value. They are the simplest to use but offer limited choice. + +| Type | Example | Usage | +| :--- | :--- | :--- | +| **Basic** | `red`, `blue`, `black`, `white` | Quick and easy, but limited to about 140 named colors. | +| **Special** | `transparent` | Makes the element completely see-through. | +| **New (Modern)** | `rebeccapurple`, `aquamarine` | Includes more modern named colors. | + +```css title="styles.css" +p { + color: darkslategrey; +} +``` + +### 2. HEX Codes (Most Common) + +**HEXadecimal** codes are the most widely used format for defining colors. They are six-digit codes preceded by a hash symbol (`#`), representing the amount of Red, Green, and Blue light. + +The code structure is `#RRGGBB`, where each pair of characters ranges from `00` (zero intensity) to `FF` (maximum intensity). + +:::info Understanding HEX Digits (0 1 2 3 4 5 6 7 8 9 A B C D E F) + +HEX uses **base-16**, meaning: + +- `0` is the lowest value +- `F` is the highest +- Each step increases brightness + +**Mapping HEX $\rightarrow$ Decimal $\rightarrow$ Intensity** + +- `0` $\rightarrow$ 0 $\rightarrow$ no light +- `3` $\rightarrow$ 51 $\rightarrow$ low light +- `7` $\rightarrow$ 119 $\rightarrow$ mid-range +- `B` $\rightarrow$ 187 $\rightarrow$ bright +- `F` $\rightarrow$ 255 $\rightarrow$ full brightness + +This is why `#F00` looks bright red (maximum red, no green/blue). +::: + +| HEX Code | RRGGBB Breakdown | Color | +| :--- | :--- | :--- | +| `#FF0000` | Red max, Green zero, Blue zero | Pure Red | +| `#3366CC` | Common color for web use | Medium Blue/Purple | +| `#000000` | All zero | Black | +| `#FFFFFF` | All max | White | + +:::tip Shorthand HEX +If the pairs of digits are identical (e.g., `#FFCC99` becomes `#FC9`), you can shorten the code to three digits (e.g., `#333` is shorthand for `#333333`). +::: + +```css title="style.css" +h1 { + color: #007bff; /* A standard blue */ +} +``` + + +
+ +### 3. RGB and RGBA (Red, Green, Blue, Alpha) + +**RGB** functions define color using three numerical values from 0 to 255 for Red, Green, and Blue. + +The **RGBA** format adds a fourth value: **Alpha ($\alpha$)**, which controls the color's **opacity** (transparency). + +| Format | Syntax | Description | +| :--- | :--- | :--- | +| **`rgb()`** | `rgb(R, G, B)` | Defines color using 0-255 values for each channel. | +| **`rgba()`** | `rgba(R, G, B, $\alpha$)` | Defines color + opacity (Alpha) from 0.0 (fully transparent) to 1.0 (fully opaque). | + +```css title="style.css" +.card-title { + color: rgb(255, 165, 0); /* Orange */ +} + +.subtext { + /* 50% opacity, allowing the background to show through */ + color: rgba(0, 0, 0, 0.5); +} +``` + +### 4. HSL and HSLA (Hue, Saturation, Lightness, Alpha) + +**HSL** is often preferred by designers because it is more intuitive than RGB. It defines a color based on the color wheel. + +| Component | Range | Description | +| :--- | :--- | :--- | +| **Hue (H)** | 0 to 360 | The position on the color wheel (0=red, 120=green, 240=blue). | +| **Saturation (S)** | 0% to 100% | The intensity or purity of the color (100% is vibrant). | +| **Lightness (L)** | 0% to 100% | How light or dark the color is (0% is black, 100% is white). | + +Like RGBA, **HSLA** adds an Alpha channel for opacity. + +```css title="style.css" +.highlight { + /* HSL: Blue with 100% saturation and 50% lightness */ + color: hsl(240, 100%, 50%); +} + +.lowlight { + /* HSLA: The same blue but 75% transparent */ + color: hsla(240, 100%, 50%, 0.25); +} +``` + + +
+ +## Interactive `color` Demo + +Use the live editor to change the text color using different formats: keyword, HEX, RGB, and HSLA. + + \ No newline at end of file diff --git a/docs/css/typography/text-style/direction.mdx b/docs/css/typography/text-style/direction.mdx index e345ed2..bd3f018 100644 --- a/docs/css/typography/text-style/direction.mdx +++ b/docs/css/typography/text-style/direction.mdx @@ -1 +1,85 @@ - \ No newline at end of file +--- +title: The direction Property +description: "Learn how to use the CSS direction property to control the base direction of text, affecting horizontal flow, alignment, and column layout for different languages." +keywords: [CSS direction property, RTL, LTR, Right-to-Left, Left-to-Right, bidirectional text, internationalization, CodeHarborHub] +tags: [CSS direction property, RTL, LTR, Right-to-Left, Left-to-Right, bidirectional text, internationalization, CodeHarborHub] +sidebar_label: direction +--- + +The CSS **`direction`** property is essential for internationalization, as it controls the base direction of block-level content and the direction of inline text, table columns, and horizontal overflow. + +This property is most important for handling languages that are read from **Right-to-Left (RTL)**, such as Arabic, Hebrew, and Persian. + + +
+ +## Syntax and Values + +The `direction` property accepts two main keyword values, representing the dominant writing system flow. + +| Value | Description | Use Case | +| :--- | :--- | :--- | +| **`ltr`** | **Left-to-Right** | The default reading direction for Latin, English, and most European languages. | +| **`rtl`** | **Right-to-Left** | Required for languages like Arabic, Hebrew, and Farsi. | + +### Example + +To set an entire page for a right-to-left language, you would typically apply the property to the `body` element: + +```css title="styles.css" +/* Sets the base direction for the entire page */ +body { + direction: rtl; +} + +/* Specific elements can override the base direction */ +.code-block { + direction: ltr; /* Ensures code blocks are still read LTR */ +} +``` + +## What the `direction` Property Affects + +Changing the `direction` property does more than just reverse the words; it fundamentally changes the structure of the element: + +1. **Text Flow:** Text starts on the right and flows to the left. +2. **Horizontal Alignment:** The default alignment for text and inline elements becomes right-aligned. +3. **Layout:** Items in a flex or grid layout will typically start from the right side of the container. +4. **Scroll Bars:** In some cases, the scroll bar position or scroll direction may be reversed. + + +
+ +## The Role of `unicode-bidi` + +When dealing with mixed directional text (like an English URL or number inserted into an Arabic sentence), the browser needs a rule to handle the conflict. This is where the **`unicode-bidi`** property comes in. + +It is used in conjunction with `direction` to implement the Unicode bidirectional algorithm, which manages text that contains both LTR and RTL characters. + +### Common `unicode-bidi` Values + +| Value | Description | +| :--- | :--- | +| **`normal`** | The default setting. The browser uses the standard Unicode algorithm to figure out the direction automatically. | +| **`embed`** | Explicitly overrides the text direction for the element and forces the text flow defined by the `direction` property. | +| **`bidi-override`** | Completely ignores the intrinsic directionality of the characters and forces the text to flow according to the `direction` property. | + +In most modern contexts, the default `unicode-bidi: normal` works well, but if you are forcing a direction, you might use: + +```css title="styles.css" +.rtl-text { + direction: rtl; + unicode-bidi: embed; +} +``` + +## Interactive `direction` Demo + +Use the live editor to change the `direction` value between `ltr` and `rtl`. Observe how the text flow, the position of the scrollbar (if visible), and the alignment of the text are affected. + + + +**Challenge:** Set `direction: rtl;` in the CSS. Then, observe how the text still flows RTL, even if you explicitly set `text-align: left;`. The direction property is more structural than simple text alignment. \ No newline at end of file diff --git a/docs/css/typography/text-style/line-height.mdx b/docs/css/typography/text-style/line-height.mdx index e345ed2..2531ea4 100644 --- a/docs/css/typography/text-style/line-height.mdx +++ b/docs/css/typography/text-style/line-height.mdx @@ -1 +1,74 @@ - \ No newline at end of file +--- +title: "The line-height Property" +description: "Master the CSS line-height property to control the vertical spacing between lines of text (leading), and learn why the unitless value is essential for readable and accessible typography." +keywords: [CSS line-height, leading, vertical text spacing, unitless line height, typography readability, CodeHarborHub] +tags: [CSS line-height, leading, vertical text spacing, unitless line height, typography readability] +sidebar_label: "line-height" +--- + +The CSS **`line-height`** property controls the amount of vertical space between the baselines of successive lines of text. This spacing, known as **leading** in traditional typography, is critical for text **readability**. Too little space makes text dense and hard to follow; too much space breaks the visual flow. + + +
+ +## Syntax and Values + +The `line-height` property accepts several types of values, but one is strongly preferred for best practice. + +### 1. Unitless Number (Recommended) + +This is the **most flexible and recommended** approach. It is specified as a simple number (e.g., `1.5`). + +* **How it works:** The final line height is the element's `font-size` multiplied by this number. +* **Advantage:** This value is **inherited** by child elements as a *multiplier*, not a fixed value, ensuring that line height always scales proportionally if the child element's font size changes. + +```css title="styles.css" +/* If body font-size is 16px, line-height is 16px * 1.5 = 24px */ +body { + line-height: 1.5; +} + +/* If a child h1 has a font-size of 32px, its line-height is 32px * 1.5 = 48px */ +h1 { + font-size: 2rem; /* 32px */ +} +``` + +### 2. Length (Fixed) + +A fixed value using units like `px`, `em`, or `rem`. + + * **How it works:** Sets the line height to that exact measurement. + * **Disadvantage:** **Avoid using fixed units on the `body` tag.** If you set `body { line-height: 24px; }` and later set an `h1` to a much larger font size (e.g., $40\text{px}$), the fixed $24\text{px}$ line height will cause the text to overlap. + +### 3. Percentage (%) + +A percentage of the element's own `font-size`. + + * **How it works:** `150%` is the same as the unitless value `1.5`. + * **Disadvantage:** Similar to fixed units, a percentage value is **calculated once** and then inherited as a fixed length by children, which can lead to scaling issues. + + +
+ +## Best Practice: Why Unitless Wins Accessibility + +The unitless value (e.g., `1.5`) is the golden standard because it directly supports **accessibility and responsiveness**. + +When you use: `body { line-height: 1.5; }`, the browser understands: "The line height should always be 1.5 times the current font size." + +If a user zooms in or overrides the font size to make text larger, the line height scales with the text size automatically, maintaining comfortable reading space. Fixed units (`px`) break this behavior. + +### Recommended Ratios + + * **Body Text:** `1.4` to `1.6` + * **Headings:** `1.0` to `1.3` (Headings generally require less vertical space because they are shorter). + +### Interactive `line-height` Demo + +Use the live editor to adjust the `line-height` value. Observe how a unitless number scales proportionally with the font size. + + \ No newline at end of file diff --git a/docs/css/typography/text-style/text-alignment.mdx b/docs/css/typography/text-style/text-alignment.mdx index e345ed2..e748e59 100644 --- a/docs/css/typography/text-style/text-alignment.mdx +++ b/docs/css/typography/text-style/text-alignment.mdx @@ -1 +1,74 @@ - \ No newline at end of file +--- +title: "The text-align Property" +description: "Learn how to use the CSS text-align property to control the horizontal alignment of inline content (text, images, spans) within a block-level container." +keywords: [CSS text-align, text alignment, center text, justify text, typography, horizontal alignment, CodeHarborHub] +tags: [CSS text-align, text alignment, center text, justify text, typography, horizontal alignment] +sidebar_label: "text-align" +--- + +The CSS **`text-align`** property is used to set the horizontal alignment of **inline-level content** (like text, images, and `` elements) within its **block-level container** (like `
`, `p`, or `h1`). + +This property controls where the content sits horizontally within the box: left, right, center, or justified. + + +
+ +## Syntax and Values + +The `text-align` property accepts several keyword values. You apply the property to the **parent container**, and it affects the inline content inside it. + +| Value | Description | Typical Use Case | +| :--- | :--- | :--- | +| **`left`** | Aligns text to the **left** edge of the container. This is the default setting for Left-to-Right (LTR) languages. | Standard body text. | +| **`right`** | Aligns text to the **right** edge of the container. | Prices, dates, or right-to-left (RTL) languages. | +| **`center`** | Centers the text line by line within the container. | Headings, titles, and captions. | +| **`justify`** | Stretches the lines of text so that both the left and right edges are flush with the container. | Magazine or newspaper columns. | + +### Example + +To center a heading, you apply `text-align: center;` to the heading itself: + +```css title="styles.css" +/* Centers the text inside the h1 container */ +h1 { + text-align: center; +} + +/* Justifies the body content for a more formal look */ +.article-body { + text-align: justify; +} +``` + +## Important Distinctions + +### 1. Affects Inline Content Only + +`text-align` only affects **inline** content. It will **not** move a block-level element itself (like an entire `
`). + + * **To move a block-level element horizontally:** Use `margin: 0 auto;` (for centering) or layout properties like `float` or Flexbox. + +### 2. The `justify` Caveat + +When using `text-align: justify;`, the browser adds space between words to make lines flush. This is generally great for large blocks of text, but if the last line of a paragraph is very short, it can be stretched awkwardly across the entire width. + +### 3. Start vs. End (Internationalization) + +In modern CSS, `start` and `end` are often preferred over `left` and `right` for better support of different writing systems: + + * **`text-align: start;`**: Aligns content to the reading side (left for English, right for Arabic). + * **`text-align: end;`**: Aligns content to the non-reading side (right for English, left for Arabic). + +Unless you are explicitly building an RTL layout, using `left`, `right`, and `center` is sufficient. + + +
+ +### Interactive `text-align` Demo + +Use the live editor to change the `text-align` value in the CSS and see how the horizontal position of the text changes instantly. + + \ No newline at end of file diff --git a/docs/css/typography/text-style/text-decoration.mdx b/docs/css/typography/text-style/text-decoration.mdx index e345ed2..1da0c74 100644 --- a/docs/css/typography/text-style/text-decoration.mdx +++ b/docs/css/typography/text-style/text-decoration.mdx @@ -1 +1,101 @@ - \ No newline at end of file +--- +title: "The text-decoration Property" +description: "Learn how to use the CSS text-decoration property to add or remove visual lines on text, controlling their style, color, and thickness, especially for links." +keywords: [CSS text-decoration, underline, overline, line-through, text-decoration shorthand, link styling, CodeHarborHub] +tags: [CSS text-decoration, underline, overline, line-through, text-decoration shorthand, link styling] +sidebar_label: "text-decoration" +--- + +The CSS **`text-decoration`** property is a powerful shorthand that allows you to add or remove various visual lines on text, such as underlines, overlines, and strikethroughs. + +Its most common use is to **remove the default underline** from anchor tags (``) and links. + + +
+ +## The `text-decoration` Shorthand + +The `text-decoration` property can be used as a shorthand to define three aspects of the line: **line type**, **color**, and **style/thickness**. + +### The Syntax + +```css title="styles.css" +/* Shorthand Order: [line] [color] [style] [thickness] */ +selector { + text-decoration: underline red wavy 2px; +} +``` + +### 1. `text-decoration-line` (The Type) + +This is the most essential value and is often used alone. + +| Value | Description | +| :--- | :--- | +| **`none`** | **Removes all decoration.** Crucial for styling links (`a { text-decoration: none; }`). | +| **`underline`** | Draws a line **under** the text. | +| **`overline`** | Draws a line **over** the text. | +| **`line-through`** | Draws a line **through** the text (strikethrough). | + +### 2. `text-decoration-color` + +Sets the color of the decoration line. By default, it inherits the text color set by the `color` property. + +```css title="styles.css" +.danger-link { + color: red; /* Text color */ + /* Line color is set explicitly to black, distinct from the red text */ + text-decoration-color: black; +} +``` + +### 3. `text-decoration-style` + +Sets the pattern of the line. + +| Value | Description | +| :--- | :--- | +| **`solid`** | A straight, unbroken line (the default). | +| **`double`** | Two parallel lines. | +| **`dotted`** | A series of dots. | +| **`dashed`** | A series of short dashes. | +| **`wavy`** | A wavy or squiggly line. | + +### 4. `text-decoration-thickness` + +Sets the width (thickness) of the line, using any valid length unit (e.g., `px`, `em`, `rem`). + + +
+ +## Best Practice: Styling Links + +The default browser behavior is to render all anchor tags (`
`) with an underline. This is a long-standing web convention that tells users an element is clickable. + +A common design pattern is to **remove the default underline** and then add a subtle effect on hover. + +```css title="styles.css" +/* 1. Remove the default underline */ +a { + text-decoration: none; + color: #007bff; /* Set the text color */ +} + +/* 2. Add a visual cue on hover */ +a:hover { + /* Use the shorthand to add a subtle, colored line only on hover */ + text-decoration: underline 2px solid #007bff; + cursor: pointer; +} +``` + +### Interactive `text-decoration` Demo + +Use the live editor to test different line types and styles using the shorthand property. + + + +**Challenge:** In the CSS, change the decoration to `underline #1e90ff wavy 3px` and hover over the text to see the different style and thickness. \ No newline at end of file diff --git a/docs/css/typography/text-style/text-shadow.mdx b/docs/css/typography/text-style/text-shadow.mdx index e345ed2..9d6a85d 100644 --- a/docs/css/typography/text-style/text-shadow.mdx +++ b/docs/css/typography/text-style/text-shadow.mdx @@ -1 +1,105 @@ - \ No newline at end of file +--- +title: "The text-shadow Property" +description: "Learn how to use the CSS text-shadow property to apply a subtle or dramatic shadow effect to text, controlling its offset, blur radius, and color." +keywords: [CSS text-shadow, text effects, drop shadow, text blur, typography, visual depth, CodeHarborHub] +tags: [CSS text-shadow, text effects, drop shadow, text blur, typography, visual depth] +sidebar_label: "text-shadow" +--- + +The CSS **`text-shadow`** property allows you to add a shadow effect behind text. This is used to increase readability against complex backgrounds, add visual depth, or create unique decorative effects. + +The property is highly versatile, accepting multiple shadow definitions separated by commas. + + +
+ +## The `text-shadow` Syntax + +A single shadow definition requires at least **three values**: the horizontal offset, the vertical offset, and the shadow color. A fourth, optional value defines the blur radius. + +### The Order of Values + +```css title="styles.css" +/* text-shadow: [horizontal-offset] [vertical-offset] [blur-radius (optional)] [color]; */ + +selector { + text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5); +} +``` + +| Value | Required / Optional | Description | +| :--- | :--- | :--- | +| **Horizontal Offset** | Required | Positive value moves the shadow right. Negative moves it left. | +| **Vertical Offset** | Required | Positive value moves the shadow down. Negative moves it up. | +| **Blur Radius** | Optional | The size of the blur. A value of `0` means a sharp, solid shadow. Larger values mean a softer, more diffused shadow. | +| **Color** | Required | The color of the shadow (can use HEX, RGB, or any color format). | + +### Example + +```css title="styles.css" +h1 { + color: white; /* Text color */ + /* Shadow: 3px right, 3px down, 2px blur, black */ + text-shadow: 3px 3px 2px black; +} +``` + + +
+ +## Practical Applications + +### 1. Readability Enhancement (Subtle Shadow) + +A tiny, subtle shadow can greatly improve text readability, especially over images or busy patterns. Notice the low offset and high blur. + +```css title="styles.css" +/* Soft, diffused shadow for contrast over images */ +.banner-text { + text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.9); +} +``` + +### 2. Creating an Outline Effect (Sharp Shadow) + +By setting the blur radius to `0` and using offsets around the text, you can simulate an outline (though the standard `text-stroke` or `text-decoration` may be better for true outlines). + +```css title="styles.css" +/* Text outline effect (no blur) */ +.outlined-text { + color: white; + text-shadow: + -1px -1px 0 #333, /* Top-Left */ + 1px -1px 0 #333, /* Top-Right */ + -1px 1px 0 #333, /* Bottom-Left */ + 1px 1px 0 #333; /* Bottom-Right */ +} +``` + +### 3. Multiple Shadows (3D/Layered Effect) + +You can define multiple shadows by separating each set of values with a comma. The shadows are layered from front to back, meaning the first shadow listed is the one closest to the text. + +```css title="styles.css" +/* Creates a layered, vintage 3D look */ +.retro-text { + text-shadow: + 2px 2px 0px #ff00ff, /* Front shadow (Magenta) */ + 4px 4px 0px #00ffff, /* Middle shadow (Cyan) */ + 6px 6px 0px #0000ff; /* Back shadow (Blue) */ +} +``` + + +
+ +## Interactive `text-shadow` Demo + +Use the live editor to adjust the offset and blur values. Try adding a second or third shadow separated by commas to create a layered look. + + + +**Challenge:** Add a second shadow to the existing rule: `text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7), 8px 8px 1px darkred;`. Notice how the dark red shadow appears farther back. \ No newline at end of file diff --git a/docs/css/typography/text-style/text-spacing.mdx b/docs/css/typography/text-style/text-spacing.mdx index e345ed2..1e173a1 100644 --- a/docs/css/typography/text-style/text-spacing.mdx +++ b/docs/css/typography/text-style/text-spacing.mdx @@ -1 +1,96 @@ - \ No newline at end of file +--- +title: "Text Spacing Properties" +description: "Learn how to use CSS properties like line-height, letter-spacing, and word-spacing to control the vertical and horizontal density of text for improved readability." +keywords: [CSS line-height, letter-spacing, word-spacing, typography, leading, kerning, readability, CodeHarborHub] +tags: [CSS line-height, letter-spacing, word-spacing, typography, leading, kerning, readability] +sidebar_label: Text Spacing +--- + +Controlling the horizontal and vertical spacing of text is crucial for creating readable, aesthetically pleasing layouts. CSS provides three primary properties to manage this density: **`line-height`**, **`letter-spacing`**, and **`word-spacing`**. + + +
+ +## 1. `line-height`: Vertical Spacing (Leading) + +The `line-height` property sets the vertical distance between the baselines of successive lines of text. In typography, this is often referred to as **leading**. Proper line height is essential for long-form readability. + +### Values and Best Practice + +| Value | Description | Best Practice | +| :--- | :--- | :--- | +| **`number` (Recommended)** | A unitless number (e.g., `1.5`). The final line height is the font size multiplied by this number. | **Use unitless values.** This value scales proportionally to the font size of the element, ensuring good accessibility. | +| **`length`** | A fixed value (e.g., `24px`). | **Avoid.** Does not scale well if the font size changes. | +| **`%`** | A percentage of the current font size (e.g., `150%`). | Acceptable, but less clear than a unitless number. | + +### Example + +A ratio of **1.4 to 1.6** is generally considered optimal for body text readability. + +```css title="styles.css" +/* If font-size is 16px, the line-height will be 16px * 1.5 = 24px */ +body { + font-size: 16px; + line-height: 1.5; +} +``` + +## 2. `letter-spacing`: Spacing Between Characters (Kerning) + +The `letter-spacing` property controls the horizontal space between individual characters (or glyphs). This is sometimes referred to as **tracking** or **kerning** (though kerning is technically more specific to individual letter pairs). + +### Use Cases + + * **Aesthetics:** Used to open up space on uppercase headings. + * **Accessibility:** Can be used to slightly increase space for users with reading disabilities. + * **Negative Spacing:** Can be used to tighten very large text, though caution is required. + +### Example + +```css title="styles.css" +/* Adds 2 pixels of extra space between every character in the heading */ +h1 { + letter-spacing: 2px; + text-transform: uppercase; +} + +/* Reduces spacing on small-caps text */ +.caption { + letter-spacing: -0.5px; +} +``` + + +
+ +## 3. `word-spacing`: Spacing Between Words + +The `word-spacing` property controls the horizontal space between words. It is applied in addition to the default word space defined by the font. + +### Use Cases + + * **Adjustment:** Can be used to slightly adjust word spacing in justified text to improve visual flow. + * **Clarity:** Rarely used for drastic changes, as large modifications can severely impact readability. + +### Example + +```css title="styles.css" +/* Adds 5 pixels of extra space between every word */ +.intro { + word-spacing: 5px; +} + +/* Decreases space slightly (can be useful for justified blocks) */ +.article-body { + word-spacing: -1px; +} +``` + +## Interactive Spacing Demo + +Use the live editor to adjust the three spacing properties and see how they instantly affect the visual density of the text. + + \ No newline at end of file diff --git a/docs/css/typography/text-style/text-transform.mdx b/docs/css/typography/text-style/text-transform.mdx index e345ed2..aff3982 100644 --- a/docs/css/typography/text-style/text-transform.mdx +++ b/docs/css/typography/text-style/text-transform.mdx @@ -1 +1,72 @@ - \ No newline at end of file +--- +title: "The text-transform Property" +description: "Learn how to use the CSS text-transform property to visually change the capitalization of text (uppercase, lowercase, capitalize) without altering the original HTML content." +keywords: [CSS text-transform, uppercase, lowercase, capitalize, text case, typography, CodeHarborHub] +tags: [CSS text-transform, uppercase, lowercase, capitalize, text case, typography] +sidebar_label: "text-transform" +--- + +The CSS **`text-transform`** property allows you to visually change the capitalization of text, overriding how it was originally typed in the HTML. This is a very useful tool for styling headings, captions, and links, ensuring consistency across your design without requiring content editors to manually change the case. + + +
+ +## Syntax and Values + +The `text-transform` property accepts several straightforward keyword values. + +| Value | Description | Example Output | +| :--- | :--- | :--- | +| **`none`** | Displays the text exactly as it was written in the HTML (the default). | `This Is Normal Text` | +| **`uppercase`** | Converts all letters to capital letters. | `THIS IS UPPERCASE TEXT` | +| **`lowercase`** | Converts all letters to small letters. | `this is lowercase text` | +| **`capitalize`** | Converts the first letter of every word to a capital letter. | `This Is Capitalized Text` | +| **`full-width`** | Converts all characters (primarily for Asian languages) into full-width characters. | `This is full-width` | + +### Example + +To force all navigation links to be uppercase, regardless of how they are typed in the CMS: + +```css title="styles.css" +/* All link text will be displayed in capital letters */ +.main-nav a { + text-transform: uppercase; + letter-spacing: 1px; /* Uppercase often needs extra spacing */ +} + +/* Titles are stylized with initial capitalization */ +h2 { + text-transform: capitalize; +} +``` + + +
+ +## Best Practices and Considerations + +### Content vs. Presentation + +One of the great advantages of `text-transform` is that it maintains the **original case** in the HTML. + + * **HTML (Content):** If the text is all lowercase, screen readers and search engines read it as lowercase. + * **CSS (Presentation):** The user **sees** it as uppercase. + +This separation is crucial: if a user copies and pastes text that is styled with `text-transform: uppercase;`, the text they paste will usually be the original, lowercase text from the HTML, which is often a better user experience. + +### Accessibility (Screen Readers) + +While `text-transform` visually changes the text, it generally does not interfere with how screen readers interpret the content, especially for `uppercase` and `lowercase`. + +### The `capitalize` Caveat + +The `capitalize` value only targets characters immediately following a non-letter character (like a space or hyphen). It does not automatically handle articles or prepositions (like "a," "the," "of") as a title case style might. + +### Interactive `text-transform` Demo + +Use the live editor to cycle through the main values: `none`, `uppercase`, `lowercase`, and `capitalize`. Observe how the appearance changes instantly. + + \ No newline at end of file diff --git a/docs/css/typography/text-style/word-wrap.mdx b/docs/css/typography/text-style/word-wrap.mdx index e345ed2..3731da7 100644 --- a/docs/css/typography/text-style/word-wrap.mdx +++ b/docs/css/typography/text-style/word-wrap.mdx @@ -1 +1,67 @@ - \ No newline at end of file +--- +title: "The overflow-wrap Property" +description: "Learn how to use the CSS overflow-wrap property to control whether the browser can break words to prevent content from overflowing its container." +keywords: [CSS overflow-wrap, word-wrap, line breaking, overflow, wrapping long words, text style, CodeHarborHub] +tags: [CSS overflow-wrap, word-wrap, line breaking, overflow, wrapping long words, text style] +sidebar_label: "overflow-wrap" +--- + +The CSS **`overflow-wrap`** property controls how the browser handles a word that is too long to fit on a single line within its container. By default, the browser tries to keep a whole word on one line, which can sometimes cause the word to extend, or **overflow**, the container boundaries. + +This property tells the browser when it is acceptable to break a long, unbroken string of characters (like a long URL or a technical hash) to maintain the layout. + + +
+ +## Syntax and Values + +The `overflow-wrap` property accepts two main keyword values: + +| Value | Description | Behavior | +| :--- | :--- | :--- | +| **`normal`** | This is the default. Words will only break at normal breaking points (like spaces or hyphens). If a single word is too long, it will **overflow** the container. | **Overflows** | +| **`break-word`** | If an unbroken word is too long to fit on its current line, the browser will force a line break, causing the word itself to **split** at an arbitrary point to fit within the container. | **Breaks to Fit** | + +### Example + +Let's assume a container is $100\text{px}$ wide, and a word is $150\text{px}$ long. + +```css title="styles.css +/* Container CSS */ +.log-box { + width: 100px; + border: 1px solid red; +} + +/* 1. Default behavior (overflows) */ +.log-box-1 { + overflow-wrap: normal; +} + +/* 2. Forces a break (stays within bounds) */ +.log-box-2 { + overflow-wrap: break-word; +} +``` + +## Historical Note: `word-wrap` + +The property was originally introduced in CSS with the name **`word-wrap`**. It was later standardized in CSS3 and renamed to **`overflow-wrap`** to better reflect its function (managing overflow). + +:::info Use the Standard Name +While all modern browsers support the `word-wrap` property as a legacy alias, it is considered best practice to use the standardized name: **`overflow-wrap`**. +::: + + +
+ +### Interactive `overflow-wrap` Demo + +Use the live editor to change the value of `overflow-wrap` in the CSS. Observe how the long, unbroken URL reacts when it can't fit in the narrow container. + + + +**Challenge:** Set `overflow-wrap: normal;` and notice the horizontal scrollbar appear or the text push outside the container. Then, change it to `break-word;` and see the text snap inside the container boundaries. \ No newline at end of file diff --git a/docusaurus.config.js b/docusaurus.config.js index 4143431..0c3db4f 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -346,13 +346,13 @@ const config = { themes: ["@docusaurus/theme-mermaid", "@docusaurus/theme-live-codeblock"], plugins: [ - [ - "vercel-analytics", - { - debug: true, - mode: "auto", - }, - ], + // [ + // "vercel-analytics", + // { + // debug: true, + // mode: "auto", + // }, + // ], [ "@docusaurus/plugin-google-tag-manager", {