The latest CSS techniques - (r)

Mar 13, 2024
Learning advanced CSS techniques

-sidebar-toc>

CSS doesn't only make sites look stunning anymore. The technology will make websites more engaging with motions and interaction that used been thought of as not practical.

As you journey through this tutorial, you'll acquire important skills that will elevate your web-based projects beyond the usual. We hope you'll walk away with a sense of inspiration.

Advanced CSS transitions

Advanced CSS transitions make UI elements more interactive, interesting as well as appealing to the eyes. Imagine a symbol that you have on your site. It's usually just at the center, however today, thanks to CSS transitions, when someone is hovering over it, it smoothly changes color or maybe grows slightly in dimensions.

The concept is based on interpolation as a method that allows for a seamless transition between states that are part of CSS property.

For you to achieve the effects you want it is necessary to understand many CSS properties that you need be familiar with

  • Transition properties: These include specifying the property you want to animate (like background-color or opacity), setting the duration of the transition, and deciding on the transition-timing-function (like ease-in or linear), which dictates how the transition progresses over its duration.
  • Timing features The functions are vital as they regulate the acceleration and deceleration during the transition. One of the best options is that of the cubic-bezier function. It allows you to create customized speed curves that give you full control over the rate of the transition. It may be difficult at first, however software such as cubic-bezier make it easier to think about and develop these curves.
cubic-bezier
A case study of the cubic-bezier motion.

It is an example of how to include this feature into your CSS:

.my-element transition-property: opacity; transition-duration: 0.5s; transition-timing-function: cubic-bezier(0.17, 0.67, 0.83, 0.67); 

In this snippet, .my-elementwill change its transparency in accordance with an exact speed curve, which is defined in its cubic-bezier function. The curve defines a particular sort of movement like beginning slow, increasing speed in the middle, then slowing toward the end.

Using transition-timing-function with custom values, you can make your web elements move in a way that feels more natural, more dynamic, or just plain different from the standard. This is a fantastic tool that can bring some character and elegance to the web animations you create.

In the case of complex techniques, here's a few ideas to consider:

  1. Juggling several properties Why should you settle for animating just one thing? CSS lets you align different properties, and simultaneously animating each one. This is useful when adding the layers of your animation.
  2. Animations that sync Also, you can link different properties in order they move with the same velocity for a more co-ordinated effect.
  3. Nested Transitions Make use of transitions on the elements in the container. In other words, when you interact with the container, children elements behave the same way as they would if you selected.

Make sure these animations aren't only visually nice, they run smoothly, especially when using smaller devices. Utilizing properties such as transformation or transparency is an excellent option because they are less demanding to use in your browsers and won't hinder the performance of your device that significantly.

Also, indicating a caution to your browser using this changing-to-will property could help it be ready for the action and ensures that everything goes smoothly.

This is a last note to ensure that this will work across all browsers. The browsers you use are selective. The prefixes provided by vendors can help to ensure your stunning transitions are compatible across every browser. It's a bit of work, however tools such as autoprefixers will take care of the task for you, making your job simple.

Modifications can be made by CSS

CSS transforms are a wonderful option to bring more attention to your web design. They may be utilized to change the appearance of items, obviously, but they also alter the general look and feel of the web page. This is why you will find that the transformation property plays the primary player on this page.

It's versatile, and it is able to move objects about from one location in one direction. For instance, it can slide images across a screen or changing the dimensions - for instance, making things look closer or farther away as if zooming in and out of the picture. If you're looking to make it even more exciting You can make things rotate.

The best part about this is the addition of 3D transforms into the mix. Utilizing functions such as translate3d, scale3d and rotate3d, the components will jump from the page to give an immersive experience in the browser.

Take a look at what happens when you flip cards. It's an interesting feature in which the card's one side has specific information, then the card is turned over, updated information appears on the reverse. This can attract the attention of potential customers.

To achieve this effect is to use this visible-backface property in a way that is effective. This will ensure that the back of the card will not be noticed until the moment it is intended to be seen.

However, why should you not just stop here? Combining these adjustments with the use of animations or transitions, you can get lots more from your CSS. It is possible to have a button that elegantly grows in size after hovering on it, or an icon that gyrates across the display. These changes dynamically add effortless and smooth feel to your elements on the web, making your user experience more pleasurable.

Designmodo has a number of beautiful images of this process in action. You can first observe the CSS used for 3D transforms broken down into smaller parts. You can then observe the code in work:

designmodo spinning donut
Designmodo is a fantastic instance of 3D transforms that perform beautifully.

Container queries

Container queries are an additional aspect of CSS worth exploring. The way you style elements is by the size of their container rather than just the screen size. Think about it this way: you've got a box and you would like your items to appear nice regardless of how large or small it is. Container queries are perfect for that.

It's a great option for when you need different elements of your website to be displayed, such as cards or sidebars that can be changed in design depending on the space that is available. Each element is able to choose its own style and can be independent of the other elements of the webpage.

Let's look at how you can make use of the following:

  • Configure the container: First, define in CSS that the part that is a web page element that's an element that is a container. It's possible to accomplish this using property names like the container type and the name of the container.
  • Write your queries: Just like media queries, but for containers. Create a rule that says, "Hey, if my container is greater than the size of this, I'll make these style changes."

The basic code will need to be:

@container (min-width 300px) Styles */

In this case it is the case that the stylings in the .component class will be used when the container has a size of 300px.

Now, container queries can be used in a range of contexts, like:

  • Footers and sidebars that can be responsive. Size and layout of footers or sidebars is determined by the dimensions of container.
  • Responsive cards alter the look or the layout within a grid or flexbox layout depending on the size of their boxes.

Container queries are being used by the principal browsers which are Chrome, Firefox, Safari and Edge It is a great idea to use the feature as an incremental improvement. Beginning with simple styles for non-supporting browsers and increase for the ones that allow container queries.

Making use of Flexbox to align vertically

Flexbox Flexbox HTML0 can be an an extremely useful tool within CSS particularly for vertical alignment. While it's been in use for several years, it's super relevant, especially when aligning objects around the cross-axis (which, depending on your style, may be vertical or horizontal).

Utilizing align-items to achieve vertical align

Its align-items property within Flexbox is the most popular choice for aligning objects vertically inside the container. This is possible when your Flex container is situated in the flexibility direction which is made up of rows. This feature lets you decide how all children of the Flex container will be aligned with respect to the cross-axis.

In the case of, say, if you're storing a lot of items in a flexible container in which you wish the entire container to be aligned horizontally using align-items such as center. Below are the main alternatives that you can use to use aligning your items:

  • Flex-start aligns the items in accordance with the location where the container starts.
  • flex-end The alignment of items is done to the edges of the container.
  • center center: Places objects in the container.
  • baseline is a way to align elements based on their baseline.
  • stretch stretch: The items stretch out to ensure that they fill the entire container (default default behavior).

By using align-self, you can control your individual controls

While aligning the items is great to align the contents of containers, you may would prefer to align an item differently. That's the reasons that align-self is an ideal choice. You can alter the aligning-items value for individual items. It is possible to use similar values to align-items..

For example, you've got the flex container which has align-items that are middle-sized; but there's one item you want to align to the beginning. It is possible to apply the align-self option: the flex-start to that specific item. This is a fantastic option to maintain precise control over the location of each item.

It's beneficial to see the process in action.

Let's say you're designing the navigation bar that includes a logo, some hyperlinks, as well as an option to search. It's crucial that the links are placed in a line with the logo, so as to be placed at the top part of the page. The bar should be aligned with the bottom of the.

Here's how to do this:

.nav-container display: flex; flex-direction: row; align-items: center; .logo align-self: flex-start; .search-bar align-self: flex-end; 

In this instance shows this .nav-container is a flexible container, with the items usually centered. The logo and search bar, however, differ from the norm of placing them in line with the starting and the end of the container.

Modern color functions in CSS

The color attributes employed in CSS have been developed dramatically and offer more sophisticated techniques for defining and manipulating color in web design. Let's look into certain of these features:

1. 1. () and Rgba()

Its RGB() function is an old method of defining hues using Red, Green, and blue channels. Each channel can be an integer between the 0 to the number up to 255. The RGBa() variant adds an alpha channel of opacity which is 1 fully transparent, and the other is 0 totally transparent.

The style should look similar to the following:

.example hue: rgb(255, 0, 0,) // /* Red */ background-color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */

2. 2.() and Hsla()

hsl() represents colors according to Saturation, Hue and Lightness. It makes it much simpler to pick the color of your choice. Like rgba(), HSL() includes an alpha channel for indicating the opacity. The same is true for HSL.

.example color: hsl(120, 100%, 50%); /* Green */ background-color: hsla(120, 100%, 50%, 0.3); /* Semi-transparent green */ 

3. 3. () and () and()

OKLCH() and the OKLAB() are more modern additions to the CSS colors. They're built on the CIELAB color space, which is created to give a consistent perception. This means that changes in color values correlate more closely with what is perceived by the human eye.

Now, specifically:

  • OKLAB() is used to determine colors within an visually homogeneous space.
  • oklch() is similar however, it employs the cylindrical coordinate system (lightness the chroma value lightness, as well as hue).

They enable a more effective and faster color manipulation especially for creating smooth color gradients. Here's what that might appear like in code:

.example shade: oklch(75%, 0.25 250) • /* Colors in the oklchor background-color: oklab(0.623, 0.172, -0.079) // The color of oklab/

Utilizing color schemes that are sophisticated

By using these functions particularly the more sophisticated Oklch() and oklab(), you are able to create colors with complex patterns that look visually attractive as well as consistent. They offer more control over how colors are displayed and perceived to ensure that your designs seem appealing and accessible.

If you mix these color functions along with CSS options such as specific properties (CSS variables) along with calculation, you can build an adaptive and flexible color system that can adapt to various styles, states and even settings.

As browser standards and web standards of these features continue to evolve, adopting the latest color features can significantly enhance the appearance as well as the user experience of your websites.

Curved text in the image

It lets you define a shape around which text inline is wrapped. It is helpful for wrapping images around text using an unrectangular shape which results in patterns that appear more natural and more visually appealing than regular rectangular text wrapping.

What's the procedure?

There are many shapes you can create like circles, ellipses or polygons, or even use an image's alpha channel to identify the shape.

It is a shape-outside property is typically utilized for floating objects. If you're floating an image, and then you apply a shape-outside that wraps the image around the image based on the specified shape.

This is a simple illustration of how to use the shape-outside method using a circle.

.image float: left; shape-outside: circle(50%); width: 200px; height: 200px; margin-right: 15px; 

In this case in this example, in this instance this .image class is used to apply the.image class to an image element. The image is floated left, and it's shaped with the shape-outside circle(50%); creates a circular shape around which the text is enclosed. Making use of the shape-outside method effectively, you can open up new possibilities in your design because it allows text to move around complicated designs making it feasible to make magazine-like layouts and attractive websites.

CSS Blend Modals

Blend modes in CSS are effective in mixing pictures and colors to create distinctive visual effects that enhance the design. Blend modes allow designers create engaging visual effects for text, images overlays, and intricate background patterns. To use background-blend-mode, let's talk about what it does first. The property may be utilized to define the way in which an element's background image as well as the color blend. If, for instance, there's a background image with a background color you can blend them by using different blend options, such as multiply screens, overlays, and others. The code would look like with the following structure:

.element background-image: url('image.jpg'); background-color: blue; background-blend-mode: multiply; 

Below are a selection of most well-known mix effects that are worth being conscious of in order to use this effect properly:

  • Multiply The hue is multiplied by the blend layer as well as the base layer. The result is the darker shade.
  • Screen creates colors that are more than light. This is the opposite of increasing. It's useful in producing effects using lighting.
  • Overlay Combining screens blend and multiplication modes. Parts that are light are made lighter while those with darker hues are made darker.
  • Lighten and darken Select the darker or lighter color, respectively.
  • The color can be used to dodge or burn Lighten or darken the base color so that it can reflect the blend's color.
  • Differential and Exclusion It is used to create creative and artistic colors.

The system adapts to the user's preferences

Being able to accommodate user preferences regarding designing for the web is an important element of designing easy-to-use websites. CSS media queries for preferences like prefers-color-scheme and prefers-reduced-motion play a significant role in this process.

Let's look over these concepts.

prefers-color-scheme

Media queries is a way to figure whether the user is asking the system to choose the dark or light theme. It's a convenient option to present the dark color scheme within an online site's style.

It is possible to make use of preferences-color-scheme to define different colors for light and dark modes.

Examples:

/* Default light mode styles */ body background-color: white; color: black; /* Dark mode styles */ @media (prefers-color-scheme: dark) body background-color: black; color: white; 

In this example in this example, standard style styles are used for light modes while the styles in the @media query are applied in cases where the user is searching for a darker colour scheme.

prefers-reduced-motion

The aim of this inquiry is to establish if a user is requesting the system to limit the number of animated or motion that it uses. This is vital for those suffering from motion sickness or suffer from vestibular disorders.

You can use prefers-reduced-motion to reduce or remove animations and transitions:

/* Standard animations */ .animate transition: transform 0.3s ease; /* Reduced motion */ @media (prefers-reduced-motion: reduce) .animate transition: none; 

Animations are able to be disabled if the user has expressed a preference to reduce motion.

Incorporating prefers-color-scheme and prefers-reduced-motion into your CSS is a step towards a more inclusive and user-friendly web, ensuring that your site is accessible and comfortable for a wide range of users with different needs and preferences.

Use :is() And Where() pseudo-selectors

The is() and :where() pseudo-selectors in CSS are powerful tools for managing specificity, and reducing the complexity of chain of selectors. Let's explore how they work and see some real-world examples.

:is() pseudo-selector

This selector allows you to focus on multiple elements using only one set of rules, which reduces the number of selectors that are similar to it. What makes () the () pseudo-class the() pseudo-class is its capability to be the most exact selector used within the arguments.

* Selects a heading, paragraph or paragraph inside an article */ thearticle :is(h1 H1, h2, h3, (p, (p,) Color: blue

:where() pseudo-selector

The syntax is akin to the  expression :is(), but it's distinguished by a crucial difference. where() always has an accuracy of zero. This makes it useful for taking over styles but not expanding the precision. In use, it might be like this:

* Selects any heading or paragraph, but with no added specificity */:where(article or subsection) P margin-bottom 1em;

Utilizing the syntax of :is() and with(), you can make flexible and durable style sheets, especially when working with complicated styles. The pseudo-selectors can be useful when you have to:

  • simplify nested selectors HTML0 can help you understand the nested and grouped selectors. This makes your CSS more accessible and simpler to handle.
  • Override style: :where() is great for creating styles with a basic design that can be quickly overridden, without having to worry about particularity.
  • Reuse styles These two types of selectors can be used to make more modular and reusable styles by allowing you to group diverse elements within the same rules.

To see this, imagine the navigation menu that has distinct sections. The menu can be designed by using the  term :is() to uniformly make all the links appear within the menu, regardless of their nesting levels like:

nav :is(ul (ul, ol, div) > > li a padding of 10 pixels; color: white;

Summary

From the elegance of CSS modifications that help allow users to interact with more fluid interfaces and a lot more effective 3D transforms, we'd love to see that you've come to a better understanding of the higher-end CSS methods that will be accessible by the year 2024, and even beyond.

Do you use one of these advanced CSS methods currently? Have you got suggestions for other users to try? Feel free to share them with us.

Jeremy Holcombe

Content & Marketing Editor , WordPress Web Developer as well as Content Writer. Aside from WordPress, I enjoy the beach, golf, as well as movies. I also have height problems ;).

Article was posted on here