Introduction:
CSS (Cascading Style Sheets) is a powerful language for styling web pages. While many developers are familiar with the basics of CSS, there are numerous lesser-known tricks and techniques that can help take your CSS skills to the next level. In this article, we will explore 100 lesser-known CSS tricks that can help you improve the design and functionality of your web pages.
Basic CSS Tricks
- Using
box-sizing: border-box;to include padding and border in the element's total width and height. - Using
:root { --variable-name: value; }to define CSS variables. - Using
calc()function to perform calculations in CSS values. - Using
:focus-withinpseudo-class to apply styles to a parent element when any of its children elements are focused. - Using
scroll-behavior: smooth;to add smooth scrolling to anchor links. - Using
:not()pseudo-class to apply styles to elements that do not match a specific selector. - Using
::beforeand::afterpseudo-elements to add content before and after an element. - Using
clip-pathproperty to create custom shapes. - Using
text-overflow: ellipsis;to truncate text with an ellipsis when it overflows its container. - Using
pointer-events: none;to make an element non-clickable.
Advanced CSS Tricks
- Using
@supportsrule to apply styles based on browser support for certain CSS features. - Using
backdrop-filterproperty to apply visual effects to the area behind an element. - Using
mix-blend-modeproperty to blend the colors of an element with its background. - Using
object-fit: cover;to scale images while maintaining their aspect ratio and covering their container. - Using
shape-outsideproperty to wrap text around a non-rectangular shape. - Using
will-changeproperty to inform the browser about the changes that are likely to happen to an element. - Using
image-set()function to provide multiple image sources with different resolutions for responsive images. - Using
font-variation-settingsproperty to apply variable fonts with custom variations. - Using
@keyframesrule to create animations. - Using
@mediarule to apply styles based on the device's screen size or other media features.
Layout and Flexbox Tricks
- Using
display: grid;to create grid layouts. - Using
grid-template-areasproperty to define named grid areas. - Using
grid-auto-flowproperty to control the direction in which grid items are placed. - Using
justify-selfandalign-selfproperties to align individual grid items. - Using
display: flex;to create flexible layouts. - Using
flex-grow,flex-shrink, andflex-basisproperties to control the flex behavior of flex items. - Using
orderproperty to change the order of flex items. - Using
align-contentproperty to align flex lines within a flex container. - Using
gapproperty to add space between grid or flex items. - Using
aspect-ratioproperty to control the aspect ratio of an element.
Animation and Transition Tricks
- Using
transitionproperty to add smooth transitions between CSS property changes. - Using
animationproperty to create complex animations with keyframes. - Using
@keyframesrule to define the steps of an animation. - Using
animation-fill-modeproperty to control the styles applied to an element before and after an animation. - Using
animation-directionproperty to control the direction of an animation. - Using
animation-timing-functionproperty to specify the speed curve of an animation. - Using
animation-iteration-countproperty to control the number of times an animation should repeat. - Using
animation-play-stateproperty to pause and resume an animation. - Using
transition-timing-functionproperty to specify the speed curve of a transition. - Using
@supportsrule to apply different animations or transitions based on browser support.
Typography and Text Tricks
- Using
text-transformproperty to transform text to uppercase, lowercase, or capitalize. - Using
text-decorationproperty to add or remove decorations such as underline or line-through. - Using
word-breakproperty to control how words break when reaching the end of a line. - Using
word-wrapproperty to control whether long words can break and wrap onto the next line. - Using
white-spaceproperty to control how white space inside an element is handled. - Using
letter-spacingproperty to adjust the spacing between characters. - Using
line-heightproperty to control the height of lines of text. - Using
font-feature-settingsproperty to enable or disable OpenType features in fonts. - Using
text-overflowproperty to specify how overflowed content should be signaled to the user. - Using
directionproperty to specify the text direction of an element.
Responsive Design Tricks
- Using media queries to apply different styles based on the device's screen size.
- Using
min-widthandmax-widthmedia queries to target specific device widths. - Using
min-heightandmax-heightmedia queries to target specific device heights. - Using
orientationmedia query to apply styles based on the device's orientation (landscape or portrait). - Using
aspect-ratiomedia query to apply styles based on the aspect ratio of the viewport. - Using
resolutionmedia query to apply styles based on the device's resolution. - Using
colormedia query to apply styles based on the device's color capabilities. - Using
prefers-reduced-motionmedia query to apply styles based on the user's preference for reduced motion. - Using
hovermedia query to apply styles when the user hovers over an element. - Using
pointermedia query to apply styles based on the type of pointing device (mouse or touch).
Accessibility and User Interface Tricks
- Using
aria-*attributes to improve the accessibility of your web pages. - Using
tabindexattribute to control the tab order of elements. - Using
:focus-visiblepseudo-class to apply styles to elements that are focused using the keyboard. - Using
:read-onlyand:read-writepseudo-classes to style elements based on their read-only or read-write state. - Using
:validand:invalidpseudo-classes to style elements based on their validation state. - Using
:requiredand:optionalpseudo-classes to style elements based on their required or optional status. - Using
:checkedpseudo-class to style checked checkboxes and radio buttons. - Using
:indeterminatepseudo-class to style indeterminate checkboxes. - Using `:placeholder-shown

Post a Comment