Search code snippets, questions, articles...

css code snippets

Center container and its child container
.parent{
    display:grid;
}
Create a Login modal popup using HTML and CSS
/* Check the article on website */
variables
:root {

    /**
     * colors
     */
  
    --spanish-gray: hsl(0, 0%, 60%);
    --sonic-silver: hsl(0, 0%, 47%);
    --eerie-black: hsl(0, 0%, 13%);
    --salmon-pink: hsl(353, 100%, 78%);
    --sandy-brown: hsl(29, 90%, 65%);
    --bittersweet: hsl(0, 100%, 70%);
    --ocean-green: hsl(152, 51%, 52%);
    --davys-gray: hsl(0, 0%, 33%);
    --cultured: hsl(0, 0%, 93%);
    --white: hsl(0, 100%, 100%);
    --onyx: hsl(0, 0%, 27%);
  
    /**
     * typography
     */
  
    --fs-1: 1.563rem;
    --fs-2: 1.375rem;
    --fs-3: 1.25rem;
    --fs-4: 1.125rem;
    --fs-5: 1rem;
    --fs-6: 0.938rem;
    --fs-7: 0.875rem;
    --fs-8: 0.813rem;
    --fs-9: 0.75rem;
    --fs-10: 0.688rem;
    --fs-11: 0.625rem;
  
    --weight-300: 300;
    --weight-400: 400;
    --weight-500: 500;
    --weight-600: 600;
    --weight-700: 700;
  
    /**
     * border-radius
     */
  
    --border-radius-md: 10px;
    --border-radius-sm: 5px;
  
    /**
     * transition 
     */
  
    --transition-timing: 0.2s ease;
  
}
Change color of <hr> element using CSS
hr {
    height: 2px;
    background-color: #ff0000;
    border: none;
}
Apply :hover on element:after and element:before using CSS
.box {
    position: relative;
}

.box:hover:before {
    content: "Before content";
}

.box:hover:after {
    content: "After content";
}
SHOW/HIDE a div when checkbox is checked using CSS only
.div_box {
    border: 1px solid #aaa;
    padding: 10px;
    display: none;
}

.checkbox:checked + .div_box {
    display: block;
}
10 methods to vertically center a text using CSS
/* Using flex */
.ele {
    display: flex;
    align-items: center;
    height: 200px;
    background: #DDDDDD;
}
Remove underline from link using CSS
.link {
    text-decoration: none;
}
CSS Input Placeholder Customization
/* SUPPORTS - chrome, firefox, opera, safari */
::placeholder {
  color: blue;
  opacity: 1;
}

/* SUPPORTS - internet explorer 10+ */
:-ms-input-placeholder { 
  color: blue;
}

/* SUPPORTS - microsoft edge */
::-ms-input-placeholder { 
  color: blue;
}

/* FOR SPECIFIC INPUT HAVING CLASS - control  */
input.control::placeholder {
    color: blue;
    opacity: 1;
}
Remove (WebKit) Chrome browser autofill background color CSS
input:-webkit-autofill,
input:-webkit-autofill:hover, 
input:-webkit-autofill:focus, 
input:-webkit-autofill:active  {
    /* -webkit-box-shadow: 0 0 0 40px white inset !important; */
    -webkit-box-shadow: 0 0 0 0 rgba(white,0.0) inset !important;

}
input:-webkit-autofill {
    -webkit-text-fill-color: yellow;
}
test.test
padding:10px;
Create a simple Vertical menu using CSS Example 1
.v_menu {
    border: 1px solid #ddd;
    width: 250px;
    margin: 0;
    padding: 5px;
    border-radius: 10px;
}
.v_menu li {
    display: block;
}
.v_menu li a {
    display: block;
    padding: 15px 20px;
    text-decoration: none;
    border-radius: 10px;
    color: #000;
    font-size: 15px;
}
.v_menu li a.active {
    background: #000;
    color: #fff;
}
sample css code
.sample{
    display: inline-block;
    background-color: red;
}
sample css
.sample{
    display: inline-block;
    background-color: red;
}
Add border radius to element CSS
/*add radius all sides*/
.element {
    border-radius: 10px;
}
/* 
add radius specific sides 
order - top-left top-right bottom-right bottom-left
*/
.element {
    border-radius: 20px 10px 30px 20px;
}
Add image to background using CSS
/* using background proprty */
body {
    background: url('https://placeimg.com/640/480/arch'); /*this will add image to background */
}

/* using background */
body {
    background-image: url('https://placeimg.com/640/480/arch'); /*this will add image to background */
}
Change Last element style using CSS
ul li:last-child {
    background: #ff0000;
    border: 1px solid #ff0000;
}
Change font color CSS
.element {
    color: #ff0000;
}
Customize placeholder color in CSS
/* SUPPORTS - chrome, firefox, opera, safari */
::placeholder {
  color: blue;
  opacity: 1;
}

/* SUPPORTS - internet explorer 10+ */
:-ms-input-placeholder { 
  color: blue;
}

/* SUPPORTS - microsoft edge */
::-ms-input-placeholder { 
  color: blue;
}

/* FOR SPECIFIC INPUT HAVING CLASS - control  */
input.control::placeholder {
    color: blue;
    opacity: 1;
}
Capitalize, Uppercase, Lowercase, text-transform in CSS
/* TO CAPITALIZE A SENTENCE */
.capitalize {
    text-transform: capitalize;
}

/* TO UPPERCASE A SENTENCE */
.uppercase {
    text-transform: uppercase;
}

/* TO LOWERCASE A SENTENCE */
.lowercase {
    text-transform: lowercase;
}
Word spacing CSS
.paragraph {
    word-spacing: 10px;
}
qwe
asdf
Remove chrome browser autofill background color CSS
input:-webkit-autofill,
input:-webkit-autofill:hover, 
input:-webkit-autofill:focus, 
input:-webkit-autofill:active  {
    -webkit-box-shadow: 0 0 0 40px white inset !important;
}
flex direction in CSS
.flex_element {
    display: flex;
    flex-direction: row;
}

.flex_element {
    display: flex;
    flex-direction: row-reverse;
}

.flex_element {
    display: flex;
    flex-direction: column;
}

.flex_element {
    display: flex;
    flex-direction: column-reverse;
}

.flex_element {
    display: flex;
    flex-direction: initial;
}
Create skew background using CSS
.skew_container  {
   max-width: 500px;
   margin: 50px auto;
   overflow: hidden;
   padding-bottom: 4%;
}

.skew_container .skew_bg {
   height: 260px;
   background: #115edb;
   text-align: center;
   padding-top: 130px;
   margin-top: -9%;
   transform: skew(0deg, 10deg); /*THIS WILL DO THE MAGIC*/
}

.skew_container .skew_bg .content {
   color: #fff;
   font-size: 30px;
   transform: skew(0deg, -10deg); /* MAKE CONTENT HORIZENTAL */
}
Left side box shadow only in CSS
.element {
    -webkit-box-shadow: -5px 0 5px -5px #333;
    -moz-box-shadow: -5px 0 5px -5px #333;
    box-shadow: -5px 0 5px -5px #333;
}

/* FOR INSET CASE */
.elemnt_inset_left_shaow {
    -webkit-box-shadow: 7px 0 4px -4px #333 inset;
    -moz-box-shadow: 7px 0 4px -4px #333 inset;
    box-shadow: 7px 0 4px -4px #333 inset;
}
CSS3 Animations
.container {
  animation-name: popin;
  animation-duration: 5s;
  animation-timing-function: linear;
  animation-delay: 2s;
  animation-iteration-count: infinite;
  animation-direction: reverse;
}

/*THE ABOVE CODE IN SHORT FORMAT CAN BE WRITTEN IN SINGLE LINE*/
.container {
    animation: popin 5s linear 2s infinite reverse;
}

/* TO GENERATE ANIMATION WE USE KEYFRAMES */
@keyframes popin {
    from {
        transform: scale(0.8);
    }
    to {
        transform: scale(1.0);
    }
}
Rounded button
.rounded_button {
   padding: 1.2rem 2.5rem;
   font-size: 1rem;
   background: #207ef9;
   color: #fff;
   border-radius: 40px;
   border: 0;
   letter-spacing: 1px;
   cursor: pointer;
   outline: 0;
}

.rounded_button:hover {
   opacity: .9;
}
Text ellipsis for multi lines in CSS
.multi_line_ellipsis {
    display: -webkit-box;
    max-height: 3rem;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    -webkit-line-clamp: 2;
    font-size: 1.4rem;
    font-weight: 500;
    line-height: 1.6rem;
}
Bottom box shadow only to element in CSS
.bottom-box-shadow {
    -webkit-box-shadow: 0 8px 6px -6px rgba(0,0,0,0.7);
       -moz-box-shadow: 0 8px 6px -6px rgba(0,0,0,0.7);
            box-shadow: 0 8px 6px -6px rgba(0,0,0,0.7);
}
Create rounded shape style button using CSS
.rounded_button {
   padding: 1.2rem 2.5rem;
   font-size: 1rem;
   background: #207ef9;
   color: #fff;
   border-radius: 40px;
   border: 0;
   letter-spacing: 1px;
   cursor: pointer;
   outline: 0;
}

.rounded_button:hover {
   opacity: .9;
}