Focusing on the alignment of an article tag

I am currently facing a challenge in centering the content within an HTML article element. As of now, the content is aligned to the left margin of the page. Below is the snippet of HTML I am working with:

<article> 
    <h1>Sign In</h1>
    <div class="display-new">
        <form action="" method="post">
            <div class="label-field">
                <label for="email">Email: </label>
                <input type="text" name="email" />
            </div>
            <div class="label-field">
                <label for="password">Password: </label>
                <input type="password" name="password" />
            </div>
            <input type="submit" value="Sign In" class="submit-button"/>
        </form>
    </div>
    <div class="forgotten-password">
         <a href="../index.php?action=resetpassword">Forgotten your password?</a>
    </div> <!-- end forgotten-password -->
</article>

And this is the CSS code being applied:

html {
    position: relative;
    min-height: 100%;
}
/* Rest of the CSS styling here... */

If you want to see a live demonstration and encounter the issue I'm experiencing, feel free to click on this fiddle link. I have tried various solutions found on StackOverflow and other resources related to centering elements, but still haven't resolved the problem. Could someone help enlighten me on where I might be misunderstanding these solutions and how they apply to my specific scenario?

Answer №1

Alright, the situation here is as follows:

  1. The article tag is indeed centered, evident by changing its background color to red:

https://i.stack.imgur.com/WTUsJ.png

  1. The "display-new" div is responsible for the left alignment of the form. You can see this by changing its background color (I made it a different color):

https://i.stack.imgur.com/mTg6o.png

  1. One way to address this is by defining a specific width for display-new so that it contains the form. Then, applying margin: 0 auto will center it:

http://jsfiddle.net/hhvbts7L/8/

.display-new {
    width: 150px;
    margin: 0 auto;
    background: blue;
    text-align: left !important;
}

With this knowledge, you should be able to find your own solution!

Answer №2

If you're searching for the solution, make sure to apply the display:block property to your input fields. To center the input elements within your article tag, simply add margin-left:auto and margin-right:auto...

CSS Centralizing Example

Answer №3

To achieve centering for the textboxes, simply remove the input element from this CSS code:

form div.label-field label, form div.label-field input {
    display: block;
}

The corrected code should look like this:

form div.label-field label, form div.label-field {
    display: block;
}

Also, make sure to center the text alignment by updating the following CSS:

form div.label-field {
    position: relative;
    margin: 0 auto;
    padding: 5px 0;
    text-align: center; //<--
}

You can view a demo of the changes on JSFIDDLE DEMO

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

Bespoke SoundCloud music player with a sleek and minimal design, ensuring the font color remains

Just moved over from github, so please forgive the double posting. I've customized the minimal player, but I'm having trouble changing the font color of the track titles. Everything else about the font works fine, except for the color--it always ...

What are the best methods for utilizing scrollbars to navigate a virtual canvas?

I am interested in developing a unique jQuery plugin that can simulate a virtual HTML5 Canvas, where the canvas is not physically larger than its appearance on the page. However, the content intended for display on the canvas may be much larger and will ne ...

Sending unstyled HTML content using JavaMail

Can anyone offer guidance on sending an email using JavaMail with unformatted HTML tags? Here is the code I am currently using: public void sendMail() throws MessagingException, IllegalStateException, IllegalArgumentException { Properties properties ...

Retrieve variables from an external JavaScript file

I need to fetch the currentID variable from renderermain.js and utilize it in renderermem.js. However, I am looking for a way to achieve this without modifying mem.html: <script src="renderermain.js"></script> <script src="renderermem.js"& ...

Set up a mouseover function for a <datalist> tag

Currently, I am delving into the world of javascript/jquery and I have set up an input field with a datalist. However, I am encountering a slight hurdle - I am unable to trigger an event when hovering over the datalist once it appears. Although I have man ...

Angular JS is having some issues with rendering the style correctly

How can I make input fields turn pink before values are entered and green after values are entered? Currently, the styling only applies to the first row. What could be causing this issue? <script src="https://ajax.googleapis.com/ajax/libs/angularjs ...

Easiest method to globally disable form autocompletion in an application without manually setting autocomplete off for every individual form

Is there a quick way to disable autocomplete in all forms within the application without individually adding "autocomplete=off" to each form? ...

Where is the best location to store the image files?

I am currently working on a web application that focuses on Image processing, however I have encountered a basic issue. The images are not loading during the runtime of my HTML page. I have tried placing the image files in the src folder, but it seems li ...

Leveraging JavaScript to generate a downloadable PDF document from the existing webpage

My goal is to have the user click a button labeled 'View PDF' or 'Download PDF' on the current webpage. This button would then execute JavaScript code to generate a PDF based on how the current page appears. I attempted using jspdf for ...

Entering in full screen mode

Is there a way to create a first screen appearance that spans the entire screen? I want the whole page to be fullscreen and when someone scrolls down, it transitions to a new screen. I am struggling to find a solution for this. body { margin: 0 0 0 0; ...

Bootstrap-tour is incompatible with a row within a table structure

Is there a way to highlight a table row effectively? I've been struggling with it and tried using the fix mentioned in this bootstrap-tour issue here Check out this demonstration on jsFiddle: jsFiddle JAVASCRIPT $("#dialog").dialog(); var t = new ...

Is there a way to deliver information to a specific element on a client's HTML page?

My current project involves using Node.js to serve a webpage that collects user inputs and stores them in a mongodb server. The web page also displays these user inputs. I am trying to determine the best way to pass the user inputs from node.js to the < ...

What is the best way to prevent images from being loaded with broken links?

Currently, I am working on a new feature that involves rendering images from servers. In the process of aligning these images, I noticed an excessive amount of white space due to loading images with broken links. https://i.stack.imgur.com/jO8BR.png Here ...

Close button located in the upper-right corner is partially cut off

My challenge involves placing a close button on the top right corner of a #main div that contains an image of varying or larger size. I need the close button to slightly protrude outside the div using negative margins. The issue is that when I use overflow ...

Pop-up website opening with fixed dimensions error

On my webpage, I have a terminal where you can type commands and receive output. For example, typing "/unsolvedproblems" displays a list of unsolved problems with a hyperlink. I've been trying to make the hyperlink open in a popup window with a fixed ...

Display a pop-up window after a set amount of time and automatically close it when clicking on an

I am looking to implement a functionality where my popup box opens automatically after a specific time interval (e.g. 5000 milliseconds) and can be closed by clicking on an anchor tag placed inside the popup box. function openPopup() { $('.popup- ...

how to edit a hyperlink tag within an HTML comment using Java

I am dealing with html content that contains a lot of comments. Within the comments, there are anchor tags present, and I need to replace the anchor starting tag with ^^. **original content** <html> <body> <!-- somthing text <a hre ...

Guide to customizing action button color on MatSnackbar?

Currently, I am encountering an issue with the snackbar in my Angular 9 project. Despite adding CSS styles to both the component.scss file and the global style.scss file, the action button on the snackbar displays the same background and text color. In an ...

Tips for adding a string variable to a JQuery HTML element attribute

Hi there, I've been working on a JQuery code to append data to a div element and it's been successful so far. Here is the code: $('#conversation').append('<div id="receiver"><p><b>' + username + ':< ...

Extracting the input's data and placing it into a table cell

Hey there! I am facing an issue where the data entered in the input fields is not getting transferred to the respective <td>. The text displayed in the (tabios) field should change according to what is entered in the input. Here's the code snipp ...