Web Development Articles

Page 586 of 801

Detect when an Element Gets Fixed in CSS position:sticky using Intersection Observer

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

The CSS position: sticky property allows elements to stick to a specific position when scrolling. To detect when a sticky element becomes fixed, we use the Intersection Observer API to monitor a reference element above the sticky element. Syntax .sticky-element { position: sticky; top: 0; } How Intersection Observer Detects Sticky States The Intersection Observer monitors when a reference element (placed just above the sticky element) enters or exits the viewport. When the reference element disappears from view, the sticky element becomes fixed. HTML Structure ...

Read More

Using WebP Images with Fallback in CSS

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 864 Views

WebP is a modern image format that provides superior compression compared to JPEG and PNG. To use WebP images while maintaining compatibility with older browsers, we can implement fallback mechanisms using the element and CSS. Syntax How It Works The browser evaluates elements in order and uses the first supported format. If WebP is supported, it loads the WebP image; otherwise, it falls back to JPEG or PNG. The element serves as the final fallback. Example ...

Read More

Crop Images in CSS with object-fit and object-position

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 449 Views

CSS object-fit and object-position properties help us crop images and specify how they are displayed within their container elements. The object-fit property controls how an image is resized to fit its container, while object-position determines the alignment of the image within the container. Syntax selector { object-fit: value; object-position: value; } Object-fit Values ValueDescription fillStretches the image to fill the container (default) containScales image to fit container while maintaining aspect ratio coverScales image to cover container while maintaining aspect ratio scale-downBehaves like contain or none, ...

Read More

Selecting Child Elements with CSS

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

CSS provides several combinators to select child elements and descendants of a parent element. The most commonly used are the child combinator (>) and the descendant combinator (space), along with pseudo-selectors like :nth-child(). Syntax /* Child combinator - selects direct children only */ parent > child { property: value; } /* Descendant combinator - selects all descendants */ parent descendant { property: value; } /* nth-child selector - selects specific child position */ element:nth-child(n) { property: value; } Child Combinator The child combinator (>) selects only the direct child elements of a ...

Read More

Selecting Sibling Elements with CSS

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

To select sibling elements with CSS, we can use the adjacent or the general sibling selectors. Let us understand them one by one with examples. Both of them allow selecting sibling elements that share the same parent element. Syntax /* Adjacent sibling selector */ selector1 + selector2 { property: value; } /* General sibling selector */ selector1 ~ selector2 { property: value; } Adjacent Sibling Selector (+) The adjacent sibling selector (+) matches an element that occurs immediately after the first selector. Both elements must ...

Read More

Creating Attractive First Lines with CSS ::first-line

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

The CSS ::first-line pseudo-element allows you to apply styles specifically to the first line of text within an element. This is particularly useful for creating attractive typography effects like drop caps or highlighted opening lines. Syntax selector::first-line { property: value; } Example 1: Bold and Colored First Line The following example makes the first line of each paragraph bold and colored − body { text-align: center; ...

Read More

Styling First-Letters with CSS ::first-letter

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

The CSS ::first-letter pseudo-element allows you to style the first letter of a block-level element. This is commonly used to create decorative drop caps in articles and enhance typography. Note that punctuation marks and special characters can affect which character is considered the "first letter". Syntax selector::first-letter { property: value; } Example 1: Styling All First Letters The following example applies styling to the first letter of all block elements − body { ...

Read More

Maintaining Aspect Ratios for HTML Videos with CSS

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 649 Views

Maintaining aspect ratios for HTML videos is crucial for responsive design. CSS provides two main approaches: using the padding-top property with percentage values or the modern aspect-ratio property to ensure videos display correctly across different screen sizes. Syntax /* Method 1: Using padding-top */ .container { padding-top: percentage; height: 0; position: relative; } /* Method 2: Using aspect-ratio property */ .container { aspect-ratio: width / height; } Method 1: Using Padding-Top Property The padding-top method works by ...

Read More

Adding Hyphens to Text with the CSS hyphens Property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 125 Views

The CSS hyphens property controls how words are broken and hyphenated when they overflow their container. This property is essential for text layout, especially in narrow columns or responsive designs where text needs to wrap gracefully. Syntax selector { hyphens: value; } Possible Values ValueDescription noneWords are not hyphenated, even at manual break points manualWords are hyphenated only at manual break points (­ or ‐). This is the default value. autoWords are automatically hyphenated based on the browser's hyphenation algorithm none: ...

Read More

Setting Tab Sizes in HTML with CSS tab-size Property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 740 Views

The CSS tab-size property allows you to control the width of tab characters in your HTML content. This property is particularly useful when displaying preformatted text or code where tab spacing is important for readability and alignment. Syntax selector { tab-size: value; } Possible Values ValueDescription numberSets the tab size as a number of space characters (default is 8) lengthSets the tab size using length units (px, em, rem, etc.) Example 1: Large Tab Size The following example demonstrates setting a large tab size of 32 ...

Read More
Showing 5851–5860 of 8,010 articles
« Prev 1 584 585 586 587 588 801 Next »
Advertisements