tips for creating unique CSS styles for both desktop and mobile devices

As I work on creating a web page, I am encountering an issue with defining CSS for both mobile and tablet devices. Despite my efforts to define separate CSS styles for each device, only one seems to be working correctly. How can I effectively specify different CSS for both mobile and tab devices?

<!DOCTYPE html>
<html>
<head>
    <title></title>


<style type="text/css">

/* CSS for desktop */ 
 .form-container{
    width: 60%;
    margin-left: 20%;
    margin-right: 20%;
    background: blue;

 }

 .form-container-inner{
    padding: 20px;
 }


/* CSS for tablet */

@media (min-width: 768px) and (max-width: 1024px) {

  .form-container{
    width: 80%;
    margin-left: 10%;
    margin-right: 10%;
    background: red;

 }

}



/* CSS for mobile */
 @media only screen and (max-width: 992px){

    .form-container{
    width: 100%;
    margin-left: 0%;
    margin-right: 0%;
    background: green;

 }
 }


</style>

<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body>

<div class="form-container">
    <div class="form-container-inner">
    <label>Name</label>
    <input type="text" name="" >
    <br>

    <label>Surname</label>
    <input type="text" name="" >

    <br>

    <label>Password</label>
    <input type="text" name="" >
    </div>
</div>


</body>
</html>

Answer №1

/* Styling for tablets and larger screens (768px and above) */

.form-container{
    width: 80%;
    margin-left: 10%;
    margin-right: 10%;
    background: red;
}

/* Adjustments for screens smaller than 768px */

@media (max-width: 767px) {
  // Your code
  .form-container{
    width: 100%;
    margin-left: 0%;
    margin-right: 0%;
    background: green;
  }
}

Any additional CSS styles specific to tablets or larger screens should be applied normally, but keep in mind they may be overridden by the mobile media query.

Answer №2

In my opinion, there is no need to specify both (min-width:*px) and (max-width: *px). It suffices to define the category's starting point.

Answer №3

To ensure your website looks great on all screen sizes, make sure to set CSS styles for various possibilities:

/* For screens 768px or smaller */
@media screen and (max-width: 768px) {
    // Add your code here
    .form-container{
        width: 100%;
        margin-left: 0%;
        margin-right: 0%;
        background: green;
    }
}

For other screens:

/* For screens between 769px and 1024px */
@media (min-width: 769px) and (max-width: 1024px) {
    // Add your code here
    .form-container{
        width: 80%;
        margin-left: 10%;
        margin-right: 10%;
        background: red;
    }
}

Note: Special thanks to @ashfaq.p for the helpful correction.

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

How can I use BS4 in Python to extract specific text?

When working with BS4, I am trying to isolate specific text. A snippet of the provided HTML is shown below. </tr><tr id="_Gonzaga" class="seedrow"> <td title="Click to show/hide ranks" class='lowrowclick' style="text-align:cente ...

Material Design Lite and jQuery experiencing issues with smooth scrolling feature

I am facing an issue with using the .animate method of jQuery in conjunction with Google's Material Design Lite (MDL) framework. I implemented MDL to create a navigation bar, but encountered problems with achieving smooth scrolling. Here is an exampl ...

Unable to adjust hover color for bootstrap 4 navbar-brand class

Check out this code snippet: .navbar-brand a:hover { color: #43cea2 !important; } <a class="navbar-brand" href="#about">Cole Caccamise</a> ...

Displaying specific data points

I am encountering an issue where I want to select multiple values and have each selected value displayed. However, when I make a selection, I only see one value from a single box. I do not wish to use append because it keeps adding onto the existing valu ...

Clearing HTML Text Input Box When User Presses Enter

Currently, I am working on a lengthy program that is functioning almost flawlessly. However, there is one persistent issue: whenever I hit the Enter key while the text box is clicked, it clears the box and appends the data to the URL as if it were a GET re ...

Utilize jQuery Function on Identical Class of <ul> Elements

I have integrated a listview control in my ASPX page. The data is being fetched from the database and displayed in the listview. I have also utilized jQuery script to implement the .fadein() and .fadeout() effects on the listview elements. However, when I ...

Adjusting Flexslider to perfectly accommodate the height and width of the window dimensions

Currently, I am using Flexslider version 1.8 and seeking to set up a fullscreen image slider on my homepage. My goal is to adjust the slider to match the browser window size. While experimenting with width:100% and height:auto properties, I have managed t ...

Unable to find element in webtable using Selenium WebDriver

I encountered an issue while attempting to retrieve values from a static webtable. The error message I received is shown in the code snippet below: import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.ope ...

Guide to align text in the middle of an image

Struggling to center my text in the middle of the page. Tried using margin-left and right set to auto but it's currently off to the left. As a newbie in front end development, I'm facing some challenges. Check out the jsFiddle link: http://jsfid ...

What is the best way to set the keyframes CSS value for an element to 0%?

Would like to create a progress indicator div using CSS animations? Check out the JSBin link provided. The desired behavior is to have the div width increase from 30% to 100% when the user clicks on stage3 after stage1, rather than starting from 0%. Althou ...

Is the practice of using data:text/css considered legitimate?

Looking to insert 3 CSS rules into the head tag by using <link /> tags. $('head').append('<link rel="stylesheet" href="data:text/css, .rule0 {} .rule1 {} .rule2 {}" />'); Would this syntax be considered valid? I am aware ...

Dropzone.js encounters a post 500 error (Internal Server Error) when attempting to upload files larger than 5mb

This is the code I'm working with: var previewNode = $("#template"); previewNode[0].id = ""; var previewTemplate = previewNode.parent().html(); previewNode.remove(); var documentsDropzone = new Dropzone("#AddDocumentModal #AddFiledr ...

What is the best way to implement this design using CSS or JavaScript?

I would like to enhance the appearance of my school website during these challenging times of the pandemic by adding some CSS or JavaScript elements. However, I am unsure how to go about it. ...

Encountered issue while initializing object from controller in AngularJS

Here is the demonstration on how the fiddle appears: var app = angular.module('testApp', []); app.controller = angular.('testAppCtrl', function ($scope) { $scope.vehicle = { type: 'car', color: 're ...

Retrieve information from an HTML input field that does not have an assigned ID or name

I am facing a challenge with an HTML table that contains multiple inputs which do not have any id or name attributes. I need to retrieve data from these inputs but struggling due to the lack of identifiers. Upon inspecting the DOM, I noticed that each inp ...

When an image is clicked, I would like it to redirect to a different page

In my slider image list, each image has an ID. If the ID becomes equal to a specific number, such as 24, I want that particular image to move to another page when clicked. Here is the code snippet: <?php $sql = "select * from ".$tblImages." Where cid= ...

What is the best way to maximize the width of this element?

Check out my website: There's a div styled with the color #D9D9D9 This is the CSS code: #full_bar { background: #D9D9D9; width: 100%; height: 100px; } I want the div to span across the full width of the website and be stuck to the footer. An ...

Techniques for transferring images directly into HTML canvas games

I created a thrilling race car game and here is the code snippet: index.html <!DOCTYPE html> <html> <head> <title> Extreme Race Car Game </title> <link rel="stylesheet" type="text/css" href="style.css"> < ...

Harvest information using BeautifulSoup and Python

Exploring the world of web scraping with BeautifulSoup in Python, I am interested in extracting the package name and price of each app from the Android Play Store. For retrieving the package name, I implemented the following code : url = "https://play.go ...

How can I make the HTML links in my Django email clickable?

I recently used Django to send the email shown below: subject, from_email, to = 'site Registration', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30434540405f424470435944551e535f5d5">[email protec ...