Prevent the second row from wrapping to a new line on smaller screens in Bootstrap 3

I'm facing a challenge that I can't quite wrap my head around.

I have a layout with 2 rows of 3 images. On screens 841px and smaller, I want the columns to be 50% - this part is working fine. However, since there are rows of 3, on mobile devices it displays as 2-1 and then moves to the next line for the second row. I understand this may be the intended behavior but my question is how can I avoid it?

I've created a jsfiddle to demonstrate what I mean. I want image 4 to fill the gap on the right of image 3 so that all images are displayed in a continuous order without any gaps. I tried floating the images to the left to see if that would resolve it, but no luck.

JSFIDDLE

HTML

<div class="row">
 <div class="col-xs-4">
  <a href="#"><img src="https://s32.postimg.org/hm1cb910x/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
 </div>

 <div class="col-xs-4">
  <a href="#"><img src="https://s32.postimg.org/f5zixeiy9/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
 </div>

 <div class="col-xs-4">
  <a href="#"><img src="https://s32.postimg.org/aldcigz8x/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
 </div>
</div>

<div class="row">
 <div class="col-xs-4">
  <a href="#"><img src="https://s32.postimg.org/kjyb4y8oh/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
 </div>

 <div class="col-xs-4">
  <a href="#"><img src="https://s32.postimg.org/duwd2ocq9/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
 </div>

 <div class="col-xs-4">
  <a href="#"><img src="https://s32.postimg.org/o68pvc4fl/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
 </div>
</div>

CSS

.margin-bottom {
padding-bottom:20px;
}

@media (max-width: 841px) {
.col-xs-4 {
width:50%;
}
}

@media (max-width: 511px) {
.col-xs-4 {
width:100%;
}

}

Answer №1

Check out the revised fiddle here

In this scenario, it is necessary to utilize multiple column classes simultaneously.

Considering different screen sizes:

xs is for mobile devices.

sm is for small devices like tablets.

md is suitable for medium-sized devices.

lg is designed for large desktops.

I recommend removing any custom CSS code you may have added.

<div class="col-xs-6 col-sm-4 col-md-3">
  <a href="#"><img src="https://s32.postimg.org/o68pvc4fl/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
 </div>

Thank you for your attention.

Answer №2

Bootstrap utilizes separate classes in its grid system to cater to different devices VIEW GRID SYSTEM FOR Mobile, tablet, desktop. The col-xs class targets extra small devices, col-md for medium devices, and col-sm for small devices.

Check out the Updated Fiddle here

<div class="row">
   <div class="col-xs-12 col-sm-6 col-md-4">
    <a href="#"><img src="https://s32.postimg.org/hm1cb910x/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
   </div>

  <div class="col-xs-12 col-sm-6 col-md-4">
    <a href="#"><img src="https://s32.postimg.org/f5zixeiy9/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
  </div>

  <div class="col-xs-12 col-sm-6 col-md-4">
    <a href="#"><img src="https://s32.postimg.org/aldcigz8x/image.jpg" alt="" class="img-responsive margin-bottom"/></a>
  </div>
</div>

On x-small screens, each image section occupies a full row; on small screens, it's two images in a row with the third wrapping; while on medium screens, it's three images in a row.

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

The width of the Div element is not determined by the parent CSS grid, leading to a lack of ellipsis

I am working on a grid layout where each column contains a div with text. My goal is to have the text show ellipsis when it overflows the column, even though the number of columns is random. I attempted to use the following CSS for the text-containing div ...

Is it necessary to create a separate class for a frequently used CSS style?

When I find myself needing a specific style frequently, like float: left, is it more efficient to create a separate class for it or include the style directly in each class where it is required? Check out this example page for clarification. JS-Fiddle Ex ...

How to align an element to the bottom using CSS without relying on the parent's height

Is there a way to align an element to the bottom of its container without knowing the exact height? I'm trying to position the "links" div next to the logo text at its bottom, but since the logo size can vary and may even be an image, setting a fixed ...

Tips for fading out two elements after completing a drag and drop action

Visit this Codepen for more examples - Codepen I have been developing a drag and drop feature in my application. Once the item is dropped, it transitions from red to green and fades out smoothly. The droppable element behind the draggable should also fad ...

CSS code to create a single content bar flanked by two sidebars

I have been working on a beginner-friendly webpage and have managed to style it using CSS to some extent. My goal is to have the content centered with a white background, flanked by two sidebars styled in blue. To work on this page, please visit: The ...

"Displaying <canvas> at its true resolution even when zoomed in, thanks to Retina technology

I am currently working with a <canvas> element to create vector graphics and I want to ensure that it displays in the highest quality possible while using minimal resources across various viewing conditions. Specifically, I aim to achieve a 1:1 mappi ...

"Enhancing User Interaction with jQuery Hover State Dropdown Menus

Here's my issue: I've created a drop-down menu and I want the text color to change when hovering over the menu. Additionally, I'd like the hover state to remain active when hovering over the submenu. Currently, I'm using this code: $( ...

Tips for creating a centered Reindeer

My reindeer code seems to be a bit off-center. Even though I followed a tutorial, my reindeer is not aligning properly as shown in the tutorial. Here is all the CSS code for the reindeer: .reindeer { height: 510px; width: 350px; position: ab ...

Position div elements randomly on the webpage when it first loads

I am trying to achieve a set of divs that will appear randomly on the page when it loads. Currently, I have the divs moving around the viewport in a random manner, but they all seem to load in the top left corner. This is the method I am currently using: ...

Bootstrap v4 navbar media query

Recently, I've encountered a problem with one of my projects. The mobile layout is not working properly on different pages of the same web application. When inspecting any page on the website IndianStandups using Chrome's inspector tools and swi ...

Struggling to find solutions for debugging HTML codes in Visual Studio Code and linking my CSS page to my HTML page

I ran into some issues with linking my CSS stylesheet to my HTML pages. I tried using the <link rel="stylesheet" href="style.css"> command, where style.css is the name of the file. However, for some reason, it wasn't working ...

iPhone users may encounter a freezing issue with the footer when scrolling

Can someone assist in keeping the footer at the bottom right of the screen on my website? When I view it on my iPhone, the footer remains fixed when scrolling down. Here is how it looks: . Your help would be greatly appreciated. Thank you and have a wonder ...

Adjust the sidebar size in Bootstrap 5 to align perfectly with the navbar brand

https://i.stack.imgur.com/Ra0c7.png I'm trying to ensure that the width of the sidebar on this page aligns consistently with the end of the logo in the top right. However, using variations of col-xxl-1 etc. is causing inconsistent sizes at different ...

I'm having trouble getting web safe fonts to work in CSS. Can someone please help me understand why this isn't working?

Apologies in advance for what may seem like a basic question, but I'm encountering a perplexing issue. Despite specifying web-safe fonts like Didot using the code: header h1{ font-family: Didot, serif; font-size: 36px; } my browser continues to only ...

Expanding image in card to fit device width using Ionic

Currently utilizing the ionic framework and attempting to expand the image within the card to fit the device width. Referencing the example provided here, here is my current code progress. <div class="list card"> <div class="item item-avatar" ...

Issue with Hiding Bootstrap Tabbed Content Correctly

I am currently struggling to make a Bootstrap tab field function correctly, encountering some obstacles along the way. While the content is successfully tabbing between each div, the issue I'm facing is that the content from each tab is actually bein ...

Chrome users may experience a jumping effect when using the jQuery .animate()

When I use a jQuery .animate() function to move an absolute positioned container, Firefox, Internet Explorer, and Opera correctly move the container to the top. However, in Chrome, the container jumps to the top and then moves down 200px from the margin. ...

Responsive design functions well in a limited desktop browser, but fails to deliver on a mobile device

I recently created a mobile-friendly layout for my website, To achieve this, I customized the CSS for smaller screens and utilized jQuery to dynamically apply the appropriate styles. I have set up two layouts with a width threshold at 940 pixels. functio ...

Tips for clearing floated images when the sidebar is also floated

Can someone assist me with this code snippet? I am trying to figure out a way to clear the nearest floated block. It seems that whenever I use the clear property, it clears any floated blocks on the page, even if they are in a different div. Here is the ...

Unlocking the Potential: Unveiling the Art of Activating Navigation in Single-Page Websites

I am currently developing a one-page website. In order to enhance the navigation experience, I want the active section or "page" to be visually highlighted in the navigation bar. At present, when I click on a link, it successfully displays with an underlin ...