Arranging Text and Images in HTML/CSS to Ensure Optimal Placement When the Window Size Changes

Hello fellow coders! I've recently started diving into the world of website development and I have a query regarding positioning elements and handling window resizing. Can anyone help me out?

I'm trying to center an image, a message, and a password input on my webpage. While it looks good on my screen, in the code snippet below, there seems to be no space below the password line (you may need to scroll around to see anything), so clearly something is amiss...it's supposed to be perfectly centered on the screen.

However, I feel like I'm not efficiently positioning my elements. I also want to ensure that when the user shrinks their window, scroll bars appear and any empty space disappears (similar to Twitter's login page: https://twitter.com/login). Unfortunately, in my current code, shrinking the window still leaves empty space which I believe is not ideal for UI.

In essence, if anyone has tips on how to effectively position text and images, as well as utilize CSS to handle window resizing gracefully, I would greatly appreciate the guidance :) Any help is welcome and thank you in advance!

.pretty {
  text-align: center;
  color: #00008b;
  font-family: Lucida Console;
  position: absolute;
  top: 115%;
  left: 39%;
}

.pwd {
  text-align: center;
  position: absolute;
  top: 155%;
  left: 38.5%;
}

.chatbubble {
  height: 14%;
  width: 6%;
  position: absolute;
  top: 102%;
  left: 48.5%;
}

body {
  background-color: #fff0f5;
}

#wrapper {
  margin-left: auto;
  margin-right: auto;
  width: 960px;
}

.contain {
  position: relative;
  height: 200px;
}

html {
  height: 100%;
}
<!DOCTYPE html>
<html>
<div id="wrapper">

  <head>
    <title> Log In </title>

    <link href="loginstyle.css" type="text/css" rel="stylesheet">

    <script language="JavaScript">
      function showPass(form, e) {
        e.preventDefault();

        var pass = form.pwd.value;
        var pass1 = "webdesign"

        if (pass == pass1) {
          window.location.href = 'https://www.google.com'
        } else {
          window.location.href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
        }
      }
    </script>

  </head>

  <body>
    <div class="contain">
      <img class="chatbubble" src="chatbubble.png" alt="Chat Bubble" />
      <p class="pretty center">Welcome to a <br/> digital photobook. <br/> Please enter the password to continue: <br/> </p>

      <form class="pwd center" onsubmit="showPass(this, event)">
        Password: <input type="password" name="pwd" />
        <input type="submit" value="Log In" />
      </form>
    </div>
  </body>
</div>

</html>

Answer №1

It is important to note that the wrapper should be positioned inside the body tag, not around the body and head tags:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
    <!-- Content, including wrappers, go here -->
</body>
</html>

Avoid using absolute positioning for responsive web design unless absolutely necessary. There are various ways to center content both horizontally and vertically, with resources available on Stack Overflow. One modern approach is utilizing flexbox.

Instead of setting a fixed width, consider using max-width for your wrapper to prevent scrolling on smaller devices.

An effective strategy for Responsive Web Design (RWD) involves starting with minimal CSS styles geared towards mobile views. Most elements will naturally adjust, but ensure images are styled responsively. As you scale up to larger screens, introduce media queries to reposition elements and adjust font sizes accordingl. Embracing a "mobile first" strategy can also aid in this process.

html {
  height: 100%;
}

body {
  min-height: 100%;
  background-color: #fff;
  margin: 0;
  padding: 0;
}

#wrapper {
  margin: 0 auto;
  padding: 1rem;
  max-width: 960px;
  background-color: #fff0f5;
}

/* login section */

.login-section {
  min-height: 100vh;
  display: flex;
  flex-flow: column wrap;
  justify-content: center;
  align-items: center;
  text-align: center;
}

/* typography */

.pretty {
  color: #00008b;
  font-family: Lucida Console;
}
<!DOCTYPE html>
<html>
<head>
  <title> Log In </title>
  <link href="loginstyle.css" type="text/css" rel="stylesheet">
  <script language="JavaScript">
    function showPass(form, e) {
      e.preventDefault();
      var pass = form.pwd.value;
      var pass1 = "webdesign"
      if (pass == pass1) {
        window.location.href = 'https://www.google.com'
      } else {
        window.location.href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
      }
    }
  </script>
</head>

<body>
  <div id="wrapper">
  
    <main>
      <section class="login-section">
        <img class="chatbubble" src="https://via.placeholder.com/150" alt="Chat Bubble" />
        <p class="pretty">Welcome to a digital photobook.<br>Please enter the password to continue:</p>
        <form class="pwd" onsubmit="showPass(this, event)">
          Password: <input type="password" name="pwd" />
          <input type="submit" value="Log In" />
        </form>
      </section>
    </main>
    
  </div>
</body>
</html>

Answer №2

Avoid using manual percentage positioning, such as left: 155%. For centering elements, utilize absolute positioning on a wrapper container and then align the content inside with text-align: center. To center containers or images, apply the display: inline-block property so they flow to the center.

Below is an updated version of your CSS code, which you can also view in the snippet:

.contain {
   position: absolute;
   top: 50%;
   left: 50%;
   text-align: center;
   -webkit-transform: translate(-50%, -50%);
   -moz-transform: translate(-50%, -50%);
   transform: translate(-50%, -50%);
}

.pretty {
  text-align: center;
  color: #00008b;
  font-family: Lucida Console;
}

.pwd {
  text-align: center;
}

.chatbubble {
    display: inline-block;
}

body {
  background-color: #fff0f5;
}

html {
  height: 100%;
}
<!DOCTYPE html>
<html>
<div id="wrapper">

  <head>
    <title> Log In </title>

    <link href="loginstyle.css" type="text/css" rel="stylesheet">

    <script language="JavaScript">
      function showPass(form, e) {
        e.preventDefault();

        var pass = form.pwd.value;
        var pass1 = "webdesign"

        if (pass == pass1) {
          window.location.href = 'https://www.google.com'
        } else {
          window.location.href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
        }
      }
    </script>

  </head>

  <body>
    <div class=“contain”>
      <img class="chatbubble" src="chatbubble.png" alt="Chat Bubble" />
      <p class="pretty center">Welcome to a <br/> digital photobook. <br/> Please enter the password to continue: <br/> </p>

      <form class="pwd center" onsubmit="showPass(this, event)">
        Password: <input type="password" name="pwd" />
        <input type="submit" value="Log In" />
      </form>
    </div>
  </body>
</div>

</html>

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 steps can I take to avoid encountering this endless loop?

When passing foo in the arguments of useEffect to use existing array values, it causes an infinite loop because setting new values triggers the f() function again. How can this be resolved? An example of imaginary code is: const [foo, setFoo] = useState&l ...

Adjust the position of a child element to be just outside the boundaries of its parent element using CSS

I am facing an issue with 2 nested divs in my HTML code. The design requires the inner div to appear "on top of" the outer div, like this: (source: examplewebsite.com) Although I have applied CSS to both elements, including a negative top margin on t ...

Steps for aligning an image to the right using Bootstrap Vue

I'm currently learning Bootstrap Vue and I'm trying to align an image on the right using a b-card header. The challenge I'm facing is that when I add the image, it disrupts the perfect size of the template header, causing the image to show a ...

Using jQuery to toggle sliding the information above a div

I am facing an issue with my customized sliding menu. The menu slides over the image but not over the content-div, pushing it aside. I have tried to fix this problem but haven't found a solution yet. My goal is for the menu to slide over all divs and ...

Combining default and named exports in Rollup configuration

Currently, I am in the process of developing a Bluetooth library for Node.js which will be utilizing TypeScript and Rollup. My goal is to allow users to import components from my library in various ways. import Sblendid from "@sblendid/sblendid"; import S ...

Chrome clipping positioned spans

Having trouble positioning a label above inline sections with spans in Chrome, as the labels are getting clipped oddly. Check out these screenshots: Firefox view: Chrome view: In the Chrome screenshot, you can see that the labels are being cut off based ...

Leveraging CSS or JavaScript for Displaying or Concealing Vacant Content Sections

I'm working on developing a page template that utilizes section headers and dynamically pulled content from a separate database. The current setup of the page resembles the following structure: <tr> <td> <h3> ...

Position a div in the center and add color to one side of the space

I am seeking a way to create a centered div with the left side filled with color, as shown in examples. I have devised two solutions without using flexbox, but both seem somewhat like hacks. body { margin: 0; padding: 0; } .header { width: ...

sending the properties from the menu component to the dish details

Having trouble with a react.js app I'm working on that involves rendering dish cards. The dish object is always null when passed as props from MenuComponent to DishDetail, resulting in nothing being displayed on the screen. Can someone please assist m ...

Angular Datatables unable to locate DataTables with Webpack Encore configuration

As I've been updating a Symfony application from version 3.4 to 4.2.2, everything has been going smoothly until I encountered an issue with getting DataTables to work through yarn installation and webpack encore using angular-datatables. The Setup Pr ...

Generate a fresh line within the source code

Currently, I am facing a challenge with dynamically loading CSS, JS, and other components as it appears messy when viewed from the source. Although this issue does not impact functionality, I am not satisfied with how it looks in the source code. When exam ...

"Utilizing Node.js and Express to return JSONP data based on

For some reason, my Express application is giving me a strange result when using res.jsonp. It looks like this: /**/ typeof jsonp1406719695757 === 'function' && jsonp1406719695757({"published":true,"can_add_to_cart":true,"updated_at":"20 ...

Trouble with the navigation of the JavaScript image gallery: next and previous buttons are not

Is it possible to have multiple galleries on a single page with captions? I've been trying to incorporate this JavaScript code, but struggling to make the next/previous links function for both galleries. Since I'm new to JavaScript, any advice or ...

Creating dynamic content in the Ajax-enabled Smart Admin theme: A step-by-step guide

As I work on developing an application for a client, my focus is on creating a web service using Laravel5 for the backend. To enhance the user interface of this web service, I have chosen to incorporate the Smart Admin Theme, specifically utilizing the Aja ...

Adjust the hue of a specific node

Is there a way to customize the color of an individual node in an eChart treemap? I attempted to set a color property for the node, but it didn't seem to work. While I was able to change the label's color, I'm interested in changing the back ...

Using angularJS and Regular Expressions to eliminate the "+" symbols in an email address

I have been given the task of disallowing email addresses with a "+" sign from registering. As someone new to regex, my understanding is that it is better to focus on defining what constitutes a valid input rather than listing what should be excluded. I re ...

"encountered net::ERR_NAME_NOT_RESOLVED error when trying to upload image to s3 storage

I am currently developing an application using Angular. I have been attempting to upload a picture to my S3 bucket, but each time I try, I encounter this error in the console. https://i.stack.imgur.com/qn3AD.png Below is the code snippet from my upload.s ...

What is the reason behind Angular's continued use of JSON for encoding requests? (specifically in $http and $httpParamSerializerJ

Is there a way to set Angular to automatically send requests as x-www-form-urlencoded instead of JSON by default? Angular version 1.4.5 I've tried the following code snippet but it doesn't seem to work: angular.module('APP').config( ...

Arranging two divs side by side while considering a responsive theme

My struggle with aligning divs side by side continues. After searching through online forums for hours, I have yet to find a solution. The goal is to create a collage using six images. However, currently all the images stack vertically on the left side on ...

What is the best way to upload a file using AJAX request in Sinatra?

I am attempting to upload a file using a jQuery AJAX function with a Ruby-Sinatra function. Here is the code snippet: <form id="ucform" method="post" action="#" enctype="multipart/form-data"> <input type="file" id="cfile" name="cfile" onchange= ...