DEV Community

Cover image for Web Dev Day 4: CSS3 Guide Part 2
Bhupesh Kumar
Bhupesh Kumar

Posted on

Web Dev Day 4: CSS3 Guide Part 2

Alpha Channel:

The alpha channel, often referred to as opacity or transparency, is a component of the RGB color model that represents the degree of transparency of a color. It determines how much of an underlying color or background is visible through a particular color.

In CSS, the alpha channel is typically represented using the rgba() color function or the hsla() color function:

1. rgba() Function:

Stands for "red, green, blue, alpha."
The alpha value is specified as a number between 0 (fully transparent) and 1 (fully opaque).

/* RGBA color with 50% opacity */
background-color: rgba(255, 0, 0, 0.5); /* Red color with 50% opacity */
Enter fullscreen mode Exit fullscreen mode

2. hsla() Function:

Stands for "hue, saturation, lightness, alpha."
The alpha value is specified as a number between 0 (fully transparent) and 1 (fully opaque).

/* HSLA color with 50% opacity */
background-color: hsla(0, 100%, 50%, 0.5); /* Red color with 50% opacity */
Enter fullscreen mode Exit fullscreen mode

Opacity:

In CSS, the opacity property is used to set the transparency level of an element. It affects the entire element, including its content and any child elements. The opacity property takes a value between 0 (completely transparent) and 1 (completely opaque).

/* Make the element 70% transparent */
div {
    opacity: 0.7;
}
Enter fullscreen mode Exit fullscreen mode

CSS Transition:

CSS transitions provide a way to smoothly animate the changing of property values over time. Transitions are often used to create subtle and visually appealing effects when interacting with web elements. Here's an overview of CSS transitions:

selector {
    property: value;
    transition: property duration timing-function delay;
}
Enter fullscreen mode Exit fullscreen mode
/* Apply a transition to the background-color property */
button {
    background-color: blue;
    transition: background-color 0.5s ease-in-out;
}

/* Change the background color on hover */
button:hover {
    background-color: red;
}

Enter fullscreen mode Exit fullscreen mode

Transition Shorthand:

The transition property can also be written using the shorthand syntax:

selector {
    transition: property duration timing-function delay;
}
Enter fullscreen mode Exit fullscreen mode

Common Transition Properties:

  • all: Transitions all properties.
  • transform: Transitions for 2D and 3D transformations.
  • opacity: Transitions for opacity changes.

Example with transform:

/* Apply a transition to the transform property */
div {
    transform: scale(1);
    transition: transform 0.3s ease-in-out;
}

/* Scale the element on hover */
div:hover {
    transform: scale(1.2);
}
Enter fullscreen mode Exit fullscreen mode

CSS Transform:

CSS transform is a CSS property that allows you to apply various visual transformations to elements on a webpage. These transformations include translation, rotation, scaling, and skewing, enabling you to manipulate the appearance and layout of elements in two-dimensional or three-dimensional space.

With CSS transform, you can:

  1. Translate: Move an element along the X and Y axes.
  2. Rotate: Rotate an element clockwise or counterclockwise around a fixed point.
  3. Scale: Resize an element along the X and Y axes.
  4. Skew: Distort an element along the X and Y axes.
  5. 3D Transformations: Apply transformations in three-dimensional space, such as rotating around the X, Y, or Z axis.

These transformations can be applied individually or combined together to create complex visual effects and animations. CSS transform is widely used in web development to create responsive designs, animations, and interactive user interfaces.

Here's an example of how CSS transform can be applied:

.element {
  transform: translate(50px, 50px) rotate(45deg) scale(1.5) skew(10deg, 20deg);
}
Enter fullscreen mode Exit fullscreen mode

This CSS rule translates the element by 50 pixels on both the X and Y axes, rotates it by 45 degrees, scales it to 1.5 times its original size, and skews it by 10 degrees along the X axis and 20 degrees along the Y axis.

Box Shadow:

CSS box-shadow is a property used to add shadow effects to elements. It takes parameters for horizontal and vertical offsets, blur radius, spread radius, color, and inset (optional).

Example:

.box {
  box-shadow: 2px 2px 4px #888;
}
Enter fullscreen mode Exit fullscreen mode

This code snippet adds a shadow with a horizontal offset of 2 pixels, vertical offset of 2 pixels, blur radius of 4 pixels, and a gray color.

Background Image:

CSS background-image is a property used to set one or more background images for an element. It takes a URL pointing to the image file to be used as the background.

Example:

.element {
  background-image: url('image-url');
}
Enter fullscreen mode Exit fullscreen mode

This code snippet sets the image specified by 'image-url' as the background for the .element.

Card Hover Effect:

A card hover effect is a visual enhancement applied to a card element when a user hovers over it. This effect typically involves changes in properties like background color, shadow, scale, or position, creating an interactive and engaging experience.

Here's a concise example using CSS:

.card {
  /* Card styles */
  transition: transform 0.3s ease;
}

.card:hover {
  transform: translateY(-5px);
}
Enter fullscreen mode Exit fullscreen mode

In this example, when the user hovers over the .card, it moves upward by 5 pixels due to the transform: translateY(-5px) property. The transition property ensures a smooth animation.

Position Property:

The CSS position property determines how an element is positioned on a webpage. It can take values like static, relative, absolute, fixed, or sticky, each affecting the element's positioning behavior in different ways.

Flexbox:

Flexbox is a layout model in CSS that allows you to create flexible and responsive layouts with ease.

  • Display Flex:

    • display: flex; turns a container's children into flex items, enabling the use of flexbox properties.
  • Flex Direction:

    • flex-direction determines the main axis of the flex container and the direction in which flex items are laid out. Values include row, row-reverse, column, and column-reverse.
  • Justify Content:

    • justify-content aligns flex items along the main axis of the flex container. Common values are flex-start, flex-end, center, space-between, and space-around.
  • Flex Wrap:

    • flex-wrap controls whether flex items are forced onto a single line or can wrap onto multiple lines. Values include nowrap, wrap, and wrap-reverse.
  • Align Items:

    • align-items aligns flex items along the cross axis of the flex container. Common values are flex-start, flex-end, center, baseline, and stretch.
  • Align Content:

    • align-content aligns flex lines (when there is more than one) along the cross axis. Values include flex-start, flex-end, center, space-between, space-around, and stretch.
  • Align Self:

    • align-self allows individual flex items to override the align-items property for alignment along the cross axis.
  • Flex Sizing:

    • Flex items can be given a flex basis, flex-grow factor, and flex-shrink factor to control their size within the flex container.
  • Flex Shorthand:

    • The flex shorthand property combines flex-grow, flex-shrink, and flex-basis properties in a single declaration.

Flexbox simplifies the process of creating complex layouts, making it easier to create responsive designs and align content in a more dynamic and flexible way.

What is Grid?

Grid is a layout model in CSS that enables you to create complex grid-based layouts with rows and columns.

  • Grid Model:

    • Grid layout divides a container into rows and columns, creating a grid structure for arranging elements.
  • Grid Template:

    • The grid-template property defines the layout of the grid using a combination of track sizing functions and line names.

    Repeat:

    • The repeat() function specifies a repeating pattern for track sizing. For example, repeat(3, 1fr) repeats the track size 1fr three times.
  • Grid Gaps:

    • The grid-gap property sets the size of the gap between grid rows and columns. It is a shorthand for grid-row-gap and grid-column-gap.
  • Grid Columns:

    • The grid-template-columns property defines the size of grid columns. You can specify the size of each column individually or use track sizing functions like auto, 1fr, etc.
  • Grid Rows:

    • The grid-template-rows property defines the size of grid rows. Similar to grid-template-columns, you can specify the size of each row individually or use track sizing functions.
  • Grid Properties:

    • Other grid properties include grid-template-areas for defining named grid areas, grid-column and grid-row for positioning grid items within the grid, and various alignment properties like justify-items, align-items, justify-content, and align-content for aligning grid items within the grid container.

Example:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 100px auto;
  grid-gap: 10px;
}

.item {
  grid-column: 2 / span 1;
  grid-row: 1;
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  • The .container is a grid container with three columns of equal width and two rows (100 pixels in height for the first row and auto for the second row).
  • The .item is a grid item positioned starting from the second column and spanning 1 column, and placed in the first row of the grid.

CSS Animation

CSS Animation:

CSS animations allow you to animate the properties of an element over a specified duration using keyframes.

  • animation-name: Specifies the name of the @keyframes rule defining the animation's keyframes.
  • animation-duration: Sets the duration of the animation in seconds or milliseconds.
  • animation-timing-function: Defines the timing function for the animation, specifying the pace of the animation's progression.
  • animation-delay: Specifies the delay before the animation starts.
  • animation-iteration-count: Sets the number of times the animation should repeat.
  • animation-direction: Defines whether the animation should play forwards, backwards, alternate between forward and backward, or alternate in reverse.

Examples:

/* Define keyframes */
@keyframes slide {
  0% { transform: translateX(0); }
  100% { transform: translateX(200px); }
}

/* Apply animation */
.element {
  animation-name: slide;
  animation-duration: 2s;
  animation-timing-function: ease-in-out;
  animation-delay: 1s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}
Enter fullscreen mode Exit fullscreen mode

Animation Shorthand

.element {
  animation: slide 2s ease-in-out 1s infinite alternate;
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  • .element is the selector for the element to be animated.
  • The animation name is slide.
  • Duration is 2 seconds.
  • Timing function is ease-in-out.
  • Delay is 1 second.
  • Iteration count is infinite.
  • Direction is alternate.

% in Animation

In CSS animations, percentages within @keyframes define different stages of the animation's duration.

@keyframes example-animation {
  0% {
    /* Styles at the beginning of the animation */
  }
  50% {
    /* Styles halfway through the animation */
  }
  100% {
    /* Styles at the end of the animation */
  }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  • 0% represents the start of the animation.
  • 50% represents the middle of the animation.
  • 100% represents the end of the animation.

You can specify styles at various percentages to create different effects throughout the animation's duration.

Media Queries

Media queries in CSS allow you to apply different styles based on various device characteristics, such as screen size, orientation, and device type.

  • Orientation:
    • Media queries can target the orientation of the device, allowing you to apply different styles based on whether the device is in portrait or landscape mode.

Example:

@media (orientation: portrait) {
  /* Styles for portrait orientation */
}

@media (orientation: landscape) {
  /* Styles for landscape orientation */
}
Enter fullscreen mode Exit fullscreen mode

Media Features: https://developer.mozilla.org/en-US/docs/Web/CSS/@media

z index

The z-index property in CSS controls the stacking order of positioned elements. It determines which elements appear on top of others when they overlap on the z-axis.

Example:

.element {
  position: relative;
  z-index: 1;
}
Enter fullscreen mode Exit fullscreen mode

Basic Design Priciples

Color Theory

Color theory explores the principles and guidelines behind the use of color in art and design. Here's a brief overview:

  1. Color Wheel:

    • The color wheel is a visual representation of colors arranged in a circular format. It consists of primary colors (red, blue, yellow), secondary colors (orange, green, purple), and tertiary colors (mixtures of primary and secondary colors).
  2. Color Harmony:

    • Color harmony refers to the pleasing combination of colors in a design. Common color harmonies include:
      • Complementary: Colors opposite each other on the color wheel.
      • Analogous: Colors adjacent to each other on the color wheel.
      • Triadic: Three colors equally spaced on the color wheel.
      • Monochromatic: Variations of a single color.
  3. Color Properties:

    • Hue: The actual color of an object (e.g., red, blue, green).
    • Saturation: The intensity or purity of a color. More saturated colors are vivid, while less saturated colors are dull.
    • Value: The lightness or darkness of a color. Tints are lighter values, while shades are darker values.
  4. Color Psychology:

    • Colors can evoke different emotions and associations. For example, red may symbolize passion or danger, while blue may represent calmness or trust.
  5. Color in Design:

    • In design, color is used to convey meaning, create visual interest, establish hierarchy, and evoke emotions. Understanding color theory helps designers make informed color choices to achieve their design goals.

Canva Color Wheel: https://www.canva.com/colors/color-wheel/

Typography

Typography is the art and technique of arranging type to make written language legible, readable, and appealing when displayed. It involves selecting fonts, adjusting font size, line spacing, and letter spacing to create a harmonious and visually pleasing design.

Google Fonts

Google Fonts is a collection of free, open-source fonts provided by Google. It offers a wide variety of high-quality fonts that can be easily integrated into web projects. Web designers and developers can access Google Fonts via a simple link or import statement in their HTML or CSS files, making it convenient to use custom fonts in their designs.

https://fonts.google.com/

Icons

https://fonts.google.com/icons

https://fontawesome.com/icons

https://cdnjs.com/libraries/font-awesome

https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css

CSS Mini Project

This repository contains a simple HTML and CSS project showcasing a sidebar menu design. The project is designed to be lightweight and demonstrate fundamental techniques in creating responsive web layouts using HTML and CSS.

Features

  • Responsive sidebar menu
  • Utilization of Font Awesome icons
  • Basic hover effects

Technologies Used

  • HTML
  • CSS
  • Font Awesome

Github Link: https://github.com/bhupeshk3014/css-mini

MDN Docs: https://developer.mozilla.org/en-US/docs/Web/CSS

Top comments (0)