Is it feasible to have content wrap around a Grid-Item?

I've been experimenting with the new CSS Grids layout module to arrange my content. I have a set of paragraphs that are 8 columns wide, and I'm looking to insert a figure that spans 3 columns in columns 1-3. After that, I want the following paragraphs to flow into the space next to the figure.

Is there a way to achieve this with CSS Grid? In traditional non-grid layouts, I would simply use float:left; on the figure. How can I replicate this behavior within CSS Grid?

The challenge is that I don't know the exact length of the paragraphs that come after the figure, making it difficult to designate specific columns for them.

To see a simplified example, you can visit this CodePen demo.

.grid-container {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  width: 50%;
  border: 1px solid #eee;
  margin: 1em auto;
}

.grid-container p {
  grid-column: 1 / 6;
}

.grid-container figure {
  grid-column: 1/3;
  background: rgba(155, 155, 255, 0.5);
  margin: 0;
  padding: 1em;
}

figure img {
  width: 100%;
  height: auto;
}

figcaption {
  margin-top: 0.5em;
}
<html>
  <head>
    <title>A Floating Grid Item?</title>
  </head>
  <body>
    <div class="grid-container">
      <p>Fake Lipsum - is that even a real thing? Lipsi lipsum lipsooo doo.</p>
      <figure>
        <img src="http://38.media.tumblr.com/3a8dee9aae420a0048907c54ff701fc8/tumblr_n8m6qv50X11r238sko1_500.jpg" alt="A cat">
        <figcaption>I want the paragraph(s) below to flow around this box.</figcaption>
      </figure>
      <p>I want this to flow around the image. Please?</p>
      <p>It would be great if this would flow too, if it's not naturally below the image because of the amount of text.</p>
      <p>Again, it would be great if this would flow too, if it's not naturally below the image because of the amount of text.</p>
      <p>Yet again, it would be great if this would flow too, if it's not naturally below the image because of the amount of text.</p>
      
    </div>
  </body>
</html>

Answer №1

Simply place the text you want to wrap around the image inside a div element and then let it flow around the figure.

<html>
<head>
<title>A Floating Grid Item?</title>
</head>
<body>
<div class="grid-container">
  <p>Lorem Ipsum - is that even a real thing? Lorem ipsum doo.</p>
  <figure>
    <img src="http://38.media.tumblr.com/3a8dee9aae420a0048907c54ff701fc8/tumblr_n8m6qv50X11r238sko1_500.jpg" alt="A cat">
    <figcaption>I want the paragraph(s) below to flow around this box.</figcaption>
  </figure>

  <div class="text">
  <p>I want this content to wrap around the image. Please?</p>
  <p>It would be great if this text could also wrap around, even if it's not naturally positioned below the image due to its length.</p>
  <p>Again, it would be fantastic if this text could also wrap around, even if it's not naturally positioned below the image due to its length.</p>
  <p>Yet again, it would be wonderful if this text could also wrap around, even if it's not naturally positioned below the image due to its length.</p>
  </div>
  </div>
  </body>
  </html>

Apply this CSS:

.text {grid-column: 3 / 6;float:left;}

Answer №2

As outlined in the CSS Grid Specification, the use of float does not impact grid-items, making it challenging to achieve the desired layout.

Keshav's suggestion is currently the most effective way to ensure that a short paragraph positioned next to a figure does not cause subsequent paragraphs to drop below the figure.

If we can anticipate that the paragraph immediately following the figure will be long enough to extend past the figure itself, a CSS workaround like the following could be implemented:

figure { grid-column: 2 / 4; }
figure + p { grid-column: 5 / 10; }

However, this approach does not address potential gaps between the paragraph following the figure and the subsequent paragraph. Additionally, neither solution allows text to smoothly flow underneath the figure.

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

deactivate a vessel, causing all of its contents to be rendered inactive

Hi there, I am in search of a container that has the ability to disable all elements within it, not just inputs but also images and other elements without the disabled property. I do not want to hide the container, but rather disable it. Do you know if suc ...

The CSS filter "progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=80)" is not functioning properly in Firefox

Trying to apply a filter effect using CSS but encountering issues in Firefox: progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=80 ) opacity: 0.5; -moz-opacity: 0.5; Any s ...

Bootstrap: Retrieve an image from a modal

I am working on a modal that contains a variety of selectable images. When an image is clicked, I want to change the text of the button in the modal to display the name of the selected image. Additionally, I would like to grab the selected image and displa ...

Changing Images with Button Click in Javascript

I am facing an issue with my buttons that should swap images once clicked. The first two buttons work perfectly, but for the third and fourth buttons, the images do not disappear when clicking another button. Below is the current code in the document head ...

What is the best way to show the current value of a checkbox element in the future?

I want to add multiple checkboxes using this method: $(".btn-sample").on("click", function() { $(".container").append( '<input class="check-sample" type="checkbox" value="'+check_value+'"/>' ); }); The issue I' ...

Is it possible to toggle between hide and show functions using Jquery?

I have a question about two jquery functions that I've been struggling with. $(document).ready(init); function init() { $(".alphaleft").hover(function (g) { $(".boxy,.yee").show(); }, function (g) { $(".boxy,.yee").hide(); }); } ...

Flexbox helps create responsive layouts with ease

Utilizing flex to centrally position my element within my layers has worked well for me, but I encountered an issue when switching to a smaller screen size. The element simply scales down in size instead of taking up the full width like it does with Bootst ...

How can I prevent my mouse click from being overridden by my mouse hover?

Currently, I am developing a Sudoku game using JavaScript and facing some challenges with mouse events. My goal is to enable users to hover over a square and have it change color, as well as click on a square to highlight the entire row, column, and quadra ...

How come my HTML form keeps refreshing instead of displaying an alert after submission, even though I've included onsubmit="return false"? Additionally, it seems to be throwing an error

Why is this basic HTML and JavaScript code not functioning properly? Instead of alerting once the form is submitted, it refreshes the page and throws an error. It has also been reported to sometimes throw a CORS error when using 'module' as scri ...

Issues with the code: $("input[type=checkbox]:checked").each(function() { var checkboxId = $(this).attr("id"); });

When submitting a form, I need to retrieve the IDs of all checked checkboxes. Currently, $(this).id() is causing an error. What is the correct code to obtain the IDs of all checked checkboxes? $("#pmhxform input:checkbox:checked").each(function() { ...

Guide to toggling the anchor tag functionality as a button with JavaScript

I am trying to dynamically enable or disable an anchor tag that is used as a button using JavaScript. Within a certain condition, I want to control the state of this button. Here is the specific button in question: <div class="row my-2 text-right& ...

Choosing particular contenteditable divisions using jQuery

Consider the following HTML structure for a specific type of blog post editor: <div class="entry"> <div class="title" contenteditable="true"> <h2>Title goes here</h2> </div> <div class="content" contenteditable ...

Tips for keeping the main section from scrolling while scrolling through the side navigation

Within my Angular application, I have implemented a sidenav and a main section. My desired behavior is to prevent any scrolling in the main section while I am scrolling in the sidenav, similar to the functionality seen on the Angular Material Design docume ...

Tips for accessing CSS properties on the img tag

I could use some assistance with CSS. I am in the process of creating a tree structure using many <ul> and <li> tags. The issue arises when I have multiple <li> elements with a specific class, each containing an <img> tag. How can ...

Site optimized for mobile devices

I need to create a website that is optimized for mobile devices like Android, iPhone, and Blackberry, as well as desktop and tablet. The screen resolutions of these devices can vary greatly. My supervisor has suggested developing one site that can adjust ...

Tips on removing the red border from a text box within an HTML form with the help of AngularJS

My form has the following appearance: https://i.stack.imgur.com/T3NQW.png Upon clicking the submit button, the textbox fields display a red border due to the required attribute. Afterwards, I aim to click the reset button in order to remove the red bord ...

Using Regular expressions in JavaScript to eliminate content between two HTML comment tags

I am currently dealing with two HTML comments in this format: <!--Delete--> Blah blah blah blah <!--Delete--> I need to remove these comments, along with any characters and newlines. I am utilizing JavaScript and Grunt for this replacement ta ...

Enhancing the appearance of individual cells within an HTML table by applying custom classes

I'm in the process of automating report generation for my organization. Our workflow involves using R to summarize data from Excel, then utilizing Rmarkdown, knitr, and the "htmlTable" package to generate HTML files. Currently, I am implementing CSS ...

What is the method with the greatest specificity for applying styles: CSS or JS?

When writing code like the example below: document.querySelector('input[type=text]').addEventListener('focus', function() { document.querySelector('#deletebutton').style.display = 'none' }) input[type=text]:focu ...

Adjusting Window Size Causes White Space to Appear Above Div

I currently have two inline-block div in my post page that showcase my latest book reviews. Typically, these sections align perfectly and occupy the same amount of space on the screen. However, for certain window sizes, one of the inline-block elements end ...