visit
And also that you are capable of doing
Have you ever wonder how can I optimize my website a little bit? You probably use Bootstrap or Foundation but not really all of the classes.You might be thinking: how hard would it be to build my own framework? And how could I, a simple human being be able to achieve such a task?In this post, I’m going to show you how I wrote a 4-thousand-lines CSS framework in less than 2 working days. You want to see it for yourself? Go to my Github .Okay, I didn’t do it all by myself, I had a very helpful coding . But still, after you are done with this you’ll be able to do it alone and even faster.This framework is based on Bootstrap which is a CSS framework. A lot of the classes’ names are the same or similar to Bootstrap. It also has the 5 breakpoints used by this famous framework for responsiveness.Here are the most important things we took into account for this process:Or your own website. This framework was done for the page (your welcome!) This is going to help for specific styling like colors, font type, font size, etc..
Later on, I’ll explain what I mean with don’t be afraid, but you’ll probably figure it out soon enough.//import.scss
@import "reset";
@import "variables";
@import "spacing";
@import "display";
@import "width-height";
@import "font-color";
these were the files:
$column-width: (100%-$gutter-width*12)/12I must share with you the code applying this formula we used to create the responsive 12 column grid and all the responsive breakpoints:
//SCSS code for 12 column grid
//VARIABLES
$xs: xs;
$sm: sm;
$md: md;
$lg: lg;
$xl: xl;
$breakpoint-xs: 575px;
$breakpoint-sm: 576px;
$breakpoint-md: 768px;
$breakpoint-lg: 992px;
$breakpoint-xl: 1200px;
$class-col: col;
$gutter-width: 1%;
$column-width: 7.33%;
//MAIN
@mixin grid-unit-normal($span) {
float: left;
margin-right: $gutter-width;
width: ($column-width * $span) + ($gutter-width * ($span - 1));
}
@mixin grid-unit($span) {
float: left;
margin-right: $gutter-width;
width: ($column-width * $span) + ($gutter-width * ($span - 1)) !important;
}
@for $i from 1 through 12 {
.#{$class-col}-#{$i} {
@include grid-unit-normal($i);
}
}
@for $i from 1 through 12 {
@media screen and (max-width: $breakpoint-xs) {
.#{$class-col}-#{$xs}-#{$i} {
@include grid-unit($i);
}
}
}
@for $i from 1 through 12 {
@media screen and (min-width: $breakpoint-sm) {
.#{$class-col}-#{$sm}-#{$i} {
@include grid-unit($i);
}
}
}
@for $i from 1 through 12 {
@media screen and (min-width: $breakpoint-md) {
.#{$class-col}-#{$md}-#{$i} {
@include grid-unit($i);
}
}
}
@for $i from 1 through 12 {
@media screen and (min-width: $breakpoint-lg) {
.#{$class-col}-#{$lg}-#{$i} {
@include grid-unit($i);
}
}
}
@for $i from 1 through 12 {
@media screen and (min-width: $breakpoint-xl) {
.#{$class-col}-#{$xl}-#{$i} {
@include grid-unit($i);
}
}
}
//12 column grid CSS code result
/*Result*/
.col-1 {
float: left;
margin-right: 1%;
width: 7.3333333333%;
}
.col-2 {
float: left;
margin-right: 1%;
width: 15.6666666667%;
}
.col-3 {
float: left;
margin-right: 1%;
width: 24%;
}
.col-4 {
float: left;
margin-right: 1%;
width: 32.3333333333%;
}
.col-5 {
float: left;
margin-right: 1%;
width: 40.6666666667%;
}
.col-6 {
float: left;
margin-right: 1%;
width: 49%;
}
.col-7 {
float: left;
margin-right: 1%;
width: 57.3333333333%;
}
.col-8 {
float: left;
margin-right: 1%;
width: 65.6666666667%;
}
.col-9 {
float: left;
margin-right: 1%;
width: 74%;
}
.col-10 {
float: left;
margin-right: 1%;
width: 82.3333333333%;
}
.col-11 {
float: left;
margin-right: 1%;
width: 90.6666666667%;
}
.col-12 {
float: left;
margin-right: 1%;
width: 99%;
}
//Breakpoints for col-1
@media screen and (max-width: 575px) {
.col-xs-1 {
float: left;
margin-right: 1%;
width: 7.3333333333% !important;
}
}
@media screen and (min-width: 576px) {
.col-sm-1 {
float: left;
margin-right: 1%;
width: 7.3333333333% !important;
}
}
@media screen and (min-width: 768px) {
.col-md-1 {
float: left;
margin-right: 1%;
width: 7.3333333333% !important;
}
}
@media screen and (min-width: 992px) {
.col-lg-1 {
float: left;
margin-right: 1%;
width: 7.3333333333% !important;
}
}
@media screen and (min-width: 1200px) {
.col-xl-1 {
float: left;
margin-right: 1%;
width: 7.3333333333% !important;
}
}
$sizes: 0, 0.5rem, 1rem, 1.5rem, 2rem, 2.5rem, 3rem, 3.5rem, 4rem, 4.5rem, 5rem;Also, Bootstrap only includes 4 levels of sizing classes for 25%, 50%, 75% and 100% width and height, in our framework, we use 10 (from 10% to 100%)and again you can add as many as you need or want to.Here is the code we use to create the classes for width and height:
//VARIABLES
$breakpoints: $sm, $md, $lg, $xl;
$breakpoint-xs: 575px;
$breakpoint-sm: 576px;
$breakpoint-md: 768px;
$breakpoint-lg: 992px;
$breakpoint-xl: 1200px;
$breaks: $breakpoint-sm, $breakpoint-md, $breakpoint-lg, $breakpoint-xl;
$wh-options: 10;
// Width and Height
@for $i from 1 through 10 {
.h-#{$wh-options * $i}{
height: ($wh-options * $i)#{"% !important"};
}
.w-#{$wh-options * $i}{
width: ($wh-options * $i)#{"% !important"};
}
}
$j: 1;
@each $break in $breaks {
@media screen and (min-width: $break) {
@for $i from 1 through 10 {
.h-#{nth($breakpoints,$j)}-#{$wh-options * $i}{
height: ($wh-options * $i)#{"% !important"};
}
.w-#{nth($breakpoints,$j)}-#{$wh-options * $i}{
width: ($wh-options * $i)#{"% !important"};
}
}
}
$j: $j + 1
}
Wich translates to 100 classes… now we are talking!
//VARIABLES
$xs: xs;
$sm: sm;
$md: md;
$lg: lg;
$xl: xl;
$breakpoints: $sm, $md, $lg, $xl;
$breakpoint-xs: 575px;
$breakpoint-sm: 576px;
$breakpoint-md: 768px;
$breakpoint-lg: 992px;
$breakpoint-xl: 1200px;
$breaks: $breakpoint-sm, $breakpoint-md, $breakpoint-lg, $breakpoint-xl;
$display-flex: justify-content, align-content;
$flex-name: start, end, around, between, center;
$flex-options: flex-start, flex-end, space-around, space-between, center;
$align-flex: align-items, align-self;
$align-names: start, end, center, stretch, baseline;
$align-options: flex-start, flex-end, center, stretch, baseline;
//DISPLAY-FLEX OPTIONS
@each $display in $display-flex {
$i: 1;
@each $option in $flex-options {
.#{$display}-#{nth($flex-name, $i)} {
#{$display}: #{$option} !important;
}
$i: $i + 1;
}
}
$j: 1;
@each $break in $breaks {
@media screen and (min-width: $break) {
@each $display in $display-flex {
$i: 1;
@each $option in $flex-options {
.#{$display}-#{nth($breakpoints,$j)}-#{nth($flex-name, $i)} {
#{$display}: #{$option} !important;
}
$i: $i + 1;
}
}
}
$j: $j + 1
}
//ALIGN-FLEX OPTIONS
@each $align in $align-flex {
$i: 1;
@each $option in $align-options {
.#{$align}-#{nth($align-names, $i)} {
#{$align}: #{$option} !important;
}
$i: $i + 1;
}
}
$j: 1;
@each $break in $breaks {
@media screen and (min-width: $break) {
@each $align in $align-flex {
$i: 1;
@each $option in $align-options {
.#{$align}-#{nth($breakpoints,$j)}-#{nth($align-names, $i)} {
#{$align}: #{$option} !important;
}
$i: $i + 1;
}
}
}
$j: $j + 1
}
@each $direction in $flex-direction {
.flex-#{$direction} {
flex-direction: #{$direction} !important;
}
}
@each $break in $breaks {
@media screen and (min-width: $break) {
@each $direction in $flex-direction {
$j: 1;
.flex-#{$direction}-#{nth($breakpoints,$j)} {
flex-direction: #{$direction} !important;
}
}
}
$j: $j + 1
}
With only 90 lines we were able to create 200 classes!And last but not least:
Add as many classes as you need or want!Remember this is not Bootstrap or Foundation, this is your own Framework and you are doing this to make your work of layering pages easier.Not only we added variables for the palette of colors of the website this framework was created for (just like Bootstrap),
we also added classes to set color to each side of the borders, unlike Bootstrap.
//Variables
$main: #001b41;
$secondary: #465a75;
$main-blue: #003d8f;
$light-blue: #0b9dcc;
$soft-blue: #28cce8;
$secondary-blue: #3364a5;
$light: #fff;
$light-gray: #f6f7f8;
$article-blue: #0357a0;
$footer-blue: #0b2863;
// change _font-color file if more colors are added
$colors: $main, $secondary, $main-blue, $light-blue, $soft-blue, $secondary-blue, $light, $light-gray, $article-blue, $footer-blue;
$color-names: main, secondary, main-blue, light-blue, soft-blue, secondary-blue, light, light-gray, article-blue, footer-blue;
$side-name: "left", "right", "top", "bottom";
//Background & Border Colors
$i: 1;
@each $color in $colors {
.bg-#{nth($color-names, $i)} {
background-color: $color;
}
.border-#{nth($color-names, $i)} {
border: solid 1px $color;
}
.#{nth($color-names, $i)} {
color: $color;
}
$i: $i +1;
}
// change if more colors are added
@each $side in $side-name {
@for $k from 1 through 10{
.border-#{$side}-#{nth($color-names, $k)} {
border-#{$side}: solid 1px #{nth($colors, $k)};
}
}
}
.border-none{
border: none !important;
}
The result for the code above is rightThis is what’s going to make the difference: you are not only saving your website from loading unused classes,
you are also creating useful classes that weren’t in the framework you were using in the first place,and you won’t have to repeat them on every CSS file of your website never again!
ta-da!
claps to you!