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

Ways to resize column widths in tree views on Odoo version 10?

I have been struggling to adjust the column width of the columns in my tree view. I have attempted a few different solutions: Trying to change the width attribute in the field tag: width="100px" or width="15%%" Using the style att ...

Change the Bootstrap components according to the size of the screen

Is there a built-in Bootstrap feature to change an element's class based on screen size? I'm working on a webpage with scrollable card elements. On desktop, the cards are arranged horizontally, but on mobile they are stacked vertically, requirin ...

What is the best way to choose all text within a code box without highlighting the entire webpage?

I developed a CSS code specifically for my blogspot site to organize and store all of my code and technical snippets. .code { background:#dae6ef; background-repeat:no-repeat; border: solid #5C7B90; border-width: 1px 1px 1px 20px; color ...

Utilizing the HTML5 Download attribute for linking to external files

I am currently developing a web application for my own personal needs. One feature I would like to implement is the ability to set the download attribute on certain links. However, I have run into an issue where the files to be downloaded are hosted on ex ...

What steps can I take to ensure that the browser prints out a PDF copy of a webpage?

Imagine you have a website page saved as example.html, and also a printable file named example.pdf. Is there a way to prompt the browser to open and print example.pdf instead of example.html when users attempt to print? If this isn't achievable, how ...

Results not showing up

After attempting to incorporate the scores into a table, I am facing an issue where they are not displaying. Can anyone offer assistance with this problem? I am currently hosting on GitHub Pages. File: https://raw.githubusercontent.com/Eternal-Network/Ete ...

What is the best way to extract data from a textarea HTML tag and store it in an array before iterating through it?

I have a project in progress where I am developing a webpage that will collect links from users and open each link in a new tab. I am using the <textarea> tag in HTML along with a submit button for link collection. Users are instructed to input only ...

Learn how to create a PHP activity feed that functions like Facebook, and discover the best methods for converting JSON responses into HTML

We are currently working on a website that functions similarly to Facebook, specifically focusing on the activity feed. To load the user's activity feed, we use AJAX to send a request to a PHP file which then processes the logic and returns the values ...

The presence of the !doctype html tag completely messes up my

I am currently working on a code snippet that is supposed to provide the screen coordinates of a given object: <!DOCTYPE HTML> <html> <head> </head> <body style="margin:0px;padding:0px;"> <div id="help" style="top:100px;r ...

Using Html to differentiate input based on type

Looking for input on the code snippet below: <table class="details-table" *ngIf="animal && animaldata"> <tr *ngFor="let attribute of animaldata.Attributes"> <td class="details-property">{{ attribute.AttributeLabel }}& ...

Headers with a 3 pixel stroke applied

I have a design on my website that includes a 3px stroke around the header text to maintain consistency. I don't want to use images for this due to issues with maintenance and site overhead. While I know about the text-stroke property, browser suppor ...

Incorporate Text, HTML, and Embed Images into PHPWord

I am currently utilizing PHPWord to create word documents using PHP. I have a specific content format that I need to adhere to, as shown below: Title Description Image1 Image1 Description(HTML CONTENT) Image2 Image2 Description(HTML CONTENT) As of now ...

Determine the width of the window and adjust the positioning of jQuery UI tooltips accordingly

Struggling to adjust the jQuery UI tooltip position based on screen width, but can't seem to figure it out. Can someone assist me in detecting the browser's width and changing the tooltip position accordingly? [fiddle] $(function () { $(doc ...

The function getComputedStyle(elem).getPropertyValue('fontSize') returns incorrect values when the font size of the element is specified in em units

My current issue involves trying to obtain the font size of an element in the following manner: getComputedStyle(MyTargetElement , "").getPropertyValue('font-size') The result is coming back as 16px, when it should actually be 14px. W ...

Ways to emphasize the four edges of a table cell using border-collapse collapse

I am trying to emphasize the borders of cells with the class active. The challenge arises from the fact that the table's border-collapse property is set to collapse, causing the top and left borders of cells (except for the leftmost and top row cells ...

The word-break CSS property is ineffective in this situation

I've been experimenting with the word-break css property, but I just can't seem to get it to work even when using a simple example. Here's my code: React: render() { return ( <h5 className="word-break">A very very long line.... ...

How can Chinese characters be transformed into XML/HTML-style numerical entities and converted into Unicode UTF-8 encoding?

I have a situation where I need to change a text that contains both English words and Chinese characters into a format that includes XML/HTML-style numerical entities for the Chinese characters. Here's an example of the original text: Title: 目录. ...

The hover state of a div will not be lost if its parent element is not being hovered over

When hovering over the second, third, or fourth item, hidden text will appear on the left side. If you hover your cursor over the hidden text, it will disappear again. I want to be able to hover over the second item, move my cursor to "hide", and click o ...

Exploration of features through leaflet interaction

Attempting to plot bus routes on a map using leaflet with geojson for coordinates. Facing issues with making the bus line bold when clicked, and reverting the style of previously clicked features back to default. Current Progress function $onEachFeature( ...

Issues with internal linking in TYPO3 are causing anchor tags to malfunction

When creating an internal anchor, it is usually defined using <a name="anchorname">Introduction</a>. To link to this anchor, you would write something like <a href="#anchorname">Top</a>. In TYPO3, an anchor is a ...