Home > Software design >  Add fixed spacing between two internal elements of a list
Add fixed spacing between two internal elements of a list

Time:02-12

I am working on building a basic to-do list.

I have a list of elements with delete option for each element next to it.

I am facing issues with having proper spacing between the list elements namely "to-do text" and "Delete" icon.

Note: I'm new to Web programming.

CodePudding user response:

In the solution below, the <li> and <button> elements are placed in the <div> first. These elements are bound as children to another <div> element that implements the .parentContainerStyle class style (the display property is assigned flex). This allows fixed spacing between <li> and <button> elements.

enter image description here

var ul = document.getElementById("itemList");
var input = document.getElementById("text");

input.addEventListener("keyup", () => {
    if (event.keyCode == 13 ) {   
        addItem();
        emptyInput();
    }
});
document.getElementById("clickButton").onclick = function() {   
    addItem();  emptyInput();
}

function addItem(){
    if (input.value.length != 0){
        var parentDiv = document.createElement("div");
        parentDiv.className = "parentContainerStyle";
  
        var deleteButton = document.createElement("button")
        deleteButton.id = "delButton"
        deleteButton.className ="fa fa-trash iconClass"
        deleteButton.onclick= function(){removeItem(this.parentElement);};
        
        var li = document.createElement("li");
        li.setAttribute('id',text.value);
        li.appendChild(document.createTextNode(`${text.value} `));
        
        var liContainer = document.createElement("div");
        liContainer.className = "liContainer";
        liContainer.appendChild(li);
        
        var buttonContainer = document.createElement("div");
        buttonContainer.className = "buttonContainer";
        buttonContainer.appendChild(deleteButton);
        
        parentDiv.appendChild(liContainer);
        parentDiv.appendChild(buttonContainer);
        ul.appendChild(parentDiv);
    }
}
function emptyInput(){
    document.getElementById("text").value = ""
}

function removeItem(item){
    ul.removeChild(item);
}
.parentContainerStyle {
  display: flex;
}

.liContainer, .buttonContainer {
  width: 100px;
}
<!DOCTYPE html>
<head>
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1"> -->
    <!-- <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
  <body>
    <h1>To Do List</h1>
    <input type="text" id = "text"  > 
    <button id="clickButton" >Add Item</button>
    <ul id= "itemList">   </ul>
  </body>
</html>

CodePudding user response:

You could set a fixed width on the container element and then use flex box:

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .container {
                display: flex;
                background-color: grey;
                border: 1px solid darkGrey;
                width: 300px;
                padding: 10px 15px;
            }
            .title {
                flex: 1;
                padding-right: 3px;
            }
        </style>
    </head>
    <body>
        <div >
            <div >sample text</div>
            <div>delete icon</div>
        </div>
        <div >
            <div >long sample text for illustration purposes.</div>
            <div>delete icon</div>
        </div>
    </body>
</html>

  • Related