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

What is the best way to adjust the height of a dropdown box in an angular application?

Is there a way to change the height of the scroll view? It seems too long for my liking, any advice? I attempted to adjust it using css but unfortunately, it didn't work out. The scroll view appears excessively lengthy as shown in the image below. ...

Achieving dynamic HTML element addition through JavaScript while maintaining their record in PHP

There is an input box where the user specifies the number of new DIV elements to be added dynamically. This request is then sent to PHP via Ajax, where it prepares an HTML DIV with some inner input tags. Each DIV has a unique id attribute generated increme ...

Retrieving data from a Ruby class based on a specific condition

Currently, I have a code snippet that adjusts the formatting of an editing page based on the number of values a question contains. <% (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aa9b8484eadbdfcfec8cdcac3c5c484dbc6ece0 ...

Ways to display the page's content within a div container utilizing Jquery

I am attempting to display the content of a URL page (such as ) within a <div> using JQuery, but so far I have been unsuccessful. Here is an example of what I am trying to achieve: <div id="contUrl"> .. content of google.fr page </div> ...

Ways to format the direct descendant of a multi-level list?

Check out my nested ul list: <ul> <li> <a href="/"></a> </li> <li> <a href="/"> </a> &l ...

Tips for adding animation to the div instead of the content

I have implemented a hover animation to animate the div, but unfortunately, when I added the animation to #i :hover {}, it ended up animating the words only. Moreover, the cursor transforms into a pointer solely when hovering over the words instead of the ...

Maintain the alignment of content in the center while expanding background images

I am looking to create a website with a split background effect, similar to the design on this site . I want to maintain a content container width of 980px while achieving this look. I attempted to accomplish this by adding height and background color pro ...

Transform an xlsx document into a HTML file using VBscript or batch script

After extensive research, I have come up empty-handed in finding a solution to the issue I am facing. The problem at hand is that I have an Excel file in .xlsx format that needs to be converted to .html on a regular basis throughout the day. Once converte ...

Can a mobile device be programmed to automatically bring up the keyboard for the autofocus element?

Currently utilizing Meteor for my project and facing an issue with a "Add new" button. When clicked, the button disappears and is replaced by a text box. The toggle logic between the button and input is complex and not relevant here. Check out the HTML co ...

Improve the Popup to seamlessly elevate

In my project, I have implemented a pop-up dialog box that rises up from the left bottom corner as the user scrolls down the page. You can view it live at this link- However, I am facing an issue where the initial lift up of the dialog box is not smooth, ...

The submit button appears as grey on IOS when using the input[type="submit"]

Why does the submit input look different on IOS only, and how can this be resolved? Thank you in advance For HTML: <input class="btn" type="submit" [disabled]="" value="ابدأ الان&qu ...

Is there a way to keep Angular from automatically re-sorting my list when I make edits to it?

I have designed a small application with Angular for managing Todolists. Each list contains multiple todos, where each todo has attributes such as name, value1, and value2. To automatically sort each list using Angular, I utilized ng-repeat="todo in selec ...

Tips for highlighting HTML syntax within JavaScript strings in Sublime Text

Is there a Sublime package available for syntax highlighting HTML within JavaScript strings specifically? (Please note that the inquiry pertains to highlighting HTML within JS strings only, not general syntax highlighting.) Currently, I am developing Ang ...

What is the best way to merge two AngularJS form validation directives criteria using CSS?

When both $dirty and $invalid are true, I would like the input fields to have a red background. However, I am only able to do this one by one, which is not ideal. input.ng-dirty && input.ng-invalid { background-color: red; } ...

Utilizing Bootstrap divs for multiline display in Yii2 platform

Currently, I am utilizing a list in Bootstrap CSS which is responsive and beneficial. However, I am faced with the challenge of creating a notification list with lengthy text akin to that on Facebook. My query pertains to the technique required to make th ...

How to center elements using Bootstrap 4 and flexbox styling

Just starting out in web development. I currently have the following HTML: <section class="boxes"> <div class="container-fluid"> <div class="row"> <div class="col-12 col-lg-4 box1&q ...

Retrieve the width of an element once the browser has finalized its dimensions

I am facing an issue with centering a pop-up box perfectly within the window using CSS properties. The code for the pop-up box styling is as follows: #pop_up { position: fixed; display: inline-block; border: 2px solid green; box-shadow: 0p ...

Creating a List with Sublists that are displayed when hovering over the parent List is a key element of effective design

Hovering over 'View Rows' should open up both New Records and Old Records <div> <li>Add Rows</li> <li>DeleteRows</li> <li>View Rows <ul> <li>View New Records</li ...

initiate an animated sequence upon the initialization of the Angular server

Is there a way to launch a Netflix animation after my server has started without success using setTimeout? I don't want to share the lengthy HTML and CSS code. You can view the code for the animation in question by visiting: https://codepen.io/claudi ...

Font that has been downloaded is not showing up on the HTML document

I recently downloaded a new font and ensured the file location is correct, but for some reason it's not working: <style> @font-face { font-family:"myfont"; src: url('fonts/Helvetica85Heavy_22449.ttf'); } h1 { font-family: ...