Is there a way to modify or alter the layout specifically for mobile devices?

Currently, my desktop version displays the image first followed by content (h1, p, button), and then repeats in that order. However, I would like to restructure the HTML for the mobile version to make it more user-friendly. How can I achieve this without duplicating code for desktop and mobile? Can this be done using media queries or jQuery?

Here is a link to my code: http://jsfiddle.net/0ucLzbkq/2/

    <section class="row-wrap">
        <div class="row-inner">
            <img class="poster" src="http://dummyimage.com/370x370/000/fff"> 

            <div class="poster-content">
                <h1>Heading</h1>
                <p>this is a paragraph</p>
                <a class="btn-wrap" target="_blank" href="#">
                    <div class="bt n">button</div>
                </a>
            </div>
        </div>
    </section>



    <section class="row-wrap">
        <div class="row-inner">
             <div class="poster-content">
                <h1>Heading</h1>
                <p>this is a paragraph</p>
                <a class="btn-wrap" target="_blank" href="#">
                    <div class="btn">button</div>
                </a>
            </div>
            <img class="poster" src="http://dummyimage.com/370x370/000/fff"> 


        </div>
    </section>

Answer №1

To achieve this, consider including the content twice and using the display: none; property to hide the unwanted content. While not the most elegant solution, it keeps file size overhead to a minimum and can be controlled without relying on JavaScript.

If you prefer to avoid duplicating code, you can explore manipulation techniques in jQuery:

https://jquery.com/

However, I recommend sticking with the simple display: none; method as shown below:

<div class="resizable-element"> (insert content here) </div>

In your CSS:

.resizable-element {display:none;}

Then, in responsive.css:

@media all and (max-width: 860px){
.responsive-image{display:block;}
}

This code will hide the resizable element but display it when the screen width is less than 860px.

Should you choose to use jQuery, refer to the manipulation API or visit:

Updated example:

HTML structure (place the mobile image DIV where you want it displayed):

<div class="desktop-view"> Content for desktop view </div>
<div class="mobile-view"> Content for mobile view </div>

Main.css:

.desktop-view {}
.mobile-view {display:none;}

Responsive.css:

@media (max-width: 860px) {
    .desktop-view {display:none;}
    .mobile-view {display:block;}
}

Utilize the same content for both div elements.

Answer №2

From my perspective, utilizing a media query is the optimal approach. It involves adjusting properties of multiple elements within your layout to achieve the desired arrangement.

Media queries excel in handling such tasks. Alternatively, experimenting with Bootstrap and its col--md-1 ... 12 classes could also be beneficial. These classes automatically adjust based on the screen size.

Answer №3

To implement this functionality using jQuery:

if ($(window).width() <= 600) {
    var poster = $(".poster").detach();
    $(".row-inner").append(poster);
}

The value of 600px was chosen arbitrarily, feel free to use any other value you prefer.

If you wish to replicate the media query feature, you can include a resize event in your jQuery code:

$(window).resize(function() {
    if ($(window).width() <= 600) {
        var poster = $(".poster").detach();
        $(".row-inner").append(poster);
    }
});

Keep in mind that if you only need this behavior for mobile devices, it may not be necessary.

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

Exploring the power of CSS grid in React/Gatsby for creating dynamic layouts with varying numbers of columns in each

I need help creating a dynamic grid layout where the number of columns alternates between rows. For example, I want the first row to have one column, the second row to have two columns, and so on. Right now, my grid only has two columns per row. Does anyon ...

Create a button that matches the dimensions of the background image

I am working with a form that includes a submit button featuring a background image. <button type="button" class="button" alt="Send" onclick="validate_form_newsletter_wide( form )"></button> Everything works well when I define the dimensions ...

invoking a JavaScript function from an HTML file

I want to create a JavaScript server on Raspbian that not only serves .html content to the browser but also allows for user input through events like button clicks. Both my .html and .js files are located in the same directory, and I have used absolute pat ...

Ensuring React's setupProxy functions properly in conjunction with express.js

I've encountered an issue while trying to pass images from express to react. My express.static is correctly set up, so when I visit localhost:5000/public/images/me.jpeg, the image loads in my browser. However, when I attempt to use <img src="/publi ...

Steps for creating a dynamic progress bar with inverted text style using only CSS

I am working on creating a dynamic progress bar with inverted text color using only CSS. To see examples of what I am trying to achieve, you can refer to the following links. Inverted Colors CSS progress bar CSS Progress Bars https://i.sstatic.net/VSs5 ...

Utilizing jQuery for cloning elements

I need to add functionality for adding and deleting rows in multiple tables on my website. Currently, I am doing this by directly inserting HTML code using jQuery, but it has become inefficient due to the number of tables I have. I am considering creating ...

Revamped styling for responsive web design

I am relatively new to experimenting with Responsive Web Design for my website. I am struggling to align the class="label" correctly after checking it on various devices. Initially, I attempted this: Initially, everything seemed fine. However, upon viewin ...

Introducing a fresh new feature incorporating various div elements through the power of JQuery

Why am I unable to retrieve the text of the newly added child divs? I am using JQuery to dynamically add these elements. Here is my sample fiddle: JSFIDDLE Could someone please guide me on what mistake I made? Javascript Code: var counter = 0; $("butto ...

Is there a way to identify if you are at the bottom row of a column defined by CSS styling?

I have implemented the new CSS-style column to divide a single unordered list into multiple columns. Now, I am trying to figure out how to target the last element in each column using JavaScript or CSS. Here is my HTML: <ul> <li /> <li ...

How can I utilize JQuery to dynamically refresh a dropdown menu?

My dropdown list is initially empty: <div> <label>Boarding Point </label> <select title="Select pickup city" id="boardingDropdown"> </select> </div> I am trying to popula ...

Is it possible to use personalized fonts alongside Google web fonts?

I'm looking to incorporate my customized font into a website using Google's font loader. Any recommendations on how to achieve this? ...

Does the Sass default compiler have built-in autoprefixing capabilities?

Suppose I have the following sample code: :fullscreen a { display: flex; } Is it possible for the default sass compiler to transform it into this: :-webkit-full-screen a { display: -webkit-box; display: flex; } :-moz-full-screen a { display: fle ...

Exploring the ancestors of an element

JS <script> $('.btn').click(function(e){ target = e.target; parent = target.parentNode.parentNode; console.log(parent); }); </script> HTML <div class="card" sty ...

Retrieving data using a class in an AJAX form submission

I am attempting to use AJAX to submit an HTML form. Here is my HTML: <form class="lyrics"> <input type="text" value="" id="song" name="song"> <button type="submit" class="btn btn-info lirik">lyrics</button> </form> ...

A chart using JavaScript that displays text values instead of a traditional website

I am a student with no background in programming, however, for my project I need to create a chart based on values from a txt file to display sensor data. I came across a chart that retrieves its values from a website, but I would like to modify it so it c ...

Personalized Pinterest button to link to a custom URL (Text Link, Image, or Both)

I've been searching for a solution without success. I'm looking to customize the image for my Pinterest (Pin It) button and pin a specific image by URL, not just the current page. Here is the custom link I created: <a href="http://pinterest. ...

How can I address multiple buttons with various events using jQuery?

I am new to learning jQuery and I'm currently working on some exercises. However, I've run into an issue with two buttons in the DOM that are supposed to perform different actions. I can't seem to figure out how to assign different functions ...

Injecting JavaScript object values into dynamically generated modal

I have a JavaScript object that contains various training courses. Each category includes multiple training courses, and each course is associated with a specific category. Currently, I am able to display the title and description of each category in a mo ...

jQuery element with listener not triggering as expected

I'm facing some confusion while working on this issue. I am attempting to create a dynamic form where users can add descriptions, checkboxes, and number inputs as they please. Currently, I have developed a simple dynamic form using jQuery, which allow ...

Changing the identification of elements

I noticed that when I open object X and Y, they have the same fields, tabs, and checkboxes. However, I am confused as to why the IDs of the checkboxes and other elements are different. Can you explain what gwt-uid(number) means? Please take a look at the a ...