Wednesday, June 15, 2022

Tabbed Panes using HTML, CSS and Javascript

TabbedPane.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Tabbed Pane</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      type="text/css"
      media="screen"
      href="TabbedPane-styles.css"
    />
    <script defer src="./TabbedPane-script.js"></script>
  </head>
  <body>
    <div class="tabs">
      <div class="tab-header">
        <div class="active">
          <h2>Hyderabad</h2>
        </div>
        <div>
          <h2>Bengaluru</h2>
        </div>
        <div>
          <h2>Chennai</h2>
        </div>
        <div>
          <h2>Mumbai</h2>
        </div>
      </div>
      <div class="tab-indicator"></div>
      <div class="tab-body">
        <div class="active">
          <h2>Welcome to Hyderabad!!</h2>
          <p>
            Hyderabad is famous for Charminar, Golkonda Fort and delicious
            Chicken Biryani!!
          </p>
        </div>
        <div>
          <h2>Welcome to Bengaluru!!</h2>
          <p>Bengaluru is silicon valley of India!!</p>
        </div>
        <div>
          <h2>Welcome to Chennai!!</h2>
          <p>Chennai is known for its Marina Beach!!</p>
        </div>
        <div>
          <h2>Welcome to Mumbai!!</h2>
          <p>Mumbai is home to many billionaires in India!!</p>
        </div>
      </div>
    </div>
  </body>
</html>


TabbedPane-styles.css

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
}

body {
  font-family: sans-serif;
  color: #343a40;
  line-height: 1;
  max-width: 120rem;
  padding: 6.4rem 3.2rem 3.2rem;
  margin: 0 auto;
}

.tabs {
  background-color: #ddd;
  border-radius: 5px;
  box-shadow: 0 5px 5px rgba(0, 0, 0, 0.2);
  height: 40rem;
}

.tabs .tab-header {
  height: 6rem;
  display: flex;
  align-items: center;
}

.tabs .tab-header > div {
  width: calc(100% / 4);
  text-align: center;
}

.tab-header h2 {
  font-size: 1.8rem;
  transition: all 0.5s ease-in;
}

.tab-header h2 {
  cursor: pointer;
}

.tab-header .active h2 {
  color: #c25f56;
  font-weight: 600;
  transform: scale(1.1);
}

.tabs .tab-indicator {
  width: calc(100% / 4);
  height: 0.5rem;
  border-radius: 5px;
  background-color: #c25f56;
  position: relative;
/*Refer Javascript-left property works for positioned elements*/
  transition: all 0.5s ease-in;
}

.tabs .tab-body {
  background-color: aquamarine;
  height: 33.5rem;
  padding: 5rem;
  position: relative;
  font-size: 1.4rem;
}

.tabs .tab-body div {
  position: absolute;
  display: none;
  transition: all 0.5s ease-in;
}

.tabs .tab-body .active {
  display: block;
}

.tabs .tab-body h2 {
  font-size: 1.8rem;
  margin-bottom: 1.2rem;
}


TabbedPane-script.js

const headerEl = document.querySelector(".tab-header");
const bodyEl = document.querySelector(".tab-body");

const panes = headerEl.querySelectorAll("div");
const paneBodies = bodyEl.querySelectorAll("div");

const indicatorEl = document.querySelector(".tab-indicator");

for (let i = 0; i < panes.length; i++) {
  panes[i].addEventListener("click", (e) => {
    e.preventDefault();
    if (panes[i] !== headerEl.querySelector(".active")) {
      headerEl.querySelector(".active").classList.remove("active");
      panes[i].classList.add("active");

      bodyEl.querySelector(".active").classList.remove("active");
      paneBodies[i].classList.add("active");

      indicatorEl.style.left = `calc(calc(100%/4)*${i})`;
    }
  });
}


Result





Monday, June 13, 2022

Clipping an image

Step 1: In order to clip an image, we use the CSS property clip-path which expects a shape like a circle, ellipse or a polygon. There are CSS functions corresponding to these shapes, 

polygon function expects the vertices of polygon in a clockwise direction.

Example 1:

clip-path: polygon(25% 0%100% 0%75% 100%0% 100%); 

Vertex 1 --25% 0%  refers to move 25% in X direction (horizontal axis) and  0% in Y direction (vertical axis) -- Marked red in the image below.

Vertex 2 --100% 0% refers to move 100% in X direction and 0% in Y direction. -- Marked green in the image below.

Vertex 3 -- 75% 100% refers to move 75% in X direction and 100% in Y direction -- Marked yellow in the image below.

Vertex 4 -- 0% 100% refers to move 0% in X direction and 100% in Y direction -- Marked blue in the image below.


Example 2: 

clip-path: circle(50% at 50% 50%);

means to clip a circle shape with 50% radius (50% of width the current image) at 50% 50% (which refers to the center of the circle i.e 50% in X direction and 50% in Y direction--Marked green in the image below).

NOTE: Make sure the image which is being clipped is a square means the width and height must be same.


Example 3: 

clip-path: ellipse(25% 40% at 50% 50%);


Similar to circle 50% 50% refers the center (marked as orange in the image below) and 25% refers to distance of the major axis (marked red in the image below) and 40% distance of the minor axis (marked green in the image below).





Code

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Learn clip-path in CSS</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
      img {
        height: 500px;
        clip-path: polygon(25% 0%, 100% 0%, 75% 100%, 0% 100%);
      }
    </style>
  </head>
  <body>
    <img src="./Leonardo.webp" alt="Leonardo" />
  </body>
</html>

Result:



Reference: Newbies can use the following websites to get the polygon coordinates of mostly used geometric shapes from the following resources:


1. https://bennettfeely.com/clippy/


2. https://www.cssportal.com/css-clip-path-generator/


Sunday, June 12, 2022

Table using HTML and CSS

Step 1: Create a file Table.html with the following content.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
    <link
      href="https://fonts.googleapis.com/css2?
      family=Inter:wght@400;500;700&display=swap"
      rel="stylesheet"
    />

    <title>Table Component</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      body {
        font-family: "Inter", sans-serif;
        color: #343a40;
        line-height: 1;
        display: flex;
        justify-content: center;
      }

      table {
        width: 800px;
        margin-top: 60px;
        font-size: 18px;
        /* border: 1px solid #343a40;*/
        border-collapse: collapse;
      }

      th,
      td {
        /* border: 1px solid #343a40; */
        padding: 16px 24px;
        text-align: left;
      }

      thead tr {
        background-color: #4c6ef5;
        color: #fff;
      }

      thead th {
        width: 25%;
      }

      tbody tr:nth-child(odd) {
        background-color: #f8f9fa;
      }

      tbody tr:nth-child(even) {
        background-color: #e9ecef;
      }
    </style>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>Image</th>
          <th>Name</th>
          <th>Office</th>
          <th>Age</th>
        </tr>
      </thead>

      <tbody>
        <tr>
          <td><img src="./Leonardo.png" /></td>
          <td>Leonardo</td>
          <td>San Francisco</td>
          <td>33</td>
        </tr>

        <tr>
          <td><img src="./Leonardo.png" /></td>
          <td>Brad Pitt</td>
          <td>London</td>
          <td>35</td>
        </tr>

        <tr>
          <td><img src="./Leonardo.png" /></td>
          <td>Anjolina</td>
          <td>New York</td>
          <td>34</td>
        </tr>

        <tr>
          <td><img src="./Leonardo.png" /></td>
          <td>Paul Walker</td>
          <td>Tokyo</td>
          <td>38</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

Result:



 

Accordion Component with HTML, CSS and Javascript

Accordion Component- This is generally used in the FAQ's section of popular websites.

Accordion.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2
      ?family=Inter:wght@400;500;700&display=swap"
      rel="stylesheet"
    />

    <link rel="stylesheet" href="./Accordion-styles.css" />
    <script defer type="text/javascript" src="./Accordion-script.js"></script>
    <title>Accordion Component</title>
  </head>
  <body>
    <div class="accordion">
      <div class="item">
        <p class="number">01</p>
        <p class="text">What are the skills required a web developer?</p>
        <div class="icon">
          <svg
            xmlns="http://www.w3.org/2000/svg"
            class="up"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            stroke-width="2"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              d="M5 15l7-7 7 7"
            />
          </svg>

          <svg
            xmlns="http://www.w3.org/2000/svg"
            class="down"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            stroke-width="2"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              d="M19 9l-7 7-7-7"
            />
          </svg>
        </div>
        <div class="hidden-box">
          <p>
            Web development is an awesome field where we can design some cool
            web pages with good design, layouts and patterns. The following are
            the skills required for a person to be a professional web developer:
          </p>
          <ul>
            <li>HTML5</li>
            <li>CSS3</li>
            <li>Javascript</li>
          </ul>
        </div>
      </div>

      <div class="item">
        <p class="number">02</p>
        <p class="text">What are the resources to acquire the skills?</p>
        <div class="icon">
          <svg
            xmlns="http://www.w3.org/2000/svg"
            class="up"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            stroke-width="2"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              d="M5 15l7-7 7 7"
            />
          </svg>

          <svg
            xmlns="http://www.w3.org/2000/svg"
            class="down"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            stroke-width="2"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              d="M19 9l-7 7-7-7"
            />
          </svg>
        </div>
        <div class="hidden-box">
          <p>
            A lot of content is availbale online to acquire the skills required
            for web development. Some of the important ones include:
          </p>
          <ul>
            <li>
              <a href="https://web-app-developers.blogspot.com/"
                >Blog for Web Developers</a
              >
            </li>
            <li>
              <a href="https://developer.mozilla.org/en-US/"
                >MDN Documentation</a
              >
            </li>
            <li><a href="https://www.w3schools.com/">W3 Schools</a></li>
          </ul>
        </div>
      </div>

      <div class="item">
        <p class="number">03</p>
        <p class="text">What are the frameworks in javascript?</p>
        <div class="icon">
          <svg
            xmlns="http://www.w3.org/2000/svg"
            class="up"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            stroke-width="2"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              d="M5 15l7-7 7 7"
            />
          </svg>

          <svg
            xmlns="http://www.w3.org/2000/svg"
            class="down"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            stroke-width="2"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              d="M19 9l-7 7-7-7"
            />
          </svg>
        </div>
        <div class="hidden-box">
          <p>
            Javascript is a popular language used in the web which makes a
            webpage dynamic by responding to user actions. However, most modern
            websites use frameworks built on top of javascript for development,
            these include:
          </p>
          <ul>
            <li>Angular JS</li>
            <li>React JS</li>
            <li>Vue JS</li>
          </ul>
        </div>
      </div>
    </div>
  </body>
</html>


Accordion-styles.css

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
}

body {
  font-family: "Inter", sans-serif;
  color: #343a40;
  line-height: 1;
}

.accordion {
  width: 130rem;
  margin: 4rem auto;

  display: flex;
  flex-direction: column;
  gap: 3.6rem;
}

.item {
  box-shadow: 0 0 3.2rem rgba(0, 0, 0, 0.1);
  padding: 3.6rem;
  display: grid;
  grid-template-columns: auto 1fr auto;
  column-gap: 3.6rem;
  row-gap: 4.8rem;
  align-items: center;
  font-size: 2.4rem;
}

.number,
.text {
  font-weight: 500;
}

.number {
  color: #ced4da;
}

.up,
.down {
  width: 3.6rem;
  height: 3.6rem;
}

.down {
  display: none;
}

.hidden-box {
  grid-column: 2;
  display: none;
}

.hidden-box p {
  line-height: 1.6;
  margin-bottom: 2.4rem;
}

.hidden-box ul {
  color: #adb5bd;
  margin-left: 3.2rem;

  display: flex;
  flex-direction: column;
  gap: 1.2rem;
}

a:link,
a:visited {
  text-decoration: none;
  color: #555;
}

a:hover,
a:active {
  color: #087f5b;
}

.open {
  border-top: 0.4rem solid #087f5b;
}

.open .hidden-box {
  display: block;
}

.open .number,
.open .text {
  color: #087f5b;
}

.open .up {
  display: none;
}

.open .down {
  stroke: #087f5b;
  display: block;
}

Accordion-script.js

console.log("Working...");
const accordionEl = document.querySelector(".accordion");
// console.log(accordionEl);

accordionEl.addEventListener("click", (e) => {
  e.preventDefault();
  const itemEl = e.target.closest(".item");
  if (!itemEl) return;

  // console.log(itemEl);

  if (itemEl.classList.contains("open")) itemEl.classList.remove("open");
  else {
  const siblings = itemEl.closest(".accordion").
querySelectorAll(".item");
   siblings.forEach((el) => {
      el.classList.remove("open");
   });
   itemEl.classList.add("open");
  }
});

NOTE: 1. The icon pack used is from heroicons.com.


Result: