Step-by-step guide to setting up a responsive grid layout with automated scaling based on breakpoints for pairing cards with images and content side by side

After reading an article on Modern CSS grid solutions to common layout problems, I attempted to implement a two-way card layout as discussed.

You can view the demo on Codepen here.

However, when viewing on smaller screens, the paragraph text overlaps the image instead of wrapping into a new line if there is insufficient space:

https://i.sstatic.net/ARbfL.png

Do you have any suggestions on how to make the paragraph wrap when there is not enough space, rather than overlapping?

Code:

article {
  display: grid;
  grid-template-columns:
    minmax(2rem, 1fr)
    minmax(auto, 65ch)
    minmax(2rem, 1fr);
}

/* Center all children */
article > * {
  grid-column: 2;
}

/* Wider & centered images */
article > img {
  grid-column: 1 / 4;
  justify-self: center;
  width: 100%;
  max-width: 100ch;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));
  grid-template-rows: auto 1fr;
  width: 100%;
}

<article>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
</article>
<div class=grid>
   
<img src=https://via.placeholder.com/400x400 alt>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aut... 
...

Answer №1

It seems that the selector for your image is not functioning properly. The img element is not a direct child of the article as specified in the CSS.

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 steps can I take to change my single-line navbar into a two-line design?

Lately, I've been working on a website with a navbar that looks like the one in this https://i.sstatic.net/5ptAj.png image. However, I want to change my navbar to look more like the one in this https://i.sstatic.net/KiHCf.png image. I've attemp ...

Ensure that both columns are of equal height

I attempted to create two columns with the same height, but it did not work as expected. Here is my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>@ViewBag.Title</title> < ...

Struggling with VueJS computed properties to automatically wrap text to the next line instead of using the <h4> tag

Currently, I have implemented a computed property to dynamically generate an interpolated string for rendering. Below is an example snippet from my code: <div class="productDetailContainer"> <nuxt-link :to="{ path: '/product/ ...

Change the default action of the hyperlink to initiate an AJAX request instead

Recently, I've been experimenting with Bootstrap on my local machine to create a website, and everything has been going well. However, I've encountered a peculiar issue that I'm not sure is related to Bootstrap or something else. My site co ...

I am looking to incorporate an OVG video onto my website

I have a video in OVG format that I want to add to my website. Since I am not very familiar with this file type, I'm concerned about how it will perform across different web browsers. I know that Firefox can play the file, but I'm unsure about In ...

Can CSS namespace attribute selectors be used with XHTML/HTML elements?

I am trying to customize elements that have the xlink:href attribute in XHTML, but I am facing some difficulties with it. Here is the code I tested: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/19 ...

How to make stylish block buttons with ReactJS and Bootstrap

I'm looking to make block buttons that fill the entire div. In React, I'm using the react-bootstrap package to achieve this. Below is the code snippet I have implemented: <Container> <Row className="justify-content-center"& ...

The relationship between formatting context and atomic inline-level boxes is crucial for understanding

Check out this link for more information on inline boxes An inline box is a unique element that falls under the category of both inline-level and participates in its containing inline formatting context. When a non-replaced element has a 'display&ap ...

Customize border color for a single cell in a table using Bootstrap 4

I'm facing an issue where I'm trying to apply a red border color to a specific cell, but it's not appearing on the top and left sides of the cell. It seems like the border color of other cells is taking precedence over the one I'm tryin ...

Tips for connecting Angular events to <input> elements in HTML?

Within my Angular application, the value of a variable is manipulated by an HTML element <input> through two-way binding like this: <input [(ngModel)]=variableName (OnKeyup)="DoSomething" > However, the two-way binding with ...

Animating list items within a grid using Vue.js

I have arranged a list of items inside a grid using vuejs, and I am dynamically updating the content of a cell within the grid by changing the class name. My goal is to change the element inside the cell using CSS3 animation. I tried applying CSS animation ...

Connecting onClick event to a dynamically created div using Dojo

While working with dojo 1.9.2, I encountered an issue when trying to add an onClick function to a dynamically created piece of HTML code like so: clickableDiv = "<div data-dojo-attach-point=\"testBtn\">Click Me!</div>"; self.racks.in ...

The serialize() function in jQuery is not functioning as expected

Greetings to all! I attempted to serialize a form using the jQuery method serialize() by its unique ID. Here is the HTML code snippet: <form name="structure_form" id="structure_form" method="post"> <div id="add_sections" class="postbox add_s ...

transform ajax data into an array structure

I need to convert the values retrieved from an AJAX call into an array so that I can access and format them accordingly. $(document).ready(function() { $('#documenter_nav > li a').click(function(){ var url = $(this).attr('data- ...

resolve styling issue with image navigation

Struggling to make a CSS image rollover navigation bar and having trouble positioning the images correctly. Seeking assistance in resolving this issue. ...

How to stop form submission when Enter key is pressed in Angular

Despite scouring stackoverflow for answers, none of the solutions have worked for me. I have tried various approaches, which I will outline below: <form (keydown.enter)="$event.preventDefault()" ...> <button (keyup.enter)="skillsHandleEnter($eve ...

Adjust the <h1> tag's size using responsive Bootstrap design

Is it possible to adjust the font size of the <h1> tag specifically for small and extra small screens? The default size of the <h1> tag is suitable for medium screens but appears very large on small and extra small screens. I would like to cu ...

Is there a method to trigger the DOM style change hook without using asynchronous code?

We have learned that css animation/transition does not work with display: block&display: none; therefore, I attempted to use someDom.style.transition = '0.3s' someDom.style.display='block' someDom.style.opacity=1 to create an anim ...

PHP and MySQL combination for efficient removal of multiple dropdown choices

I'm struggling to figure this out. Essentially, it's a form with multiple dropdown menus where users can select one or more items to filter MySQL data results. I want to dynamically update the other dropdown menus as the user makes selections. Fo ...

Transfer a document to a php server using AJAX and jQuery in place of a traditional form

Is there a way to use AJAX for uploading files in PHP without reloading the page or performing additional functions? I want to keep it simple. Any suggestions? <form action="upload.php" method="post" enctype="multipart/form-data"> <label st ...