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

A guide to filling an irregular shape (such as a star) based on a percentage using CSS in a React project

I am currently working on developing a React component that showcases a rating percentage using a star icon with two different shades. For example, if the input rating is 79%, I want to display a star with 79% of it in gold color and the remaining 21% in ...

What is the best way to grasp the concepts behind MaterialUI styled components?

Can someone please help me understand how to apply styles to a component using MaterialUI? I've been reading the documentation but it's all very confusing to me. What exactly are classes and how do I connect the const style to the BeerList compon ...

The css property of *ngContainerOutlet is ineffective when applied to an ng-component with encapsulation

When I utilize *ngContainerOutlet to dynamically insert components, it wraps the component's template within an ng-component tag which causes my CSS styles to become ineffective. For example: <div class="my-class"> <ng-container *ngComp ...

Update the text content in the specific span element when the class attribute matches the selected option in the

How can I dynamically update text inside a span based on matching classes and attributes without hardcoding them all? An ideal solution would involve a JavaScript function that searches for matches between span classes and select field options, updating t ...

Attempting to create a JavaScript class within an HTML document

Having an issue with instantiating a JavaScript class in a separate HTML file. The JavaScript class looks like this: class Puzzle { constructor(fenStart, pgnEnd) { this.fenStart = fenStart; this.pgnEnd = pgnEnd; } } module.exports = Puzzle; ...

Issues arise when implementing a sticky position using CSS with incomplete functionality

I want to create a website with a menu bar that stays at the top even on long pages. However, when I use position: sticky in CSS, the menu disappears after scrolling a certain distance. Does anyone know how to fix this issue? HTML: <nav> <ul cla ...

Using flexbox to adjust the height of a block in the layout

Is there a way to modify the height of .half-containers1 and .half-containers2? Additionally, how can I create the triangle shape that is shown in the image? Is it achievable with CSS alone or do I need to use an image? Check out my fiddle for reference ...

Tips on attaching a class to elements in a loop when a click event occurs

In my HTML, I am displaying information boxes from an array of objects that are selectable. To achieve this, I bind a class on the click event. However, since I am retrieving the elements through a v-for loop, when I select one box, the class gets bound to ...

Any ideas on how to fix the error that pops up during the installation of the bootstrap package in Node

The npm command is not recognized as a valid cmdlet, function, script file, or operable program. Please double check the spelling of the command and ensure that the path is correct before trying again. This error occurred at line 1. npm i bootstrap + ...

Tips for using a unique format for a single field in jqGrid

My JQGRID site has table configuration set up like this: configid config rule 1. Application_name 'MyApps' 2. Max number 1,000,00 3. Application End 12/31/2012 Some cells are for numbers and others for dates. If a user wants to edit a date ce ...

What is the best way to horizontally align an image beneath a navigation menu that is built using <ul> tags?

Seeking a way to center an arrow graphic under the selected item in a simple CSS drop-down menu? Considering using the :after selector, but currently being used for drawing dividing lines between menu options: HTML: <ul> <li>Option Zero&l ...

How can I enable the "edit" function for a button within a list item?

I am currently working on a feature that allows users to edit list rows by clicking a button. However, I am facing an issue where the "edit" button only edits the first row instead of the matched line. This functionality is part of a program I am developi ...

"Using jQuery to trigger a click event on a specific column within

Welcome to my HTML table. <table id="status_table" class="table table-bordered"> <tr> <th>Serial Number</th> <th>Product Number</th> <th>Description< ...

Use percentages for the width in CSS and pixels for the height, ensuring that both dimensions are equal

I'm attempting to create a square that adjusts its size according to the screen size. Currently, I have the width set to 25%. Is there a way to ensure that the height remains the same length in pixels as the width? Appreciate any assistance on this ...

Reposition picture "overlays" additional HTML components

I am struggling with rotating an image by clicking on a button as the image overlaps the buttons. Can anyone provide guidance on how to achieve this without overlapping? For reference, here is my code and an example: here (http://jsfiddle.net/jj03b17n/5/) ...

Use jQuery to trigger a click event when an element is in focus, unless it was clicked to

I am currently developing a website using the MDL framework. One issue I encountered is that there is no default select form element provided. After some research, I found a solution by utilizing a menu component that displays when the input is clicked. Th ...

Linking the location of the pop-up to the currently selected text box

I am currently experimenting with setting the top and left values relative to the selected_element (which is active at the time of triggering the popup) in a manner similar to a tooltip. I attempted to use $().position() in combination with jQuery, but it ...

Disregarding 'zIndex' directive within JavaScript function, an image stands apart

There is an issue with the z-index values of rows of images on my webpage. Normally, the z-index values increase as you scroll further down the page. However, I want certain items to have a lower z-index than the rest (except on hover). These items are i ...

Having trouble rendering the response text from the API fetched in Next.js on a webpage

I've written some code to back up a session border controller (SBC) and it seems to be working well based on the output from console.log. The issue I'm facing is that the response comes in as a text/ini file object and I'm unable to display ...

Conceal the element if the offspring is devoid of content

I want to implement a feature where a div is hidden when its child element is empty. To achieve this, I aim to assign the class .no-content to the div if it contains no elements. Below is my existing code with spaces: <div class="ee-posts-list&quo ...