What is the best way to ensure my responsive form is centered and aligned properly with all other elements on the page

Link to website

This final step is all that stands between me and the completion of my website deployment. However, I am faced with a persistent issue that has me stumped. It seems that there is an unexplained margin on the left side of the contact form, throwing off the measurements. Upon closer inspection of the borders I've set for testing, I realize that it should be perfectly aligned and centered with the green border of my content on medium and small device screens. Yet, when I shrink the page down on a modern browser like Firefox, the form shifts to the left and creates an unwanted margin on the right side. Surprisingly, the appearance remains consistent across both modern browsers and IE, which is a positive. Any proposed fixes should ideally remain compatible with these older browsers as well. I have tried adjusting the width of the form and experimenting with container layouts (similar to what I did for the social icons), but so far, no luck (unless I'm missing something). If anyone can assist me in resolving this issue, I would greatly appreciate it. I'll be online throughout the weekend, continuously making updates and changes, allowing you to see the impact of your suggestions immediately.

Visual Representation of problem:

Placeholder for visual representation

HTML:

<div id="main_section">
                <div id = "center">

                <div id="contact_section">
                    <form method="POST" action="contact.php">
                    <span class="label">Name</span><br/>
                    <input type="text" class="textfield" name="name" size="50" maxlength="50"><br/>
                    <span class="label">Email</span><br/>
                    <input type="text" class="textfield" name="email" size="50" maxlength="50"><br/>
                    <span class="label">Subject</span><br/>
                    <input type="text" class="textfield" name="subject" size="50" maxlength="50"><br/>
                    <span class="label">Message</span><br/>
                    <textarea rows="5" cols="50" name="message" id="textarea" maxlength="500"></textarea><br/>
                    <input type="submit" value="Send" id="submit" name="action"> 
                    </form>
                </div>
                </div>

        </div>

Main CSS:

#contact_section {
    max-width:100%;
    margin-bottom:40px;
    margin-right: 0px;

    margin-top:20px;
    padding: 20px 6px 20px 6px; /*Make this smaller for 100% responsiveness*/
    border-radius:10px;
    background:#f1f1f1;
    box-shadow:0px 0px 15px #272727; 
    float:right;
    text-align:center;
}

Medium and small screen CSS:

#center{
width: 100%;
  display: block;
  margin-left: auto;
  margin-right: auto;
  max-width:100%;
border: 1px red solid;
text-align:center;
}

#contact_section {
width:95%;
max-width:100%;
 margin-left: auto;
  margin-right: auto;
  text-align:center;
  border: 1px yellow solid;
}

Answer №1

One issue lies in the fact that your #contact_section is floated to the right in your main CSS, causing it to always align to the right. To fix this, try adding float:none; to both #details_section and #contact_section in the CSS specific to small screens.

UPDATE

To address your concern about the overall layout, consider setting the widths of your divs as percentages:


#details_section {
  width: 40%;
  margin-right: 5%;
  float: left;
}

#contact_section {
  width: 55%;
  float: left;
}

#contact_section form{
  padding: 20px 6px 20px 6px;
}

You may also need to adjust or remove the max-width property on your inputs for a better display.

Answer №2

It's worth noting that the alignment of both your green frame and contact form could use some adjustment. When you apply float:right, having margin-right:auto; and margin-left:auto won't necessarily center them, as the float will still push to the right. One solution would be to set the width of both containers to 94% instead of 95%, and include margin-right:3% and margin-left:3%.

To clarify:

#center {
    width: 94%;
    display: block;
    margin-left: 3%;
    margin-right: 3%;
    max-width: 100%;
    border: 1px red solid;
    text-align: center;
}

Adjusting float:none, as suggested by koala_dev, may disrupt your layout since floating elements have been integral so far (even your container is floated). It's best to proceed with caution if making this change.

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

Error encountered when attempting to utilize a variable within an EJS template document

I seem to be encountering some difficulties while attempting to get an EJS template file to recognize a variable that holds the rows from an SQLite3 table query in a related .js file. Whenever I try to access that route by launching the server, I receive a ...

Ways to dynamically insert a new row into a table based on user input in a Postman-like reactive table

Is there a way to dynamically insert a row when a single character is entered into an input field in the header tab, similar to how Postman functions? 1) If a user types any single character in the td of the first row, then a new row should be added below ...

Creating CSS-style overlayed images that are resizable

I have a collection of images all the same size, and I want them to be overlaid on top of each other. I've tried setting the position of the images to absolute, but this causes an issue with the container not adjusting its size accordingly. Additiona ...

Photo on vertical display

I have been working on displaying dynamic photos from a backend file and looping through them. I have successfully displayed the images vertically using CSS. vertical-align: middle; However, when I tried to add a photo name for each image, the positionin ...

How can I use htaccess to forward all links within a specific folder to index.php with a GET variable attached?

Is it possible to redirect all links inside a folder to index.php while passing the file name as a GET variable? I have never worked with htaccess before and don't know how to search for it. For instance: localhost/folder/file-123 should redirect to ...

Is there a way to update the color of a button once the correct answer is clicked? I'm specifically looking to implement this feature in PHP using CodeIgniter

Within my interface, I have multiple rows containing unique buttons. Upon clicking a button, the system verifies if it corresponds to the correct answer in that specific row. The functionality of validating the responses is already functional. However, I a ...

Animating ng-switch transitions within a div element

Utilizing bootstrap 3 panel along with ng-switch for a sliding animation: Check out the Plunker here I am facing an issue where the animation extends past the borders of the panel instead of staying within it. How can I fix this in my Plunker? The desire ...

Show the form only if the individual is not a member

Having some issues with my code. Individually, the form and PHP script both work fine, but when combined, I run into trouble. <?php $check = $mysqli->query("SELECT is_member FROM users WHERE username = '$username'"); $isMember = $check ...

Develop adaptable flex items

I am working with a container element that contains variable child elements whose width I want to scale. To see an example of what I'm trying to achieve, take a look at this simple plunker: https://plnkr.co/edit/ef0AGPsK7FJJyBqgWuMi?p=preview When ...

Manipulate classes based on scrolling (Wordpress)

Trying to manipulate a widget by adding and removing classes based on user scroll behavior has proven to be quite challenging. Specifically, I aim to add one class when the user scrolls down by 50px and remove it when they scroll back up. Website: Check o ...

Extract Information from a Website

Is there a way to extract data from another website using JavaScript and save it into a .TXT or possibly an XML file? If JavaScript is not the best solution, I am open to other suggestions. I am specifically interested in extracting the price and item na ...

Sending multiple unique forms with the same structure using JavaScript and Ajax on a single PHP page

In my PHP page, there are 12 individual forms with unique form IDs, checkboxes, and dropdowns. Take a look at this image: https://i.stack.imgur.com/pODzk.png Each form has an Update Zone that fetches Name, Enable status, Time, Dim information, and sends ...

Step-by-step guide on dynamically fetching additional images from a directory using php, jquery, and ajax

I have a scenario where I have multiple folders, each containing up to 20 images. On my HTML page, I want to display the first 5 images and provide a "click to view more" option to show the remaining 15 images when clicked. Currently, the issue I am facin ...

Tips for setting a checkbox as checked by default using AngularJS

I would like the checkbox to be checked by default. HTML : <div class="form-group col-sm-6" ng-class="{ 'has-error': pForm.caddress.$dirty && pForm.caddress.$error.required }"> <label class="contro ...

Unable to get CSS First Child Pseudo Element to Work Properly

I'm having trouble with this code in CSS and would appreciate any help. I can't figure out why the margin-left won't apply to the first i element. Below is the CSS I'm using: header.site-header .right_side_menu i:first-child { marg ...

Tips for styling React Material-UI list items with fontAwesome icons and Tailwind.css

I would like to align the text of my list items to the left. Additionally, I want all the icons to be the same size as the envelope icon used in Gmail list item. Currently, my code looks like this: https://i.stack.imgur.com/9LNOs.png How can I achieve th ...

Ensuring the same height for the navigation bar and logo with aligned vertical text

I'm in the process of recreating a simple navigation menu and I've reached the section where I am encountering an issue with the list items. The orange li element is not filling up 100% of the width, and I also need the links to align in the midd ...

Troubleshooting media queries on an example webpage

Hey there! So, I'm having a bit of trouble with running media queries on a simple HTML page. It's been a while since I worked on this stuff, and I feel like I must be doing something silly that I can't quite pinpoint. If you want to take a ...

Unable to generate a vertical navigation bar

When attempting to create a vertical menu, the final result doesn't align as expected. https://i.stack.imgur.com/2ok47.jpg This is the current code being used: $("#example-one").append("<li id='magic-line'></li>") ...

Troubleshooting on Safari for iOS

As I work on my portfolio website using only HTML 5 without any JavaScript, I encountered an issue with the about page not functioning properly on iPhones. There seems to be a problem with scrolling as some elements are fixed and do not move along with t ...