Arranging element in a vertical column

Looking for Help with CSS Positioning

Solution to align 3 divs (left/center/right) inside another div

I am struggling to position text in a column order. I have tried using float, but it affects other elements on the page.

Below is my code snippet:

<!DOCTYPE html>
<html>
  <head>
    <style>
      h1 {
        color: white;
        font-family: arial black;
        font-weight: 900;
        background-color: cornflowerblue;
        padding: 20px;
        text-align: center;
        font-size: 40px;
      }
      * {
          padding: 0 2%;
          color: #2e3e50;
          font-family: sans-serif
      }
      .description {
        color: gray;
        padding: 15px;
        margin: 5px;
        text-align: center;
        border: 1px solid red;
        font-size: 18px;
      }
      h2, h3 {
        text-align: center;
      }
      .ingredients {
        // float: left;
  width: 33.33%; /* three boxes (use 25% for four, and 50% for two, etc) */
  padding: 50px; /* if you want space between the images */
      }
    </style>
  </head>
  <body>
    <a href="/">Back To Recipe List</a>
    <h1>{{ template_recipe | title }}</h1>
    {% if template_description %}
      <p class="description">{{ template_description }}</p>
    {%else%}
      <!--<p>A {{ template_recipe }} recipe.</p> --> 
    {% endif %}
    <h3>Ingredients - {{ template_ingredients | length}}</h3>
      <!-- Implement a for loop to iterate through 
      `template_ingredients`-->
      
      {% for ingredient in template_ingredients %}
        <p class="ingredients">{{ ingredient }}</p> 
      {% endfor%}
    <h3>Instructions</h3>
    <ul>
    {% for key, instruction in template_instructions|dictsort %}
      <!-- Add the correct dictionary element to list 
      the instructions -->
      <li>{{key}}: {{instruction}}</li>
    {% endfor %}
    </ul>
  </body>
</html>

This is the current output:

text 



text 


text

The desired output format:

Text           Text                  Text 

Note: Using float will impact the layout of other elements on the page.

If you have any suggestions or solutions, please help. I'm new to CSS and also using Flask!

Answer №1

To create a structured layout, you should add rows and columns. Below is an example with 3 equal columns where the code has been tested and works perfectly:

 <!DOCTYPE html>
<html>
  <head>
    <style>
     * {
      box-sizing: border-box;
    }
    
    /* Create 3 equal columns that float next to each other */
    .column {
      float: left;
      width: 33.33%;
      padding: 10px;
      height: 300px; /* Can be removed, used for demonstration */
    }
    
    /* Clear floats after the columns */
    .row:after {
      content: "";
      display: table;
      clear: both;
    }
    
    h1 {
      color: white;
      font-family: arial black;
      font-weight: 900;
      background-color: cornflowerblue;
      padding: 20px;
      text-align: center;
      font-size: 40px;
    }
    
    * {
      padding: 0 2%;
      color: #2e3e50;
      font-family: sans-serif
    }
    
    .description {
      color: gray;
      padding: 15px;
      margin: 5px;
      text-align: center;
      border: 1px solid red;
      font-size: 18px;
    }
    
    h2, h3 {
      text-align: center;
    }
    
    .ingredients {
      width: 33.33%; 
      padding: 50px; 
    }
    </style>
  </head>
  
  <body>
    <a href="/">Back To Recipe List</a>
    <h1>{{ template_recipe | title }}</h1>
    
    {% if template_description %}
      <p class="description">{{ template_description }}</p>
    {% else %}
      <!--<p>A {{ template_recipe }} recipe.</p> --> 
    {% endif %}
    
    <h3>Ingredients - {{ template_ingredients | length}}</h3>
     
     <div class="row">
       <div class="column" style="">
         <h2>Column 1</h2>
         <p>Some text..</p>
       </div>
       
       <div class="column" style="">
         <h2>Column 2</h2>
         <p>Some text..</p>
       </div>
       
       <div class="column" style="">
         <h2>Column 3</h2>
         <p>Some text..</p>
       </div>
     </div>
      
      {% for ingredient in template_ingredients %}
        <p class="ingredients">{{ ingredient }}</p> 
      {% endfor%}
      
    <h3>Instructions</h3>
    <ul>
     {% for key, instruction in template_instructions|dictsort %}
       <li>{{key}}: {{instruction}}</li>
     {% endfor %}
    </ul>
  </body>
</html>

Answer №2

Flex also functions efficiently.

     <!DOCTYPE html>
          <html>
            <head>
              <style>
                .div1{
                   display: flex;
                   justify-content: space-between;
                   padding: 20px;
                }
              </style>
            </head>
            <body>
              <div class="div1">
                   <p>Lorem ipsum dolor sit, amet consectetur adipisicing eliArchitecto voluptates, voluptas tempora fuga earum quasi.</p>
                   <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Architecto voluptates, voluptas tempora fuga earum quasi.</p>
                 <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Architecto voluptates, voluptas tempora fuga earum quasi.</p>
           </body>
         </html>

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

displaying an image that has been uploaded inside a div element

Is it possible to display the uploaded image within a red box? Here is the code snippet: http://codepen.io/anon/pen/ZWXmpd <div class="upload-image"> <div class="upload-image-preview"></div> <input type="file" name="file" val ...

What is the best way to decrease the height of my table in Bootstrap 4?

I'm trying to decrease the height of my table rows, but adjusting the height through CSS on the <tr> element doesn't seem to be working. Even if I set the height to less than 50px, I don't see any noticeable change. How can I make the ...

Applying CSS transitions to an element whose value is dynamically set using JavaScript

When I set a transition for an element in my style sheet, along with some template HTML code, the transition works fine if I delay setting the attribute in JavaScript. However, it doesn't work without the delay. I think this is because both processes ...

Try adding three periods to the ngbind directive

I have a string that looks like this: <a href="#" ng-bind="::MyString | limitTo:40"></a> However, I want to add three dots after the string only if it is longer than 40 characters. Is there a way to achieve this? ...

Guide on fetching and parsing JSON data in jQuery by utilizing a URL

As a newcomer to jquery, I am struggling to understand and parse the json format. In my icenium project, I have included this JavaScript function in my js folder but it doesn't seem to be functioning properly. $(document).ready(function () { ...

Error message in PHP involving the require_once function

I'm currently using CODA and MAMP while working locally from my HTdocs folder. I've been delving into PHP and MVC structure, but encountering an issue with require_once. Despite trying various solutions found in other posts, none seem to work for ...

"Showcase a Pair of Images with Just a Single Click. Leveraging PHP and

With two div boxes, one for Input and the other for Output as displayed below: https://i.sstatic.net/C8dhm.png My goal is to upload an image using PHP and store it in both the Input and Output folders. Upon clicking the Submit button, I want the image f ...

Selecting options from the Gmail dropdown menu

I'm attempting to create a dropdown menu similar to the one in Gmail using Bootstrap. https://i.sstatic.net/K1NCg.png However, I have encountered 2 issues: 1- I am unable to click on the input checkbox to select all 2 - I would like it so that if ...

AngularJS's ng-model is able to handle dynamic changes effortlessly

I am working with an array of objects that can be viewed https://i.sstatic.net/Fg1L8.png Within the dropdown box, I have names listed and a text box that displays the value from the selected object https://i.sstatic.net/rY0ET.png The input box is current ...

AngularJS encountered an error: The token '|' was unexpected and expected ':' instead

My HTML file contains a condition where certain fields need to be displayed automatically in landscape mode using filters. However, when I run the program, I encounter the following code: <tbody> <tr ng-repeat="ledger in vm.ledgers ...

Changing the style of Vuetify v-list-item when hovering over it

I just started using Vuetify and I came across a v-list sample that I need help with. Here is the link to the Vuetify v-list sample on Github. My v-list: https://i.sstatic.net/ykTIi.jpg Code: <template> <v-card max-width="500" class= ...

In PHP, you can customize the colors to emphasize different data sets

In my MySQL database connected through PHP, I have data displayed in table format. My goal is to highlight certain data based on age with two conditions: Condition 1: - Color red (#FF7676) for ages greater than 24 Condition 2: - Color yellow (#F8FF00) fo ...

After deploying DRF, the CSS in Django admin is not displaying

After developing a web application using Django Rest Framework and React, I faced an issue during deployment on IIS. While the deployment is successful, I encountered a problem with the Django Admin where the styles were not displaying properly. This led t ...

Tips for reaching a node.js application deployed on Heroku

Trying to deploy my node.js app to Heroku has been a challenge. Despite watching various tutorials and reading the documentation, I'm stuck on how to send and receive data from the hosted node.js app. The communication between my webpage and the node. ...

I am encountering a 404 error when attempting to make a GET request for a file located in the same directory

Here is the Javascript code that utilizes the get method. The directory contains both files - the HTML file where the JS code resides, and the text file. Below is an image of the console displaying errors. ...

As a newcomer to Javascript, I am currently working on developing a CRUD-based application and could use some assistance with implementing the Edit functionality

I am currently in the process of developing a Javascript CRUD application that showcases data within an HTML Table. I have successfully implemented the Create, Read, and Delete functions. However, for the Edit function, I am looking to display the data in ...

What is the best way to display items within a table using React?

I'm just starting to learn React. Can someone show me how to use the "map" function to list elements from two different arrays in two columns? state = { dates: ["2000", "2001", "2002"], cases: ["1", "2", "3"] } render() { return ( <thea ...

What are the steps for adding information to a MySQL database with GWT, HTML, and either JDBC or Hibernate?

I have been working with GWT (Google Web Toolkit) version 2.5.1 and successfully developed a sample application to display the username and password. However, I am now facing challenges in inserting these values into a MySQL database table using JDBC or Hi ...

I am struggling to apply custom CSS styles to the scrollbar within a Card component using MUI react

import React from "react"; import Card from "@mui/material/Card"; import CardActions from "@mui/material/CardActions"; import CardContent from "@mui/material/CardContent"; import CardMedia from "@mui/material/Ca ...

Creating a versatile button that adjusts seamlessly across different screen sizes, from smartphones to larger

I need assistance with a straightforward task - creating a button that remains centered at all times. What would be the most effective method to achieve this, and is it possible to do so easily? Currently, I have managed to create a blue button; however, ...