Tips on aligning objects within div elements

I am currently in the process of learning how to create websites.

Below is the body tag of my website:

<body>
    <h1 id="title">Title</h1>

    <div id="loginContainer">

        <img id="usernameIcon" src="assets/images/usernameIcon.png" alt="">
        <input id="usernameInput" type="text">

        <img id="passwordIcon" src="assets/images/passwordIcon.png" alt="">
        <input id="passwordInput" type="password">

        <button id="loginButton">Sign in</button>
    </div>
</body>

Here is a snippet from my CSS file:

#title {
    color: black;
    text-align: center;
    letter-spacing: 1px;
    font-weight: bold;
}

#loginContainer{
    width: 60%;
    height: 400px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 80px;
    background-color: white;
    border-radius: 20px;
    box-shadow: 10px 10px 72px -26px rgba(0,0,0,0.75);
}
...
... (remaining CSS code)

In an attempt to adjust the layout, I added margin-top: 150px; to the #passwordInput element in the CSS file. This resulted in a change of position for all UI elements on the site. I am seeking assistance and would appreciate any guidance you can offer as I continue to learn about website development and design.

Answer №1

When all elements are contained within the same div, it will expand when any of the elements receive a margin.

To avoid this issue, try using position: relative and top: 150px

#passwordInput {
  position: relative;
  top: 150px;
}

Answer №2

After making some adjustments to the HTML and CSS, I believe the design should now function correctly. You can view the updated code on CodePen.

#title {
  color: black;
  text-align: center;
  letter-spacing: 1px;
  font-weight: bold;
  font-family: sans-serif;
}

#loginContainer {
  width: 60%;
  margin-left: auto;
  margin-right: auto;
  margin-top: 80px;
  background-color: white;
  border-radius: 20px;
  box-shadow: 10px 10px 72px -26px rgba(0, 0, 0, 0.75);
  box-sizing: border-box;
  padding: 125px 0 50px;
}

#usernameIcon,
#passwordIcon {
  width: 30px;
  height: 30px;
  position: relative;
  top: 10px;
  margin-right: 15px;
}

#usernameInput,
#passwordInput {
  width: 30%;
  height: 30px;
}

#passwordInput:focus-within {}

#loginButton {
  width: 40%;
  height: 40px;
  border-radius: 15px;
  border-width: 0;
  background-color: green;
  margin-top: 150px;
  margin-left: 30%;
  margin-right: 30%;
}

.wraper {
  outline: 1px solid red;
  width: 40%;
  margin: 0 auto;
}


/* input-group */

.input-group {
  overflow: hidden;
  text-align: center;
}
<body>
  <h1 id="title">Title</h1>

  <form id="loginContainer">
    <div class="input-group">
      <img id="usernameIcon" src="https://ya-webdesign.com/images600_/vector-login-username-1.png" alt="">
      <input id="usernameInput" type="text">
    </div>
    <div class="input-group">
      <img id="passwordIcon" src="https://ya-webdesign.com/images600_/password-icon-png-2.png" alt="">
      <input id="passwordInput" type="password">
    </div>

    <button id="loginButton">Sign in</button>
  </form>
</body>

Answer №3

I have made adjustments to the HTML and CSS to achieve the desired design. I'm confident that it will function properly. You can find the code on codepen

#title {
  color: black;
  text-align: center;
  letter-spacing: 1px;
  font-weight: bold;
  font-family: sans-serif;
}

#loginContainer {
  width: 60%;
  margin-left: auto;
  margin-right: auto;
  margin-top: 80px;
  background-color: white;
  border-radius: 20px;
  box-shadow: 10px 10px 72px -26px rgba(0, 0, 0, 0.75);
  box-sizing: border-box;
  padding: 125px 0 50px;
}

#usernameIcon,
#passwordIcon {
  width: 30px;
  height: 30px;
  position: relative;
  top: 10px;
  margin-right: 15px;
}

#usernameInput,
#passwordInput {
  width: 30%;
  height: 30px;
}

#passwordInput:focus-within {}

#loginButton {
  width: 40%;
  height: 40px;
  border-radius: 15px;
  border-width: 0;
  background-color: green;
  margin-top: 120px;
  margin-left: 30%;
  margin-right: 30%;
}

.wraper {
  outline: 1px solid red;
  width: 40%;
  margin: 0 auto;
}


/* input-group */

.input-group {
  overflow: hidden;
  text-align: center;
  margin-bottom: 15px;
}
<body>
  <h1 id="title">Title</h1>

  <form id="loginContainer">
    <div class="input-group">
      <img id="usernameIcon" src="https://ya-webdesign.com/images600_/vector-login-username-1.png" alt="">
      <input id="usernameInput" type="text">
    </div>
    <div class="input-group">
      <img id="passwordIcon" src="https://ya-webdesign.com/images600_/password-icon-png-2.png" alt="">
      <input id="passwordInput" type="password">
    </div>

    <button id="loginButton">Sign in</button>
  </form>
</body>

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

Notification with jQuery

I am looking to display an alert message when val = -1. However, the issue I am facing is that this message always appears at the bottom of the page regardless of the value of val. if ($val == -1) echo ' <script> $( ...

CSS: Placing a Column below a Float Positioned Column

While performing some maintenance on a website, I noticed that one page is not displaying correctly. There is a container div for a column on the left and a container div for content on the right, both within an overall page content container. Below these, ...

Turn off drag and drop functionality and activate selection in HTML

Recently, I encountered a strange issue where selected text became draggable and droppable onto other text, causing it to concatenate. To resolve this problem, I added the following code: ondragstart="return false" onmousedown="return false" However, thi ...

Trouble with Showing Bootstrap 5 Icon

My current project involves creating a website using Bootstrap 5 icons, but I'm encountering an issue where the icon is not displaying correctly. The expected appearance should be like this [![Example1][1]][1], but it actually looks like this [![Examp ...

Issue arises after injecting HTML element into the DOM due to memory constraints and display inconsistencies

Upon executing the following script in Firefox... var d = $("<div class='test'></div>"); d.hide(); $("body").prepend(d); d.show(); ...and examining the HTML, the inserted element will have the style attribute: style="display: blo ...

What is the best way to align three images horizontally using bootstrap?

While working on my JavaScript class and creating one-page web apps, I had the idea to create a central hub to store and link them all together. Similar to Android or iOS layouts, I designed a page with icons that serve as links to each app. I managed to ...

Prevent ng-repeat from rendering the contents of my div

Currently, I am in the process of modifying a website for which I need AngularJS. I am trying to use the ng-repeat command to display some documentation on the site. However, whenever I add the ng-repeat within a div, it seems to "destroy" the layout and I ...

Tips for identifying emails from protected platforms such as ProtonMail or Hushmail

A question for the community: So, I have a website that's totally above board and legitimate. Lately, I've noticed some shady characters trying to register from email domains like Proton and Hush - yikes! Before I dive into PhoneText validation, ...

Tips for showcasing HTML code retrieved from a fetch request in a Vue component

Trying to integrate HTML code received from an API into my VueJS code. The API provides the following: <p>blah blah blah</p> <ul> <li> 1 </li> <li> 2 </li> </ul> Saving this code snippet into a variable, but ...

Display an HTML image within a span tag and ensure it is aligned to the bottom

I am facing a layout issue with 2 images that are placed side by side within their tag . The sizes of the images are different, but they are both anchored at the top of their parent element. <span style="position: relative; left: 0; top: 0; vertical- ...

The background image stubbornly refuses to stretch to the full width of the section container

Looking at my code example and the screenshot provided, it's clear that the image is not fitting into the container even when set to 100% width or utilizing other properties. Below you will see the code snippet: .about-us{ background-image: url(i ...

Can you use "or" or "not" syntax with CSS input type selectors?

In the world of programming, if they actually exist, Suppose I have created an HTML form with the following input fields: <input type="text" /> <input type="password" /> <input type="checkbox" /> I wish to style all input fields that a ...

Is it possible to use JavaScript to upload and manipulate multiple HTML files, replacing text within them to create new output HTML pages?

Is it possible to modify the following code to accept multiple files, process them, and then return the output? <html> <head> </head> <body> <div> <label for="text">Choose file</label> <inp ...

Understanding image sizes for uploads on Tumblr can be a bit confusing, especially when comparing pages to posts. Learn how to implement lazyloading for post

I'm currently working on a highly customized Tumblr account that features a mix of pages and posts. I am looking to access content and assets, particularly images, from these pages/posts for use in other parts of the site. When I upload an image to a ...

A guide on utilizing Node.js and Express to read a text file and convert it into an HTML table

As a Node.js developer using Express, I am delving into the realm of Javascript for the first time. My goal is to parse a text file and extract specific data from it to display in an HTML table. Currently, I can successfully open and read the file using fs ...

How can we prevent a div from displaying if no image is pulled in using Custom Field? Looking for a solution in WordPress, PHP, and CSS

Here is the code snippet I am currently using: <div class="banner"> <?php if(get_post_meta($post->ID, 'banner', true)) : ?> <img src="<?php echo get_post_meta($post->ID, 'banner', true); ?>" /> <?php el ...

Is it feasible to utilize Sublime Editor for building SCSS/SASS files? Learn how to compile SCSS/SASS with Sublime Editor

Despite numerous attempts, I have been unable to figure out how to effectively use the Sublime Editor for creating scss/sass files. ...

When a checkbox is clicked, how can we use Angular 4 to automatically mark all preceding checkboxes as checked?

I have a series of checkboxes with labels such as (Beginner, Intermediate, Advanced, Expert, etc.). When I click on any checkbox, I want all previous checkboxes to be checked. For example: If I click on Advanced, only the preceding checkboxes should get ...

Connect my php search outcomes to a different webpage

Hello, I am new to the world of php and I have written a search result function that allows users to click on items. However, I am facing an issue with making a specific search item clickable to open in another page with full details. Below is the code I h ...

Having an excessive number of select2 elements (more than 50) on a single screen with identical dropdown values can significantly slow down the page load time

Our screen features a dynamic table where users can add new rows. Each row includes a select2 element, and the table could potentially grow to 50 or more rows. With up to 1000 options in the select2 dropdown, this has been causing slow page loading times. ...