What could be causing my right-floating elements to shift more towards the left with each occurrence?

After following a todo list tutorial, everything was running smoothly until I decided to add some styling to it. I placed a Font Awesome trash can icon inside a button with the class "remove." Essentially, I removed all the styles from the button, leaving just the trash icon. Users can click it to delete their todo list item, which works as expected. I attempted to align the trash can icon to the right of the task that pops up. It aligns correctly, but with each new task added, the trash can icon drifts further right.

I will attempt to share only the relevant code as I am unsure why this issue is occurring. Bootstrap is being utilized for styling, and it appears that neither it nor the circular plus sign button is impacting the tasks being generated. Any suggestions would be greatly appreciated, thank you!

View the image here


function get_todos() {
    var todos = new Array;
    var todos_str = localStorage.getItem('todo');
    if (todos_str !== null) {
        todos = JSON.parse(todos_str); 
    }
    return todos;
}

function add() {
    var task = document.getElementById('task').value;

    var todos = get_todos();
    todos.push(task);
    localStorage.setItem('todo', JSON.stringify(todos));

    show();

    return false;
}

//This is mostly where the remove button takes place

function remove() {
    var id = this.getAttribute('id');
    var todos = get_todos();
    todos.splice(id, 1);
    localStorage.setItem('todo', JSON.stringify(todos));

    show();

    return false;
}

function show() {
    var todos = get_todos();

    var html = '<ul>';
    for(var i=0; i < todos.length; i++) {
        html += '<li>' + todos[i] + '<button class="remove" id="' + i  + '"><i class="fa fa-trash" aria-hidden="true"></i></button></li>';
    };
    html += '</ul>';

    document.getElementById('todos').innerHTML = html;

    var buttons = document.getElementsByClassName('remove');
    for (var i=0; i < buttons.length; i++) {
        buttons[i].addEventListener('click', remove);
    };
}

document.getElementById('add').addEventListener('click', add);
show();
                

.remove { /* Trash can button */
    color: #C0C0C0;
    font-size: 24px;
    border: 0;
    padding: 0;
    background-color: transparent;
    float: right;
}
                

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" <br> integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" <br> crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" <br> integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" <br> crossorigin="annoymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" <br> integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" <br> crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" <br> integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" <br> crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <div class="container" style="margin-top:200px;">
        <div class="row justify-content-center">
            <div class="col-sm-12 col-lg-8">
                <h1 style="padding-bottom:20px;">Test</h1>
                <div class="form-group">
                    <input type="text" class="form-control" id="task">
                </div>
            </div>
            <div class="row justify-content-center" style="margin-top:20px;">
                <div class="col-sm-12 col-sm-8">
                    <div id="todos"></div>
                </div> 
            </div>
            <div class="row">
                <div class="col"></div>
                <div class="col-xs-2" style="margin-right:15px; margin-top:30px;">
                    <button id="add" style="border-radius:50%; font-size:35px; width:65px;" class="btn btn-danger">+</button>
                </div>
            </div>
        </div>
    

View the fiddle here

Answer №1

The reason for this issue is the presence of the floated element blocking it. To resolve this, you can include either clear: right or clear: both in the styles of the floated element.

.delete { /*Trash can icon*/
    color:#909090;
    font-size:20px;
    border:0;
    padding:0;
    background-color:#f5f5f5;
    float:right;
    clear:right;
}

Answer №2

I experimented with the CSS and discovered some effective styles. However, after some adjustments, I found that the changes below greatly enhance the appearance of the list.

for the <ul> element

include

font-size: 20px;

for the .remove class

remove

font-size: 24px;
float:right;

add

position: absolute;
right: 20px;

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

Arranging DIV elements into rows and columns within a table

After working all night to fix a problem, my goal is to create this timetable: https://i.sstatic.net/dAt1J.png Specifically, I'm struggling to find a solution for rows 5, 6, and 7 in the first column. The image is from a program that only runs in IE ...

Tips on maintaining the numerical value while using JSON.stringify()

Having trouble using the JSON.stringify() method to convert some values into JSON format. The variable amount is a string and I want the final value in JSON format to be a number, but it's not working as expected. Even after applying JSON.stringify(), ...

Dynamically changing the date format within the item-text of v-autocomplete in Vuetify

My current v-autocomplete component is fetching an array of items from GrowthTasks.edges to display. <v-autocomplete label="Start Week" :items="GrowthTasks.edges" item-text="node.growthStartDate" item-value="node.grow ...

The total sum of various divs containing a mix of numbers and letters

I have a group of div elements with the same class, each containing a value in the format (X1), (X2),... I need to add up these numeric values only, like 1 + 2 + .... Since these divs are generated dynamically, I won't know how many there will be. How ...

JavaScript: Efficiently Sorting a Multidimensional Array

Here is the array that needs to be sorted based on the third item: const array = [ [1, "Convention Hall", "Mumbai", 10, "XYZ Company"], [2, "Auditorium", "Delhi", 10, "ABC Company"], [3, "CenterHall", "Bangalore", 10, "ZZZ Company"], ... ...

Create a JavaScript button that increases a progress bar value by 1 and adjusts its width style by 10 units

Here is the code for managing a progress bar using JavaScript: function getProgress() { return document.getElementById("progressbar").getAttribute("aria-valuenow"); } function setProgress(value) { document.getElementById("progressbar").setAttri ...

Traverse a tree structure of nested child objects in an Angular template using a JavaScript

Check out the Javascript object below: { "id": "1554038371930_ajhnms9ft", "name": "CPS", "nodes": [ { "id": "1554038905938_le34li2cg", "name": "Consumer Journey", "nodes": [ { ...

Flex container scrollable element not functioning consistently between different browsers

Today, I've spent most of my time experimenting with various solutions but I'm facing difficulty making them work consistently across different browsers. My goal is to have 2 sections within a fixed div: the first section with a set height and t ...

Tips for choosing a specific point on a Vuetify <v-slider> using Cypress

Currently, I am exploring how my application responds when a user interacts with a specific area on a basic Vuetify <v-slider>. How can I replicate this scenario in a Cypress integration test effectively? For instance, to click on the center of the ...

Utilize JavaScript to Replace KeyPress Event on Input Field

While attempting to substitute the key press of "K" with "Z" in an input field, I managed to accomplish it successfully. However, there seems to be a slight delay that causes the user to observe the transition from "K" to "Z". Below is the code that I use ...

What is the purpose of using *//<![CDATA[* and *//]]>* in a jQuery code snippet?

Check out this JavaScript code snippet that I have: <script type="text/javascript> //<![CDATA[ jQuery(document).ready(function() { jQuery("#page_template option[value='sidebar-page.php']").remove(); }); ...

What is the best way to align a div of variable size in a container of variable size using CSS?

My website features a 3-level structure that consists of a "workingArea" and an "imageContainer" within it, containing an image. <div class="workingArea"> <div class="imageContainer"> <img class="theImage" /> </div> </div> ...

Guide to updating 2 iframes simultaneously with a single link/button using HTML and JavaScript

Just joined the community and looking for help on how to refresh two different iframes within a single page. I came across a solution on Google that involves using getElementById. However, I've heard Firefox can be tricky with IDs. Appreciate any as ...

Discovering the value of a checkbox using jQuery and Ajax

I have a challenge with sending the state of multiple checkboxes on my page back to the database using ajax. While I am comfortable working with jquery and ajax for SELECT and OPTIONS elements, I am struggling to do the same for checkboxes and extracting t ...

Fetch information from MySQL, create a new row for each data entry

Currently, I am working on a project for my school that involves retrieving student works from a database. For the homepage of my project, I have set up 10 divs to hold the data returned from a query. The reason I preset these divs is because I only need ...

How to efficiently calculate totals in an HTML table using JavaScript/jQuery

I have a gridview that displays product details and includes a quantity textbox that is not linked to any database. For each row, I want it to show the cost (price * quantity) and the total cost for all rows in a label below. However, I am encountering a f ...

Is it possible for me to transfer a class attribute to a directive template in AngularJS?

I recently came across this directive in AngularJS: productApp.directive('notification', function($timeout) { return { restrict : 'E', replace : true, scope : { type: "@", message: "@ ...

What are some ways to improve performance in JavaScript?

Can you help me determine which approach would be more efficient - using native functions of the language that involve 2 iterations or a simple for loop? The goal is to locate the index in an array of objects where the property filterId matches a specific ...

Check to see if the menu item exceeds the allotted space, and if so, display the mobile menu

Currently, I am in the process of creating a unique responsive menu for my website that I think will need some JavaScript implementation. My skills with JavaScript are not very strong, so I am looking for guidance, code examples, or resources that can assi ...

Transferring data between AngularJS and non-AngularJS environments

Within my AngularJS application, the $scope.mydata variable holds some important data within a controller. I am currently working on a regular JSP page without any AngularJS functionality, but I need to access the data stored in the scope variable (mydat ...