Skip to main content

HTML CSS Practice based Questions and Solutions


HTML CSS and Javascript Practice based Questions and Solutions


1. Basic Button

Tech Stack: HTML, JavaScript
Description: Create a button that displays an alert when clicked.

html

Solution

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Basic Button</title>

</head>

<body>

    <button id="alertButton">Click Me</button>

 

    <script>

        document.getElementById("alertButton").addEventListener("click", () => {

            alert("Button clicked!");

        });

    </script>

</body>

</html>


2. Navigation Bar

Tech Stack: HTML, CSS
Description: Create a horizontal navigation bar with links.

html

Solution

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Navigation Bar</title>

    <style>

        body {

            margin: 0;

            font-family: Arial, sans-serif;

        }

        nav {

            background-color: #333;

            overflow: hidden;

        }

        nav a {

            float: left;

            display: block;

            color: white;

            text-align: center;

            padding: 14px 20px;

            text-decoration: none;

        }

        nav a:hover {

            background-color: #575757;

        }

    </style>

</head>

<body>

    <nav>

        <a href="#home">Home</a>

        <a href="#about">About</a>

        <a href="#services">Services</a>

        <a href="#contact">Contact</a>

    </nav>

</body>

</html>


3. Form Design

Tech Stack: HTML, CSS
Description: Design a simple form with input fields and a submit button.

html

Solution

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Form Design</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

            background-color: #f2f2f2;

        }

        form {

            background: white;

            padding: 20px;

            border-radius: 8px;

            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);

        }

        input[type="text"], input[type="email"], input[type="submit"] {

            width: 100%;

            padding: 10px;

            margin: 10px 0;

            border: 1px solid #ccc;

            border-radius: 4px;

        }

        input[type="submit"] {

            background-color: #4CAF50;

            color: white;

            border: none;

        }

        input[type="submit"]:hover {

            background-color: #45a049;

        }

    </style>

</head>

<body>

    <form>

        <label for="name">Name:</label>

        <input type="text" id="name" name="name" placeholder="Enter your name" required>

 

        <label for="email">Email:</label>

        <input type="email" id="email" name="email" placeholder="Enter your email" required>

 

        <input type="submit" value="Submit">

    </form>

</body>

</html>


4. Image Gallery

Tech Stack: HTML, CSS
Description: Create an image gallery with a responsive grid layout.

html

Solution

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Image Gallery</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            margin: 0;

            padding: 0;

            display: flex;

            justify-content: center;

            align-items: center;

            min-height: 100vh;

            background-color: #f0f0f0;

        }

        .gallery {

            display: grid;

            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));

            gap: 10px;

            max-width: 800px;

            width: 100%;

            padding: 10px;

        }

        .gallery img {

            width: 100%;

            border-radius: 8px;

            transition: transform 0.3s;

        }

        .gallery img:hover {

            transform: scale(1.1);

        }

    </style>

</head>

<body>

    <div class="gallery">

        <img src="https://via.placeholder.com/150" alt="Image 1">

        <img src="https://via.placeholder.com/150" alt="Image 2">

        <img src="https://via.placeholder.com/150" alt="Image 3">

        <img src="https://via.placeholder.com/150" alt="Image 4">

        <img src="https://via.placeholder.com/150" alt="Image 5">

        <img src="https://via.placeholder.com/150" alt="Image 6">

    </div>

</body>

</html>

Explanation:

1.      Body Styling:

    • display: flex; centers the gallery in the middle of the screen.
    • background-color: #f0f0f0; sets a light grey background.

2.      Grid Layout for Gallery:

    • display: grid; turns the gallery container into a grid layout.
    • grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));:
      • Automatically adjusts the number of columns.
      • Each image takes a minimum width of 150px and stretches to fill available space.
    • gap: 10px; adds space between images.

3.      Image Styling:

    • width: 100%; ensures images fill their grid cells.
    • border-radius: 8px; rounds the corners of each image.
    • transition: transform 0.3s; animates the hover effect smoothly.

4.      Hover Effect:

    • transform: scale(1.1); slightly enlarges the image when hovered over.
    • Adds interactivity and visual appeal.

Step 3: Making It Responsive

The CSS grid-template-columns property automatically adjusts the number of columns based on screen size.
For smaller devices, images stack vertically, while on larger screens, they form a grid.


Result

When you open this in a browser:

  1. You'll see a grid of images.
  2. Each image resizes based on the screen size.
  3. When you hover over an image, it enlarges slightly with a smooth animation.

 


Comments

Popular posts from this blog

User Registration Form: Purpose, Design, and Code Explanation

  Why Do We Need to Create Forms? Forms are essential in web development as they provide a way for users to interact with web applications by submitting data. They are used for various purposes, including: Collecting User Data : For registration, login, feedback, surveys, and more. Enabling Interactions : Allowing users to send information to the server. Dynamic Applications : Forms are key in creating dynamic web applications like e-commerce websites, social media platforms, etc. Improving User Experience : Forms enable tailored content and services by collecting user preferences or queries. Code Explanation HTML The HTML provides the structure of the form and its elements. Here's a detailed breakdown: html .. <div class="form-container"> A container that wraps the entire form, styled with CSS for layout and design. html .. <h1>Sign Up</h1> A heading that indicates the purpose of ...

CSS Box Model

  CSS Box Model: Comprehensive Guide Overview The CSS Box Model is a foundational concept in web design that governs the layout and spacing of elements on a webpage. It treats every HTML element as a rectangular box, defining how its content, padding, border, and margin interact. Why Do We Need the CSS Box Model? 1.      Precise Layout Control : o     Allows developers to define the exact size and space of elements, including margins, borders, and padding. 2.      Consistency : o     Ensures uniform spacing and layout across different browsers and devices. 3.      Flexibility : o     Adjusts spacing and borders without changing the core content dimensions. Where Do We Need the CSS Box Model? 1.      Website Layout Design : o     Essential for spacing headers, footers, sidebars, and content sections. 2. ...

CSS Units

CSS Units: Comprehensive Notes What Are CSS Units? CSS (Cascading Style Sheets) units are a way to define the size of elements, spacing, and positioning in a web page. These units provide flexibility to design responsive, visually appealing, and functional layouts. CSS units are used in defining properties such as width, height, margin, padding, font size, and more. Example 1: <style>   div {     width: 100px;     height: 50px;   } </style> <div>Box</div> Here, 100px and 50px define the width and height of the div . Example 2: <style>   p {     font-size: 16px;   } </style> <p>This is a paragraph.</p> The font size of the paragraph is set to 16 pixels using CSS units. Purpose of CSS Units CSS units help define scalable and fixed measurements for web elements, ensuring: Consistency in layout design. Adaptability to differ...