Tips for replicating a float layout with CSS grid

In the layout I have designed, there is an image on the left and text content on the right. The structure resembles this:

.left {
  max-width: calc(100% - 150px);
  float: left;
}

img {
  width: 300px;
  max-width: 100%;
}
<div class="wrapper">
  <div class="left"><img src="https://i.sstatic.net/W6Sd8.png" /></div>
  <div class="right">Hello, I am Kitteh</div>
</div>

The concept behind this design is to allow the image to be a maximum of 300px, while ensuring it always leaves at least 150px for the text content positioned next to it. The text will take up as much space as needed, with a minimum of 150px available due to the presence of the image.

I aim to replicate this layout using CSS grid, however, I am unsure how to utilize grid-template-columns effectively to achieve the same functionality. Currently, my attempt looks like this:

.wrapper {
  display: grid;
  grid-template-columns: auto minmax(150px, auto);
}

img {
  width: 300px;
  max-width: 100%;
}
<div class="wrapper">
  <div class="left"><img src="https://i.sstatic.net/W6Sd8.png" /></div>
  <div class="right">Hello, I am Kitteh</div>
</div>

While this setup enforces the 150px minimum width for the right column, it lacks the flexibility to expand to accommodate the image's size. How can I modify the grid layout to emulate the behavior achieved by floating elements in my initial example?

Answer №1

replace minmax(150px, auto); with minmax(150px, 1fr)

fr unit will occupy the remaining space.

The 'fr' unit functions similarly to percentages (%), but with added convenience. For instance, if you desire 4 columns of equal size, instead of using 25% four times, you can simply use 1fr for each column.

When employing grid-gap: 20px, using percentages may result in a scrollbar. However, utilizing the fr unit resolves this issue by distributing the available space without causing scrolls.

In addition, converting grid-template-columns: 200px 1fr into percentages would require

grid-template-columns: 200px calc(100% - 200px)
, demonstrating the beneficial efficiency of the fr unit.

Consider the fr unit as a flexible resource that efficiently manages available free space.

Want to learn more? Check out this informative css grid video series here

Hopefully, this explanation proves helpful.

To see it in action, visit this link

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

Activate the drop-down division when the search box is in focus with xoxco technology

Currently, I am incorporating Xoxco's tag input plugin which can be found at the following link: In my customization, I have implemented JQuery's focus() function <input id="tags_1" class="tag-holder" type="text" class="tags" /></p> ...

Rotating Images in 3D with CSS

I am looking for guidance on how to create a rotating image effect on a webpage using CSS/JS. The image should rotate into the plane of the page, similar to the example shown in this post. Can you assist me with this? To better illustrate what I'm tr ...

Issue with continuous loader malfunction

I integrated a 3-second mini-loading animation on my website. It shows up every time I refresh the site or navigate to another page. The issue I'm facing is that once I add the loading section, it never stops (it should only last for 3 seconds). Usua ...

Optimizing WebGrid Alignment in MVC3 Using RAZOR

I need to customize the width and alignment of each column in a WEBGRID within MVC 3. Specifically, I have 5 columns and I want to set different width and alignment for each header. Here is my code snippet: @{WebGrid grid = new WebGrid(); if (@Model.V ...

Troubleshooting a JQuery accordion malfunction within a table

I am populating the data dynamically. However, the Accordion feature is not functioning correctly. JSFiddle link http://jsfiddle.net/aff4vL5g/360/ Please note: I am unable to modify the HTML structure. Current table Desired output The first accordio ...

Middle alignment not functioning properly in vertical layout

vertical-align: middle is not behaving as expected. This resource discusses vertical align A reference states the following: The element should be positioned in the center of its parent element. However, when examining the link below, "world" does no ...

Is there a way to display the full text of a lengthy select option?

I'm facing a challenge with long <option>s in my <select> box - I don't want the box to be too wide, but when I style it smaller, the full text gets cut off. One idea is to hover a duplicate of the text exactly over the option the mou ...

Is there a way to eliminate a style from the final element of a list?

I have just completed a project that looks similar to this: I am wondering if there is a way to remove the line at the end of the last column? (it's within a PHP while loop) Here is the code snippet: <div style="border-bottom: 1px solid #cdc ...

Spinner loading animation not showing correctly

Even though it shows up when I hover over and inspect, the image remains completely invisible. It's saved in the same folder and I believe all the file names are correct, but for some reason, it's just not working as expected. Any help would be g ...

Offspring component fails to reverse the skew as intended

Is it possible to skew a navigation bar without affecting the text inside it? I tried skewing the main div of the navigation bar and using a negative skew value on the text, but it didn't work as expected. How can I achieve this effect successfully? ...

Output the name of the file while executing the ant process

Currently, I am utilizing YUI compressor within an ant build process to compress CSS and JavaScript files. I would like the compressor to display the name of each file it is processing as it goes along, so that in case of an error, I can easily pinpoint wh ...

What are some alternative ways to create a text field with the same style as Material UI's without relying on the Material UI

With the power of HTML, CSS, and Reactjs I am aiming to create a functionality where the placeholder animates up to the border upon clicking on the text field An example can be seen with Material UI text field: https://material-ui.com/components/text-fie ...

CSS - using numbers to create dynamic background images

I am looking to create a design where the background images in CSS display increasing numbers per article, like this example: This idea is similar to another post on Stack Overflow discussing using text as background images for CSS. You can find it here: ...

Top and bottom fieldset legends in Bootstrap 4

Looking for suggestions on how to include legend text on the bottom border in bootstrap 4. I attempted to use position and margin, but it didn't work. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.mi ...

Encountered a parsing error while attempting to include the navigation bar page

<?php echo <<< END <!DOCTYPE html> <!-- --> <html> <head> <title>Nav</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0 ...

Having trouble with elFinder? Encountering an error stating "Undefined is not a function" when attempting to

I am currently in the process of integrating the elFinder file manager into my project. Here is a summary of what I have accomplished so far. I have included the following files: $arr_css = array( "main.css", "jquery-ui.cs ...

Sideways and alternative directional scrolling

I need assistance achieving a horizontal scroll with the main content centered in a div, while having links that move left and right. <div id="left"> Scrolls left </div> <div id="home"> Opens on this </div> <div id="right"> ...

Adjusting image width to fit the screen while maintaining its aspect ratio and maximum size

I am seeking a solution to display an image that adjusts its width to fit the browser screen, while also resizing its height according to the aspect ratio without exceeding its original size. I came across this post, and although it provided some guidance, ...

CSS styles from Angular 2 components spilling outside of their boundaries

Check out the Plunker where I'm facing an issue. "If you comment out the styles in the ThirdComponent, you can see it affecting the parent and sibling components' styles." – adriancarriger (Special thanks to Adrian) Within my component, I am i ...

Strange behavior observed with CSS intellisense in Visual Code

While working on a ReactJS project, I noticed that when I opt for the style Jsx Attribute for inline CSS, . CSS intellisense seems to not work or only work after I manually add some fields. However, if I manually type the style attribute without using t ...