Organize elements by stacking them together using HTML and CSS for a tailored selection experience

Do you have a list of words that you want to arrange in columns to optimize space in an HTML file for a Django project?

If you're not very familiar with web development, feel free to break it down to me like I'm 10 years old!

{% extends "todo/base.html" %}

{% block content %}

   <style>
      .elements {
        display: block;
      }
      ul.items {
        display: flex;
        margin: 0;
        padding: 0;
        flex-wrap: wrap;
        list-style: none;
      }
      li.item {
        flex: 1 0 20%;
        padding: 8px;
        margin: 2px;
        border-radius: 8px;
        border: 1px solid rgba(61, 86, 233, 0.3);
        text-align: center;
      }
      .col {
        display: flex;
        flex-direction: column;
        align-items: center;
      }
    </style>

  <!-- Header -->
  <header class="intro-header">
    <div class="container">
      <div class="col-lg-5 mr-auto order-lg-2">
      <h3><br>Introduce yourself... </h3>
      </div>
    </div>
  </header>

  <!-- Page Content -->
  <section class="content-section-a">

    <div class="container">
      <dt>
        <span>Select keywords to express your fragrance preferences</span>
      </dt>

    <form action = "/getmatch" method="POST">
      {% for keyword in keywords %}
        <div class="elements">
            <ul class="items ">
               <li class="item col-xs-6 col-sm-3 col-md-3 col-lg-3">
                 <div data-toogle="buttons" class="col">
                    <span>{{ keyword.title }}</span>
                 </div>
               </li>
            </ul>
        </div>
      {% endfor %}
      {% csrf_token %}
        <button type="submit">Submit</button>
    </form>
    </div>
  </section>

  <!-- Bootstrap core JavaScript -->
  <script src="static/vendor/jquery/jquery.min.js"></script>
  <script src="static/vendor/popper/popper.min.js"></script>
  <script src="static/vendor/bootstrap/js/bootstrap.min.js"></script>



{% endblock %}

If you're unsure whether to include this in a specific CSS file like style.css or if adding it within <style> tags in your file is better, you can go with the latter for simplicity.

Currently, the buttons look good but are not aligned properly:

https://i.sstatic.net/y9gKj.png

Additionally, the multi-selection effect is not working as expected.

In base.html, the following style.css is loaded with this line:

<link href="static/css/style.css" rel="stylesheet">
/*!
 * Start Bootstrap - Landing Page (http://startbootstrap.com/template-overviews/landing-page)
 * Copyright 2013-2017 Start Bootstrap
 * Licensed under MIT (https://github.com/BlackrockDigital/startbootstrap-landing-page/blob/master/LICENSE)
 */

<!-- CSS styles for better button alignment -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <title>Test Document</title>
    <style>
      .elements {
        display: block;
      }
      ul.items {
        display: flex;
        margin: 0;
        padding: 0;
        flex-wrap: wrap;
        list-style: none;
      }
      li.item {
        display: flex;
        flex-direction: column;
        align-items: center;
        flex: 1 0 20%;
        padding: 8px;
        margin: 2px;
        border-radius: 8px;
        border: 1px solid rgba(61, 86, 233, 0.3);
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h1>Buttons</h1>
    <div class="elements">
      <ul class="items">
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
        <li class="item">
          <i class="fa fa-home"></i>
          <span>Label</span>
        </li>
      </ul>
    </div>
  </body>
</html>

You can consider arranging them as shown above for a better visual presentation.

Answer №1

One way to structure your code is to use a parent container with a specific width, and then apply a col-12 class on each iteration within it.

   <div class="row" style="width:30%">
     {% for keyword in keywords %}
       <div class="col-12">
          <h3>{{ keyword.title }}</h3>
       </div>
     {% endfor %}
   </div>

UPDATE

Apologies for the typos in my previous message which was sent from my cellphone. Here is another approach that may help you find a solution.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<title>stack</title>
<style>
  .container{
    width:900px;
    min-height: 300px;
    display:flex;
    flex-direction:column;
    grid-row-gap: 5px; /* Will be used instead by browsers that do not support `row-gap` */
    row-gap: 5px; /* Used by browsers that support `row-gap` */
    background-color:#ddd;
}
</style>
</head>
<body>
  <div class="container">
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
    <div>keyword</div>
  </div>
</body>
</html>

This approach is straightforward and easy to implement.

https://i.sstatic.net/pNDEP.png

Best regards

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

Is there a reason why I can't load all Google charts on one webpage?

I am trying to use the Google Charts API to display various charts on my web page. However, I seem to be encountering an issue where the last chart is not loading correctly. Can someone point out what I might be missing in my code? I would greatly apprecia ...

Having trouble downloading text files in Django

log_path = settings.BASE_DIR + '/uploaded_files/logs/' # log_file = user + '_' +datetime.datetime.now().__str__() log_file = str(random.randint(1, 100)) + '_' + datetime.datetime.now().__str__() log_file = log_path + '%s ...

Animation scaling on the iPhone seems to abruptly jump away once it finishes

Encountering an issue with animations that is causing unexpected behavior on physical iPhone devices but not in the simulators. The problem arises when the background zooms in and then the div suddenly jumps to the left after the animation completes, as sh ...

Customizing color schemes of bootstrap buttons and dropdown-toggle elements

My button group looks like this: HTML <div class="btn-group"> <button type="button" class="btn btn-default btn-xs red-outline-button"> Sync </button> <button type="button" class="btn btn-default btn-xs gold-outline-button" da ...

Tips for shrinking the circumference of a circle

Currently, I have a circular div that has been styled using CSS animations. My goal is to keep the size of the circle consistent when it moves to the bottom, but reduce its size when it bounces back to the top. I am uncertain if this can be achieved solely ...

When submitting the form, a new browser tab opens displaying a blank PHP script

Currently revamping my website using a template and editing the content with notepad++. Struggling to display a success message on the contact me page after form submission. The issue arises when I submit information in the text boxes and click submit, it ...

When the text is in motion, the ::before element will stay static

I'm working on creating a unique custom SVG underline for a header design. My goal is to achieve something similar to this: https://i.sstatic.net/EI1aT.png Here's what my code currently looks like: HTML: <div class="container"> ...

The CSS hamburger menu halves in size when being closed

I have successfully created a CSS/JS hamburger menu with a transforming icon upon clicking. However, I encountered a bug where, upon clicking the 'close' button on the hamburger, the navigation menu cuts behind the header, resulting in a messy ap ...

Optimal-minimal behavior with a versatile CSS grid system

Currently, the use of grid-template-columns: repeat(auto-fit, minmax(25rem, 1fr)); Results in this behavior: If there are more items than can fit in a row (based on the minimum), new rows are created. If there are fewer items than the row could accommodat ...

Ways to access the value of an attribute in an AngularJS object

Is there a way to access the value of field.jobtype within a controller? var app=angular.module('myapp',['ui.bootstrap','ui.select']); app.controller('mycontroller',function($scope){ $scope.onStateSelected = func ...

I'm curious about the meaning behind this code snippet: `$Odd = ($Odd == "even") ? "odd" : "even";`. Any ideas?

<?php $Odd = "even"; $query = $MySQLi->query("SELECT id, look, username, motto FROM users WHERE rank = '7'"); if($query->num_rows > 0): while($UserRow = $query->fetch_assoc()) { $Odd = ($Odd == "even") ? "odd" : "even"; ?&g ...

Expand a section of the background picture

I am working with an image that contains multiple flag images: https://i.stack.imgur.com/xg0iM.jpg I have a div element with the dimensions: width:100px; height:50px. My goal is to resize each flag to fit inside it: .flag{ wi ...

Chrome distorts background images with CSS when resized

One thing I noticed is that Chrome seems to display CSS background-images differently compared to using the <img /> tag. I tested two identical images resized to the same dimensions The first image set as a CSS background-image appeared pixelated W ...

The MinWidth property in ColumnDefinition is not functioning as expected

Currently, I am encountering an unusual issue while using a Grid in WPF (XAML) and setting the MinWidth property in a ColumnDefinition. When I have 9 ColumnDefinitions with 'Width="*"' specified for each one, and then apply the MinWidth property ...

Transform the header style columns of react-js Material-tables into rows

Currently experimenting with gradient designs on a material table. While I am able to apply the right color combination to the rows, I seem to be getting column-based results on the title of the table. Attached is a screenshot of my output for reference. ...

Arranging navigation links around a centrally positioned navigation brand using Bootstrap

Struggling to create a Navbar using bootstrap right now. The issue I'm facing is with centering the nav-links on both sides of the nav-brand. Here's my HTML code: <nav class="navbar navbar-expand-sm navbar-light "> <button class="na ...

What is the method for triggering the output of a function's value with specified parameters by clicking in an HTML

I am struggling to display a random number output below a button when it is clicked using a function. <!DOCTYPE html> <html> <body> <form> <input type="button" value="Click me" onclick="genRand()"> </form> <scri ...

What methods are recommended for expanding the size of a select/dropdown menu?

Managing a long list of values can be challenging, especially when it continues to grow. Consider the following example: <select> <option value="1">1, some test</option> <option value="2">2, some text</option> <optio ...

Challenges with implementing ng2-auto-complete in Angular2

I successfully integrated the Angular 2 ng2-auto-complete component into my project by following this helpful tutorial. The code is also available on GitHub. The current challenge I am encountering involves the formatting of my data source. It is structur ...

I am experiencing an issue where the custom form validation directive in AngularJS is not functioning properly within my modal

To enhance the validation of my input boxes, I created a custom directive called nx-validate. This directive is designed to be added to a bootstrap div.form-group, which will then check if the <input/> field is $dirty or $invalid, and apply the appro ...