How can a Bootstrap card be designed with the image positioned at the top left, but adjusting based on different

When it comes to Bootstrap 4 cards with images, most have the image positioned at the top using .card-img-top. But what if you want the image to be at the left or at the top depending on the width?

Currently, I have two types of cards - one with the image at the top and the other with the image on the left using additional columns to create a horizontal layout.

https://i.sstatic.net/6mafd.png

Is it possible to have a single card that dynamically positions the image based on the column width?

For example, at smaller screen sizes (xs, sm), the columns are stacked vertically and the image should be on top. But at larger screen sizes (md, lg), the column widths vary and the image should be on the left.

Is there a way to achieve this using a single card code, or would it require more complex logic?

Here is the code for vertical cards:


<div class="d-flex align-items-stretch col-xs-12 col-sm-4 col-md-6 col-lg-3 p-2">
    <div class="card rounded-0 w-100 bg-white shadow-sm">
        <a href="http://www.thedrum.com/news/2016/11/29/something-going-have-change-quickly-fts-elli-papadaki-programmatic-transparency">
            <img src="http://www.example.com/image/http://www.example.com/wp-content/uploads/article_drum.jpeg" class="card-img-top img-fluid rounded-0">
        </a>
        <div class="card-body pb-2">
            <h6 class="card-title"><a href="http://www.thedrum.com/news/2016/11/29/something-going-have-change-quickly-fts-elli-papadaki-programmatic-transparency" class="text-dark">‘Something is going to have to change quickly,’ FT’s Elli Papadaki on programmatic transparency</a></h6>
        </div>
        <div class="card-footer bg-white text-muted small pt-0 border-top-0 px-3">
            <img src="http://www.example.com/wp-content/uploads/fav_drum.png" width="17" class="rounded-circle">
            <span class="ml-1"><a href="/source/the-drum/" class="text-muted">The Drum</a></span>
        </div>
    </div>
</div>

And here is the code for horizontal cards:


<div class="row">
    <div class="d-flex align-items-stretch col-xs-12 col-sm-4 col-md-6 col-lg-6 p-2">
        <div class="card rounded-0 bg-white shadow-sm">
            <div class="row">
                <div class="col-md-4">
                    <a href="http://www.thedrum.com/news/2016/11/29/something-going-have-change-quickly-fts-elli-papadaki-programmatic-transparency">
                        <img src="http://www.example.com/image/http://www.example.com/wp-content/uploads/article_drum.jpeg" class="img-fluid rounded-0" style="object-fit:cover">
                    </a>
                </div>
                <div class="col-md-8 pl-0 py-2">
                    <div class="card-block">
                        <h6 class="card-title"><a href="http://www.thedrum.com/news/2016/11/29/something-going-have-change-quickly-fts-elli-papadaki-programmatic-transparency" class="text-dark">‘Something is going to have to change quickly,’ FT’s Elli Papadaki on programmatic transparency</a></h6>
                        <img src="http://www.example.com/wp-content/uploads/fav_drum.png" width="17" class="rounded-circle">
                        <span class="small pl-0 ml-0"><a href="/source/the-drum/" class="text-muted">The Drum</a></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

(I am working on making the images fill the available space with a "cover" style. The images on vertical cards should maintain a landscape orientation while on horizontal cards they should be square. Note: there is currently a spacing issue with my horizontal-card code when the right text is longer than the left image.)

Answer №1

If I correctly grasped your inquiry (quite lengthy to read), you have the option to utilize the flex-direction attribute.

I devised a straightforward illustration for you. Initially, the columns are arranged side by side (horizontally) with the content within each.

Then, upon reaching a specified breakpoint (600px), the columns transition into a vertical layout.

You can also experiment with Bootstrap classes to achieve a similar outcome.

Check out the code snippet below or view it on jsFiddle

.col-4 {
  flex-direction: row;
  display: flex;
}

@media only screen and (max-width: 600px) {
  .col-4 {
    flex-direction: column;
  }
}
<div class="row">
  <div class="col-4">
    <img src="https://via.placeholder.com/150">
    <div class="text">
      some text
    </div>
  </div>
  <div class="col-4">
    <img src="https://via.placeholder.com/150">
    <div class="text">
      some text
    </div>
  </div>
  <div class="col-4">
    <img src="https://via.placeholder.com/150">
    <div class="text">
      some text
    </div>
  </div>
</div>

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

Remove the class upon clicking

I recently created a toggle-menu for my website that includes some cool effects on the hamburger menu icon. The issue I am facing is that I added a JavaScript function to add a "close" class when clicking on the menu icon, transforming it into an "X". Whil ...

Safari shows a contrasting outcome

Having an issue with compatibility in the safari browser You can check out the site here: The problem lies with the text inside the image, I want it to display properly like it does in Chrome. Here is the desired result for Safari as well. Below is the ...

What are the steps to change table characteristics?

I currently have this segment of HTML code: <table cellspacing="0" cellpadding="0" width="100%"> After validating my HTML document, I discovered that these attributes are outdated. However, removing them would affect the alignment of my website. I ...

Necessary input in Html.TextBoxFor

Is there a way to format the value of a TextBox using the following structure: <td>@Html.TextBoxFor(m =>(m.Code), new { @required = "required"})</td> Initially, this format works as intended. However, when a default value is set for the T ...

Identifying elements using xpath - Streamlined Xpath Techniques

I successfully found the element using the xpath copied directly from the code. Can someone assist me in creating a simpler xpath for the following code snippet, which is fully functional! WebElement oCheckbox = myDriver.findElement(By.xpath(".//*[@id=&ap ...

HTML - maintain centered text positioning while implementing padding on the left side

Here are the HTML and CSS code snippets I am working with: #page { background-color: #000; color: #fff; height: 360px; width: 360px; min-height: 100%; } #viewport { font-size: 20pt; text-align: center; } .taskTitle { height: 13%; fon ...

Do not apply hover effect to a particular child div when the hover effect is already applied to the parent

I am encountering a problem with the hover effect. I have 3 child div's structured as follows: <div class="parent"> <div class="child1">A</div> <div class="child2">B</div> <div class="child3">C</div ...

Hover-activated dropdown menu

After reading numerous tutorials and spending hours trying to create a submenu on hover, I still can't seem to get it to work. My goal is to display "Zimmer", "Reservierung", and "Preise" in a vertical menu when hovering over "Hotel," and so on. Here ...

Stop the execution of javascript code based on the screen width

On my website, I have two menus located in different sections of the page. One menu is shown when the screen width is greater than 555px, while the other menu appears when the screen width is less than or equal to 555px. Although the menus are essentially ...

Creating a Star Rating system with jQuery using a dropdown menu for multiple selections

Recently, I came across a simple jQuery Star rating system that I want to implement on my website. I have been following the code from http://jsfiddle.net/, and it works perfectly for one star rating system. However, I have multiple star rating systems on ...

issue with jquery fade toggle not working properly on Bootstrap website

My website is built using Bootstrap and jQuery. I am facing an issue where the fadeToggle() function on a div element is not displaying properly - it remains hidden all the time. Can anyone provide insight into what might be causing this problem, or sugg ...

What is the best way to apply an active class to the parent li element when selecting an item within it? I want to achieve this effect

$(function() { var pgurl = window.location.href.substr(window.location.href .lastIndexOf("/") + 1); $("#nav ul li a").each(function() { if ($(this).attr("href") == pgurl || $(this).attr("href") == '') $(thi ...

What is the best way to adjust the height of a div based on its child content

I've been experimenting with various methods to extend the white background around the title to cover the rest of the content. I've tried using overflow, clear, min-height, max-height, and even the '*' selector but nothing seems to work ...

the battle between width and max-width for creating responsive web layouts

Many tutorials suggest using max-width:100% for images in responsive web design to ensure flexibility. However, why is it also possible to achieve the same result by using width:100%? ...

Looking for a solution to toggle the visibility of a div based on whether search results are found or not using JavaScript

Running this code <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Searc ...

Generating pop-up upon loading with CSS3

I have searched through numerous threads and forums in the hopes of finding a solution, but I haven't been successful. My issue lies with triggering a popup on my website. I followed online tutorials to create a popup window, which I was able to do su ...

When I hover over my images, they begin to flash before my eyes

My layout is set up so that when I hover over the printer image, it should switch to another image. However, instead of smoothly transitioning, the image starts to flash. This issue occurs in all browsers, indicating that I have made a mistake somewhere. ...

Can you suggest an efficient method for routing with EJS templates on an Express server without constantly repeating code?

Assuming my app consists of two views: index.ejs and profile/index.ejs, I aim to set up routing using express code as shown below. /* GET home page. */ router.get('/', function (req, res, next) { res.render('index'); }); /* GET pr ...

In IE8, a border is applied to the max-width property when the width is set to

I'm attempting to design a box that adjusts in size according to the parent container, utilizing a mix of max-width and width:100%. With this setup, the box's width is determined by the max-width property, ensuring it shrinks within its boundari ...

Utilize Java to launch the HTML file in a web browser

I have a specialized need for my HTML files to be displayed on a platform that is browser-free. The solution that immediately comes to mind for me in this scenario is utilizing applet. Therefore, I am interested in having my HTML file (including CSS and JS ...