Demystifying Responsive Design: How Bootstrap Uses col-lg-*, col-md-*, col-sm-*

2024-07-27

  • CSS (Cascading Style Sheets) is a language used to style web pages, controlling the visual appearance of elements like fonts, colors, backgrounds, and layout.
  • Responsive design is a web design approach that ensures a website looks good and functions properly across various devices, from large desktops to tablets and smartphones. Bootstrap provides tools and classes to achieve responsiveness through its grid system.

Bootstrap Grid System Classes

  • Bootstrap's grid system is built on a 12-column layout. You divide the available space into these columns using specific CSS classes.
  • The classes col-lg-*, col-md-*, and col-sm-* are part of this grid system and control the width of columns on different screen sizes:
    • col-lg-*: Defines column width on large screens (generally desktops with a minimum width of 1200 pixels).

How They Work

  1. Basic Structure: You wrap content within a row element (.row) and then use col-* classes on individual elements within the row to define their width on different screen sizes.
  2. Responsive Breakpoints: Bootstrap uses media queries, which are CSS rules that apply styles based on the screen size. These media queries define breakpoints (specific screen widths) where the column behavior changes.
    • For example, a class like .col-lg-4 might make the element span 4 out of 12 columns on large screens, but it could become full width (12 columns) on smaller screens like tablets or phones if a corresponding media query is present.

Example

<div class="row">
  <div class="col-lg-4 col-md-6 col-sm-12">Content 1</div>
  <div class="col-lg-4 col-md-6 col-sm-12">Content 2</div>
</div>
  • In this example:
    • On large screens, each .col-lg-4 element will take up 4 columns, resulting in two columns side-by-side.
    • On medium screens, each .col-md-6 element will take up 6 columns, meaning they'll still be side-by-side but might occupy slightly less width compared to large screens.
    • On small screens, both elements will become full width (.col-sm-12) to ensure proper stacking and readability on smaller devices.

Bootstrap Versions (3 vs. 4/5)

  • The specific media query breakpoint values might differ slightly between Bootstrap 3 and Bootstrap 4/5. However, the core concept of using responsive classes to control column behavior on different screen sizes remains the same.

Additional Notes

  • There might be additional grid classes like col-xs-* for extra-small screens in older Bootstrap versions, but they're generally not used in modern development due to the increased prevalence of larger mobile devices.
  • Bootstrap offers more advanced grid options like offsets and nesting for complex layouts. Refer to the official Bootstrap documentation for comprehensive details.



This example creates two columns that appear side-by-side on larger screens (desktops) but stack on top of each other on smaller screens (tablets and phones).

<div class="container">  <div class="row">
    <div class="col-lg-6 col-md-6 col-sm-12">  <h2>Large Screen: 50% Width</h2>
      <p>This content appears as 50% width on large screens (desktops) and stacks on top of the other content on smaller screens.</p>
    </div>
    <div class="col-lg-6 col-md-6 col-sm-12">  <h2>Large Screen: 50% Width</h2>
      <p>This content also appears as 50% width on large screens and stacks on top of the first content on smaller screens.</p>
    </div>
  </div>
</div>

Multiple Columns with Different Sizes

This example creates three columns:

  • The first column takes up 33% width on all screen sizes.
  • The second column takes up 40% width on large and medium screens, but becomes full width (100%) on small screens.
<div class="container">
  <div class="row">
    <div class="col-lg-3 col-md-3 col-sm-12">  <h2>33% Width</h2>
      <p>This content remains 33% width across all screen sizes.</p>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-12">  <h2>Large/Medium: 40%, Small: 100%</h2>
      <p>This content adjusts its width based on screen size.</p>
    </div>
    <div class="col-lg-5 col-md-5 col-sm-12">  <h2>Large/Medium: 27%, Small: 100%</h2>
      <p>This content also adjusts its width for responsiveness.</p>
    </div>
  </div>
</div>

Remember:

  • Include Bootstrap's CSS files in your HTML for these classes to work.
  • Adjust the content and styles as needed for your specific project.



Bootstrap offers built-in flexbox utilities that allow you to control how child elements are laid out within a container. Here's an example:

<div class="container">
  <div class="row d-flex justify-content-between">
    <div class="col flex-grow-1">Content 1</div>
    <div class="col flex-grow-1">Content 2</div>
  </div>
</div>
  • In this example:
    • The .row class uses the d-flex utility to enable flexbox layout.
    • The justify-content-between utility aligns the child elements horizontally with space between them.
    • Both .col elements use flex-grow-1 to make them expand equally within the available space on all screen sizes.

Grid System Utilities (Bootstrap 5):

Bootstrap 5 introduces the .row-cols-* classes that allow you to define the number of columns directly on the row element. Here's an example:

<div class="container">
  <div class="row row-cols-md-2">
    <div>Content 1</div>
    <div>Content 2</div>
  </div>
</div>
  • In this example:
    • The .row class uses row-cols-md-2 to specify two columns on medium screens and larger.
    • The child elements (<div>) don't require any specific classes, as the number of columns is defined on the row.

Custom CSS Media Queries:

You can write your own CSS media queries to target specific screen sizes and define custom layout behavior for your elements. This provides more granular control but requires a deeper understanding of CSS media queries.

Choosing the Right Method:

  • The built-in Bootstrap grid system (col-lg-*, etc.) is generally the easiest and most recommended approach for most responsive layouts.
  • Flexbox offers more flexibility for complex layouts and can be combined with the grid system.
  • Grid System Utilities (Bootstrap 5) offer a simpler way to define columns but might not be as widely supported in older Bootstrap versions.
  • Custom CSS media queries provide the most control but require more development effort.

Additional Considerations:

  • Evaluate the complexity of your layout and choose the method that provides the right balance of flexibility and maintainability.
  • Consider using a combination of these methods for different parts of your layout if needed.
  • Always refer to the Bootstrap documentation for the latest information and available classes.

css twitter-bootstrap twitter-bootstrap-3



Example Codes for Customizing Numbering in HTML Ordered Lists

In HTML, ordered lists are created using the <ol> tag.Each item within the list is defined using the <li> tag.By default...


Understanding HTML, CSS, and XHTML for 100% Min-Height Layouts

HTML (HyperText Markup Language) is the building block of web pages. It defines the structure and content of a webpage using elements like headings...


Tables for Data, DIVs for Design: The Right Tools for the Job in HTML and CSS

Tables (HTML): These are meant for presenting data in a tabular format, like rows and columns. They have elements like <tr> (table row), <td> (table cell), etc...


Optimize Your Webpages: Tools for Unused Resources

Browser Developer Tools: Most modern browsers like Chrome and Firefox have built-in developer tools. These tools allow you to inspect the website's code and identify potential issues...


Conquering Div Alignment: Your Guide to Horizontal Placement in CSS

Two or more divs side-by-side: This is the most common scenario. You want your divs to display horizontally next to each other...



css twitter bootstrap 3

Fixing Width Collapse in Percentage-Width Child Elements with Absolutely Positioned Parents in Internet Explorer 7

In IE7, when you set a child element's width as a percentage (%) within an absolutely positioned parent that doesn't have an explicitly defined width


Unveiling Website Fonts: Techniques for Developers and Designers

The most reliable method is using your browser's developer tools. Here's a general process (specific keys might differ slightly):


Interactive Backgrounds with JavaScript: A Guide to Changing Colors on the Fly

Provides the structure and content of a web page.You create elements like <div>, <p>, etc. , to define different sections of your page


Cross-Browser Rounded Corners Made Easy: Mastering the border-radius Property in CSS

In CSS (Cascading Style Sheets), the border-radius property allows you to add a curved effect to the corners of an element's outer border


Enhancing Textarea Usability: The Art of Auto-sizing

We'll create a container element, typically a <div>, to hold the actual <textarea> element and another hidden <div>. This hidden element will be used to mirror the content of the textarea