Transitioning from a two-column layout to a three-column layout as the screen width increases

Is there a way to dynamically change the number of columns in an unordered list when the screen expands past a certain width? I've heard Bootstrap might have a solution for this, but I haven't had experience using it before. Here's a link to my code on jsfiddle if anyone wants to take a look: http://jsfiddle.net/eN9qM/

The HTML:

<div class="sidebar">
   <div class="search-filters">Filters</div>
   <div class="search-results">
      <div class="listings">
         <ul class="container">
            <li class="item">Hello</li>
            <li class="item">Byebye</li>
            <li class="item">Chuck</li>
            <li class="item">Paul</li>
         </ul>
      </div>
      <div class="listings-footer"></div>
   </div>
</div>

The CSS:

.sidebar {
   position: fixed;
   top: 47px;
   right: 0;
   bottom: 0;
   width: 60%;
   overflow-y: scroll;
   background-color: aqua;
}
.search-filters {
  background-color: white;
  height: 40%;
}
.search-results {
  padding: 10px 0 20px 20px;
  border: 3px solid black;
  height: 100%;
}
.listings {
  border: 3px solid red;
  height: 100%;
}
.listings-footer {
  border: 3px solid blue;
}
.ul {
  list-style-type: none;
  background-color: pink;
}
.li {
  background: green;
  float: left;
  height: 250px;
  margin: 0 10px 10px 0;
}
@media(min-width: 1200px) {
  .li {
    width: 25%;
  }
}
.li:nth-child(even) {
  margin-right: 0;
}

Answer №1

You're making great progress with that media query. Just remember to adjust the width value inside it.

@media (min-width: 1200px) {
    li, li:nth-child(even) {
        width:30%;
        margin-right: 3.33%;
    }
}

(Check out JSFiddle) This means that when the screen is at least 1200px wide, columns will take up 30% of their container and have a right margin of 3.33%. I've also adjusted li:nth-child(even) so the second column has a right margin too.

Bootstrap employs a 12-column grid system, where 12 columns represent 100% of the parent's width. For example, if an element has the classes col-lg-6 col-sm-3, it will be 50% wide on large screens (lg) and 25% on small screens (sm).

In your case, all your li elements should have the classes col-lg-4 col-md-6, meaning they'll take up 1/3 of the container in large screens and 1/2 in medium-sized devices. (Large refers to screens wider than 1200px, while medium covers sizes between 992px and 1199px. You can find more details here.)

Take a look at this JSFiddle. Note that without a col-sm-X class specified, each li takes up 100% width on small screens by default. You can change this behavior by assigning specific classes for different screen sizes if needed. (Understanding all this becomes easier once you go through the Bootstrap documentation.)

By utilizing Bootstrap, you can minimize the need for numerous media queries and focus on maintaining concise and clear CSS code.

Answer №2

If you need to specify the number of <li> elements in each column using bootstrap, it can be done easily as shown below:

http://www.bootply.com/bsVWymTjED

  <div class="col-md-6 col-lg-4">
    <ul>
      <li>Column 1</li>
      <li>Byebye</li>
      <li>Chuck</li>
      <li>Paul</li>
     </ul>
  </div>
  <div class="col-md-6 col-lg-4">
    <ul>
      <li>Column 2</li>
      <li>Byebye</li>
      <li>Chuck</li>
      <li>Paul</li>
     </ul>
  </div>
  <div class="col-md-6 col-lg-4">
  ...

If you want the <li> elements to transition from one column to the next as their number increases, that's a different scenario. It is definitely feasible and probably not too difficult, but I don't have the exact solution at hand...

EDIT: Disregard the previous note - the alternative option is also straightforward, refer to the updated second answer

Answer №3

To have your list items <li> flow seamlessly from one column to the next without specifying how many in each column, simply add the col-* classes directly to the <li> elements like this:

<ul>
  <li class="col-md-6 col-lg-4">Hello</li>
  <li class="col-md-6 col-lg-4">Goodbye</li>
  <li class="col-md-6 col-lg-4">Chuck</li>
  <li class="col-md-6 col-lg-4">Paul</li>
</ul>

http://www.bootply.com/rr2Qh5K9OF

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 encountered: The jQuery DevExtreme datagrid function $(...).dxDataGrid is throwing an Uncaught TypeError

I'm encountering an issue with the DevExtreme DataGrid while using ASP.NET MVC. Even after downloading NuGet packages, adding scripts to the head section, I am still facing this error: Uncaught TypeError: $(...).dxDataGrid is not a function. Do I ...

Deactivate the linear x axis labels in jQChart

I have a jQchart Linear chart that is displaying correctly and functioning properly. I am looking to remove or disable the X axis labels from the chart. ...

How can I adjust margins based on the positioning of surrounding elements?

Apologies if the title is not accurate, but I will try my best to explain clearly. If you have any ideas on how to improve it, please share, and I will make changes. Here is the issue I am facing: https://i.sstatic.net/kGMjw.png (I apologize for any lang ...

Ways to conceal text or a div element and substitute it with asterisks

I'm seeking assistance with a concept involving a simple toggle button that can hide an object and replace the empty area with ****. My initial idea was similar to a password form input where you can conceal the password by clicking an eye icon. Howe ...

Is it possible to create a Footer with a 100% width in CSS?

I have a simple HTML page with all elements contained within a mainWrapper div and secondWrapper div. Everything is currently sized to 960px, including the pageHeader, pageContent, and pageFooter. However, I need to keep everything at 960px except for th ...

How can I activate the mobile zoom feature on kinetic js?

I am currently working on a website using kinetic js + html5 canvas. Everything looks great on the web, but I am facing some issues with the display on mobile devices. Is there a solution to enable the mobile pan and zoom function in kinetic js? Thank you ...

Is it possible for Susy to output a pixel-based span?

As a newbie to Susy, I hope you don't mind if I ask a seemingly silly question... I'm trying to figure out how to calculate responsive padding within a Susy grid. The standard formula is: (target / context) x 100. Is there a way for Susy to pr ...

Safari isn't recognizing the :focus-within pseudo-class

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-w ...

Inspired by the organization and depth provided by nested lists

I am facing an issue with my ul list where adding a nested ul causes the li items above to move. Can anyone explain why this is happening and suggest a solution? Here is an example: http://jsfiddle.net/y5DtE/ HTML: <ul> <li> first ...

Issue with Bootstrap 5 navbar failing to collapse

Encountered an issue with the Bootstrap 5 navbar toggle feature - it's not toggling as expected. Interestingly, the exact code works perfectly fine with Bootstrap 4. Attempts to rectify the situation by removing the navbar-toggler class from the butt ...

What is the method for writing to an HTML file with the use of expressjs?

Alright, I have an interesting idea here. I am looking for a way to allow users to push a button and have a new p element permanently added to the HTML file. This means that even after reloading the page, everyone should be able to see this new element. An ...

Locate integer and add a decimal point followed by two zeros

I want to add .00 to a number if it is not already a decimal. However, the code I am using below seems to be converting the entire number to 0.00 instead of just appending .00 at the end. For instance, if the number is 12,200, it ends up as 0.00 after appl ...

What is the best way to align product cards side by side instead of stacking them on separate lines?

I am currently working on a mock-up website for a school project. For this project, I am developing a products page where I intend to showcase the items available for sale. Although I managed to successfully copy a template from w3schools, my challenge lie ...

Using a `right: 0` value will not be effective when using absolute positioning

Currently, I am in the process of developing a website utilizing Bootstrap 3.3.6. After going through a tutorial on how to create a responsive banner slider, I found this resource helpful: http://www.jqueryscript.net/slider/Full-Width-Responsive-Carousel- ...

Voice recognition feature in the latest version of Chrome on Android devices

When using a PC with Chrome 25 (non beta), I see a microphone icon that prompts for input when clicked. After speaking, my alert call is successfully executed. However, on a Galaxy Note smartphone with Chrome 25 and an Android 4.04 operating system, the sa ...

Highcharts plots only appear once the browser window is adjusted

Having some issues while testing out the Highcharts javascript charting library on a specific page. The problem I'm encountering is that none of the data appears until I adjust the browser's size slightly. Prior to resizing, the tooltip does dis ...

How can I customize the appearance of the arrow in a Material UI tooltip?

Looking to enhance the appearance of a Material UI tooltip arrow with custom styling, but struggling to set the border and background colors. Here's the current configuration in React: const useStylesBootstrap = makeStyles(theme => ({ arrow: { ...

Learn how to incorporate a vertical divider pipe into your website using Bootstrap

Hello, I'm having trouble adding a separator between nav-links that actually works. I tried adding the following code snippet: nav-link:after { content:"|"; } But unfortunately, it's either not working at all or appearing on the left side of th ...

Tips for submitting a form with multiple fields that share the same name attribute using ajax:

I have created an HTML form <html> <head></head> <form> <input type="text" name="question[]" /> <input type="text" name="question[]" /> <input type="file" name="image" /> <input type="submit ...

Accessing a precise div element in a webpage

Currently, I am utilizing Vue.js and Nuxt for my web development. One issue I am facing is related to anchors. Specifically, when I navigate to mysite.com/page1#section1, I want the page to scroll to section1. In my code, I have the following snippet: < ...