Position the input element in the middle of the div

Is it possible to center a form input element within a div element without it changing position when the browser window size is adjusted? I attempted using margin: auto and position: relative, but encountered issues with the element moving. How can this be resolved while still achieving the desired center alignment?

View Screenshot

#stretch5 {
  display: flex;
  justify-content: center; 
  align-items: center;
  height: 30em;
  background-image: url(img/newsletter.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: 50% 50%;
}
.frame {
  position: relative;
  display: inline-block;
  height: 25em;
  top: 50px;
  bottom: 100px;
  margin: auto;
  width: 100%;
  border: 5px solid #fff;
  margin-left: auto;
  margin-right: auto;
  transform: scale(0.8975, 0.8975) translate(-68.5808px, -31.2925px);;
}
.frame h2 {
  font-size: 1.4em;
}
.frame input[type=submit] {
  padding: 5px 15px;
  background: #f7e0cd;
  text-transform: uppercase;
  border: 0 none;
  display: inline-block;
  margin: auto;
  outline: 0;
  cursor: pointer;
  width: 2em;  
}
<div id="stretch5" class="newsletter-opt-in"> 
    <div class="frame">
      <h2>Join our<span class="vip">VIP list</span></h2>
      <p class="vip-list-paragraph">For exclusive tips, free resources and the best insights</p>
      <form action="#">
        <input type="submit" value="SUBSCRIBE">
      </form>
    </div>
</div>

Answer №1

Make a change in the .frame input section to set the element's display property to flex;. This will ensure that the button remains centered on the page even when the page is resized.

.frame input[type=submit] {
  padding: 5px 15px;
  background: #f7e0cd;
  text-transform: uppercase;
  border: 0 none;
  display: flex;
  margin: auto;
  outline: 0;
  cursor: pointer;
  width: 2em;  
}

Hopefully, this solution resolves your issue.

Answer №2

To center the form, simply add text-align:center to the form.

#stretch5 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 30em;
  background-image: url(img/newsletter.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: 50% 50%;
}

.frame {
  height: 25em;
  border: 5px solid #fff;
  border: 1px solid red;
  display: flex;
  flex-direction: column;
}

.frame h2 {
  font-size: 1.4em;
}

form {
  text-align: center;
}

.frame input[type=submit] {
  padding: 5px 15px;
  background: #f7e0cd;
  text-transform: uppercase;
  outline: 0;
  cursor: pointer;
}
<div id="stretch5" class="newsletter-opt-in">
  <div class="frame">
    <h2>Join our<span class="vip">VIP list</span></h2>
    <p class="vip-list-paragraph">For the exclusive tips, free resources and best tips</p>
    <form action="#">
      <input type="submit" value="SUBSCRIBE">
    </form>
  </div>
</div>

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

Unraveling the intricacies of Wordpress, PHP, and HTML

What is the purpose of the post, ID, and other elements within the article tag? I expected the post_class to be defined in my CSS, but I cannot locate it. If I remove the entire code block from the <article>, my website layout breaks. However, remov ...

Tips for creating a consistently positioned div element, regardless of the screen size

Having trouble positioning two bars with green or blue filling in the bottom right corner? They keep moving around and not staying in place regardless of screen size. Check out how they are off-screen here Below is the CSS-Styling + HTML code: .eleme ...

Is it possible for HTML to provide alternative text for unique characters, such as accented characters?

I have a vision for HTML support that may incorporate the following: <span alt="Antonin Dvorak">Anton&iacute;n Dvo&#345;&aacute;k</span> In this scenario, if a browser cannot display special characters, it would default to showing ...

Is there a way to click on a button and have its background color change randomly each time?

I am facing an issue with a button on my HTML page. When the button is clicked, I want its background color to change to a random different color. However, despite trying various sources, I am unable to get it right. It seems like I am not placing the code ...

Ensure that the browser is instructed to utilize a designated Accept field within the HTTP request

Can a web browser be directed to use a specific mime-type in an HTML request? For example, can we achieve this with the following code: <a type="application/pdf" href="https://url.com/">PDF</a> or <a type="text/html ...

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 ...

Maintaining Image Aspect Ratio within a Div with a Specified %width

Apologies for the repeated question, but my research hasn't yielded a solution I can apply. Inside a div with the specified styles, there is an image: <div class="thumb grid_6"> <img src="img/test2.jpg" alt="flavin" /> </div> . ...

Aligning Text at the Center in Your React Native Application

I'm facing a few issues with my visualization: Despite aligning it to the center, the text on my first button is positioned at the right end of the button. How can I move the text 'login' to the center? Currently, the 'Sign Up Her ...

Creating dynamic templates for table rows in AngularJS directives

Is it possible to dynamically load an AngularJS Directive templateUrl while working within a table? In my scenario, I have the following HTML structure where I am repeating a tr element with a fw-rule directive: <tbody> <tr ng-repeat="rule in ...

In an AJAX response, the button will be disabled if any checkboxes are left unchecked, regardless of their group

This text has been inspired by a thread on Stack Overflow regarding disabling a button based on checkbox selection In the original post, the button is disabled unless at least one checkbox is checked. In my scenario, I have two sets of checkboxes: <d ...

Distributing information to modules made of Polymer

Is there a way to create a single source for all data that my elements can utilize but not change (one-way data-binding)? How can I achieve this without using a behavior? I attempted the following in my code: <script type="text/javascript" src="/data. ...

specific css styles only for Safari and Internet Explorer

Imagine a scenario where I have a div with the class 'x': <div class = 'x' /> This div has some CSS properties and what's interesting is that it appears differently in Safari and the latest version of IE compared to other ...

What causes the discrepancy between a website's source code and the code viewed in the browser inspection tool? (Regarding web scraping)

This is my first programming project and I apologize in advance for any mistakes in terminology. My Objective: I am currently working on a project to scrape data from my local library's website. My main goal is to automate the process of renewing bo ...

Displaying varied information based on JSON feedback using AngularJS

I am currently in the process of developing an app using AngularJS and the Ionic framework. The app will display a list of data with various subcategories for each item, depending on the account type. View sample image here The issue I am facing is that ...

How to center align a fixed div position

I am looking for a way to center align a fixed position div on my page. In the past, I have been able to achieve this with absolutely positioned divs using a specific technique. left: 50%; width: 400px; margin-left: -200px; However, this method does no ...

Creating a dropdown menu using Vue.js

My latest project involves coding an html page that features a drop-down list using HTML, CSS, and VueJS. The goal is to have something displayed in the console when a specific option from the drop-down list is selected. Here's the snippet of my html ...

Adding CSS classes through the .addClass method - A guide on utilizing linked CSS files

I came across this code snippet and I have a quick question. $(window).scroll(function() { var y_scroll_pos = window.pageYOffset; var scroll_pos_test = 150; // adjust as needed if(y_scroll_pos > scroll_pos_test) { $( "#cssmenu" ).addCl ...

Transforming a Java Array or Collection into a JSON array

After running my Java code, the returned Collection (ArrayList) produces JSON through JAXB that looks like: {"todo":[{"name":"CAMPBELL","sales":"3","time":"1331662363931"}, {"name":"FRESNO","sales":"2","time":"1331662363931"}]} However, I am wondering if ...

Implementing a Slide animation in React

I am currently working on a carousel feature that includes two buttons, Next and Previous. When the user clicks on Next, I want the carousel to slide from left to right, and when they click on Previous, it should slide from right to left. One important not ...

Is there a way to show a progress bar that functions as the background for a table row

My table is structured as follows: th1 th2 th3 th4 th5 th6 td1 td2 td3 td4 td5 td6 td1 td2 td3 td4 td5 td6 I am looking to incorporate a feature where new rows are dynamically added using a form that triggers ...