Eight CSS Tips for Advanced Layouts and Effects

The realm of web front-end development has made considerable progress over the last few years. However, the web front-end, as the users see it, is still the same: HTML markup styled with CSS.

Many layout problems can seem simple at first but often proves to be tricky. Without extensive knowledge of how certain CSS features work, these advanced layouts can seem impossible to achieve with CSS alone.

In this article, you will find eight expert CSS tips and tricks that leverage lesser known CSS features to implement some of these advanced layouts and effects.

1. Maximizing CSS Sibling Selectors

The problem: You are losing optimization opportunities by not using sibling selectors.

The solution: Use sibling selectors whenever it makes sense. Whenever you are working with a list of items, and you need to treat the first or the last item differently, your first instinct may be to use the

1

:first-child

and

1

:last-child

pseudo CSS selectors.

For example, when creating a CSS-only hamburger menu icon:

This makes sense: Each bar has a margin-bottom, except for the last one.

Yet the same effect is also possible through the adjacent sibling selector (+):

This also makes sense: Everything after the first bar has a margin-top. Not only does this CSS trick save a few extra bytes (which can easily add up for any medium-sized project), but it also opens up a world of possibilities.

Consider this list of cards:

Each one has a title and text, the latter of which is hidden by default. If you want to make only the text of the active card (with the

1

.active

class) and the ones following it visible you can do it quickly using just CSS:

is just the aspect ratio’s height divided by its width, times 100. For example, if the aspect ratio of the video embed is 16:9, the padding-bottom percentage should be 9 divided by 16 (.5625) and multiplied by 100 (56.25).

The problem: Keeping an HTML element’s width proportional to its height.

The solution: Use font-size as the basis for the element’s dimensions.

Now, what about the reverse, or containers that change width as their height does? This time, it’s

1

font-size

to the rescue. Remember that width and height can be in

1

em

s, meaning they can be a ratio of the element’s

1

font-size

.

An element with a

1

font-size

of 40px, a width of

1

2em

, and a height of

1

1em

would be 80 pixels (40 x 2) wide and 40 pixels (40 x 1) tall.

Want to change

1

.container

’s height? Change font-size.

The only caveat is that it’s impossible to make an element’s font-size match the height of its parent automatically through only CSS. Yet this technique allows a Javascript resize script to be cut down from:

The borders of the first and last column don’t go all the way down; they don’t match the height of the taller second column. To fix this, just add

1

overflow: hidden

to

1

.row

. Then set each

1

.col

’s

1

margin-bottom

to 99999px and its

1

padding-bottom

to 100009px (99999px + the 10px padding applied to

1

.col

’s other sides).

A more straightforward alternative is Flexbox. Again, only use this if Internet Explorer support isn’t a must.

One more alternative with better browser support: CSS tables (without

1

vertical-align: middle

).

7. Going Beyond the Box

The problem: Boxes and straight lines are so clichéd.

The solution: Use

1

transform: rotate(x)

, or

1

border-radius

.

Take a typical series of panes from a marketing or brochure website: a vertical stack of slides with a singular point. Its markup and CSS could look something like this:

At the cost of making the markup much more complicated, these boxy panes could be turned into a stack of parallelograms.

There’s a lot going on here:

The height of each pane is controlled by .pane-container. The negative margin-bottom makes sure the panes stack up snugly.

1

.pane-background

, its child

1

.mask-box

, and its grandchild

1

.image

are all set to

1

position: absolute

. Each element has different

1

top

,

1

left

,

1

bottom

, and

1

right

values. This eliminates any spacing created by the rotations detailed below.

1

.mask-box

is rotated 2 degrees (counter-clockwise).

1

.image

is rotated -2 degrees to counteract

1

.mask-box

’s rotation.

1

.mask-box

’s overflow is hidden so that its rotated top and bottom sides clip the

1

.image

element.

Feel free to play around with this codepen. Try using lower rotate values and see what happens.

On a related note, turning an image into a circle or oval is dead simple. Just apply

1

border-radius: 100%

to the

1

img

element.

Real-time CSS modifications such as these lessen the need to prepare content before it’s published on a website. Instead of applying a circle mask to a photo in Photoshop, the web developer can just apply the same effect through CSS without changing the original photo.

The additional advantage is that by leaving the content untouched and not reliant on the website’s current design, future redesigns or revamps are facilitated.

8. Night Mode

The problem: Implementing a night mode without creating a new stylesheet.

The solution: Use CSS filters.

Some apps feature a night mode, where the interface darkens for better readability under low light. On newer browsers, CSS filters can create the same effect, by applying Photoshop-like effects.

A useful CSS filter is

1

invert

, which (no surprise) inverts the colors of everything inside an element. This makes creating and applying a new set of styles unnecessary.

Using this filter on black text and a white background simulates night mode.

1

!important

ensures that these new colors override any existing styles.

Unfortunately, the image looks weird, because its colors were inverted with everything else. The good news is that multiple filters can apply at the same time. Adding the hue-rotate filter switches images and other visual content back to normal:

Why does this work?

1

hue-rotate(180deg)

just creates the same effect as

1

invert(1)

did. Here’s a demo of how night-mode CSS would work when toggled through a JavaScript-powered button.

Make the Most out of CSS

Unless the browser or how websites are built changes dramatically in the future, a good knowledge of CSS will remain a fundamental skill in the web development space.

All of these CSS tips share something in common: They maximize the use of CSS as a styling language, letting the browser itself do the heavy lifting. And, when done right, this will always yield better results, better performance, and hence a better user experience.

Let us know if you have any CSS trick that you find interesting and useful in the comments section below.