Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself.

I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, I want each title and input box to be displayed on the same row.

Although I have looked into media queries, I am still struggling to fully grasp how to implement them effectively.

<html>
    <head>
        <style>

        </style>
    </head>
    <body>
        <form>

            <center>
            <div class="left">
                First name:
            </div>
            <div class="right">
                <input type="text" name="firstname"/>
            </div>
            <div class="left">
                Last name:
            </div>
             ... 
            <div class="right">
                <input type="submit" name="submit" value="submit"/>
            </div>

            </center>
        </form>
    </body>
</html>

Answer №1

If you're looking to make your grid responsive on window resize, there are a few different options available. You could leverage Bootstrap's capabilities for easily adjusting grid display, or explore using media queries in combination with Flexbox or Grid layouts. Another possibility is incorporating JQuery and the window resize function into your solution. Personally, I prefer using Flexbox and setting the flex-direction property when the window size resembles that of a smartphone or tablet. Writing a media query can be as simple as typing something like "@media screen and (max-width: 640px)" and defining your styles within the curly brackets.

Answer №2

This is an example of a code snippet.

<!DOCTYPE html>
<html>
<head>
    <style>
       /* CSS Styles go here */
    </style>
</head>

<body>
    <form class="my-form">
        /* Form inputs and labels go here */
    </form>
</body>

</html>

Answer №3

To ensure your website is responsive on different devices, you need to implement Media Queries in your CSS code. Media queries allow you to write specific CSS rules for various screen widths. You can learn more about Media Queries from this resource: https://www.w3schools.com/css/css3_mediaqueries_ex.asp

Another useful article on Media Queries can be found here: https://css-tricks.com/snippets/css/media-queries-for-standard-devices/

If you prefer using jQuery, you can achieve the same effect with matchmedia. Here's an example on JSbin to demonstrate how it works: https://jsbin.com/kutacuzece/edit

(function($) {

    /*
        * Convert the code into a function
        * This ensures changes are applied on document ready and when browser is resized.
        */

        function mediaSize() { 
            /* Define the matchMedia */
            if (window.matchMedia('(min-width: 768px)').matches) {
                /* Apply changes for min-width condition */
                $('body').css('background', '#222');
                $('strong').css('color', 'tomato');

            } else {
                /* Reset CSS changes - A better method may be needed */
                $('body, strong').removeAttr('style');
            }
        };

        /* Call the function */
        mediaSize();
        /* Add the function to resize event listener */
        window.addEventListener('resize', mediaSize, false);  

    })(jQuery);

Alternatively, you can use a simpler approach like this:

if ($(window).width() < 960) {
   $(selector).css({property:value, property:value, ...})
}
else if ($(window).width() < 768) {
    $(selector).css({property:value, property:value, ...})
}
else {
     $(selector).css({property:value, property:value, ...})
}

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

Struggling with implementing Vue.js for making a task list using Bootstrap 5

I'm trying to get the hang of Vue.js. I've been working on setting up a to-do list where users can input tasks, but I'm having trouble getting the list to display correctly when I define the method. It seems like my add() function isn't ...

Result of mixing CSS colors

Is there a way to retrieve the result of blending two colors using the color-mix function? In cases where the color-mix feature is not supported, is it possible to set a fixed value instead? ...

CSS Switchable Style Feature

I am in need of some assistance with the navigation on my website. You can find the current code at this link: http://jsfiddle.net/Sharon_J/cf2bm0vs/ Currently, when you click on the 'Plus' sign, the submenus under that category are displayed bu ...

Eliminate any hyperlink mentions from the Media Print Screen

I'm experiencing a small issue where I am unable to remove the link reference from the print view. Below is the HTML code snippet: <table style="width: 436px; height: 374px;" border="0" cellpadding="5"> <tbody> <tr style=" ...

How can the refresh event be detected within an iframe by the parent document?

Consider this scenario: We are dealing with a file upload page where we aim to avoid reloading the entire page upon completion of the upload process. To achieve this, we have enclosed the form within an iframe. The form within the iframe posts to itself an ...

What is the significance of a colon appearing at the start of HTML attribute names in a Vue template?

<circle r="3" :cx="airport.x" :cy="airport.y" class="airport__marker" /> Are the attributes v-bind:cx and v-bind:cy equivalent to what is shown above? ...

Tips on updating the arrow icon after clicking on a dropdown menu

HTML: <div class="customSelectContainer"> <ul> <li class="initial">Choose</li> <li data-value="value 1">Option 1</li> <li data-value="value 2">Option 2& ...

Directly insert an image into your webpage by uploading it from the input field without the need to go through the server

Can an image be uploaded directly from <input type="file" /> into an HTML page, for example through the use of Javascript, without needing to first save the image to the server? I am aware that AJAX can accomplish this, but if there is a way to ...

Ways to display title attributes when focused using jQuery?

Typically, title attributes in all browsers only appear when the mouse hovers over them. I am looking to also display them when users are focused on them via keyboard navigation. Unfortunately, without using JavaScript, this cannot be achieved solely throu ...

enclosing the pre tag within a table cell

Despite using the latest Twitter Bootstrap, I am facing some issues with pre tags inside table cells. When a line is too long, it should create a horizontal scroll, but unfortunately, that does not happen. Check out this example without using the pre tag. ...

ReactJS input range issue: Cannot preventDefault within a passive event listener invocation

I've been encountering some issues with the react-input-range component in my React App. It functions perfectly on larger viewports such as PCs and desktops, but on smaller devices like mobile phones and tablets, I'm seeing an error message "Unab ...

What is the proper application of counter-reset when utilizing CSS counters to automatically number headings?

In my quest to automatically number headings with multiple levels of hierarchy in an HTML document using CSS, I have encountered various examples online that heavily rely on counter-reset. However, despite testing it in different configurations, I haven&ap ...

Switching the default z-index for child elements within an HTML container

According to the specification, elements are typically drawn "in tree order" for in-flow, non-positioned elements of similar block level or float status and identical z-index. This means that elements declared last in the HTML markup appear on top. But wha ...

Adding an image to a Div using Sencha Touch

For the past 20 days, I have been working with Sencha and learning a lot. I finally managed to access the device camera and capture photos using it. However, I am facing an issue where I am unable to display the captured image in a Div on my webpage upon c ...

What is the best way to dynamically update form fields in a database after making a selection in a <select> component?

Currently, I have a form that displays a list of stars (stellar objects) with a <select> control and two <inputs>. I am seeking guidance on how to populate the two inputs from the database when the user changes the selection in the <select&g ...

JavaScript animation not functioning properly when height is set to "auto"

Can someone help me figure out why setting the height to "auto" in the code snippet below isn't working as expected? I want the DIV to adjust its size based on the content, but it's not happening. Any ideas on how to fix this issue would be great ...

How can we eliminate all elements from jQuery except for the first and second elements?

HTML <div class="geo_select"> <h3>header 3</h3> in Above HTML code i want to remove all element except <h3> and default content<div> inside the <div class='geo_select'> in jquery.. How to remove all ...

Add a touch of mystery to a triangle SVG with CSS shadows

Is there a way to apply shadows to SVG images, like a triangle? I've tried using polyfill solutions but they didn't give me the desired effect. Check out this JSFiddle where I showcase what I'm trying to achieve. This is my HTML: <div c ...

PHP does not automatically escape HTML special characters

Within my ZF2 application, there is a .phtml layout that includes the following code: <p>Created in 2015 <?php echo htmlspecialchars("by Jérôme Verstrynge",ENT_HTML5); ?></p> However, when I review the source of the pages returned by ...

What is the best way to ensure that a sidebar occupies the entire height of our webpage?

Here is the current layout of my sidebar: I want it to always occupy full height, how can I achieve this? I've tried using height: 100%, but it doesn't seem to work. Here is my CSS code: #sidebar { /* Make sure to include all previously men ...