When working on a small screen, Bootstrap generates divs one by one

Hey there! I've been working with bootstrap CSS library version 5.1.3 and I'm trying to create a simple card layout where there's an image on the left side and text on the right side. Everything looks great on a large screen, but I'm struggling with aligning the image first and then the text below it on a smaller screen. Can anyone assist me in fixing this issue? If it's not possible with the current code, I'm open to suggestions on how to achieve this card design in a different way.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <title>Test</title>
        <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="54363b3b2021202635241451736c73786815">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">  
      </head>
<body>
    <div class="container py-4">
        <div class="bg-light d-flex"> 
            <div class="flex-shrink-0">
                <img src="https://i.ibb.co/kDv9tyc/img.jpg" height="350">
            </div>
              
            <div class="flex-grow-1 px-3">
                <h1>Title</h1>
                <h2>SubText</h2>
                <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Cupiditate in fugit aspernatur perferendis repudiandae accusantium illum autem hic numquam, non optio tempore, omnis, exercitationem quidem atque iusto mollitia deleniti. Eos minima numquam rem quis ullam quam repudiandae, voluptatibus temporibus dolorem quidem? In cumque exercitationem dicta fugit ipsum facilis quaerat consequuntur eligendi a culpa cum sed ab, magni earum voluptatem? Id et velit enim reiciendis doloribus veniam distinctio alias mollitia, repellendus quisquam ab eos incidunt corrupti earum quidem laudantium quae suscipit.</p>
            </div> 
       </div>
</body>
</html>

Output on large size screen:

https://i.sstatic.net/kX40B.png

Current output on small size screen:

https://i.sstatic.net/TTsdH.png

Expected output:

https://i.sstatic.net/1h8Iu.png

Answer №1

To implement a responsive layout using Bootstrap, utilize the flex grid system. By creating a row with two columns, you can ensure your design adapts to different screen sizes.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <title>Test</title>
        <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b0904041f181f190a1b2b5e455a4558">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">  
      </head>
<body>
    <div class="container py-4">
        <div class="bg-light row"> 
            <div class="col-md-3 p-3">
                <img src="https://i.ibb.co/kDv9tyc/img.jpg" class="my-image text-center img-fluid">
            </div>
              
            <div class="col-md-9 p-3">
                <h1>Title</h1>
                <h2>SubText</h2>
                <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Cupiditate in fugit aspernatur perferendis repudiandae accusantium illum autem hic numquam, non optio tempore, omnis, exercitationem quidem atque iusto mollitia deleniti. Eos minima numquam rem quis ullam quam repudiandae, voluptatibus temporibus dolorem quidem? In cumque exercitationem dicta fugit ipsum facilis quaerat consequuntur eligendi a culpa cum sed ab, magni earum voluptatem? Id et velit enim reiciendis doloribus veniam distinctio alias mollitia, repellendus quisquam ab eos incidunt corrupti earum quidem laudantium quae suscipit.</p>
            </div> 
       </div>
</body>
</html>

CSS To enhance the code structure, consider moving the height property from the HTML file to the CSS file. Utilize a media query to set the image height to 350px for screens wider than 768px. Additionally, ensure the image width is set to 100%.

Below is an example of the CSS code:

img.my-image{
      width:100%
  }
/* // Medium devices (tablets, 768px and up) */
  @media (min-width: 768px) { 
   img.my-image{
     height:350px;
   
    }
 }

For a live demonstration, you can view the code on CodePen:

https://codepen.io/shingai/pen/vYpbdKB

Answer №2

If you're utilizing bootstrap, it is recommended to make use of the Bootstrap grid system. This will transform your code into the following structure:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <title>Test</title>
        <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5d50504b4c4b4b05e4f7f0a110e110c">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">  
      </head>
<body>
    <div class="container">
        <div class="bg-light row"> 
            <div class="col-md-3">
                <img class="w-100" src="https://i.ibb.co/kDv9tyc/img.jpg">
            </div>
              
            <div class="col-md-9">
                <h1>Title</h1>
                <h2>SubText</h2>
                <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Cupiditate in fugit aspernatur perferendis repudiandae accusantium illum autem hic numquam, non optio tempore, omnis, exercitationem quidem atque iusto mollitia deleniti. Eos minima numquam rem quis ullam quam repudiandae, voluptatibus temporibus dolorem quidem? In cumque exercitationem dicta fugit ipsum facilis quaerat consequuntur eligendi a culpa cum sed ab, magni earum voluptatem? Id et velit enim reiciendis doloribus veniam distinctio alias mollitia, repellendus quisquam ab eos incidunt corrupti earum quidem laudantium quae suscipit.</p>
            </div> 
       </div>
</body>
</html>

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

What's the best way to update the menu element colors as I scroll down the page?

I am looking to update the color scheme of menu elements as the page scrolls using the provided template. I tried a few methods, but they lack dynamism. How can I make it more dynamic? $(document).ready(function(){ //scroll animation $('.navigati ...

Storing HTML form information into a CSV file using Python and Flask

I am struggling with a small .py program that renders 2 HTML pages. One of the pages contains a basic form asking for a name and comment. I am having difficulty figuring out how to take the data entered in the form and store it into a csv file. While I h ...

Creating a dynamic background color that pulses with animation

I recently created a script to animate the menu li element when hovering over the corresponding a element. Everything is functioning as expected, but now I'm looking to enhance the effect by having it continue as long as the mouse remains over the a e ...

The fixed table header is covering the navigation bar

Currently, I am building a website using Bootstrap and the DataTables library. The problem I am encountering is that the sticky table header is working fine, but it overlaps with the sticky nav bar. Is there a way to make the navbar "push down" the table h ...

Something seems to be preventing Div from appearing and there are no error messages appearing

I've been attempting to create a menu, but the toggle div for the menu isn't visible Following a tutorial where an individual sets up a menu, there is a div with a menu icon in the top left corner. Despite meticulously copying the code from the ...

Ensuring that all of the content is adaptable across various devices, from smartphones to high-definition 1080p screens

I am currently working on making my entire content responsive. While I have successfully made the background responsive using different @media resolutions, I am facing an issue with the image in the foreground that is not scaling accordingly. I tried putti ...

It is important for the CSS :active state to transition to an "inactive" state after a certain period of

For mobile, I utilized the :hover and :active pseudo-classes to create a transition effect when tapping on a tab that enlarges the elements for more information. However, I am seeking a way for the activated element to return to its original state after a ...

CSS filter for Internet Explorer

Can this CSS class be made to work in Internet Explorer 11? .inactive { filter: contrast(0.5) sepia(100%) hue-rotate(116deg) brightness(1.2) saturate(0.28); } I attempted to implement the SVG grayscale filter but it was unsuccessful, causing issu ...

The Angular Material Table experienced a collapse when trying to render over 20 columns simultaneously

Currently, I am experiencing an issue in Angular Version 5 where the Angular Material Table collapses when rendering more than 20 columns. Here is a snapshot of what my table looks like: https://i.stack.imgur.com/MXfvQ.png https://i.stack.imgur.com/XHWgq ...

Remove HTML element and launch in a separate browser tab while preserving styles

I am currently working on developing a single-page application with Polymer3 (Javascript ES6 with imports). One of the key functionalities of my application involves moving widgets to new browser windows, allowing users to spread them across multiple scree ...

Steps for eliminating a column from a table

By including the detailPanel option, a new column with an icon button will be automatically added. https://i.sstatic.net/RoNue.png Is there a way to eliminate this additional column that is generated? Appreciate your help! ...

Modifying the color of an empty string is not feasible in Javascript

Is it possible to change the color of FirstName only when there is text input? Currently, it turns green when there's text, but I want it to turn red when it's empty. How can this be achieved? $(document).on("click", '.btn-info.mailCo ...

Move the text above within the footer using materialize framework

I'm facing an issue with my materialize footer's text disappearing when I set the height to 35px. Whenever I decrease the size of the footer, the text goes off the screen. How can I adjust it so that it stays visible? <footer style="positio ...

Incorporating ASP includes within an HTML file

Is there a method to utilize ASP or ASP.NET for including HTML in another HTML file, similar to PHP includes with HTML and the AddType handler in .htaccess? I am exploring options for updating navigation on a Windows server that does not have PHP installe ...

Including CSS in an HTML file does not function properly

I've been attempting to import a CSS file into an HTML document, but for some reason it's not working. I have also tried linking the path directly, but that hasn't worked either. <!DOCTYPE html> <html lang="en"> <head> < ...

Transfer data from href link to specific detail page

I am currently working on a table that displays all the users. My goal is to turn the usernames into clickable links that direct you to their profile page. What steps should I take to convert the username into an href link and send it to the detail page? ...

Adjust the background color of a list item using Typescript

At the top of my page, there's a question followed by a list of answers and the option to add new ones. You can see an example in the image below. https://i.stack.imgur.com/NPVh7.jpg The format for each answer is "(username)'s response: at this ...

CSS PopUp positioning

Can anyone help me with applying the position top and bottom of header to the NewUser div? I've tried but it's not working. How can I add !important; to the CSS? Maybe something like $("header")!important} <div id="divNewUser" class="CreateUs ...

Using .get methods with jQuery's .on

I need to retrieve the tag name of the element that is clicked inside an li when the user interacts with it. The li element gets dynamically added to the HTML code. I have implemented the following code, but unfortunately, it does not seem to be functionin ...

Showcased a few items from the model.forms dataset

My table has three fields: id, name, status. When I use {{ form|bootstrap }}, all fields are displayed. The 'name' field is a drop-down in HTML. I only want to show entries where the status is equal to 1. How can I achieve this? ...