Grid is failing to adjust its size to accommodate the width of the items

Is there a way to adjust the width of each grid box based on the content nested inside? For instance, in the case of the second testimonial where the item has a max-width of 500px, but the grid box remains unchanged. I attempted to set widths and max-widths for all different testimonials without success.

HTML & CSS

Answer №1

Concept

Your question entails achieving a grid layout similar to the one shown in your desired end result. Essentially, you need a grid box with 4 boxes per row. Below are examples of how the content will occupy spaces within multiple boxes:

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

You can modify the concept to resemble this structure:

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

Solution (based on your code)

The following modifications have been made based on your existing code, with minimal changes that work well in full-screen mode:

@import url('https://fonts.googleapis.com/css2?family=Barlow+Semi+Condensed:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');

*{
  font-family: "Barlow Semi Condensed", sans-serif;
  font-weight: 400;
  font-style: normal;
}

#general-container{
  display: grid;
  gap: 1rem; /* added to get the spacing between boxes */
  grid-template-columns: repeat(4, 1fr);
}

/* Additional CSS styles for different sections and elements */

.testimony-one{
  background-color: hsl(263, 55%, 52%);
  border-radius: 10px;
  grid-column: span 2;
}

[other testimonial classes and their respective styles]

.testimony-five aside{
  display: flex;
  align-items: center;
  justify-content: center;
}

[testimony individual styling as per requirements]

The Way I Would Approach This

To address this problem, my approach includes the following:

@import url('https://fonts.googleapis.com/css2?family=Barlow+Semi+Condensed:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

* {
  font-family: "Barlow Semi Condensed", sans-serif;
  font-weight: 400;
  font-style: normal;
}

#general-container {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(4, 1fr);
}

/* Individual styling for testimonials */

.testimonial {
  border-radius: 20px;
  border: 1px solid black;
  padding: 1.5rem;
}

.info {
  position: relative;
  display: flex;
  gap: 1rem;
  align-items: center;
}

/* Specific styling for first, last, and nth-child testimonials */

.testimonial:first-child,
.testimonial:nth-child(4) {
  grid-column: span 2;
}

.testimonial:last-child {
  grid-column: 4;
  grid-row: 1 / 3;
}

.additional testimonial styling for unique designs

What I have modified

In the HTML, I standardized all testimonies under the testimonial class for consistent styling across all elements. In the CSS, I utilized pseudo-classes like :first-child, :last-child, and :nth-child() for manageable styling of different testimonials.

Key Concepts

The solution incorporates the usage of span in grid-column to allow items to occupy multiple grid boxes horizontally. Additionally, techniques such as grid-row: span and grid-area were explored for vertical alignment control within the grid layout.

Things That Are Not Covered Here

While focusing primarily on aligning testimonial boxes correctly, aspects like container width management, aesthetic enhancements, and responsive design implementations were not addressed but can be included according to project requirements.

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

Do you believe that using bower to install HTML5Boilerplate is a beneficial decision?

Is it recommended to use HTML5 Boilerplate with Bower for installation? What steps should one take afterwards, considering that it has its own directory structure for CSS and JavaScript, leading everything to be contained under bower_components/html5boil ...

Tips for adjusting custom spacing margins within Material UI Grid

Is there a way to adjust the spacing between Grid items in Material UI? Here's the code I currently have: <Grid container spacing={1}> <Grid item xs={6}>Node 1</Grid> <Grid item xs={6}>Node 2</Grid> ...and so ...

PHP script redirects to its own URL successfully, but the functionality fails when triggered by CRON

I am facing an issue with a program that calls itself iteratively. redirect($_SERVER["PHP_SELF"]); function redirect($url,$seconds=0) { $html="<html><head><meta http-equiv='refresh' content='$seconds; URL=$url'>< ...

What is the best way to reveal the square's id value upon hovering over it exclusively?

In this assignment, I am refraining from using HTML and focusing on JavaScript to create functionality. Here's my progress so far: document.addEventListener("DOMContentLoaded", function () { let button = document.createElement('button' ...

Ways to manage the size of Material-UI vertical tabs?

After receiving assistance from others on the thread about Creating a Material-UI tab with image tabs instead of text labels, I was able to successfully implement images for my Material-UI tabs. However, I am facing an issue where I am unable to control th ...

Can a font size be adjusted dynamically based on the width of its containing class?

I have encountered this issue before, but the previous solution did not work for me. I am now seeking a viable alternative. Currently, I am developing a website and have an event announcement block that needs to display the event date spanning the entire ...

Is there a way to view images embedded in local .msg files in a web browser in HTML format?

Does anyone know how to display embedded pictures in local .msg files when opening them in a web browser using HTML format? Currently, when I open the .msg file with embedded pictures, all I see is a blank space indicating "picture not found." Interestin ...

How do I customize the border color for the Select component in Material UI?

I have been working on customizing the Select component provided by Material UI. Here is a visual representation of what it currently looks like: https://i.stack.imgur.com/YGheB.png My goal is to change the border-color of the select component from the ...

What is the best way to perfectly align an SVG inside its container?

Is there a way to horizontally center this SVG within .svg_wrapper? .svg_wrapper { border: 1px solid grey; width: 200px; } <div class="svg_wrapper"> <svg viewBox="0 0 600 425"> <path d="M 175, 175 m 0, -75 a 75,75 0 1,0 ...

Issue with CSS transition bug in Offcanvas on Android version 4.1.x

I am attempting to develop an offcanvas menu similar to the one featured in the Google Plus app. Currently, my code functions properly on most devices (android/ios) and browsers (ff, chrome, IE8+). The only issue I am encountering is on a Samsung Galaxy ...

What is the method for activating scrolling in an inner div instead of the browser window?

In the code snippet provided, I am encountering an issue where the browser window scrolls instead of the specified div with overflow-y: scroll when minimizing the browser window. How can I ensure that scrolling occurs within the div itself without making ...

Using HTML and JavaScript to automatically update one input date based on changes made to another

Currently, I am developing an Angular application and encountered a challenge with two date input fields: <div class="col-lg-3"> <div> {{ dataInizioLabel }} </div> <input required type="datetime-local" ...

Styling borders of an image

I am working on an application where I have a collection of images. When the user clicks on an image, it receives a thick border around it at a certain distance. Now, I want to customize this border to reduce the spacing between the image and the border b ...

Django - issue with table display showing empty row due to query set

In my project, I have two models: one named Season and the other one named Game: # Season class Season(models.Model): teams = models.ManyToManyField('Team', related_name='season_teams', blank=True) current = models.BooleanF ...

Ways to perfectly align an icon both horizontally and vertically in an <a> tag that covers the entire container

Looking to center an icon within an "a" tag that spans the entire div container? The goal is to make the entire container clickable for carousel navigation, rather than just the icon itself. Currently, I can achieve each desired effect separately. On the ...

Is there a way to override the padding of a container?

Working with Wordpress for the first time has been quite a challenge. Adjusting to everything is causing me some major headaches. So, I've got this container div and added a lazy 10px padding. However, for the theme I'm attempting to develop, I ...

The Data from Req.Body Doesn't Appear After Adding Form Fields

I've been struggling to find a solution to this issue, but here's the gist of it: I have a button that allows users to add a question and answer pair one at a time by clicking on an "Add Question" button. This is achieved through the append featu ...

JavaScript button with an event listener to enable sorting functionality

I am looking to implement a button that will reset all the filters on my page. Any ideas? Currently, I have multiple radio buttons for filtering items based on price, size, and color. My goal is to create a reset button that will remove all filters and r ...

AngularJS - directive template is not being compiled correctly

I am facing an issue with AngularJS. .directive('field', ['$routeParams', function($routeParams){ return { restrict: 'E', compile: function(tElement, tAttributes) { var template ...

Exploring individual elements within a set of data, the age-old art of classic ASP

I am looking to extract specific elements from a "select" statement in order to populate a table on a website using Classic ASP. For instance, if my select statement below fetches these values; Name ID Alex Alex123 On the webpage, I would like th ...