GithubHelp home page GithubHelp logo

frgryr2001 / css-tips-tricks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from devsyedmohsin/css-tips-tricks

1.0 0.0 1.0 30.9 MB

A handmade collection of pro css tips tricks. Give it a star if you find it useful ๐ŸŒŸ

css-tips-tricks's Introduction

paintbrush drawing rainbow circle

CSS Tips Tricks

A handmade collection of pro css tips tricks ๐ŸŒŸ

Table of Contents

  1. Create Documentation Styled Layout
  2. Make Webpages Scroll Smoothly
  3. Adding Stroke to Text
  4. Check If Selector Is Supported
  5. Check If Property Is Supported
  6. Improve Media Defaults
  7. Style :optional Form Elements
  8. The Custom Cursors
  9. Move Table Caption to the bottom
  10. Create Text Columns
  11. Styling video states via :paused and :playing` pseudo classes
  12. Change Writing Mode
  13. Providing Fallback Values for Variables
  14. Zooming Images on Hover
  15. Create Gradient Shadows
  16. Five Ways Centering of Divs
  17. Fill Text With Images
  18. Style Drop Caps
  19. Add Leading Zeros to Ordered Lists
  20. Using Emoji as List Style Type
  21. Add Dark Mode Support on Your Website
  22. Disable Textarea Resizing
  23. Rainbow Animation
  24. Use clamp() for Responsive Typography
  25. Create A Sticky Footer

Create Documentation Styled Layout

You can craft a responsive documentation-styled layout using CSS grid with only two lines of CSS.

<div class="parent">
    <aside>Sidebar</aside>
    <main>Documentation</main>
</div>
.parent{
  display: grid;
  grid-template-columns: minmax(150px, 25%) 1fr;
}
See result documentation styled layout

back to table of contents

Smooth Scrolling

For implementing smooth scrolling for a page add scroll-behavior: smooth to the html element.

 html {
  scroll-behavior: smooth;
}
See result smooth scrolling to different sections

back to table of contents

Adding Stroke to Text

Use text-stroke property it adds a stroke or outline to the text elements.

/* Width and color values */
h1 {
  -webkit-text-stroke: 5px crimson;
  text-stroke: 5px crimson;
}
See result Netlify

back to table of contents

Check If Selector Is Supported

You can check if a selector is supported by your browser or not using the selector() within the @supports rule.

@supports (selector(div:has(pre))) {
/* Code that will only run if the selector is supported */
  p {
    color: crimson;
  }
}

back to table of contents

Check If Property Is Supported

You can also detect properties support using the @supports rule.

@supports (display: grid) {
/* Code that will only run if the selector is supported */
   main {
    display: grid;
  }
}

Creator of codepen @chriscoyier has done an exceptional job of providing valuable insights and information on the @supports rule, Also Known as Feature queries. Read here.

back to table of contents

Improve Media Defaults

Images are inline elements, and by setting the default value to display:block; we can avoid many potential issues. Setting max-width:100%; we prevent images from overflowing when they are in a container that is not wide enough to contain them.

img, picture, video, svg {
  display: block;
  max-width: 100%;
  object-fit: contain;
}

Additionally, I have set object-fit:contain; to ensure that images preserve a nice aspect ratio.

back to table of contents

Style :optional Form Elements

You can style form fields like input, select, and textarea that do not have a required attribute on them using the :optional pseudo-class.

/* Selects all optional form fields on the page */
*:optional{
  background-color: green;
}

Note: Use :required pseudo-class to select required form fields.

back to table of contents

The Custom Cursor

You can customize your cursor from an arrow pointer to a custom image.

html{
  cursor: url('images/no.jpg'), auto;
}

Note: auto will be used as falback value in case image does not load for some reason.

See result custom cursor

back to table of contents

Move Table Caption to Bottom

Use the caption-side property to place the table caption or table title on a specified side of the table.

table{
  caption-side: bottom;
}
See result changing table caption side from top to bottom

back to table of contents

Styling video states via :paused and :playing pseudo classes

Use :paused selector to style media elements like audio, and video when in paused state likewise paused we also have :palying pseudo-class selector.

video:paused {
  opacity: 0.6;
}

Note: At the moment, this feature is only supported in Safari, but you can use this helpful tool to check for the latest support across different browsers.

See result plam tree gif on a beach

back to table of contents

Create Text Columns

Craft nice column layouts for text elements using column properties.

p{
  column-count: 3;
  column-gap: 4.45rem;          
  column-rule: 2px dotted crimson;
}
See result 3 text columns

back to table of contents

Change Writing Mode

You can use the writing-mode property to specify how text should be laid out on your website.

/* Specifies the text layout direction to sideways-lr  */
h1 {
  writing-mode: sideways-lr;
}

/* Keyword values (Reference: MDN)  */
writing-mode: horizontal-tb;
writing-mode: vertical-rl;
writing-mode: vertical-lr;
See result text starting from sideways-lr

back to table of contents

Providing Fallback Values for Variables

Specify fallback values for custom properties. In case a variable is not defined or found for some reason the fallback value will be used.

/* Purple color will be applied as var(--black) is not defined */
:root {
  --orange: orange;
  --coral: coral;
}

h1 {
  color: var(--black, purple);
}

back to table of contents

Zooming Images on Hover

You can create a zoom-in effect when hovering over an image, this is a technique commonly used on e-commerce websites.

/* Define the height and width of the image container & hide overflow */
.img-container {
  height: 250px; width: 250px; overflow: hidden;
 }

/* Make the image inside the container fill the container */
.img-container img {
  height: 100%;
  width: 100%;
  object-fit: cover; 
  transition: transform 200ms ease-in;
 }

 img:hover{
  transform: scale(1.2);
 }
See result shoping bag on grey tiles

back to table of contents

Create Gradient Shadows

This is how you can create gradient shadows for an exclusive user experience.

:root{
  --gradient: linear-gradient(to bottom right, crimson, coral);
}

div {
  height: 200px;
  width: 200px;
  background-image: var(--gradient);
  border-radius: 1rem;
  position: relative;
}

div::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: var(--gradient);
  border-radius: inherit;
  filter: blur(25px) brightness(1.5);
  transform: translateY(15%) scale(0.95);
  z-index: -1;
}
See result box with gradient shadow

back to table of contents

Five Ways of Centering Divs

Center a div both vertically and horizontally.

/* 1.Centering with grid */
.parent{
  height: 100vh;
  display: grid;
  place-items: center;
}

/* 2.Centering with grid & margins */
.parent{
  display: grid;
}
.child{
  margin: auto;
}
/* 3.Centering with positioning */
div{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}
/* 4.Centering with flexbox  */
.parent{
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 5.Centering with flexbox & margins */
.parent{
  display: flex;
}
.child{
  margin: auto;
}

Note: When using layout tools like grid, flexbox or positioning elements are centered relative to the height of the parent element which is 100vh here in our case, Also check out this great article by @bramus on using new viewport units.

back to table of contents

Fill Text With Images

You can fill your text content with a beautiful image with a few lines of CSS.

h1{
  background-image: url('images/flower.jpg');
  background-clip: text;
  color: transparent;
  background-color: white;
}

Note: Always specify background-color when using this technique as this will be used as a fallback value in case the image does not load for some reason.

See result text filled with flower image

back to table of contents

Style Drop Caps

Avoid unnecessary spans and use pseudo elements instead to style your content likewise first-letter pseudo-element we also have first-line pseudo-element.

 h1::first-letter{
  font-size: 2rem;
  color:#ff8A00;
}
See result first letter of text styled differently

back to table of contents

Add Leading Zeros to Ordered Lists

If you want to add leading zeros to the numbers in your ordered list items, you can use the CSS property list-style-type with the value decimal-leading-zero.

li{
  list-style-type:decimal-leading-zero;
}

This technique enhances visual consistency and readability by adding leading zeros to the numbers in your ordered list items.

See result decimal leading zero before list numbers

back to table of contents

Using Emoji as list-style-type

You can use emojis as list style types in CSS It's a fun way to add some personality to your lists.

li{
  list-style-type: '๐Ÿถ';
}

back to table of contents

Add Dark Mode Support to Your Website

You can add dark mode support to your website using CSS variables and the prefers-color-scheme media query.

:root {
  --bg-color: white;
  --text-color: black;
}

body {
  background-color: var(--bg-color);
  color: var(--text-color);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: black;
    --text-color: white;
  }
}

Disable Textarea Resizing

Prevent textarea resizing by setting the resize property to none.

textarea{
  resize:none;
}

back to table of contents

Rainbow Animation

Creates a continuously looping color animation for elements to grab user attention.

button{
  animation: rainbow-animation 200ms linear infinite;
}

@keyframes rainbow-animation {
  to{
    filter: hue-rotate(0deg);
  }
 from{
    filter: hue-rotate(360deg);
  }
}
/* If the user prefers reduced motion, then don't use any animations on button */
@media (prefers-reduced-motion: reduce) {
  button {
    animation: none;
  }
}

Note: When working with animations make use of prefers-reduced-motion media feature to make sure that your website is accessible for the users who may have any vestibular disorders. Give this gem a read written by @tomayac.

See result hue rotate filter on button

back to table of contents

Use clamp() for Responsive Typography

Instead of using media queries for responsive and fluid typography use the clamp() function.

/* Syntax: clamp(minimum, preferred, maximum) */
h1{
  font-size: clamp(2.25rem,6vw,4rem);
}
See result font-size changing based on screen size

back to table of contents

Create A Sticky Footer

You can create a footer that always stick to the bottom of the browser window with only a few lines of CSS.

<div class='layout'>

  <main>
     <!-- your content here -->
  </main>

  <footer>
     <!-- your footer content here -->
  </footer>

</div>
.layout{
  height: 100vh;
  display: flex;
  flex-direction: column;
}

footer{
  margin-top: auto;
  padding: 1rem;
}
See result sticky footer

back to table of contents

Contributing

If you have a CSS tip or trick that you'd like to share with the community, I'd love to hear from you!

When submitting a pull request, please be sure to include a detailed description of the tip or trick, along with a code snippet and any relevant images.

back to table of contents

Support

If you encounter any issues or have questions about this project, please feel free to reach out to me for support. You can contact me via email at [email protected].

Please consider supporting this project. Your support enables me to continue working on this project and creating more resources in the future.

back to table of contents

License

This project is licensed under the MIT License.

back to table of contents

css-tips-tricks's People

Contributors

devsyedmohsin avatar

Stargazers

Roman avatar

Forkers

thangsuperman

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.