Changing the display property causes ordered lists to shift towards the left

// Ensure the list is displayed when checked and hidden when unchecked.

function toggleList(event) {
  let ol = document.getElementById('list');
  if (event.currentTarget.checked) {
    ol.style.display = 'initial';
  } else {
    ol.style.display = 'none';
  }
}

show.addEventListener('click', toggleList, false);
ol    {display: none;}
label {user-select: none;}
<input id="show" type="checkbox" />
<label for="show">Show list</label>

<ol id="list">
  <li>list</li>
</ol>

The list seems to be aligned to the left with some part sticking out. What is causing this? How can it be fixed? I tried putting the list in a flex container but they still appear misaligned:

function toggleList(event) {
  let ol = document.getElementById('list');
  if (event.currentTarget.checked) {
    ol.style.display = 'initial';
  } else {
    ol.style.display = 'none';
  }
}

show.addEventListener('click', toggleList, false);
ol {
  display: none;
}

label {
  user-select: none;
}

div {
  display: flex;
  justify-content: left;
}
<input id="show" type="checkbox" />
<label for="show">Show List 1</label>

<div>
  <ol id="list">
    <li>list 1</li>
  </ol>
</div>

Appreciate your help!

Answer №1

Check out this CSS solution:

ol    {display: none;
list-style-type: none;}
label {user-select: none;}
input {padding: 0; margin: 0;}

The input originally had some padding and margin that I've removed. I've also taken out the styling from the list using list-style-type. You can view the code in action on Codepen

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

Tips for creating dynamic rope animations with canvas and CSS3 styling

Interested in creating a canvas animation, or possibly using CSS3 for the same purpose. Looking to add an automatic rope animation, similar to it swaying gently. I've searched for scripts and other solutions, but haven't found anything suitable. ...

Fade in and out of div elements upon clicking on an # anchor

I have been attempting to create a function where different divs fade in and out when clicking on an image with an <a href>. Unfortunately, I have not been successful in getting it to work despite several attempts. Here is the code that I am using: ...

Browsing through a collection of images Tinder-style, compatible across multiple platforms (Hybrid / HTML5 compatibility accepted)

I have a vision to develop an app similar to Tinder, where users can swipe through a stack of photos. I am wondering if anyone has knowledge of how to achieve this effect on multiple platforms. My current plan involves creating a web app using jQuery Mobil ...

A guide on extracting JSON data from a script tag using Cheerio

I am in the process of extracting metadata from various websites. While utilizing Cheerio to retrieve elements like $('meta[property="article:published_time"]').attr('content') works smoothly for most sites, there are some whe ...

A guide to iterating through a specified range in Google Sheets

I am currently facing an issue while trying to display data from a Google Spreadsheets using Express. After specifying the cell range to render and implementing a loop to iterate through it, I encounter an error message: "Error [ERR_HTTP_HEADERS_SENT]: Can ...

Is it possible to create a column break within a CSS multicolumn design?

My text content sprawls over a CSS multicolumn layout with two, three, or four columns, utilizing CSS hyphenation. Sometimes, I want to terminate the text in one column early to allow the next paragraph to begin at the top of the next column. Is there a s ...

Combine two entities to create a new entity that mirrors the structure of the first entity

Can someone assist me with a data structure issue? Here are the objects I am working with: const elements = { item1: {color: 'blue', name: 'Item One' }, item2: {color: 'green', name: 'Item Two' }, item3: {colo ...

Struggling with combining model and textures in three.js

Here in this fiddle, you can find an example of the issue I am facing: http://jsfiddle.net/saward/78Bjk/7/ If you uncomment scene.add(tree_mesh) and scene.add(mesh2), and comment out scene.add(mesh), you will be able to see both objects. However, when I m ...

The modal remains visible

Has anyone encountered a problem with hiding a Bootstrap modal based on form validation in JavaScript? I'm facing an issue where the submit function is not executed when I click the button. I've attempted using onsubmit=validateForm(), but it doe ...

Converting Arrays into Objects with Multiple Dimensions

I have been attempting to transform this complex array into an object. However, I am facing an issue where only the second part of the array is being saved in the object, while the first part is missing. Is there a way to ensure that the entire array gets ...

Can context be passed into a component that is created using ReactDOM.render()?

TL;DR In this given example code: ReactDOM.render(<MyComponent prop1={someVar} />, someDomNode); Can one manually provide React context to the instance of MyComponent? This might seem like an unusual question considering React's usual behavio ...

Transferring an organized array to processing

My goal is to transfer an array of integers from a php file called load.php to a JS script, which will then forward it to a Processing file written in JavaScript. In load.php, I define the array and send it using JSON (the array contains a minimum of 40 i ...

Numerous occurrences of Autodesk Forge Viewer

I am facing a challenge in implementing multiple instances of the Autodesk Forge Viewer (v6.2) on my webpage, each hidden within tabs. The goal is to show and hide the viewer with a loaded model as the user switches between tabs. Although I have managed ...

Customize dropdown item colors in React using a color picker

I am currently utilizing a react color picker to create a personalized 16 color gradient. The process involves selecting a color from a dropdown menu and then using the color picker to make adjustments. This action updates an array that dictates the stylin ...

Resizing Div elements in React

I am currently working on a Drawer navigation feature and I am exploring the option of enabling mouse drag resize functionality. To achieve this, I have included a div element where I listen for the onMouseDown event. Once triggered, I then add an event li ...

Accessing information from a database in ReactJS and passing the resulting promises to the return function via the .then method

Summary; I am currently working on integrating data from an API into my React application upon loading to ensure immediate availability. I am open to revising and restructuring the code if necessary, as long as I can achieve this functionality. It seems th ...

Tips for personalizing Bootstrap columns using identical class names?

Is there a way to customize the background color of each individual .col-5 within the same row while using Bootstrap and a custom CSS file? Changing the styles for one column seems to affect all columns with the same class name. Edit: I'm looking f ...

Stop the Text Table from being highlighted

My webpage includes a dynamic table where users can select multiple rows. jQuery events and CSS are utilized to provide visual feedback when a row is selected. However, pressing the shift key sometimes causes text to become highlighted, which is not idea ...

I'm experiencing an issue where a function call within a Vue.js v-for directive causes the loop to continue indefinitely, but I'm unsure of the cause

Here is the template I am working with: <template> <div> <div v-for="report in reports"> <div class="map" v-bind:id="mapID = report.started.toUpperCase()" v-text="report.started"> {{hello(mapID)}} </div& ...

Stop the ThreeJS rendering process

Currently, I am using ThreeJS to create a basic 3D scene with OrbitControls. The problem I'm facing is that it causes my entire website to lag, even when the user is not actively looking at the scene. I am in need of a function that can start and stop ...