Creating dynamic DIVs that adjust fluidly to different screen

I have a section containing 4 centered divs.

Here is what I want to achieve:

When the screen width is over 1280px, display all 4 divs in a row. When the screen width is less than 1280px, show 2 divs per row. And when the screen width is under 640px, display one div per row.

HTML

<section class="gallery">
  <div class="flex flex--center">
    <div class="gallery-div">    
    </div>
    <div class="gallery-div">    
    </div>
    <div class="gallery-div">    
    </div>
    <div class="gallery-div">    
    </div>
  </div>
</section>

CSS

.flex{
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}

.flex--center{
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
}
.gallery-div{
    background-color:pink;
    width:300px;
    height300px;
    margin:10px 5px;}

@media (max-width: 640px) {
  .home-section .flex {
    display: block;
  }

Answer №1

Avoid using Bootstrap unnecessarily for this task!

You can achieve the desired design with just a few lines of CSS:

.gallery-div{
    background-color:pink;
    width: 100%;
    height:300px;
    float: left;
}
.gallery-div:nth-of-type(even) {
  background-color: cornflowerblue;
}
.clear {
  clear: both;  
}

@media (min-width: 640px) {
    .gallery-div{
      width: 50%;
    }
}
@media (min-width: 1280px) {
    .gallery-div{
      width: 25%;
    }
}
<section class="gallery">
  <div class="center">
    <div class="gallery-div">    
    </div>
    <div class="gallery-div">    
    </div>
    <div class="gallery-div">    
    </div>
    <div class="gallery-div">    
    </div>
    <div class="clear"></div>
  </div>
</section>

Answer №2

To achieve different column widths for various screen sizes, utilize the Bootstrap grid system.

The Bootstrap grid system encompasses four key classes:

  • xs - Extra Small (suitable for phones)
  • sm - Small (ideal for tablets)
  • md - Medium (optimized for desktops)
  • lg - Large (designed for larger desktops)

http://getbootstrap.com/css/#grid

You have the flexibility to incorporate multiple classes within a single element, such as:

<div class="col-xs-12 col-sm-6 col-md-3"></div>

Answer №3

To enhance your gallery-div, simply add this class.

<div class="col-xs-12 col-sm-6 col-md-3"></div>

This class configuration will adjust the display of images in your row: 1 image for xs (extrasmall), 2 images for sm (medium), and 4 images on larger screens like md (medium) and lg (large).

We hope this explanation helps clarify things a bit more :)

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 implement CSS styling for a particular HTML element

HTML: <div class="tagline">Web Portal Name</div><br /> <div id="logged_in_user_dtls" style="color:#FFF; margin-top:15px; font:bold; width:100%; position:relative; margin-left:800px;float:left;"&g ...

Ways to adjust a specific div within an ng repeat using the value from JSON in AngularJS

When I select a different option from the dropdown menu, such as cities or states, the values are populated from a JSON file. My specific requirement is to hide a button only when the value 'No data' is populated upon changing the dropdown select ...

Designing an HTML banner with 100% width and 660 pixels in height for optimal responsiveness

I am in need of a responsive banner that will be displayed at 100% width and 600px height on fullscreen. Below is the code I have: <style> /* default height */ #ad { height: 660px; width: 100%; } @media o ...

What is the best way to line up three divs horizontally and center the contents inside each one?

I'm brand new to CSS and need some help simplifying things. I am trying to create 3 divs that are all the same size, placed next to each other, with centered content in each one. Currently, I have a central div with a rotating image, and on the left a ...

Ways to customize the selected item's color in md-select using Angular Material?

Currently, I am utilizing the dark theme in angular-material 1.0.4. The appearance of the select element is different when an item is selected: https://i.sstatic.net/FRU0r.png Comparatively, this is how it appears with the default theme applied: https:/ ...

Struggling to perfect your CSS menu design?

For some time now, I have been experimenting with CSS menus that have three levels, but unfortunately, I am struggling to get the layout exactly how I want it. The menu structure itself is simply a series of nested unordered lists within the HTML: <ul ...

The CSS styling feature is not functional within the JavaMail API

After receiving an email sent using the JavaMail API, I noticed that the CSS styles are not being applied and only the HTML content is rendered. HTML CODE <!doctype html> <html lang="en"> <head> <meta charset="u ...

Every time I attempt to create a detail page for a table, I am consistently greeted with an error message

Error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/2909kher/entabell/detaljer.php on line 23 <!doctype ...

What is the best way to connect added elements together?

I am currently utilizing Plupload to upload files. Due to specific requirements, I need to place FilesAdded "inside" init as demonstrated below. The issue I'm facing is the inability to utilize jQuery.remove() on elements that have been appended usin ...

How can the "Search within page" feature locate words that are divided between different elements?

I need to present text in groups of a specific length while still being able to search for text that spans multiple groups on the page. Additionally, I want to include a character above each group. My current approach is as follows: body { marg ...

Exploring methods to iterate through an extracted div using Selenium in Python

In my previous step, I encountered a challenge while trying to extract text from a div. After several hours of troubleshooting, I was finally able to achieve the desired result. However, the next step proved to be equally difficult as I needed to implement ...

URLs not being gathered by the web scraping tool

Currently involved in scraping data from this specific website to compile a database. Previously, I had functioning Python code available on GitHub that successfully accomplished this task. However, due to a major overhaul in the HTML structure of the site ...

Struggling to get the knockout js remove function to function properly within a nested table structure

I've been encountering issues while trying to eliminate the formulation elements with the 'Delete comp' link. I can't seem to figure out why it's not functioning as expected. Moreover, the 'Add another composition' link o ...

Decoding JavaScript content with Python

Currently, I am dissecting this specific portion of HTML <script type="text/javascript"> var spConfig = new Product.Config({"attributes":{"150":{"id":"150","code":"size_shoe","label":"Schuhgr\u00f6\u00dfe","options":[{"id":"494","label ...

Is there a method to track the progress of webpage loading?

I am working on a website built with static HTML pages. My goal is to implement a full-screen loading status complete with a progress bar that indicates the page's load progress, including all images and external assets. Once the page has fully loaded ...

What is the best way to halt a CSS transition using JavaScript when dealing with an image?

I am facing an issue while attempting to create a basic CSS transition using JavaScript. The goal is for the start button to trigger the movement of an element, based on the duration of the keyframe animation. Then, clicking the end button should make the ...

jQuery sidebar with a fixed position

Is there a way to implement a sidebar menu feature using jQuery that reappears on the left as the user scrolls down the page? You can see an example sidebar menu here. ...

The autofocus feature does not function properly in an input tag that is not located within a form element

Is there a way to set the autofocus property in an input element that is not part of a form? I have tried adding the "autofocus" attribute to the input tag but it doesn't seem to be working. <div> //I have added the autofocus property her ...

Tips for stopping the loading of background images on an image slider

I am utilizing unslider to showcase an image slider on the landing page of my website. The slides are designed with background images in the CSS, and they adjust based on the media query. My concern is that I don't want all the slider images to load a ...

Update the href attribute when a button is clicked

Note: The buttons in the corner will not be submitting the search. The go button would still need to be clicked to submit it. Currently, I am working on a fun project during my free time at work. This project is not meant to be anything serious, but rathe ...