Is there a way to automatically float right on smaller screens and float left on larger screens?

Is there a way to create an effect where the image is on the left side of the text, but when the screen size is reduced, the text goes on top and the image is below?

Currently, I have the opposite layout - the image appears above the text when the screen is smaller. How can I reverse this?

Below is the code I am using (with separate divs for the image and description text):

<div class="container productwrap shadow-lg mt-3">
    <div class="row">
        <div class="col-md-4 p-2 m-3 border float-left">
            <img src="../Pictures/snacks3.jpeg" alt="snacks2">
        </div>
        <div class="col-md-7 p-2 my-3 border rounded float-right">
            <p class="newarrival text-center pt-3 font-weight-bold">Trendy Snacks</p>
            <h2 style="display: inline-block" class="p-3 ml-2 border rounded">Brown Sugar Yogourt</h2>
            <span class="badge badge-success align-top m-1">NEW!</span>
            <p class="lead font-weight-bold text-warning m-4">CAD $9,99</p>
            <p class="m-4"><strong>Unit: </strong>2 x 100g</p>
            <p class="m-4"><strong>Availability: </strong>In Stock</p>
            <label for="qty" class="ml-4"><strong>Quantity:</strong>
                <input type="text" id="qty" name="qty" class="small w-25 text-right" value="1"><button class="btn btn-outline-success btn-sm" href="#" role="submit">Add to Cart</button></label>
            <p>
                <button class="btn btn-info btn-sm m-2 mr-3 rounded-pill" data-toggle="collapse" data-target="#moreinfo">More info</button>
                <div id="moreinfo" class="collapse text-wrap">
                This delicacy instills the careful mix of brown sugar and yogourt. Its rich nutriments will revitalize your spirit and set you ready for your day.
                </div>
            </p>
        </div>
    </div>
</div>

I have attempted to use float-right/left properties, but it does not seem to work as expected.

Answer №1

I am looking to create an effect where the image is positioned on the right side of text, but switches to being below the text on smaller screens. Currently, the image appears on the left side of the text...

It seems like you currently have the latter situation. On larger screens, the picture is on the left side of the text...

If you swap the order of the image structure and text structure, you can achieve both effects:

<div class="container">
    <div class="row">
        <div class="col-md-7">
            ...
        </div>
        <div class="col-md-4">
            <img />
        </div>
    </div>
</div>

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

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


Check out the demo: https://jsfiddle.net/davidliang2008/5m2z4s90/4/



Updates:

You can utilize the order utilities class to achieve this desired layout:

<div class="container">
    <div class="row">
        <div class="col-md-4 order-2 order-md-1">
            <img />
        </div>
        <div class="col-md-7 order-1 order-md-2">
            ...
        </div>
    </div>
</div>

On small screens, the image will be the second item and appear at the bottom. On medium breakpoints and above, it will switch positions to appear on the left side of the row.

Check out the updated demo here: https://jsfiddle.net/davidliang2008/5m2z4s90/9/

Answer №2

Utilize CSS Grid to customize alignment according to your preferences by default. As the viewport decreases, apply a media query to resize the div and adjust the layout of the grid accordingly.

Answer №3

One effective technique for creating a more advanced responsive design is to utilize 'Media Queries' within CSS.

Firstly, arrange the elements in their desired default layout. Then, establish a threshold for resizing (e.g. 'width less than 600px'). Once this parameter is set, you can initiate a media query as follows:

@media (max-width: 600px) {
  *selector* {
    *changes you wish to implement when screen size is smaller than 600px*;
  }
}

By employing this method, any modifications made to the CSS rules will automatically take effect whenever the screen width falls below 600px, and revert back to the original values once the screen size increases.

Although Media Queries can become more intricate and elaborate than illustrated here, they prove to be highly beneficial once mastered. I recommend delving further into their capabilities.

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

Attempting to insert a line break tag within the text using React

Having trouble inserting line breaks in text using React. Can anyone guide me on how to achieve this? I've attempted adding the br tag, but it is displaying as in the browser. Here's a snippet of my code. Appreciate any help or advice. let sp ...

What is the best way to prevent scrolling in one column while allowing another column to scroll normally?

On my website, I have two divs positioned side by side. I am looking to keep one div fixed without scrolling while allowing the second div to scroll normally. How can I achieve this? For a clearer visual, refer to this image: Sample Image ...

identifying the element targeted on dynamically created links with the help of jquery

I have created dynamic links where clicking on a country link displays the cities of that particular country. I am trying to retrieve the event.target.id of the dynamically generated city link when it is clicked. $(".countryName").children().on('cl ...

What is the best way to position an image in the center over a video using HTML and CSS?

I am currently developing a website with a background video and I want to overlay a company logo on top of it while keeping it centered. My coding language of choice for this project is using HTML and CSS. Here's the HTML code snippet I have for this ...

Dynamic status bar for WordPress using Advanced Custom Fields

I am currently working on developing an order tracking system using Wordpress, ACF, and a form plugin that is yet to be determined for the user interface. While the back-end functionality is working smoothly, my focus now shifts towards creating a visual ...

What methods can I use to display or conceal certain content based on the user's location?

I'm looking to display specific content exclusively to local users. While there are APIs available for this purpose, I'm not sure how to implement them. I'm interested in creating a feature similar to Google Ads, where ads are tailored base ...

Using an overlay with figure-img in Bootstrap 4 is a visually appealing design

I need assistance with adding an overlay to only the bootstrap 4 figure-img. In order to achieve this, I inserted a div with the class overlay beneath the figure-img. Below is the code snippet: <div class="col-md-4"> <figure class="service tex ...

Turn off Chrome 69's autofill functionality

I've recently encountered an issue with Chrome's password autofill feature that has been troubling me for a few days now. This problem began when I was using Chrome version 69. After much trial and error, I found a solution by removing the id an ...

The website functions perfectly on a local server, but once it's uploaded, the images fail

My website is functioning properly on my computer, however, after uploading it to the server, I noticed that some of the links are not appearing. Upon inspecting these links through developer tools, it shows an image size of 'Natural 1 X 1' despi ...

Retrieving data from an API after the onClick event is activated

I'm facing a bit of a challenge with an HTML & JavaScript issue, which I believe should be straightforward to resolve. Since frontend development is not really my forte, I am hoping to find some assistance here. Task: A friend who owns a website buil ...

Issue with Bootstrap 4 list items not displaying in block format

Based on the information from , using li tags as block elements should result in a vertical menu. However, with Bootstrap 4, when I use li tags, the menu stays horizontal on toggle for small devices. Is this the expected behavior? <ul class="nav navb ...

Is the scrapy user agent programmed to overlook custom data attributes?

https://i.sstatic.net/y82uq.png According to user agents, custom data-attributes are likely to be ignored. Referencing an image from w3schools, I am wondering if scrapy is also disregarding these tags since I am receiving an empty list, possibly due to th ...

instructions on how to eliminate slideUp using jquery

I am trying to eliminate the slide-up function from my code $( document ).ready(function() { $("#tabBar ul.buttons > li > a").on("click", function(e){ //if submenu is hidden, does not have active class if(!$(this).hasClass("activ ...

What happens when HTML5 Video quality starts to decline?

Exploring the capabilities of the HTML5 video tag has left me pondering the best approach for graceful degradation in cases where a codec is not supported. Handling older browsers (or IE) that lack support for the video tag altogether is quite straightfor ...

I tried to upload a unique theme on my WordPress website, but unfortunately, the design is not displaying correctly. I am having

After uploading a custom theme on WordPress as a parent theme, I encountered an issue where only simple text appeared on the screen without any styling, images, or sliders. Being new to PHP, I struggled to understand how to enqueue the styles in the functi ...

Tips for inserting HTML content into a Flex TextArea

There was once a panel where I had added a fadeIn show effect. Initially, the effect only applied to the panel itself, with text appearing without the desired fadeIn effect. Fortunately, a colleague suggested embedding the fonts, which miraculously solved ...

Unusual phenomenon of child unordered list interacting with parent list item's animation

http://jsfiddle.net/RedKnight91/Z6Ueu/4/ Hey there! Take a look at the last menu (at the bottom). When you hover over one of the LIs with the "+" symbol that have children UL, which is a submenu, the slideToggle displays the child UL, but after the animat ...

Achieving a transparent background in WebGLRender: A guide

I've been experimenting with placing objects in front of CSS3DObjects using the THREE.NoBlending hack. However, in the latest revisions (tested on r65 and r66), I only see the black plane without the CSS3DObject. Here is a small example I created: in ...

Creating a personalized contact form with a mailto link that also includes the ability to automatically copy information into the email

I'm in the process of developing a basic contact form that includes input fields for name and subject, as well as a send button (which is an <a> element inside a <div>. I am utilizing an <a> tag because I intend to use mailto functio ...

How can you determine whether the CSS of a <div> is defined in a class or an id using JavaScript/jQuery?

Hey there, I'm currently working on dynamically changing the CSS of a website. Let's say we have a div like this: <div id="fooid" class="fooclass" ></div> The div has a class "fooclass" and an ID "fooid", and we're getting its ...