I am struggling to style a form effectively with CSS grid

I am working on setting up a 2-column HTML form using CSS grid.

In the first column, I have labels placed on individual rows. In the second column, I have form elements also placed on individual rows.

This is my initial attempt at this task and I am still learning CSS and HTML. If anyone could help me understand where I may be going wrong, that would be really helpful. Thank you.

Below is the CSS and HTML code for the form.

@media screen and (min-width: 600px) {
      form {justify-self: start;
            max-width: 600px;
            display: grid;
            grid-template-columns: 20% 1fr%;
            grid-template-rows: repeat(6, 20%);
            grid-gap: 20px;
            font-family: Arial, Helvetica, sans-serif;
      }

      label[for="menu"] {
            grid-area: 1 / 1 / 1 / 2;
      }

      label[for="bio"] {
            grid-area: 2 / 1 / 2 / 2;
      }

      label[for="name"]{
            grid-area: 3 / 1 / 3 / 2;
      }

      label [for="email"]{
            grid-area: 4 / 1 / 4 / 2;
      }

      .grid5 {
            grid-area: 5 / 1 / 5 / 2;
      }

      .grid6 {
            grid-area: 6 / 1 / 6 / 2;
      }

      select {
            grid-area: 1 / 2 / 1 / 3;
      }

      textarea {
            grid-area: 2 / 2 / 2 / 3;
      }

      #name {
            grid-area: 3 / 2 / 3 / 3;
      }

      #email {
            grid-area: 4 / 2 / 4 / 3;
      }


}
<div class="form">
    <form action="" method="post">
      <div id="grid1"> <label for="menu">Type</label> <select name="pet_name" id="menu">
          <option>Cat</option>
          <option>Dog</option>
          <option>Hamster</option>
          <option>Zebra</option>
          <option>Other</option>
        </select>
      </div>
      <div id="grid2"> <label for="bio">Biography</label> <textarea name="pet_bio" id="bio"></textarea> </div>
      <div id="grid3"> <label for="name">Name</label> <input type="text" id="name" name="pet_name"> </div>
      <div id="grid4"> <label for="email">Owners Email</label> <input id="email" name="pet_owner_email"></div>
      <div id="grid5"> <button type="submit" id="new-pet-submit-button">Create New Pet</button> </div>
      <div id="grid6"> <button type="reset" id="reset">Reset</button></div>

    </form>
  </div>

Answer №1

CSS Grids have an impact on the first-level objects exclusively.

By removing the div containers around your form elements, your CSS code will function correctly.

Here is an example:

@media screen and (min-width: 600px) {
      form {justify-self: start;
            max-width: 600px;
            display: grid;
            grid-template-columns: 20% 1fr%;
            grid-template-rows: repeat(6, 20%);
            grid-gap: 20px;
            font-family: Arial, Helvetica, sans-serif;
      }

      label[for="menu"] {
            grid-area: 1 / 1 / 1 / 2;
      }

      label[for="bio"] {
            grid-area: 2 / 1 / 2 / 2;
      }

      label[for="name"]{
            grid-area: 3 / 1 / 3 / 2;
      }

      label [for="email"]{
            grid-area: 4 / 1 / 4 / 2;
      }

      .grid5 {
            grid-area: 5 / 1 / 5 / 2;
      }

      .grid6 {
            grid-area: 6 / 1 / 6 / 2;
      }

      select {
            grid-area: 1 / 2 / 1 / 3;
      }

      textarea {
            grid-area: 2 / 2 / 2 / 3;
      }

      #name {
            grid-area: 3 / 2 / 3 / 3;
      }

      #email {
            grid-area: 4 / 2 / 4 / 3;
      }
}
<div class="form">
   <form action="/pets" method="post">
     <label for="menu">Type</label> <select name="pet_name" id="menu">
         <option>Cat</option>
         <option>Dog</option>
         <option>Hamster</option>
         <option>Zebra</option>
         <option>Other</option>
       </select>
    
     <label for="bio">Biography</label> <textarea name="pet_bio" id="bio"></textarea>
     <label for="name">Name</label> <input type="text" id="name" name="pet_name">
     <label for="email">Owners Email</label> <input id="email" name="pet_owner_email">
     <button type="submit" id="new-pet-submit-button">Create New Pet</button>
      <button type="reset" id="reset">Reset</button>

   </form>
 </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with HTML not being displayed during Eclipse Eclipse html not showing up on

This is an example of a servlet: package userInterface; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servle ...

Manipulating anchor links with jQuery by adding and removing classes

I am struggling to understand why I cannot remove a class from an element and then add a new one. Changing the CSS using .css(...) works, but for some reason .removeClass and .addClass are not functioning. Interestingly, I am able to successfully add and ...

Is there a way to create an <a> element so that clicking on it does not update the URL in the address bar?

Within my JSP, there is an anchor tag that looks like this: <a href="patient/tools.do?Id=<%=mp.get("FROM_RANGE") %>"> <%= mp.get("DESCRITPION") %></a>. Whenever I click on the anchor tag, the URL appears in the Address bar. How can ...

Align an image with text using CSS and HTML

I'm having trouble with my CSS and HTML code. I want to align the text to the right of an image in a grid format, but it keeps appearing below the image instead. Here is the code snippet: <html> <head> <style type="text/css"> #conta ...

App.jsx line 11 is throwing an error due to an undefined property 'TodoComponent'

While attempting to style a ReactJS component, I encountered an error in my browser's development console: App.jsx:11 Uncaught TypeError: Cannot read property 'TodoComponent' of undefined at App.render (App.jsx:11) I have tried imp ...

Share data between two PHP files using $.post

I have implemented a contact form that sends data through jQuery using $.post. JavaScript Code $(function () { $("#contact_form").submit(function (event) { event.preventDefault(); $.post("<?php echo home_url('/_asset/contact.p ...

Get the ID from a row that was created dynamically

I have a webpage that pulls information from my database and displays it. Users should be able to click on the "details" link to see more details about a particular record, or click on an input box in the "check" column and submit to update the record stat ...

Creating immersive experiences with fabric.js in fullscreen mode on canvas

Attempting to implement a fullscreen mode (using HTML5 fullscreen) for a canvas object created with Fabric.js. var canvas = fabricCanvas.getSelectionElement(); if(canvas.requestFullScreen) { canvas.requestFullScreen(); } else if(canvas.webkitRequestFu ...

Is it possible to make nested HTML list items align to the left?

Is it possible to align nested HTML list items to the left? For example: Bat Cat Ratonetwothree Mat <ul> <li>Bat</li> <li>Cat</li> <li>Rat<ul><li>one</li><li>two< ...

Having trouble with cc email addresses causing problems in Outlook

I'm encountering an issue where there is a '&' symbol in one of the cc email addresses. Due to the default behavior of the browser, the complete email address is not being displayed properly in Outlook. Here's an example: let to='a ...

The powers of jQuery and CSS selectors

Previously, I utilized the following code to extract text from the data-placeholder attribute and apply it as a placeholder for my div. [contentEditable=true]:empty:not(:focus):before { content:attr(data-placeholder) } While this method worked well b ...

Collaborating with multiple forms and files in PHP using jQuery and AJAX operations

Need help with the title for this question. There are two input fields and buttons in index.php <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST"> <input type="text" class="for ...

What's the best way to arrange my containers for optimal placement?

I'm attempting to create a Thymeleaf template for rendering a printable PDF. Here is what I currently have: <html xmlns:th="http://www.thymeleaf.org" > <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" ...

Guide to adjusting the screen resolution on your iPad 3

Hello everyone, I'm fairly new to designing for the iPad 3. Could someone please provide some guidance on how to set media queries specifically for the iPad 3? Thank you in advance. ...

What is the best way to crop an image to focus solely on the center portion?

Using Bootstrap 3, how can I display an image cropped in a .col-sm-6 grid column? The image is 720px wide. Typically, with the .img-responsive class, the image will resize proportionally when the display size changes. However, I want to crop the left and ...

Adjusting element position while scrolling

Objective The goal is to have the page navigation display lower on the page by default. This will provide a cleaner layout as shown in the image below. Context An interactive element for navigation was implemented utilizing Headroom.js. This library d ...

Deciphering embedded struct data in GoLang

A specific struct in my code contains an array of another struct, for example: type Struct1 struct { Value string Items []Struct2 } type Struct2 struct { Value string } While using gorilla schema to decode Form values into S ...

Using HTML and PHP, I am able to automatically redirect users to another

In the mix of files A.html, B.php, and File C.php there was a creation of a button that redirects from A to B, carrying over values provided by the user in A: <form action="B" method=post> <tr><td colspan="2" align="center"><input typ ...

Is there a way to display a loader when an item is selected from a dropdown menu? For example, while data is being fetched from the backend, can we

This is the HTML form division I have created: <div class="col-lg-2 "> <select name="limitCount" id="limitCount" ng-model="limitCount" class="form-control col-md-2 panel-refresh" ...

Placing an Image in the Right Corner Using jQuery Mobile

I am currently developing a mobile app and I need to align three images one below the other at the right corner next to some text. Any suggestions would be greatly appreciated. Thank you in advance. Below is my code snippet: <div> <ul data-r ...