Creating responsive list items using Bootstrap 4 and Flexbox: Adjusting the width of <li> elements to fit within containers

Struggling with expanding li elements to match the container width? Despite tweaking individual widths and Flexbox properties, the desired outcome remains elusive. How can the width of each li be adjusted to align with the container, mirroring the dimensions of the To do List header and the text box?

Visual representation of the webpage can be viewed here: https://i.sstatic.net/7WgR9.png

Provided below is the HTML and CSS code snippet:

body {
  font-family: Roboto;
  background: -webkit-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%); /* Chrome 10+, Saf5.1+ */
  background:    -moz-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%); /* FF3.6+ */
  background:     -ms-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%); /* IE10 */
  background:      -o-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%); /* Opera 11.10+ */
  background:         linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%); /* W3C */
}

.heading {
    background-color: #2980b9;
    color: white;
}

.container {
    margin-top: 100px;
    width: 400px;
    padding: 0;
}

ul {
    list-style-type: none;
}

.item {
    background-color: #fff;
    width: 100%;
}

.item:nth-child(2n) {
    background-color: #f7f7f7;
}

.header {
    font-size: 24px;
    font-weight: normal;
}

input {
    width: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<body>
    <div class="container">
            <div class="row d-flex justify-content-between align-items-center heading">
                <h1 class="ml-4 header">TO-DO LIST</h1>
                <h4><i class="fa fa-plus mr-2 mt-2" aria-hidden="true"></i></h4>
            </div>
        <div class="row d-flex">
            <input class= "pb-5"type="text" placeholder="Add New Todo">
        </div>
        <div>
            <ul class="row d-flex no-gutter">
                <div class="d-flex row item"><li><span>X</span> Row 1</li></div>
                <div class="d-flex row item"><li><span>X</span> Row 2</li></div>
                <div class="d-flex row item"><li><span>X</span> Row 3</li></div>
            </ul>
        </div>
    </div>
</body>

Answer №1

Your code has some errors in the syntax. I suggest utilizing Bootstrap layout classes for a cleaner and more effective design. Check out the improved code below for a better result. Cheers!

body {
  font-family: Roboto;
  background: -webkit-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%);
  background:    -moz-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%);
  background:     -ms-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%);
  background:      -o-linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%);
  background:         linear-gradient(90deg, #2BC0E4 10%, #EAECC6 90%);
}

.heading {
    background-color: #2980b9;
    color: white;
}

.container {
    margin-top: 100px;
    width: 400px;
}

.list-group-item:first-child {
  border-top-right-radius: 0 !important;
  border-top-left-radius: 0 !important;
}

.list-group li:nth-child(2n) {
    background-color: #f7f7f7;
}

.header {
    font-size: 24px;
    font-weight: normal;
}

input {
    width: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="row d-flex justify-content-between align-items-center heading">
      <h1 class="ml-4 header">TO-DO LIST</h1>
      <h4><i class="fa fa-plus mr-2 mt-2" aria-hidden="true"></i></h4>
  </div>
  <div class="row">
      <input class="pb-5" type="text" placeholder="Add New Todo">
  </div>
  <ul class="row list-group">
      <li class="list-group-item"><span>X</span> Row 1</li>
      <li class="list-group-item"><span>X</span> Row 2</li>
      <li class="list-group-item"><span>X</span> Row 3</li>
  </ul>
</div>

Answer №2

Parece que no estás siguiendo el patrón de diseño de bootstrap, que es el siguiente:

root-body
    div->.container
        div->.row
          div->.column
            div->.row
              ul->.list w-100
                 li/ul->.tu-nombre-de-clase w-100

En tu caso, no estás incluyendo el div con la clase column y el hijo row con las clases w-100 de bootstrap para hacer que el elemento utilice el 100% del ancho de la pantalla.

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

When you try to upload an image using php/ajax, it causes the page to refresh

I'm currently experiencing an issue with my script. I am successfully using formData() to upload an image via Ajax, which is being saved to the designated folder. However, I am puzzled as to why my page keeps refreshing after move_uploaded_file() is e ...

best way to eliminate empty p tags and non-breaking spaces from html code in react-native

How can I clean up dynamic HTML content by removing empty <p> tags and &nbsp? The whitespace they create is unwanted and I want to get rid of it. Here's the HTML content retrieved from an API response. I'm using the react-native-render ...

Tips for utilizing the getJson method to pass a variable to a PHP file

I need help with my JavaScript code, as I am trying to pass a datastring containing the value "no" to data.php using getJson in order to receive JSON as a response. However, my JavaScript code is not functioning correctly. Below is the code that I have: J ...

A step-by-step guide on how to use ajax/jquery to access an external webpage without relying on iframe

Is there a more effective way to call another page, such as http://www.google.com, and load it into my specific div without using an iframe? I attempted to do so with ajax... $.ajax({ url : 'http://www.google.com', success : function ( ...

Using Vue.js to alter the CSS class property

I am exploring Vue.js and looking to modify a CSS class property. Here is the HTML code utilizing the specified class: <div class="customTimer"></div> Here is the corresponding CSS code: .customTimer { width: 100%; height: 1 ...

Continue looping in Javascript until an empty array is identified

Currently, I am in search of a solution to create a loop in Javascript that continues until the array of objects is empty. The object I am working with looks like this: "chain": { "evolves_to": [{ "evolves_to": [{ ...

Creating dynamic routes in express.js with fixed components

I'm exploring how to create a route in express that captures URLs like this: /events/0.json Here's what I've attempted so far (but it's not working as expected): router.put('/events.json/:id.json', isLogged, events.update) ...

Tips for incorporating HTML elements into XML emails

I'm facing an issue with my email template where the body of the email is being pulled as a String, resulting in raw html appearing in the email. Does anyone know how to insert an html tag into the body of an XML email using a String? <body> ...

producing base64 encoding that results in a blank image

I have some code that is supposed to get an image from a video using canvas. However, when I save this base64 code into an image, I end up with a black image. What could be causing this issue? Here is my JavaScript code: var input = document.getElementBy ...

Maintain the central alignment of the entire page (including the horizontal scrollbar) while zooming in

I have successfully centered elements, but now I am trying to center the entire page (i.e. have the horizontal scrollbar in the middle) when zooming in. Currently, the scrollbar stays at the very left end as I zoom in. * { margin: 0; padding: 0; box ...

Scheduling casperjs on Heroku for automated tasks

Recently, I developed an application using casperjs to extract sports data from a specific website. My goal is to automate this application on heroku by setting up a cronjob to store the sports results in either a CSV file, database, or possibly external ...

Copy both the image and JSON object to the clipboard

I am attempting to utilize the clipboard API to write an image and JSON object to the window clipboard. I am working with Vue and Electron and have successfully written an image and plain text, but I encounter an error when trying to write a JSON object: ...

Get Onsen UI 2 up and running without the need for Angular

Is it possible to install Onsen UI 2 without Angular? I have tried following various guides from , but when attempting the JavaScript method (> npm install onsenui) I consistently encounter a ReferenceError: angular is not defined. How can I properly in ...

What is the best way to transform an array that contains arrays into an array that contains objects with newly-defined properties?

Is there a way to transform this: [[a1, b1], [a2, b2]] into this format? [{x1: a1, y2: b1}, {x2: a2, y2: b2}]? Please note that the properties x1 and x2 are newly introduced with values a1, b1, etc. I attempted to achieve this with the following code sni ...

Tips on how to display a Vue component on a new page with Vue.js router

My current challenge is getting my App to render on a new page instead of the same page. Despite trying render: h => h(App), it still renders on the same page. This is the Vue file (Risks.vue) where the router will be linked: <router-link to="/risk ...

Unable to make changes to inherited Jinja template in other files

This is the code from my base.html file: {%load static%} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> ...

What is the best way to utilize a single component from Twitter Bootstrap?

Is there a way to use the Twitter Bootstrap plugin from this website ( https://github.com/silviomoreto/bootstrap-select ) for only one specific element on my webpage? I don't want all elements on my site to adopt the Twitter Bootstrap styling (such as ...

I am encountering the error 'user.matchPassword is not a function' while making a call to my API using bcryptjs in my Node.js and Express application. Can someone help me understand why

const checkUserAuth = asyncHandler( async (req,res)=>{ const { email , password } = req.body; const foundUser = User.findOne({email}); if(foundUser && (await foundUser.verifyPassword(password))){ generate ...

Leveraging Amplify for offline storage while transitioning between HTTP and HTTPS protocols

Recently, I encountered an issue with Amplify 1.0a1 on my website. It seems that when storing the value of prod_id in localStorage on a page using HTTP, and then navigating to a page using HTTPS (such as the 'list' page), I am unable to access th ...

The command `sequelize.sync({force:true}) is running into issues and the log output is coming up empty

I encountered a multitude of questions while working on this particular issue. None of them seemed to have the same problem, and after 20 straight hours of trying to solve it, I'm feeling quite stressed. So, I apologize if I missed a similar issue. Th ...