25 Killer CSS One-Liners That’ll Make You Look Like a Pro

Tanmay Roy
4 min readNov 28, 2023

--

Cascading Style Sheets (CSS) is a crucial technology in web development, enabling developers to style and format web pages with ease. While mastering CSS can take time, there are several one-liners — concise and powerful snippets of code — that can elevate your styling game instantly. In this article, we’ll explore 25 killer CSS one-liners that will not only save you time but also make you look like a pro.

1. Universal Box Sizing:

* { box-sizing: border-box; }

This one-liner ensures that the box model includes padding and borders in the element’s total width and height. It’s a time-saver and prevents unexpected layout issues.

2. Remove Default List Styles:

ul, ol { list-style: none; }

Make images responsive by constraining their maximum width to 100% of the parent container.

3. Responsive Images:

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

Make images responsive by constraining their maximum width to 100% of the parent container.

4. Center an Element Horizontally and Vertically:

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

Perfect for centering elements within their parent container.

5. Disable Text Selection:

.no-select { user-select: none; }

Prevent users from selecting text within a specific element.

6. Gradient Text:

.gradient-text { background: linear-gradient(45deg, #e66465, #9198e5); -webkit-background-clip: text; color: transparent; }

Apply a gradient to the text without using an image.

7. Smooth Scrolling:

html { scroll-behavior: smooth; }

Create a smooth scrolling effect when navigating through anchor links.

8. Sticky Header:

header { position: sticky; top: 0; z-index: 100; }

Keep the header fixed at the top of the viewport as users scroll down.

9. Hide Scrollbar:

body { overflow: hidden; }

Conceal the scrollbar while maintaining scrolling functionality.

10. Custom Checkbox Styles:

input[type="checkbox"] { appearance: none; -webkit-appearance: none; }

Style checkboxes without relying on default browser styles.

11. Equal Width Flex Items:

.flex-container { display: flex; justify-content: space-between; }
.flex-item { flex: 1; }

Distribute flex items evenly within a container.

12. Smoother Transitions:

.smooth { transition: all 0.3s ease; }

Apply smooth transitions to elements for a polished user experience.

13. Hide Elements Accessibly:

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

Visually hide elements while keeping them accessible to screen readers.

14. Fixed Aspect Ratio:

.aspect-ratio { position: relative; width: 100%; padding-bottom: 75%; }
.aspect-ratio > iframe { position: absolute; width: 100%; height: 100%; }

Maintain a fixed aspect ratio for responsive videos.

15. Custom Focus Styles:

:focus { outline: 2px solid #3498db; outline-offset: 2px; }

Enhance focus styles for better accessibility.

16. Truncate Text with Ellipsis:

.truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

Limit text length and add an ellipsis for overflow.

17. Full-Width Container with Padding:

.full-width { width: 100vw; margin-left: 50%; transform: translateX(-50%); padding: 0 20px; }

Create a full-width container with padding while centering it horizontally.

18. Zebra Striping:

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

Alternating row colors for better table readability.

19. Custom Font Smoothing:

body { text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }

Improve font rendering for a cleaner look.

20. Fluid Typography:

body { font-size: calc(16px + (20 - 16) * ((100vw - 320px) / (1920 - 320))); }

Adjust font size based on viewport width for responsive typography.

21. Interactive Links:

a { color: #3498db; transition: color 0.3s ease; }
a:hover { color: #e74c3c; }

Create smooth color transitions for hyperlinks.

22. Custom Cursor:

body { cursor: url('custom-cursor.png'), auto; }

Replace the default cursor with a custom image.

23. Sticky Footer:

body { display: flex; flex-direction: column; min-height: 100vh; }
.content { flex: 1; }

Keep the footer at the bottom of the page, even with minimal content.

24. CSS Variables:

:root { --primary-color: #3498db; }
.element { color: var(--primary-color); }

Leverage CSS variables for easy theming.

25. Blur Background:

.blur-background { background: url('background.jpg') center/cover fixed; filter: blur(5px); height: 100vh; }

Apply a blurred background image for a visually appealing effect.

These 25 CSS one-liners are powerful tools that can significantly enhance your web development workflow. By incorporating these snippets into your projects, you’ll not only save time but also demonstrate a mastery of CSS that will make you look like a pro in the eyes of your peers and clients. Experiment with these one-liners and watch your web designs reach new heights of sophistication and efficiency.

--

--

Tanmay Roy

Web Developer | Cyber-Security Professional | Network and Server Engineer