Please align new tasks to the left under the form in the to-do list

My current project involves creating a to-do list using jQuery and centering everything on the page with text-align: center in the CSS file. However, I am facing an issue with left-aligning newly added list items below the form.

The JavaScript section of my code is provided below (I suspect this is where modifications need to be made), but you can also check out my CodePen for the HTML and CSS files.

$(function() {
  $('.button').click(function() {
    let toAdd = $('input[name=checkListItem]').val();
    // Appends specified element as last child of target element
    $('.list').append('<div class="item">' + toAdd + '</div>');
    // Clears input box after clicking 'add'
    $('input[name=checkListItem]').val('');
  });

  $('input[name=checkListItem]').keypress(function(e) {
    if (e.which == 13) {
      $('.button').click();
      // e.preventDefault() prevents default event from occurring, e.stopPropagation() prevents event from bubbling up, and return false does both
      return false;
    }
  });

  $(document).on('click', '.item', function() {
    $(this).remove();
  });
});

Answer №1

Here is a simple snippet to achieve the desired alignment:

.item {
  text-align: left;
}

This code snippet demonstrates how it works:

$(function() {
  $('.button').click(function() {
    let toAdd = $('input[name=checkListItem]').val();
    // appends specified element as last child of target element
    $('.list').append('<div class="item">' + toAdd + '</div>');
    // clears input box after clicking 'add'
    $('input[name=checkListItem]').val('');
  });

  $('input[name=checkListItem]').keypress(function(e) {
    if (e.which == 13) {
      $('.button').click();
      // e.preventDefault() stops default event, e.stopPropagation() blocks event bubbling, and return false does both
      return false;
    }
  });

  $(document).on('click', '.item', function() {
    $(this).remove();
  });
});
body {
  text-align: center;
  background: #dfdfdf;
  font-family: Helvetica;
  color: #666;
  background: linear-gradient(to left, #DA4453, #89216B);
}

.container {
  padding: 30px;
  padding-top: 10px;
  width: 300px;
  margin: 0 auto;
  margin-top: 40px;
  background: white;
  border-radius: 5px;
}

form {
  display: inline-block;
}

input[type=text] {
  border: 1px solid #ccc;
  height: 1.6em;
  width: 15em;
  color: #666;
}

.button {
  display: inline-block;
  box-shadow: inset 0px 1px 0 0 #fff;
  background: linear-gradient(#f9f9f9 5%, #e9e9e9 100%);
  border: 1px solid #ccc;
  background-color: #c00;
  font-size: 0.7em;
  font-family: Arial;
  font-weight: bold;
  border-radius: 0.33em;
  padding: 0.5em 0.9em;
  text-shadow: 0 1px #fff;
  text-align: center;
}

.button:hover {
  cursor: pointer;
  opacity: .8;
}

.item {
  text-align: left;
}
<html>

<head>
  <meta charset="UTF-8">
  <title>To-Do List</title>
  <link rel="stylesheet" href="stylesheet.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="script.js"></script>
</head>

<body>
  <div class="container">
    <h2>To Do</h2>
    <form name="checkListForm">
      <input type="text" name="checkListItem" />
    </form>
    <div class="button">Add</div>
    <br>
    <div class="list"></div>
  </div>
</body>

</html>

To add some margin-top to the first item, consider using the :first-of-type pseudo-class:

.item:first-of-type {
  margin-top: 20px;
}

This addition can be demonstrated below:

$(function() {
  $('.button').click(function() {
    let toAdd = $('input[name=checkListItem]').val();
    // appends specified element as last child of target element
    $('.list').append('<div class="item">' + toAdd + '</div>');
    // clears input box after clicking 'add'
    $('input[name=checkListItem]').val('');
  });

  $('input[name=checkListItem]').keypress(function(e) {
    if (e.which == 13) {
      $('.button').click();
      // e.preventDefault() stops default event, e.stopPropagation() blocks event bubbling, and return false does both
      return false;
    }
  });

  $(document).on('click', '.item', function() {
    $(this).remove();
  });
});
body {
  text-align: center;
  background: #dfdfdf;
  font-family: Helvetica;
  color: #666;
  background: linear-gradient(to left, #DA4453, #89216B);
}

.container {
  padding: 30px;
  padding-top: 10px;
  width: 300px;
  margin: 0 auto;
  margin-top: 40px;
  background: white;
  border-radius: 5px;
}

form {
  display: inline-block;
}

input[type=text] {
  border: 1px solid #ccc;
  height: 1.6em;
  width: 15em;
  color: #666;
}

.button {
  display: inline-block;
  box-shadow: inset 0px 1px 0 0 #fff;
  background: linear-gradient(#f9f9f9 5%, #e9e9e9 100%);
  border: 1px solid #ccc;
  background-color: #c00;
  font-size: 0.7em;
  font-family: Arial;
  font-weight: bold;
  border-radius: 0.33em;
  padding: 0.5em 0.9em;
  text-shadow: 0 1px #fff;
  text-align: center;
}

.button:hover {
  cursor: pointer;
  opacity: .8;
}

.item {
  text-align: left;
}

.item:first-of-type {
  margin-top: 20px;
}
<html>

<head>
  <meta charset="UTF-8">
  <title>To-Do List</title>
  <link rel="stylesheet" href="stylesheet.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="script.js"></script>
</head>

<body>
  <div class="container">
    <h2>To Do</h2>
    <form name="checkListForm">
      <input type="text" name="checkListItem" />
    </form>
    <div class="button">Add</div>
    <br>
    <div class="list"></div>
  </div>
</body>

</html>

Answer №2

After incorporating this CSS code into your project, it effectively resolved the issue:

.items { 
  text-align: center;
  margin-left: 20px;
}

You have the flexibility to adjust the margin to meet your specific needs.

Answer №3

To align text to the left in a list, you can utilize the following CSS code:

.list { text-align: left; }

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

Managing the grouping of values from an HTML form by key becomes a challenge when input fields can be dynamically added or removed

One interesting feature in my form is the ability to add and remove rows dynamically by clicking on a button. When multiple invoicerow elements are present, I want to group all results together for better organization. However, the array structure in the p ...

Could someone clarify why EventEmitter leads to issues with global variables?

I recently encountered an error that took me some time to troubleshoot. Initially, I decided to create a subclass of EventEmitter In the file Client.js var bindToProcess = function(func) { if (func && process.domain) { return process.domai ...

HTML5 coding can be enhanced by applying unique CSS rules for alignment purposes

html { font-family: "Open Sans", sans-serif; font-weight: 100; background-color: #fbfbfb; } body { font-family: "Open Sans", sans-serif; font-weight: 100; } body h1 { font-weight: 100; } body h3 { font-family: "Open Sans", sans-serif; fo ...

I am encountering unexpected issues with the functionality of img srcset and sizes

Attempting to enhance the responsiveness of images on my website has led me to discover the srcset and sizes attributes. The objective is clear: If the screen size exceeds 600px or falls below 300px, a 250x250 image should be displayed For sizes in betw ...

Transfer a term to a different division - JavaScript Object Model

I am trying to achieve a specific task where I need to move one term under another in SharePoint. However, despite my efforts using JSOM and SharePoint 2013, I have not been able to find a direct method for moving terms. The code snippet I have used below ...

Copy and paste content from Outlook, Word, or any Office software directly into the embedded browser

Our application is performing well, but there is an issue with some users copying text to Word before pasting it into the app. The HTML gets parsed out somewhat correctly, but it often includes tags from outlook or word that our XHTML engine doesn't r ...

The hidden inner div fades away within the confines of a trapezoid-shaped container

I've been attempting to position a div within another trapezoid-shaped div. However, the inner div remains invisible no matter what I try. Z-indexes have also been tested. .trapezoid { border-color: transparent transparent rgba(255,0,0, ...

Ways to show text on a donut chart when hovering with the mouse

I have been attempting to make adjustments to this sample. My goal is to display a word in the center of the donut chart upon mouseover, similar to this: https://i.sstatic.net/dCPKP.png Although I have included code for mouseover, it seems to not be func ...

Reset all divs to their default state except for the one that is currently active using jQuery

Here's the plan for my script: Once a user clicks on a "learn more" button, it will reveal the relevant information in the corresponding box. I'm aiming to implement a feature where if you click on another "learn more" button while one box is a ...

Create a basic cache within an AngularJS service to store data retrieved from HTTP requests, ensuring that the cache returns an object

Looking to implement a simple caching mechanism in an AngularJS service for data retrieved from HTTP requests. The goal is to always reference the same object to avoid duplicating requests. Below is an example code snippet showcasing the issue at hand. Li ...

Vue 3 + Vite: The router path was not found

I'm currently working on a project using Vue 3 and Vite, where I need to verify if the user is logged in with AWS Cognito before accessing the main page. Here's an example of my router.js: import { createRouter, createWebHistory } from &apo ...

Mastering special characters in JavaScript: The usage of the "/" character

I have upgraded my vbulletin to version 4.2.0 and I successfully added a custom button to its editor following this tutorial: Now, I am trying to incorporate a syntax highlighter code using this button. When I use the code provided below, it works perfec ...

Implementing phone verification code delivery using ReactJS and Twilio client: Step-by-step guide

I've been scouring the internet for the past 8 hours and haven't been able to find a tutorial on using Twilio to send SMS messages with ReactJS. Does anyone know of a good tutorial for this? ...

Tips for preserving CSS styling following a hover event

While working on implementing a Menu control using jQuery and CSS, I encountered the following issue: Here is a live demo that demonstrates my current problem When I hover over 'Menu Item 1', this item successfully changes its style (background ...

Progressing with Jquery AJAX sequentially

I'm attempting to send two ajax requests in succession. My plan was to utilize the ajax success() function for this task: $.ajax({ ... success: function() { //perform the second ajax request here } }); The issue is that the first ...

Utilize Javascript to acquire the AspNet.ApplicationCookie

I'm completely clueless if this is even doable, but I have to inquire. Is there a method to retrieve the AspNet.ApplicationCookie from Cookies? I've attempted the following: `$.cookie('.AspNet.ApplicationCookie');` `document.cookie;` ...

What is the best approach for optimizing CSS changes across multiple pages in WordPress?

I am looking to customize the background images for different groups of pages on my WordPress site. Currently, I am manually assigning the background images to individual pages by using PageID in the code snippet below. With over 1000 pages, this method i ...

Having difficulty converting the navbar into a horizontal layout

I'm struggling to figure out how to display the navbar horizontally across the page instead of vertically. I've experimented with different solutions, but it seems like the issue lies within the ul class. Unfortunately, I haven't been able t ...

The json_decode function is returning a NULL value even though the input JSON

Alright, here's the situation: In my javascript file, I have the following code: this.fields = { 'zone': ['DouroMinho','TrasosMontes', 'BeiraLitoral', 'BeiraInterior'], 'flower': [&apos ...

Error: Attempting to access the 'filter' property of an undefined value is not permitted in React. This issue has caused an undefined error

I'm encountering an issue with my API call. I'm trying to implement a search bar in my project where the data is fetched from an API. However, when I add a filter function, I encounter an error. Error: Cannot read properties of undefined (reading ...