Styling code for a layout with two columns aligned to the left using

I am looking to create a layout where two pieces of text are displayed side by side in columns using CSS. The left-hand column will have variable length text, while the right-hand column will have fixed length text.

The desired layout should show the two columns floating next to each other like this:

[Variable Text] [Fixed text]

If the variable text is long, I want it to wrap around to the next line. For example:

Here is a very long  Hello World
piece of variable
text which wraps

Currently, my code works when the variable text is short, but unwanted white spaces appear when the variable text wraps around. For instance:

Here is a very long               Hello World
piece of variable
text which wraps

This is the code that I have implemented:

#wrapper {
  margin-right: 100px;
}
#left-col {
  float: left;
  max-width: 100%;
  background-color: #CCF;
}
#right-col {
  float: left;
  width: 100px;
  margin-right: -100px;
  background-color: #FFA;
}
#cleared {
  clear: both;
}
<div id="wrapper">
      <div id="left-col">Here is a very long piece of text which wraps</div>
      <div id="right-col">Hello World</div>
      <div id="cleared"></div>
</div>

Answer №1

In my opinion, if we change the property max-width of #left-col to something other than 100%, it should function properly. For instance:

#wrapper {
  margin-right: 100px;
}
#left-col {
  float: left;
  max-width: 200px;
  background-color: #CCF;
}
#right-col {
  float: left;
  width: 100px;
  background-color: #FFA;
}
#cleared {
  clear: both;
}

You can view a jsfiddle with different text lengths for better understanding here.

Answer №2

Consider updating your HTML to make it more meaningful and easier to style without relying heavily on class or id. You could use alternative tags like <dl>, which may not be the most efficient but are easy to style.

Utilizing calc() could be beneficial in this case :)

#container {
width:15em;/* for demonstration purposes */
  }
dl,dt,dd {
  padding:0;
  margin:0;
  
}
dt {
  float: left;
  clear:left;
  text-align:justify;/* to fill up entire first lines when text wraps*/
  max-width: calc(100% - 100px);
  background-color: #CCF;
  margin-top:0.1em;
}
dd {float:left;
  width: 100px;
  background-color: #FFA;
}
<dl id="container">
      <dt>1 This is a very long piece of text that will wrap around</dt>
      <dd>Hello World 1</dd>
      <dt>2 Another long piece of text that continues</dt>
      <dd>Hello World 2</dd>
      <dt>3 A lengthy piece of content goes here</dt>
      <dd>Hello World 3</dd>
      <dt>4 More detailed information will go here</dt>
      <dd>Hello World 4</dd>
      <dt>5 short description</dt>
      <dd>5</dd>
</dl>

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

What is the best way to align a div in the middle of an input-group div?

When dealing with a nested div, where the parent div is styled with an input-group class, my goal is to have the nested div centered within the available line or on the next free line space. This is the desired layout I want to achieve regardless of the n ...

What impact do negative positioning have on CSS elements in terms of responsibility?

Just diving into CSS and I have a question to pose: Is it considered good practice to utilize negative positioning in CSS? Are there potential compatibility issues with browsers like IE7 and IE8? What is your suggestion? #example-bottom { position: relat ...

Creating a universal function to handle setTimeout and setInterval globally, inclusive of clearTimeout and clearInterval for all functions

Is it possible to create a universal setTimeout and setInterval function with corresponding clearTimeout and clearInterval for all functions while passing values to them? The situation is as follows: 1. More than 8 functions utilizing setInterval for act ...

How can I ensure that Chakra UI MenuList items are always visible on the screen?

Currently, I am utilizing Chakra UI to design a menu and here is what I have so far: <Menu> <MenuButton>hover over this</MenuButton> <MenuList> <Flex>To show/hide this</Flex> </MenuList> </ ...

Turn off a feature on older buttons

For my project, I am looking to dynamically add specific elements like buttons. However, every time a new button is added, the function call for the old button gets duplicated. var btnElem ='<button type="button"class="doSomething">Button< ...

Create interactive elements (such as circles, rectangles, and other shapes) that can be moved around the screen, all

Currently, I am developing a PHP web application for restaurant management. One of the key features I am working on is creating a floor plan layout of tables. This will allow the admin to easily drag and drop tables from the left side onto the canvas wher ...

Unexpected vertical line in MUI5 Stepper appears when invoking from a function

Greetings! I'm currently working on developing a MUI5 vertical stepper, but I've encountered an issue when rendering steps from a function. Specifically, there is an extra connector line appearing on top of the first step. If you'd like to ...

Counting the visible elements in Slick carousel

I have implemented slick.js to display a grid of 6 items (2 rows, 3 columns) per slide. I am looking for a way to include both the standard prev and next arrow navigation as well as an indication of the active item count for pagination assistance. For exa ...

I must modify the initial statement to appear in bold from the paragraph with the use of JavaScript and CSS

Mr. XYZ and Mrs. ABC are known for their integrity. They own a stylish car. In the next paragraph, I would like to emphasize the first sentence by making it bold, We admire our exceptional leader J.K.L. He shows great potential. In this section, I wan ...

What is a sophisticated method to hide an element from view?

My dilemma involves a dropdown menu and a list of elements that are initially set to hidden using CSS. When an item is selected from the dropdown, it becomes visible. However, I am struggling with finding a way to revert the previously selected element b ...

Display or conceal various content within div elements using multiple buttons

I have a set of 5 image buttons, each meant to trigger different content within a div. When the page loads, there should be default content displayed in the div that gets replaced by the content of the button clicked. The previously displayed content shoul ...

Implement the color codes specified in the AngularJS file into the SASS file

Once the user logs in, I retrieve a color code that is stored in localstorage and use it throughout the application. However, the challenge lies in replacing this color code in the SASS file. $Primary-color:#491e6a; $Secondary-color:#e5673a; $button-color ...

What steps can be taken to turn off specific warning rules for CSS mode in ACE editor?

Utilizing the Ace Editor (through Brace and React-Ace) is a key aspect of my project. In our implementation, we specify the editor's mode as "css" and integrate it into our webpage. While this setup functions correctly, we have observed that some of ...

When viewing my website on a mobile device in landscape mode, the CSS divs sized at 20% do not match the size of the div set at

Currently, I am working on a mobile application that is HTML5 cross-platform compatible. To design the app, I am utilizing Bootstrap 3, although it seems unrelated to the specific issue I am facing. Here's the problem at hand: Within my application, ...

When my script is located in the head of the HTML page, I am unable to

My goal is to make my JavaScript code function properly when I insert it into either the head or body elements of an HTML document. Let's look at some examples: First, I insert the script into the body as shown in this example (works correctly): ...

What can be achieved by combining Text Wrangler and terminal?

I'm curious about how to open an index.html file in terminal. For instance, I know that using sublime works like this: sublime index.html But what about textWrangler? And in general, how do I locate similar commands like "sublime" for any program of ...

Identifying specific text enclosed within tags in an external file using PHP

I recently started learning about php, and I am looking for a script that can identify text between specific tags in an external file. I came across a solution here that extracts the text within tags from a given string. However, I am unsure how to mo ...

Only scroll the div if it is not within the visible window

I've been looking at this snippet: The sidebar division is what I'm focusing on right now. I want the div to scroll along as I scroll down the page, but I need it to stop scrolling when its bottom is in view. The same should apply when scrollin ...

How can I extract text from nested HTML elements with a <a href> tag using the Jericho library?

Here is a snippet of my HTML code: <div class="itm hasOverlay lastrow"> <a id="3:LE343SPABGLIANID" class="itm-link itm-drk trackingOnClick" title="League Sepatu Casual Geof S/L LO - Hitam/Biru" href="league-sepatu-casual-geof-sl-lo-hitambiru-6816 ...

Using CSS to apply a gradient over an img element

Looking to add a gradient overlay to an <img> tag with the src attribute set to angular-item. Here's an example: <img src={{value.angitem.image}}> I attempted to create a CSS class: .pickgradient { background: -webkit-gradient(linear ...