Adapt the stylesheet for mobile devices using CSS

I am struggling with updating file paths in CSS depending on whether the end user is accessing my site from a PC or mobile device. Below is my CSS code, where I attempted to redirect users if they are on a handheld device:

<link rel="stylesheet" type="text/css" href="/css/style.css" />  
<link rel="stylesheet" type="text/css" href="/css/mobile.css" media="handheld" />

Can anyone confirm if this approach is correct, or should I consider using JavaScript to adjust the file path dynamically?

Answer №1

Why make life difficult by trying to detect a browser and device type when you can simply use Media Queries?

Consider using Media Queries like this:

@media only screen and (max-device-width: 480px) 

While all devices are well known, the resolution will dictate the CSS style you provide to the client.

For more information, check out: MediaQueries

You might want to consider using frameworks like Bootstrap3 or Foundation, which come with pre-defined styles and a solid framework for writing CSS.

Focus on the grid system, such as Bootstrap's col-size-n grid of 12 columns, which is responsive.

For more documentation and answers to other questions, visit Bootstrap.

Happy coding :)

Answer №2

Instead of relying solely on Javascript for responsive design, the recommended approach is to use Media Queries.

As pointed out by another responder to your query, if you do need to incorporate Javascript, you can consider the following function:

function AdjustLayout() {
     width = window.innerWidth;
     height = window.innerHeight;

     if(width < 1200 && height < 600) {
          // Make necessary style adjustments here.
     }
}

Of course, you can also take measurements and comparisons using:

  • Browser's Inner Width / Height
  • User Agent

While these are just a couple of examples, Media Queries remain the preferred and optimal solution for responsive design. The specifics of implementing Media Queries were covered in another response.

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

Containing more than a full page of information, the footer stays fixed at the bottom of the screen, even as the user scrolls to

As I work on developing my artist website to showcase my music, I have encountered an issue with the footer placement. My goal is seemingly simple - I want a footer that always sits at the bottom of the site, just under the content/body section. There shou ...

The Fetch API's Post functionality is currently experiencing issues

fetch('http://localhost:9000/api/app/contact', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ email: this.state.email, contactNumber: ...

Creating mock objects for unit testing is a valuable skill to have in your coding

What is the best approach for writing test cases as a beginner? I need guidance on how to write test cases effectively. Exploring Angular functions and classes. picklistScroll() { const picklistWrapper = document.getElementsByClassName('pic ...

Showing data from the POST request in a dialog box

Greetings to all! I am in the process of building a webpage to test my API. The goal is to send a form to my nodejs backend and receive a JSON response back. So far, sending the request is functioning properly and I can track its progress on my VPS conso ...

Unable to adjust the canvas size or scale using Fabric.js

Is there a way to display a large canvas as a smaller one for better user interaction without compromising the size of the uploaded canvas later on? I'm currently working with Fabric.js and exploring the Canvas CSS trick that involves setting the widt ...

A guide to organizing page components across multiple `/pages` directories in a Next.js application

As I delve into my first project using Next.js, I find that my pages directory has expanded significantly. Now, I am keen on organizing my pages by grouping them into modules, resulting in a structure like 'src/modules/*/pages/*'. In my quest fo ...

The getJSON function works perfectly on a regular page, but for some reason, it's not

I am encountering an unusual issue: I am trying to check if an email is already in use as a user enters it. It works fine on a simple page, but when I attempt to call it on a modal, it does not function. Below is the jQuery code: $('#user_email&ap ...

Creating a harmonious relationship between a generator using recursion and promises

In Elasticsearch, it's feasible to make a "Scrolling" request. This method involves keeping a cursor open and retrieving large chunks of data gradually. Demo code is available for reference. The provided code uses callbacks and recursion to fetch dat ...

SailsJS failing to detect changes in local JSON file

https://i.stack.imgur.com/RG13Y.png This sailsjs application does not utilize any database. Instead, it relies on multiple JSON files located in the data folder within the root directory. The controller accesses this data as follows: req.session.categori ...

AngularJS is failing to properly register custom directives

Looking to enhance the SEO caching capabilities of my AngularJS website, I've embarked on implementing a couple of directives for custom page titles and descriptions. Incorporated within my app config (angular.module('website', [...'we ...

Utilizing React Router V4 to Render Dual Components on a Single Route

Looking for help with these routes <Route exact path={`/admin/caters/:id`} component={Cater} /> <Route exact path={'/admin/caters/create'} component={CreateCater} /> After visiting the first route, I see a cater with an ID display ...

Ways to link numerous sockets within a single Vue.js project?

import VueSocketIOExt from 'vue-socket.io-extended'; import { io } from 'socket.io-client'; const socketOne = io('http://localhost:3200/'); const socketTwo = io('http://localhost:3100/'); Vue.use(VueSocketIOExt, so ...

Using AJAX to send both data input values and file attachments simultaneously to a PHP server

I am currently having an issue with sending form values to my PHP page using AJAX. My form includes text inputs and a file input for image uploads. Below are the AJAX codes I have been working on: function sendval() { var form = $('#user_update_for ...

Customizing the CSS for individual posts in WordPress based on their post

I am interested in establishing a unique look for each individual post on my wordpress.org blog. Is there a way to customize the CSS code of individual posts without changing the design of other posts? I have tried using categories to create a new PHP fi ...

Obtaining information from the HttpServletResponse through an AJAX form

In my "first.jsp", there is a form containing hidden input values and pointing to another jsp file. <form id="popupForm" name="popupForm" action="<%= cqRequest.externalizeHref(handle) %>" method="post"> <input type= ...

What could be causing the unexpected gap between the first two elements and the third in my flexbox layout?

I am facing an issue with the layout of my third child element. Instead of appearing below the first two children, it is wrapping around to the bottom of the container. Can anyone explain why this is happening and suggest a solution? Even though I have se ...

margin leakage: potential misalignment caused by nested DIV elements

I am experiencing a strange issue where the margin of a nested DIV is "leaking" out of its parent container. For a better understanding, check out this test case: http://jsbin.com/ibaze The outer wrapper (highlighted in red) does not align perfectly a ...

Is it possible to conceal a link once it has been visited?

I am trying to figure out how to remove a link from a page once it has been visited. However, I am facing privacy restrictions with the :visited pseudo-class. Is there a way to achieve this without using display: none? For example: .someclass a:link {dis ...

After spending a while working on my React project, I started encountering errors consistently whenever I attempted to run the 'npm install' command

An issue has been encountered: '[email protected] requires a peer of [email protected] - 3 but none is installed. You must install peer dependencies yourself.' Has anyone else faced this error before? Can someone help me understand wha ...

Identifying when a user has inputted incorrect $routeparams

How can I restrict user input to only "id" as a query parameter in the URL? $scope.$on('$routeUpdate', function () { var id = $routeParams.id //check if user has entered any params other than "id". //if yes do someting }); I want ...