Extensive titles disrupt the layout of a grid on smaller screens that adapt to different devices

My approach to creating a responsive grid involves using clean CSS. You can check out the example here: https://tympanus.net/codrops/2013/04/17/responsive-full-width-grid/

While this method works well for displaying gallery images with fixed-width titles, it tends to break when the title lengths vary. Here's an example:

http://codepen.io/anon/pen/ObqJOg

.cbp-rfgrid {
    margin: 35px 0 0 0;
    padding: 0;
    list-style: none;
    position: relative;
    width: 100%;
}

.cbp-rfgrid li {
    position: relative;
    float: left;
    overflow: hidden;
    width: 16.6666667%; /* Fallback */
    width: -webkit-calc(100% / 6);
    width: calc(100% / 6);
}

.cbp-rfgrid li a,
.cbp-rfgrid li a img {
    display: block;
    width: 100%;
    cursor: pointer;
}

.cbp-rfgrid li a img {
    max-width: 100%;
}

/* Flexbox is used for centering the heading */
.cbp-rfgrid li a div {
    left: 20px;
    top: 20px;
    right: 20px;
    bottom: 20px;
    background: rgba(71,163,218,0.2);

    -webkit-align-items: center;
    -moz-align-items: center;
    -ms-align-items: center;
    align-items: center;
    text-align: center;
    opacity: 1;
  position:relative;
  background-color:red;
  padding:5px;
}

.cbp-rfgrid li a:hover div {
    opacity: 1;
}

.cbp-rfgrid li a div h3 {
    width: 100%;
    color: #fff;
    text-transform: uppercase;
    font-size: 1.4em;
    letter-spacing: 2px;
    padding: 0 10px;
}

/* Example for media query: change number of items per row */

@media screen and (max-width: 1190px) {
    .cbp-rfgrid li {
        width: 20%; /* Fallback */
        width: -webkit-calc(100% / 5);
        width: calc(100% / 5);
    }
}

@media screen and (max-width: 945px) {
    .cbp-rfgrid li {
        width: 25%; /* Fallback */
        width: -webkit-calc(100% / 4);
        width: calc(100% / 4);
    }
}

@media screen and (max-width: 660px) {
    .cbp-rfgrid li {
        width: 33.3333333%; /* Fallback */
        width: -webkit-calc(100% / 3);
        width: calc(100% / 3);
    }
}

@media screen and (max-width: 660px) {
    .cbp-rfgrid li {
        width: 33.3333333%; /* Fallback */
        width: -webkit-calc(100% / 3);
        width: calc(100% / 3);
    }
}

@media screen and (max-width: 400px) {
    .cbp-rfgrid li {
        width: 50%; /* Fallback */
        width: -webkit-calc(100% / 2);
        width: calc(100% / 2);
    }
}

@media screen and (max-width: 300px) {
    .cbp-rfgrid li {
        width: 100%;
    }
}

How can I align the rows of columns so that the design won't break?

Answer №2

To position this div, you can adjust the offset to place it wherever you desire.

.cbp-rfgrid li a div {
    left: 20px;
    right: 20px;
    bottom: 0;
    background: rgba(71,163,218,0.2);
    -webkit-align-items: center;
    -moz-align-items: center;
    -ms-align-items: center;
    align-items: center;
    text-align: center;
    opacity: 1;
    position: absolute;
    background-color: red;
    padding: 5px;
}

Additionally, apply the relative position to the following elements:

.cbp-rfgrid li a {
    display: block;
    width: 100%;
    cursor: pointer;
    position: relative;
}

Answer №3

If you want to replace the float property with something else, here are two alternative methods to consider:

Method #01:

Option 1: Implement using css3 flexbox:

  .cbp-rfgrid {
    /* These properties should be added to the parent element */
    flex-wrap: wrap;
    display: flex;
  }

.cbp-rfgrid {
  margin: 35px 0 0 0;
  padding: 0;
  list-style: none;
  position: relative;
  flex-wrap: wrap;
  display: flex;
  /* Additional styles for child elements */
}

.cbp-rfgrid li {
  position: relative;
  /* Other styles */
}

/* Media queries for different screen sizes */

@media screen and (max-width: 1190px) {
  .cbp-rfgrid li {
    width: 20%;
  }
}

/* More media queries... */

<ul class="cbp-rfgrid">
  <li>
    <a href="#"><img src="image-url.jpg" />
      <div>
        <h3>Title 1</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="image-url.jpg" />
      <div>
        <h3>Title 2</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="image-url.jpg" />
      <div>
        <h3>Title 3</h3></div>
    </a>
  </li>
  <!-- ... -->
</ul>

Method #02:

Option 2: Use display: inline-block instead of float:

.cbp-rfgrid {
  /* These properties will eliminate extra spacing caused by inline-block */
  letter-spacing: -4px;
  font-size: 0; 
}
.cbp-rfgrid li {
  display: inline-block;
  vertical-align: top;
  /* Resetting styles to default */
  letter-spacing: 0;
  font-size: 14px;
}

.cbp-rfgrid {
  margin: 35px 0 0 0;
  padding: 0;
  list-style: none;
  position: relative;
  letter-spacing: 0;
  font-size: 0;
}

.cbp-rfgrid li {
  display: inline-block;
  vertical-align: top;
  position: relative;
  letter-spacing: 0;
  font-size: 14px;
  /* Other styles */
}

/* Media queries for different screen sizes */

@media screen and (max-width: 1190px) {
  .cbp-rfgrid li {
    width: 20%;
  }
}

/* More media queries... */

<ul class="cbp-rfgrid">
  <li>
    <a href="#"><img src="image-url.jpg" />
      <div>
        <h3>Title 1</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="image-url.jpg" />
      <div>
        <h3>Title 2</h3></div>
    </a>
  </li>
  <li>
    <a href="#"><img src="image-url.jpg" />
      <div>
        <h3>Title 3</h3></div>
    </a>
  </li>
  <!-- ... -->
</ul>

Answer №4

It appears that there are some unnecessary styles that may be hindering the functionality of your website.

I suggest simplifying your CSS code by optimizing the following:

.cbp-rfgrid li a div {
  background: rgba(71,163,218,0.2);
  opacity: 1;
  background-color: red;
  padding: 5px;
  margin: 5px;
}

.cbp-rfgrid li a div h3 {
  width: 80%;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
  color: #fff;
  text-transform: uppercase;
  font-size: 1.4em;
  letter-spacing: 2px;
  padding: 0 10px;
}

Check out the DEMO here

Answer №5

In my opinion, a versatile solution should be able to handle titles of varying lengths. I have come up with a method that utilizes CSS to truncate the title and include an ellipsis if it exceeds the available space:
http://example.com

The adjusted code snippet is as follows:

.cbp-rfgrid li a div h3 {
  width: 95%;  /* Adjusted to leave space for the ellipsis */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Answer №6

Instead of relying on CSS or HTML, I have opted to use jQuery for this particular solution.

window.onload = function () {
    var heights = $(".bod-title-w").map(function ()
    {
        return $(this).height();
    }).get(),

    maxHeight = Math.max.apply(null, heights);
    // alert(maxHeight);
    $(".bod-title-w").css("min-height", maxHeight+15);
    setTimeout(function () {
      //  $(".bod-title-w").css("min-height", maxHeight);
    }, 1000);
};

My approach involves identifying the div with the greatest height and then applying that same height to all other divs or wrappers.

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

Error arises when uploading csv files to Node.js with Multer due to an unexpected field presence

I'm currently working on implementing a file upload feature with the use of Node.js, Vue, and Multer. Below is the Vue.js front-end code: export default { data(){ return{ selected: "How do you want to input the data?", options: [ ...

How to create a looping animation effect for a div using CSS on hover

Using Bootstrap framework with Wordpress site .test { position: absolute; z-index: 9; left: 50%; height: 10em; width: 10em; margin-left: -5em; background-size: cover; opacity: 0; transition: opacity 0.5s ease; transform: translateY(- ...

Tips for refreshing captcha without the need to refresh the entire page

<img id="imgCaptcha" src="SandCaptcha.aspx?CHA=0azSeOdr7S7gTkFxtL6/5B6h2vj+naZnDR5jl/dvceoJHNXcooHfP2MkoWxPRVWvdK7NJHckH" style="height:60px;width:160px;"> (function() { 'use strict'; function refreshCaptcha() { document.querySe ...

Is It Possible to Align Text Horizontally within a Box?

Hey everyone! I'm trying to figure out how to display two elements, "1" and "new referral," side by side without the new referral having a green background. Any help would be greatly appreciated. I've shared my HTML and CSS code snippets below. I ...

Make window.print() trigger the download of a PDF file

Recently, I added a new feature to my website allowing users to download the site's content by following these instructions: Printing with Javascript || Mozilla Dev. Essentially, the user can print a newly formatted site stored in a hidden iframe. Now ...

Having difficulty formatting text alignment using CSS

Is there a way to maintain the alignment of text in the output of the 'About' section even when the content changes dynamically? I want the new line to appear just below 'Lorem', but currently, it shifts below the colon(:). Since the le ...

Browser resize affects the overlap of DIVs

I've seen similar posts before, but I believe the issue with my website (***) is unique. At full size, the website looks exactly how I want it to. However, when I resize the browser window, the text ("ABOUT INTERNATIONAL PARK OF COMMERCE.." and below ...

Dropbox menu within an extended webpage

I am looking to create a dropdown menu that behaves like the one on this website. The goal is for the dropdown to cover the entire webpage, hide the scroll bar, and "unmount" the other elements of the page, while still displaying them during the transition ...

Saving changes made in HTML is not supported when a checkbox is selected and the page is navigated in a React application using Bootstrap

In my array, there are multiple "question" elements with a similar structure like this: <><div className="row"><div className="col-12 col-md-9 col-lg-10 col-xl-10 col-xxl-10"><span>Question 1</span></div ...

What is the best way to smoothly switch from one Font Awesome icon to another?

I am working on an HTML button with the following code: <button id="sidebar-toggle-button" class="btn btn-primary header-btn" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample&q ...

Issue with Mootools Ajax call and form submission

I'm dealing with a table that displays data from a database. I'm trying to implement a way to (a) delete rows from the table and (b) edit the content of a row in real-time. Deleting rows is working perfectly, but editing the content is proving to ...

How to integrate a toggle switch into an Angular datepicker component

Can the toggle switch be placed inside the calendar? I would like to have it positioned at the top of the open calendar so that users can choose to view date details using the toggle switch <form #uploadForm="ngForm" (keydown.enter)="$event.preventDe ...

The hover function stops working once the dropdown class has been removed

I am currently experimenting with a bootstrap template. In the navigation bar, there is an option for "Blog" and "Test". For the "Test" button, I decided to remove the li class="dropdown " because I wanted to create a button that changes color on hover si ...

The flashing of Bootstrap tabs can be seen on various pages

Currently, I am encountering an issue with bootstrap tabs on my website. There seems to be a blinking problem with the tabs on certain pages. Check out this video showcasing the blinking tab in the search result page: Watch Here Interestingly, the same ta ...

Adaptable backdrop picture within a full-width container

Currently experiencing an issue with my website that requires some assistance. When a user visits my site, the image of the 'cutlery pouch' will resize based on the size of the user's browser or monitor. However, some users are seeing the b ...

Can Anyone Provide Assistance with CSS for Customizing the Bootstrap 4 Navbar Burger Icon

I am working on implementing a bootstrap 4 navbar into my Angular code base. When the screen size of the browser becomes smaller, I want to hide the navbar and instead display a hamburger icon with a side menu. I have tried a few solutions, but have not b ...

Angular Material Toolbar Experiencing Color Distortion

To see the issue in action, check out this CodePen: An ongoing problem I've encountered with Angular Material involves distorted colors on the toolbar. The edges of the toolbar display one shade of green, while the middle shows a different shade. Tak ...

Instructions for encoding a video using PHP, saving the encoded video to a file, and then decoding and storing it in a database

I have successfully encoded a video in PHP using HTML, and now I am trying to figure out how to decode and save it in PHP. However, I'm uncertain about the process of saving the encoded video and how to access it for decoding. Any assistance would be ...

Is it possible to generate an HTML element by utilizing an array of coordinates?

I have a set of 4 x/y coordinates that looks like this: [{x: 10, y: 5}, {x:10, y:15}, {x:20, y:10}, {x:20, y:20}] Is there a way to create an HTML element where each corner matches one of the coordinates in the array? I am aware that this can be done usi ...

Activate CSS transition only when not in active state

I have a situation where I want to change the background color immediately upon hovering over an element, and then revert back to the original color when no longer hovering. The CSS for this is straightforward: #el { background-color: black; } #el:hov ...