10 CSS One-Liners to Transform Your Web App

Leverage these 10 CSS One-Liners To Improve Your Web App In Under a Few Minutes!

Nipuni Arunodi
Bits and Pieces

--

CSS is a lets you create beautiful and responsive web designs.

However, writing CSS code can be time-consuming and challenging, especially if you’re not familiar with the latest techniques and best practices.

Fortunately, there are several one-liners that can help you streamline your CSS code and make your projects more efficient.

In this article, I’ll discuss 10 CSS one-liners that can transform your project and help you create stunning web designs with ease.

1. Centering a div

Centering a div involves positioning a div element at the center of a page or within an element acting as its container. There are several ways to center a div using CSS, including using thetext-align, position, and display properties.

  • Centering Horizontally: To center a div horizontally, you can use the following CSS code:
.container {
display: flex;
justify-content: center;
}

This will create a flex container that will arrange its child elements in a row and center them horizontally.

  • Centering Vertically: To center a div vertically, you can use the following CSS code:
.container {
display: flex;
align-items: center;
}

This will create a flex container that will arrange its child elements in a column and center them vertically.

  • Centering Horizontally and Vertically: To center a div both horizontally and vertically, you can use the following CSS code:
.container {
display: flex;
justify-content: center;
align-items: center;
}

This will create a flex container that will arrange its child elements in a row and center them horizontally and vertically.

2. Creating Free Flowing Elements

display: flexis a CSS property that allows you to create a flex container. It is used to create free-flowing elements that can be arranged in a row or column, and can be resized to fit the available space.

Here is an example of how to use display: flex to create a row of elements:

.container {
display: flex;
}

This will create a flex container that will arrange its child elements in a row. You can then use other CSS properties such as justify-content, align-items, and flex-wrap to further customize the layout of the elements.

3. Positioning elements anywhere in the DOM

Relative positioning is used to modify the final position of an element, including making it overlap other elements on the page. It works by setting the position property of the element to relative. Once the element has taken its place in the normal flow, you can then modify its final position using the top, bottom, left, and right properties. Here’s an example:

.element {
position: relative;
top: 10px;
left: 20px;
}

This will move the element 10 px down and 20 px to the right from its original position.

Absolute positioning is used to position an element relative to its closest positioned ancestor. It works by setting the position property of the element to absolute. Here’s an example:

.element {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

This will center the element both horizontally and vertically within its closest positioned ancestor.

4. Universal box sizing

One of the most common CSS mistakes is to forget to set the box-sizing property. This property determines how the width and height of an element are calculated. By default, the box-sizing property is set to content-box, which means that the width and height of an element only include its padding and border. This can lead to unexpected layout problems.

To fix this, you can simply add the following one-liner to your CSS:

{
box-sizing: border-box;
}

This will set the box-sizing property to border-box for all elements on your page. This means that the width and height of an element will include its padding and border, as well as its content.

5. Responsive images

Images are an important part of any website, but they can also be a challenge to make responsive. If you don’t code your images correctly, they can appear stretched or distorted on different screen sizes.

To make your images responsive, you can use the following one-liner:

img {
max-width: 100%;
height: auto;
}

This will make sure that your images never take up more than 100% of the available space, and that they maintain their aspect ratio.

6. Centering elements

Centering elements horizontally and vertically can be a bit tricky in CSS. However, there is a simple one-liner that you can use to center any element on your page:

.center {
margin: 0 auto;
}

This will center the element horizontally on the page. You can also add the following code to center the element vertically:

.center {
text-align: center;
}

7. Creating custom checkbox styles

Checkboxes are often used in forms, but they can be pretty boring to look at. With a little CSS, you can easily create custom checkbox styles that match the look and feel of your website.

Here is a one-liner that you can use to create a simple checkbox style:

input[type="checkbox"] {
-webkit-appearance: none;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
width: 16px;
height: 16px;
display: inline-block;
}

input[type="checkbox"]:checked {
background-color: #ddd;
}

This will create a checkbox with a gray background and a blue border. When the checkbox is checked, the background color will change to light gray.

8. Making text smoother

Text can sometimes appear jagged or blurry on screens. This is because of the way that fonts are rendered. You can make your text look smoother by using the following one-liner:

text-rendering: optimizeLegibility;

This will tell the browser to use a rendering technique that makes text look smoother, even on low-resolution screens.

9. Clearing floats

Floats can be a useful CSS layout technique, but they can also cause problems if you’re not careful. One common problem is that floated elements can overlap with other elements on your page.

To prevent this from happening, you can use the following one-liner to clear the floats:

.clearfix:after {
content: "";
display: block;
clear: both;
}

This will add a clearfix class to any element that you want to clear the floats after.

10. Resetting all styles

Sometimes, you may want to start from scratch with your CSS. To reset all of the styles on a page, you can use the following one-liner:

{
margin: 0;
padding: 0;
border: 0;
font-size: 16px;
font-family: sans-serif;
}

This will reset all of the margins, padding, borders, font sizes, and font families to their default values.

Wrapping Up

That was easy wasn’t it?

You can use these 10 CSS One-Liners in your web app to transform it’s look and feel in under a few minutes!

I hope you learned something new in this article. If you feel like there are better CSS One-Liners that can benefit the developer community, feel free to drop them in the comments!

Thank you for reading.

--

--