What is the most effective method to center an element while ensuring it remains responsive?

If you want to check out the issue, follow this link to my code: https://jsfiddle.net/0sbcdfLk/2/

Currently, I am utilizing a responsive login form template from Bootstrap. For my logged-in screen design, I have duplicated the formContent section of the code. Now, I have two containers, but I need the second container to be aligned normally on the left and directly below on mobile devices.

Trying to align it using margin messes up the design on mobile screens. Any suggestions for best practices in responsive design? This is the exact Bootstrap template that I'm working with:

I have replicated this portion of the code:

<div id="formContent">
<!-- Tabs Titles -->

<!-- Icon -->
<div class="fadeIn first">
  <img src="https://www.b-cube.in/wp-content/uploads/2014/05/aditya-300x177.jpg" id="icon" alt="User Icon" />
  <h1>Aditya News</h1>
</div>

<!-- Login Form -->
<form>
  <input type="text" id="login" class="fadeIn second" name="login" placeholder="username">
  <input type="text" id="password" class="fadeIn third" name="login" placeholder="password">
  <input type="submit" class="fadeIn fourth" value="Log In">
</form>

<!-- Remind Password -->
<div id="formFooter">
  <a class="underlineHover" href="#">Go to the Site</a>
</div>

For the bottom one, I tried adding a style with margin: -50% 0px -6px -95%, which worked well on PC, but on mobile devices it appears half off the screen, overlapping the first form.

Answer №1

To make your website responsive, utilize "CSS @media" rules by adjusting the CSS based on the width of the window. By creating @media rules for various device screen sizes such as mobiles, tablets, PCs, and wide screens, you can customize the display to suit each one accordingly. It allows you to control the appearance just like you normally would. When setting up a form content div in your HTML, it is better to use a class instead of multiple IDs for styling purposes, even if it's only used once:

<div id="formContent" class="user-profile">

Include a media rule that positions the form to the left when the screen size is large enough:

@media only screen and (min-width: 1400px) {
    .account-details{
        position: absolute !important;
        left: 0;
        margin-left: -150px;
    }
}

For reference, here is a working example https://jsfiddle.net/y2sgobLz/

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

Align Divs Horizontally to Center

I'm trying to find a way to horizontally center multiple DIVs in a straight line. Here's how my code currently looks: HTML: <div id="circle"> <div id="circle1"></div> <div id="circle2"></div> </div> CSS ...

Carousel malfunctioning, refusing to slide smoothly

I recently copied the bootstrap 5 Carousel code and tweaked it with some additions, however, it's not functioning correctly. It seems to only go to the 2nd slide and then stops. Strangely enough, when I open the developer tools (F12) and go to inspect ...

Managing image alignment in flexbox containers with nested rows and columns

In the following design demonstration, there are three blocks to explore. Block 1: Consists of three columns. The middle column contains two rows, both set to flex:1. Block 2: Also has three columns. The middle column includes two rows, but with a unique ...

Tips for styling CSS for individual "ID" elements within a primary "Class" container

Struggling with crafting CSS code to style a specific HTML snippet. Take a look at the following HTML: <div class="FourSquares"> <div id="first"></div> <div id="second"></div> <div id="third"></div> ...

Having issues with Jsoup parser specifically on one URL

I'm currently utilizing Jsoup to retrieve the content of a webpage and parse it. public static void main(String[] args) throws IOException { Document document = Jsoup.connect("http://www.toysrus.ch/product/index.jsp?productId=89689681").get() ...

Refresh the image following the ajax request

I seem to be encountering some difficulties. Whenever I click to rotate an image using ajax and save the new image, it does so without refreshing the page. However, the issue arises when the old image is not updated with the new one that is refreshed. Fu ...

Is there a way to modify the height and width of a div layer?

How can I adjust the height and width of the layer on the card? Also, I want only the close button to be clickable and everything else to be unclickable. Can anyone help me find a solution? <link rel="stylesheet" href="https://stackpath.bootstrapcdn. ...

Tips on displaying text inside an icon

As a self-taught newcomer to web development, I apologize if this is a basic question. I'm trying to figure out how to display text inside an icon, like putting a number inside a heart. Would using a webfont icon not be suitable for this purpose? Is ...

Issues with Bootstrap's "img-fluid" class functionality

I recently started the web development course by Colt Steele and ran into an issue while working on the "Museum of Candy project" (I made some modifications to it). The problem is that I am using a widescreen monitor, and when I expand my window, the image ...

Resize background image to perfectly fit within div dimensions

One of my divs has a background image that is getting clipped: <div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center"> Is there a soluti ...

Retrieve data from an external website containing an HTML table without a table ID using Java Script and transform it into JSON

I have developed a script that can convert HTML table data into a JSON Object. To accomplish this task, I utilized the jquery plugin created by lightswitch05. With this code, I am able to extract data from an HTML table on the same web page using: var t ...

Exploring Ways to Utilize Multiple Access Levels with PHP and MySQL

Currently, I am developing a PHP & MySQL management system for a college level institution. In this system, each employee and student will have their own account and I aim to display personalized pages for each user. In my scenario, there are 3 levels o ...

How to Send a File Using jQuery

Querying about file upload using Ajax. How can I send a file with $.ajax() without the need for a specific js-plugin?: <form action="javascript:return false;"> <input type="text" id="name" /> <input type="file" id="myfile" /> ...

Activate just the show more / show less button on the website that has several buttons with identical ids

Whenever the "show more" button is clicked, additional images are displayed in the gallery and the button text changes to "show less". However, in my ExpressionEngine (CMS) templates and entries, all "show more" buttons share the same id, causing other but ...

Having trouble with my ajax code that reads a file into a div. I must be overlooking a small detail

Despite the simplicity of my task, I'm encountering an issue. Perhaps there is something crucial that I am overlooking. The objective is to retrieve the content of a text file using AJAX and display it within a div. While I can successfully write to ...

How to create a smooth scroll effect to a specific section using an <a>

Currently studying web design, I am working on creating a parallax website. I have managed to slow down the scrolling background in certain areas, but I want to enhance it by adding buttons in the header that will smoothly scroll the page to a specific Div ...

Having trouble with Fancybox Loading On Page Start function not working properly in Internet Explorer?

My website has a Fancybox that is supposed to load as soon as the page loads. It works perfectly in Firefox, but unfortunately it does not work in Internet Explorer and I am unsure why. There are no errors displayed in IE when the page loads, it simply doe ...

"Encountered an Ajax Error while appending request parameters to the same link during a

I can't figure out why my ajax request is sending to the same link instead of the specified url. It's only this ajax request on the entire page that is behaving strangely. Can anyone shed some light on this issue? $(document).ready(function(){ ...

Switching between fixed and unfixed divs causes two absolute divs to alternate

I am currently working on a code to keep a fixed div ("two") in place between two absolute positioned divs ("one" and "footer") while scrolling. However, there is an issue that arises when the browser window is resized. The distance between the footer and ...

Incorporate geographical data from a JSON file into my Google Maps application

Hey there, I'm a newbie on this forum and could really use your expertise. I've put together an html5 page with Google maps using the API key from Google (My code is shown below), it's working fine with a central marker in place and loads pe ...