avatarSarah

Summary

This content provides ten advanced CSS tricks for web developers looking to enhance their design skills, including custom underlines, hidden accessibility text, aspect ratio boxes, and dynamic styling with CSS variables.

Abstract

The article "10 lesser known CSS tricks part 3" delves into a new set of CSS techniques that can elevate web design. It starts by discussing how to customize underlines on links using a combination of border-bottom and text-decoration properties. The piece then covers the use of the sr-only class to hide elements visually while keeping them screen reader accessible, which is crucial for web accessibility. The concept of maintaining aspect ratios for media elements using padding is also explained. Additionally, the article demonstrates how to style even and odd elements differently, implement automatic numbering with CSS counters, apply multiple background images, and improve text readability with hyphenation. It also highlights the importance of CSS variables for dynamic and reusable styles, focus styles for keyboard navigation, and how to create smooth transitions for gradient backgrounds. The article encourages readers to apply these tricks to enhance their web design capabilities and ensure better accessibility and user experience.

Opinions

  • The author believes that these CSS tricks are not widely known, suggesting that they can provide a competitive edge in web design.
  • There is an emphasis on the importance of accessibility, as seen with the inclusion of hidden accessibility text and improved focus styles for keyboard navigation.
  • The article implies that maintaining aspect ratios without JavaScript is valuable, proposing a CSS-only solution.
  • The use of CSS counters is presented as a powerful alternative to traditional HTML list numbering.
  • The article suggests that multiple background images can add depth and interest to web design without relying on additional HTML elements.
  • Automatic hyphenation is recommended as a means to improve the readability of text content on web pages.
  • CSS variables are touted as a modern approach to theming and dynamic styling, promoting code maintainability and ease of updates.
  • The inclusion of smooth gradient transitions indicates the author's opinion that subtle animations can significantly enhance the polish and interactivity of a website.

10 lesser known CSS tricks part 3

There are more than just 20 CSS tricks that you might haven’t heard about yet. That’s why we dive into part 3 of the lesser known CSS tricks series. I hope you have fun and these tricks will be helpful for improving your web design game. So what are you waiting for?

01. Custom underlines

Customize the style of underlines on links using a combination of border-bottom and text-decoration.

a {
    text-decoration: none;
    border-bottom: 1px solid #3498db;
}

02. Hidden accessibility text

Use the class sr-only to visually hide elements but keep them accessible to screen readers.

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

03. Aspect ratio boxes

Maintain aspect ratio for elements like images or videos by using the padding trick.

.aspect-ratio-box {
    position: relative;
    width: 100%;
    padding-bottom: 75%; /* Adjust as needed */
}

.aspect-ratio-box > iframe {
    position: absolute;
    width: 100%;
    height: 100%;
}

04. Selecting even and odd elements

Style alternate elements using the :nth-child pseudo-class.

li:nth-child(even) {
    background-color: #f2f2f2;
}

li:nth-child(odd) {
    background-color: #e6e6e6;
}

05. CSS Counter

Use the counter-reset and counter-increment properties to create automatic numbering in lists.

ol {
    counter-reset: item;
}

li {
    counter-increment: item;
}

li::before {
    content: counter(item) ". ";
}

06. Multiple background images

Apply multiple background images to an element with different properties.

.bg {
    background-image: url('image1.jpg'), url('image2.jpg');
    background-position: top left, bottom right;
    background-repeat: no-repeat, repeat-x;
}

07. Hyphens for better text flow

Improve text readability by allowing automatic hyphenation using the hyphens property.

p {
    hyphens: auto;
}

08. CSS variables for dynamic styling

Leverage CSS variables to create dynamic and reusable styles.

:root {
    --main-color: #3498db;
}

.element {
    color: var(--main-color);
}

09. Focus styles for keyboard navigation

Improve focus styles for better keyboard navigation and accessibility.

:focus {
    outline: 2px solid #27ae60;
}

10. Smooth gradient transitions

Apply smooth transitions to gradient backgrounds for a polished effect.

.gradient-box {
    background: linear-gradient(45deg, #3498db, #2ecc71);
    transition: background 0.5s ease;
}

.gradient-box:hover {
    background: linear-gradient(45deg, #e74c3c, #f39c12);
}
CSS
Web Development
Web Design
Front End Development
Frontend
Recommended from ReadMedium
avatarCreative Byte
5 useful CSS snippets

Zoom on hover

2 min read