Arranged in individual rows, the items are placed from left to right

I am looking to display multiple rows of data, with each row potentially having a spacer element that shifts the text on that row to the right. The spacer needs to be contained in its own div element for styling purposes such as adding borders or background colors. Similarly, the text within each row also needs to be in a separate div for individual styling options. It is important to note that the text content within these divs may exceed the boundaries of the containing box.

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

I have experimented with CSS properties like float:left, but I am having difficulty clearing the float to achieve proper row alignment. I have also tried using display:inline-block, but have not been successful in implementing it effectively.

Below is a snippet of the basic HTML structure I have been working with. Despite trying various CSS configurations, I believe starting from scratch may result in a more optimized solution.

<div class="row">
    <div class="text" style="width:500px;">Row one text</div>
</div>
<div class="row">
    <div class="spacer" style="width:500px;">Row two spacer</div>
    <div class="text" style="width:200px;>Row 2 text</div>
</div>

<div class="row">
    <div class="spacer" style="width:700px;">Row three spacer</div>
    <div class="text" style="width:100px;">Row 3 text overflowing</div>
</div>

Answer №1

If you're looking to achieve a layout similar to the one shown in the image, complete with spaces and all, I suggest utilizing the power of Flexbox.

I've taken the liberty to recreate the code for your convenience. Please let me know if you encounter any issues.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
  </head>
  <body>
    <style>
      .container {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
      }

      .item {
        flex: 0 32%;
        height: 100px;
        margin-bottom: 2%; /* (100-32*3)/2 */
        font-size: 30px;
      }
      .itemSec {
        font-size: 15px;
      }
    </style>

    <div class="container">
      <div class="item">
        first
        <div class="itemSec">column add text</div>
      </div>

      <div class="item">
        second
        <div class="itemSec">column add text</div>
      </div>

      <div class="item">
        third
        <div class="itemSec">column add text</div>
      </div>
    </div>
  </body>
</html>

Answer №2

To solve this issue, one approach is to insert a <div> in each row that clears the float:left. Here's an example:

<div class="row">
    <div class="text" style="width:500px; float:left;">Row one text</div>
    <div style='clear:both;'></div>
</div>
<div class="row">
    <div class="spacer" style="width:500px; height:25px; float:left;">Row two spacer</div>
    <div class="text" style="width:200px; float:left;">Row 2 text</div>
    <div style='clear:both;'></div>
</div>
<div class="row">
    <div class="spacer" style="width:700px; height:25px; float:left;">Row three spacer</div>
    <div class="text" style="width:100px; height:25px; float:left;">Row 3 text overflowing</div>
    <div style='clear:both;'></div>
</div>

While this method may not be the most elegant, it effectively resolves the issue.

The CSS code below helps handle the overflowing text:

.text { white-space: nowrap; }

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

Creating a form with an input field and an image side by side in HTML

I've been attempting to display an HTML input element and an SVG image on the same line without success. I've tried various solutions from stackoverflow, but none seem to work for me. Here's my current HTML code: <div id = "inline_eleme ...

javascript for each and every textarea

I am looking to apply my JavaScript code to all the Textarea elements on my page. $_PAGE->addJSOnLoad(" $('.textarea').each(function() { $(this).keyup(function() { var characterCount = $(this).val().length; var mes ...

Is it possible to create a CSS grid with a varying number of columns?

I recently set up a webpage with a CSS grid that centers the main section at 80% width. <html> .... <body> <main> <section> </section> </main> </body> .... </html> main { display: grid; j ...

A guide to creating a personalized horizontal dashed separator in Material UI

I'm looking to customize a divider template by turning it into a horizontal dashed divider, but I'm struggling to override it. Even when attempting an sx edit with a borderRadius, the divider doesn't change as expected. source code: import ...

Is there a way to create a miniature camera or map within a scene in three.js without cropping the viewport?

Is there a way to create a mini-map or mini-cam using three.js? The idea is to have two camera views - one mini-map or "mini-cam" in the top right corner (as shown in the image) and the main camera view covering the rest of the scene without the border. ...

Personalized labels for your JQuery Mobile sliders

Struggling to make this work correctly, I aim to introduce tick marks and custom labels into jQuery Mobile's slider widget. My goal is to add tick markers at 0, 25, 50, 75, and 100 with a unique string above each tick. Additionally, I want these label ...

updating the HTML DOM elements using JavaScript is not yielding any response

One way that I am trying to change the background of a div is by using a function. Below is an example of the html code I am working with: $scope.Background = 'img/seg5en.png'; document.getElementById("Bstyle").style.background = "url("+$scope.B ...

Unable to get CSS transition to function properly after adding a class using jQuery

I am trying to create a hover effect that shows an image when the mouse is over a specific div, but for some reason, the transition is not working as expected. I understand that using visibility: hidden cannot be animated. Here is the code snippet: $(d ...

The <span> tag creating a surprise gap following a word

Using Angular4, I've come across an odd issue where one of my span elements is adding an extra space to the end of words like this: https://i.stack.imgur.com/H4TD7.png The only CSS properties that are set include font-family, -webkit-font-smoothing, ...

The number of contents determines the dynamic CSS border style

Could you please assist me in identifying what this design element is called? I have tried searching on Google, but had no luck finding exactly what I am looking for. Here is an example of the look I am aiming for: https://i.sstatic.net/AXUvq.jpg Can so ...

Issues arise when the elements <html> and <body> fail to adhere to a 100

Lately, I've been struggling with various problems related to heights. I was able to resolve the height issues of certain elements using flex:auto and flex-shrink:0. However, I'm facing difficulties getting the HTML and Body tags to cooperate ( ...

The behavior of the div element is not as expected when using "<select>", but it functions properly when using "<input>"

I found this code snippet on http://jsfiddle.net/Lijo/PJcZQ/. It features two divs labeled "GROUP1" and "GROUP2" with slightly different content. GROUP 1 <div class="secondTextBox"> <select name="ctl00$detailContentPlaceholder$ddlStatus" id= ...

Including a Textbox completely disrupts the Menu design

I have been working on a project involving ASP and C#. Within the project, I am utilizing a masterpage for the navigation bar located at the top of my pages. It appears as follows: https://i.sstatic.net/Qb1P8.png The navigation bar includes standard nav ...

Can anyone provide a method for identifying the location of the page fold on a specific viewport?

Many innovative designs have successfully managed to position their content above the page fold in a way that it always remains visible within the viewport. Currently, I am using JavaScript to calculate the height, but this approach has its drawbacks. Her ...

How to set DIVS to be hidden when the page first loads

I am currently working on a project where I need to use JavaScript to hide certain <div> elements when the page loads. I have also included jQuery in my code. Below is what I have tried so far: $(document).ready(function() { hide(); function hid ...

The issue of blank spaces appearing in Outlook 2007 within conditional comments

Having been immersed in the world of coding HTML emails for several years, I've encountered numerous conditional comment-based 'bulletproof buttons' available online. While most of them do the job just fine, I've hit a roadblock with Ou ...

Is the HTML templating mechanism compatible with Angular?

Trying to implement HTML templating in my Angular application, my code looks like the following: <script type='text/html' id="sampleId">...</script> However, upon loading the HTML, I noticed that the script block was not present in ...

The sidebar on the right side of the screen is content to relax beneath my

The positioning of my sidebar is currently below the content on the left side of the page, but I want it to sit beside the content on the right side. I've tried various solutions from similar questions without success. Here is an example of how I woul ...

Conceal Class When Input Value is "X"

Is there a way to dynamically hide a specific seller (<div class="seller">) from a table using CSS and PHP based on a get parameter? Each seller has a unique ID represented by <input type="hidden" value="1" name="seller_id">. For example, if I ...

Attempting to implement Ajax for my AddToCart feature while iterating through a form

At the moment, I am facing an issue where only the first item is being added to the cart and the page remains unchanged. On each page, there are a minimum of 3 items, with a new form created for each one. However, the other products do not get added to the ...