Arranging content in a horizontal row using div elements

My webpage features two div elements. One of the div represents header content, while the other div displays details content. I want both sets of content to be aligned side by side. Specifically, I need the details content to always appear on the right-hand side. If the detail content exceeds the available space, any extra content should automatically move to the next line. For example, if File4.pdf and File5.pdf are too long, they should wrap onto a new line.

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

I initially achieved this layout using a table, but now I would prefer to use div elements instead. However, when I attempt to use divs, the entire content shifts to a new line when it doesn't fit within the designated area.

To see my current implementation, check out the JSFiddle.

Answer №1

To accommodate the right side elements, you'll need a container with the following style:

float:left;
width:calc(100% - 222px); /*the -222px is the width of the left element + the padding + the margin*/
overflow:hidden;

Here's an updated example (adapted from provided jsfiddle):

dl {
  margin-bottom: 30px;
}
dl dt {
  float: left;
  font-weight: bold;
  margin-right: 2px;
  width: 200px;
}
dl dd {
  margin: 2px 0;
}
.details-container {
  float: left;
  width: calc(100% - 222px);
  overflow: hidden;
}
<div style="float:left;padding-right:20px;">
  <dl>
    <dt>Name:</dt>
    <dd>Foo Bar</dd>
    <dt>Date:</dt>
    <dd>01/04/2017</dd>
  </dl>
</div>
<div class="details-container">
  <div style="float:left">
    <dl>
      <dt>File Name:</dt>
      <dd>File1.pdf</dd>
      <dt>Page Count</dt>
      <dd>10</dd>
    </dl>
  </div>
  <div style="float:left">
    <dl>
      <dt>File Name:</dt>
      <dd>File2.pdf</dd>
      <dt>Page Count</dt>
      <dd>10</dd>
    </dl>
  </div>
  <div style="float:left">
    <dl>
      <dt>File Name:</dt>
      <dd>File3.pdf</dd>
      <dt>Page Count</dt>
      <dd>10</dd>
    </dl>
  </div>
  <div style="float:left">
    <dl>
      <dt>File Name:</dt>
      <dd>File4.pdf</dd>
      <dt>Page Count</dt>
      <dd>10</dd>
    </dl>
  </div>
</div>

Answer №2

To achieve your desired outcome, it is important to specify what exactly you are looking for. There are various approaches depending on factors such as the number of elements per line, responsiveness, or whether the content is within a fixed-size element. Do you need scrollbars to appear if the content exceeds the container's size?

Consider the following hints:

  • According to Gaby aka G. Petrioli, using a wrapper div around one or both of your divs may be necessary
  • If you have a fixed line height and are okay with scrollbars appearing when content wraps, set the height of both divs equally with 'overflow:scroll'. You can specify horizontal or vertical scrollbars using 'overflow-x:scroll' or 'overflow-y:scroll'
  • If you want items that don't fit to be hidden, define dimensions and add 'overflow:hidden'. This will make anything outside the wrapping div disappear
  • In cases where only one dimension is set, like in Gaby aka G. Petrioli's example, using 'overflow:hidden' can prevent overlapping when content wraps to a new line
  • If you require a certain number of items per line with each tile resized or scrolled, consider using percentages for layout. This approach is more complex and requires a specific question for a complete answer

Answer №3

Have you tried using FlexBox for your layout?

<div class="container">
<div class="left">
  <div class="item">
    <div class="title">Title</div>
    <div class="value">Value asd asd as das d</div>
  </div>
</div>
<div class="right">
  <div class="item">
    <div class="title">Title</div>
    <div class="value">Value asd asd as</div>
  </div>
  <div class="item">
    <div class="title">Title</div>
    <div class="value">Value adasd</div>
  </div>
  <div class="item">
    <div class="title">Title</div>
    <div class="value">Value</div>
  </div>
  <div class="item">
    <div class="title">Title</div>
    <div class="value">Value</div>
  </div>
  <div class="item">
    <div class="title">Title</div>
    <div class="value">Value</div>
  </div>
</div>
</div>

.container {
   display: flex;
 }

 .item {
   padding-bottom: 30px;
 }

  .container .left {
    display: flex;
    align-items: center;
    padding-right: 50px;
  }
    .container .right {
      display: flex;
      width: 70%;
      flex-wrap: wrap;
    }

    .container .right .item {
      width: 30%;
    }

Check out an example of this layout in action here

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

My @media queries are causing me some issues

Struggling with my @media queries. Once I upload my website on github, it fails to scale properly. However, when testing it in Webstorm (JetBrains), everything seems to be working fine. Any assistance would be greatly appreciated! Here is the link to my w ...

Adjustments made to the size of the h1 font

I am perplexed by the fact that these two h1 tags have different font sizes, even though they have identical properties and declarations. From what little knowledge I have, it seems like it may be related to inheritance; considering one h1 tag is nested an ...

Please select the links located beneath the see-through triangular or polygonal shapes

Review the code snippet provided at the following link: http://jsfiddle.net/q8Ycz <div style="background-color: red; width: 200px;" onclick="alert('behind')"> <div><a href="test">test test test test test test test test test ...

Leveraging JSON data with jQuery's ajax function

Utilizing jQuery to retrieve data from this API () has been a success. I have successfully fetched the main details such as "name", "height", and "mass". However, I am facing a challenge when trying to access the values of "homeworld", "films", "species", ...

Tips for resuming a video playback in HTML5 after pausing it for a brief moment

I am working with a video called introVid, and my goal is for it to pause for 2 seconds when it reaches the 1 second mark before resuming playback. Although I've attempted to achieve this using the code below, the video remains in a paused state after ...

Instructions for using jQuery to replace the final <a> tag within a specific section

Here is a section that I am working with: <section class="breadCrumb"> <a href="/">Home</a> <span class="divider">&gt;</span> <a class="CMSBreadCrumbsLink" href="/SCCU.Stg/Events-Calendar.aspx">Events Calendar ...

Focusing solely on the presence of lowercase characters within the text field

Is it possible to apply a different color to lowercase letters in a text area? I am looking to highlight all lowercase letters in red while keeping the uppercase letters black. ...

Best Practices for Handling Form State in ReactJS with Redux

Currently in ReactJS + Redux, I am utilizing Material-UI's TextField for a form where users input their firstName, lastName, birthMonth, birthDay, and birthYear. The existing setup works but appears redundant, especially when handling the birth date f ...

Exploring the power of Vue.js by utilizing nested components within single file components

I have been attempting to implement nested single file components, but it's not working as expected. Below is a snippet of my main.js file : import Vue from 'vue' import BootstrapVue from "bootstrap-vue" import App from './App.vue&apos ...

How to choose an option from a dropdown menu using React

In a React application, I am creating a straightforward autocomplete feature. The code is outlined below. index.js import React from 'react'; import { render } from 'react-dom'; import Autocomplete from './Autocomplete'; co ...

Authenticating with Objective-C

I am currently developing an iPhone app that sends a username and password to a website in order to retrieve the HTML source code of the loaded page. The login information is included in the NSString *post. When I check the _responseData instance variable ...

Hide the navigation menu when a page link is selected

Within my Angular 8 application, a fixed navigation bar is positioned at the top of the screen. Upon hovering over a dropdown nav link, a menu will appear for the user to explore. However, when a user clicks on one of these menu links, Angular Routing is ...

Using JavaScript, add a complex JSON record to a JSON variable by pushing it

I have been attempting to insert a complex JSON data record into a JSON variable using the following code: var marks=[]; var studentData="student1":[{ "term1":[ {"LifeSkills":[{"obtained":"17","grade":"A","gp":"5"}]}, {"Work":[{"obtained":"13"," ...

Arranging CSS text in a parallel format and aligning it vertically

I am working on a layout with two columns of text placed side by side, where one column is right aligned and the other is left aligned. Below is my current code: .alignleft { float: left; } .alignright { float: right; } .list-unstyled { padding ...

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...

JavaScript incorporates input range sliding, causing a freeze when the mouse slides rapidly

Currently working on a custom slider and encountering an issue. When quickly moving the mouse outside the slider's range horizontally, exceeding its width, the slider doesn't smoothly transition to minimum or maximum values. Instead, there seems ...

When attempting to retrieve a value from a dropdown menu in HTML using PHP, an undefined index error occurs

Having trouble extracting a value from a select tag in HTML using PHP, as it keeps reporting an undefined index error. Here's my HTML form: <form method="POST" action="proceso.php"> <label for="language">Language</label> <se ...

Deleting a division with the help of media queries

As a challenge, I am attempting to eliminate the .navbar-brand>img element once the screen size exceeds 768px by using media queries. Unfortunately, my efforts have not been successful so far. Although the styling applied to the img is successfully remo ...

The most effective method for positioning a child element to the left and top using Flexbox grid

I am currently working on visualizing queues and processes within a system. Each process is associated with a specific queue from which it retrieves data. I aim to create a layout similar to the following: https://i.stack.imgur.com/BXyts.png However, I a ...

Creating an animated transition for an element's height with CSS

I need to animate the height of a div that doesn't have a specified height. Using max-height isn't an option due to multiple potential height amounts. I attempted adding transition: height 0.2s ease to the div, but it didn't work as expecte ...