Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

Mastering CSS Styling

Learn how to style your HTML documents with CSS. From basics to advanced layouts.

jiocoder TeamUpdated Jun 6, 20264 min readCSS
Mastering CSS Styling

What is CSS?#

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML. CSS describes how elements should be rendered on screen, on paper, or in other media. It controls the layout, colors, fonts, and overall visual appearance of web pages.

History of CSS#

CSS was first proposed by Håkon Wium Lie in 1994. CSS1 was released in 1996, CSS2 in 1998, and CSS3 started development in 1999. CSS3 introduced modules, allowing different parts of CSS to be developed independently.

CSS Syntax#

The basic syntax consists of a selector and a declaration block:

Code
selector {
  property: value;
  property: value;
}

Types of CSS#

There are three ways to add CSS to HTML documents:

  • Inline CSS - Styles applied directly to HTML elements using the style attribute
  • Internal CSS - Styles defined within <style> tags in the HTML document head
  • External CSS - Styles defined in separate .css files and linked using <link> tag
Code
<!-- Inline CSS -->
<p style="color: blue; font-size: 16px;">Text</p>

<!-- Internal CSS -->
<style>
  p { color: blue; }
</style>

<!-- External CSS -->
<link rel="stylesheet" href="styles.css">

CSS Selectors#

Selectors are patterns used to select the element(s) you want to style:

Code
/* Element Selector - selects all <p> elements */
p { color: blue; }

/* Class Selector - selects elements with class="container" */
.container { width: 100%; }

/* ID Selector - selects element with id="header" */
#header { background: #333; }

/* Attribute Selector */
input[type="text"] { border: 1px solid #ccc; }

/* Descendant Selector */
div p { color: red; }

/* Child Selector */
div > p { color: green; }

/* Pseudo-class Selector */
a:hover { color: red; }
button:active { background: blue; }

/* Pseudo-element Selector */
p::first-line { font-weight: bold; }
p::before { content: "Note: "; }

CSS Box Model#

Every HTML element is represented as a rectangular box with four areas:

  • Content - The actual content (text, images, etc.)
  • Padding - Space between content and border
  • Border - A border around the padding
  • Margin - Space outside the border
Code
.box {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
  margin: 10px;
  box-sizing: border-box; /* includes padding and border in width */
}

CSS Colors#

Colors can be specified in various ways:

Code
/* Named colors */
color: red;
color: blue;

/* Hexadecimal */
color: #ff0000;
color: #00ff00;

/* RGB */
color: rgb(255, 0, 0);
color: rgba(255, 0, 0, 0.5); /* with opacity */

/* HSL */
color: hsl(0, 100%, 50%);
color: hsla(0, 100%, 50%, 0.5);

CSS Typography#

Code
p {
  font-family: Arial, sans-serif;
  font-size: 16px;
  font-weight: bold;
  font-style: italic;
  line-height: 1.5;
  text-align: center;
  text-decoration: underline;
  text-transform: uppercase;
  letter-spacing: 2px;
  word-spacing: 5px;
}

CSS Display Property#

The display property specifies how an element is displayed:

Code
/* Block - takes full width, starts on new line */
display: block;

/* Inline - takes only necessary width, stays in line */
display: inline;

/* Inline-block - inline but can have width/height */
display: inline-block;

/* None - element is hidden */
display: none;

/* Flex - flexible box layout */
display: flex;

/* Grid - grid layout */
display: grid;

Flexbox Layout#

Flexbox is a one-dimensional layout method for laying out items in rows or columns:

Code
.container {
  display: flex;
  flex-direction: row; /* or column */
  justify-content: center; /* main axis alignment */
  align-items: center; /* cross axis alignment */
  flex-wrap: wrap; /* wrap items */
  gap: 20px; /* space between items */
}

.item {
  flex: 1; /* grow and shrink */
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 200px;
}

CSS Grid#

Grid is a two-dimensional layout system for creating complex layouts:

Code
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
  grid-template-rows: auto;
  gap: 20px; /* space between grid items */
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
}

.grid-item {
  grid-column: span 2; /* span 2 columns */
  grid-row: 1; /* row 1 */
  grid-area: header; /* named area */
}

CSS Positioning#

Code
/* Static - default positioning */
position: static;

/* Relative - relative to normal position */
position: relative;
top: 10px;
left: 20px;

/* Absolute - relative to nearest positioned ancestor */
position: absolute;
top: 0;
right: 0;

/* Fixed - relative to viewport */
position: fixed;
bottom: 0;
left: 0;

/* Sticky - relative until scrolled, then fixed */
position: sticky;
top: 0;

CSS Responsive Design#

Media queries allow you to apply styles based on device characteristics:

Code
/* Mobile first approach */
.container {
  width: 100%;
}

/* Tablet */
@media (min-width: 768px) {
  .container {
    width: 750px;
  }
}

/* Desktop */
@media (min-width: 1024px) {
  .container {
    width: 1200px;
  }
}

CSS Animations and Transitions#

Code
/* Transition */
.button {
  transition: background-color 0.3s ease;
}
.button:hover {
  background-color: blue;
}

/* Animation */
@keyframes slide {
  from { transform: translateX(0); }
  to { transform: translateX(100px); }
}

.element {
  animation: slide 2s infinite;
}

Best Practices#

  • Use external CSS files for better organization
  • Follow a naming convention (BEM, OOCSS, etc.)
  • Use CSS variables for reusable values
  • Write mobile-first responsive CSS
  • Minimize specificity conflicts
  • Use flexbox and grid for layouts
  • Optimize CSS for performance
  • Use semantic class names

Ready to test your knowledge?

Take the quiz to reinforce what you've learned in this article.

Start Quiz

Article discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment