Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                //PEN HEADER
.header
  h1.header__title Gradient Navigation
  .header__btns.btns
    a.header__btn.btn(href="https://github.com/nat-davydova/gradient-navigation" title="Check on Github" target="_blanc") Check on Github
    
//PEN CONTENT
.content

  .content__section
  
    h2.content__title Pick a gradient direction
    
    .content__section-inner
      
      .btns
      
        button.btn.grad-btn.grad-btn--top(type="button" title="to top" data-dir="top")
          i(class="fas fa-long-arrow-alt-right")

        button.btn.grad-btn.grad-btn--left(type="button" title="to left" data-dir="left")
          i(class="fas fa-long-arrow-alt-right")

        button.btn.btn--sm.grad-btn.grad-btn--bottom(type="button" title="to bottom" data-dir="bottom")
          i(class="fas fa-long-arrow-alt-right")

        button.btn.btn--sm.grad-btn(type="button" title="to right" data-dir="right")
          i(class="fas fa-long-arrow-alt-right")

        button.btn.btn--sm.grad-btn.grad-btn--top-left(type="button" title="to top left" data-dir="top left")
          i(class="fas fa-long-arrow-alt-right")

        button.btn.btn--sm.grad-btn.grad-btn--top-right.js-active(type="button" title="to top right" data-dir="top right")
          i(class="fas fa-long-arrow-alt-right")

        button.btn.btn--sm.grad-btn.grad-btn--bottom-left(type="button" title="to bottom left" data-dir="bottom left")
          i(class="fas fa-long-arrow-alt-right")

        button.btn.btn--sm.grad-btn.grad-btn--bottom-right(type="button" title="to bottom right" data-dir="bottom right")
          i(class="fas fa-long-arrow-alt-right")
          
  .content__section
    
    .content__section-inner.nav-area(data-gradient="#00fff0, #5c9ff7 40%, #d322ff 80%" data-gradient-direction="right top")
      
      .nav-area__item
      
        .nav-area__item-icon
          i(class="fas fa-home")
          
        .nav-area__item-title
          p Home
          
      .nav-area__item
      
        .nav-area__item-icon
          i(class="fas fa-user")
          
        .nav-area__item-title
          p Profile
          
      .nav-area__item
      
        .nav-area__item-icon
          i(class="fas fa-cog")
          
        .nav-area__item-title
          p Settings
          
      .nav-area__item
      
        .nav-area__item-icon
          i(class="fas fa-credit-card")
          
        .nav-area__item-title
          p Payments
          
      .nav-area__item
      
        .nav-area__item-icon
          i(class="fas fa-phone-alt")
          
        .nav-area__item-title
          p Contact
          
      .nav-area__item
      
        .nav-area__item-icon
          i(class="fas fa-comment")
          
        .nav-area__item-title
          p Support
        
      
     
        
      
              
            
!

CSS

              
                //mixins
@mixin transition-mix ($property: all, $duration: 0.2s, $timing: linear, $delay: 0s) {
  transition-property: $property;
  transition-duration: $duration;
  transition-timing-function: $timing;
  transition-delay: $delay;
}

//variables
$theme-font-color: #2c2c2c;


//common styles
body {
  font: {
    family: 'Roboto', sans-serif;
  }
  color: $theme-font-color;
  
  a {
    color: inherit;
    text-decoration: none;
  }
}

//btn styles
.btns {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

.btn {
  @include transition-mix;
  
  padding: 10px 20px;
  margin-right: 10px;
  margin-bottom: 10px;
  
  background-color: #fff;
  border: 1px solid $theme-font-color;
  border-radius: 3px;
  
  cursor: pointer;
  
  outline: none;
  
  &:last-child {
    margin-right: 0;
  }
  
  &:hover,
  &.js-active{
    color: #fff;
    
    background-color: $theme-font-color;
  }
}

//header styles
.header {
  max-width: 500px;
  margin: 50px auto;
  
  text-align: center;
}

.header__title {
  font: {
    weight: 500;
  }
}

//content styles
.content {
  max-width: 700px;
  margin: auto;
}

.content__section {
  margin-bottom: 50px;
}

.content__title {
  font: {
    weight: 500;
  }
  text-align: center;
}

//gradient btns styles
.grad-btn--top {
  
  i {
    transform: rotate(- 90deg);
  }
}

.grad-btn--bottom {
  
  i {
    transform: rotate(90deg);
  }
}

.grad-btn--left {
  
  i {
    transform: rotate(180deg);
  }
}

.grad-btn--top-left {
  
  i {
    transform: rotate(-135deg);
  }
}

.grad-btn--top-right {
  
  i {
    transform: rotate(-45deg);
  }
}

.grad-btn--bottom-left {
  
  i {
    transform: rotate(135deg);
  }
}

.grad-btn--bottom-right {
  
  i {
    transform: rotate(45deg);
  }
}

//nav area styles
.nav-area {
  display: grid;
  grid-gap: 20px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.nav-area__item {
  @include transition-mix();
  
  padding: 40px 20px;
  
  text-align: center;
  color: #fff;
  
  border-radius: 5px;
  background-repeat: no-repeat;
  
  cursor: pointer;
  
  &:hover {
    box-shadow: 
      0 4px 5px 0 rgba(0,0,0,0.14), 
      0 1px 10px 0 rgba(0,0,0,0.12), 
      0 2px 4px -1px rgba(0,0,0,0.3);
  }
}

.nav-area__item-icon {
  font: {
    size: 25px;
  }
}

.nav-area__item-title  {
  
  & > p {
    margin-bottom: 0;
  }
  
}
              
            
!

JS

              
                const nav = document.querySelector('.nav-area');
const navItems = document.querySelectorAll('.nav-area__item');

//grabbing gradient value
const gradValue = () => {
  
  return nav.dataset.gradient;
  
};

//grabbing gradient direction
const gradDir = () => {
  
  return nav.dataset.gradientDirection; 
  
};

//building a gradient image
const buildGrad = (value, direction) => {
  
  return `to ${direction}, ${value}`;
  
};

//calc background-size
const gradSize = () => {
  
  //get number of columns
  let firstItemPosX = navItems[0].getBoundingClientRect().left;
  let cols = 1;
  
  for(let i = 1; i < navItems.length; i++) {
    
    if (navItems[i].getBoundingClientRect().left > firstItemPosX) {
      cols++
    } else {
      break;
    }
    
  }
  
  //get number of rows
  let itemPosY = navItems[0].getBoundingClientRect().top;
  let rows = 1;
  
  for(let i = 1; i < navItems.length; i++) {
    
    if (navItems[i].getBoundingClientRect().top > itemPosY) {
      rows++
      
      itemPosY = navItems[i].getBoundingClientRect().top;
    }
    
  }
  
  // get size of 1 item
  const itemWidth = navItems[0].offsetWidth;
  const itemHeight = navItems[0].offsetHeight;
  
  //get gradient size
  let gradSizeX = itemWidth * cols;
  let gradSizeY = itemHeight * rows;
  
  return [gradSizeX, gradSizeY, cols, rows];
  
};

// add col and row for every item
const colRow = () => {
  
  let row = 1;
  let col = 1;
  
  let xItemPos = navItems[0].getBoundingClientRect().left;
  
  //setting cols
  for(let i = 0; i < navItems.length; i++) {
    
      if (navItems[i].getBoundingClientRect().left > xItemPos) {
        col++;
    } else {
      col = 1;
    }
    
    navItems[i].dataset.col = col;
  }
  
  //setting rows
  let yItemPos = navItems[0].getBoundingClientRect().top;
  
  for(let i = 0; i < navItems.length; i++) {
    
    if (navItems[i].getBoundingClientRect().top > yItemPos) {
      row++
      
      yItemPos = navItems[i].getBoundingClientRect().top;
    }
    
    navItems[i].dataset.row = row;
    
  }
};

//setting gradient styles
const setGrad = elem => {
  
  //background image style
  const bgImg = `linear-gradient(${buildGrad(gradValue(), gradDir())})`;
  
  elem.style.backgroundImage = bgImg;
  
  //background size style
  const bgSizeX = `${gradSize()[0]}`;
  const bgSizeY = `${gradSize()[1] + 5}`;
  
  elem.style.backgroundSize = `${bgSizeX}px ${bgSizeY}px`;
  
  //background position style
  const bgPosX = bgSizeX * (elem.dataset.col - 1) / gradSize()[2];
  const bgPosY = bgSizeY * (elem.dataset.row - 1) / gradSize()[3];
  
  elem.style.backgroundPosition = `${-bgPosX}px ${-bgPosY}px`;
  
};

//setting gradient styles function
const setGradientBg = () => {
    navItems.forEach(elem => {

      colRow();

      setGrad(elem);

  });
}

//setting gradient onload
setGradientBg();

//recalc and set gradient on resize
window.addEventListener('resize', setGradientBg);

//rebuild grad on direction button click 
const dirBtns = document.querySelectorAll('.grad-btn');

dirBtns.forEach(elem => {
  
  elem.addEventListener('click', () => {
    
    //setting new direction
    const dir = elem.dataset.dir;
    
    nav.dataset.gradientDirection = dir;
    
    //recalc background
    setGradientBg();
    
    //make clicked btn active
    dirBtns.forEach(elem => elem.classList.remove('js-active'));
    
    elem.classList.add('js-active');

  });
  
});


              
            
!
999px

Console