How to perfectly center a text box in HTML

UPDATE: I have included the relevant JavaScript code. Essentially, a text box appears when a checkbox is clicked, but I want the text box to be centered on the page.

I am attempting to center align a text box on the page, but I am struggling to achieve this. Here is my current code, which only centers the text inside the text box itself.

.selecttier input {
    text-align: center;
}
<div class="selecttier">
    <h1>4. Select Tier</h1>
    <input type="checkbox" class="<1000clicks" name="basictier" value="0.20" id="basictier" onclick="TierFunction()" >0 - 1,000 clicks<br>
    <input id="basictiertextbox" type="text" value="0.00" data-total="0" style="display:none;" />
    <input type="checkbox" class="1000-5000clicks" name="standardtier" value="0.30">1,000 - 5,000 clicks<br>
    <input type="checkbox" class=">5000clicks" name="premiumtier" value="0.40">5,000+ clicks<br>
</div>

JavaScript:

function TierFunction() {
  // Get the checkbox
  var checkBox = document.getElementById("basictier");
  // Get the output text
  var text = document.getElementById("basictiertextbox");

  // If the checkbox is checked, display the output text
  if (checkBox.checked == true) {
    text.style.display = 'block';
  } else {
    text.style.display = 'none';
  }
}

Answer №1

There seems to be some confusion about whether the intention is to center the .selecttier element on the page or justify its contents.

To center a block, consider using flexbox:

.wrapper {
  display: flex;
  justify-content:center;
}
<div class="wrapper">
  <div class="selecttier">
    <h1>4. Select Tier</h1>
    <input type="checkbox" class="0-1000clicks" name="basictier" value="0.20" id="basictier" onclick="TierFunction()">0 - 1,000 clicks<br>
    <input id="basictiertextbox" type="text" value="0.00" data-total="0" style="display:none;" />
    <input type="checkbox" class="1000-5000clicks" name="standardtier" value="0.30">1,000 - 5,000 clicks<br>
    <input type="checkbox" class="5000clicks" name="premiumtier" value="0.40">5,000+ clicks<br>
  </div>
</div>

If you want to justify the children within the element, you can use text-align:

.selecttier {
text-align:center;
}
  <div class="selecttier">
    <h1>4. Select Tier</h1>
    <input type="checkbox" class="0-1000clicks" name="basictier" value="0.20" id="basictier" onclick="TierFunction()">0 - 1,000 clicks<br>
    <input id="basictiertextbox" type="text" value="0.00" data-total="0" style="display:none;" />
    <input type="checkbox" class="1000-5000clicks" name="standardtier" value="0.30">1,000 - 5,000 clicks<br>
    <input type="checkbox" class="5000clicks" name="premiumtier" value="0.40">5,000+ clicks<br>
  </div>

Answer №2

To center the content in the .selecttier class, simply add text-align: center;

.selecttier {
    text-align: center;
}


<div class="selecttier">
  <h1>4. Select Tier</h1>
  <input type="checkbox" class="<1000clicks" name="basictier" value="0.20" id="basictier" onclick="TierFunction()" />0 - 1,000 clicks<br>
  <input id="basictiertextbox" type="text" value="0.00" data-total="0" style="display:none;" />
  <input type="checkbox" class="1000-5000clicks" name="standardtier" value="0.30">1,000 - 5,000 clicks<br>
  <input type="checkbox" class=">5000clicks" name="premiumtier" value="0.40">5,000+ clicks<br>
</div>

Answer №3

If you want to achieve this layout easily, using flexbox is the way to go. Give this code a try after I have made some edits to your HTML structure:

.container{
    width:100%;
}
.selecttier > h1{
  text-align:center;
}
.selecttier > div{
  width:100%;
  position:relative;
  display:flex;
  justify-content:center;
}
.selecttier > div > div{
  display:flex;
  flex-direction:column;
}
.selecttier > div > div > div{
  margin-bottom:5px;
}
.selecttier > div > div > div input{
  margin-right:10px;
}
<div class="container">
  <div class="selecttier">
    <h1>4. Select Tier</h1>
    <div>
      <div>
        <div>
          <input type="checkbox" class="<1000clicks" name="basictier" value="0.20" id="basictier" onclick="TierFunction()" >0 - 1,000 clicks
        </div>
        <div>
          <input id="basictiertextbox" type="text" value="0.00" data-total="0" style="display:none;" />
          <input type="checkbox" class="1000-5000clicks" name="standardtier" value="0.30">1,000 - 5,000 clicks
        </div>
        <div>
          <input type="checkbox" class=">5000clicks" name="premiumtier" value="0.40">5,000+ clicks   
        </div>
      </div>   
    </div>

  </div>
</div>

Answer №4

To rectify the issue, try adding this code snippet:

#basictiertextbox{
display: block;
margin: 0px auto;
}
<div class="selecttier">
    <h1>4. Select Tier</h1>
    <input type="checkbox" class="<1000clicks" name="basictier" value="0.20" id="basictier" onclick="TierFunction()" >0 - 1,000 clicks<br>
    <input id="basictiertextbox" type="text" value="0.00" data-total="0" />
    <input type="checkbox" class="1000-5000clicks" name="standardtier" value="0.30">1,000 - 5,000 clicks<br>
    <input type="checkbox" class=">5000clicks" name="premiumtier" value="0.40">5,000+ clicks<br>
</div>

#basictiertextbox{
    display: block;
    margin: 0px auto;
}

Answer №5

To visualize how the code operates, you can execute the snippet. The text is now positioned in the middle of the screen.

.selecttier h1{
  text-align: center;
 }
 .selecttier input {
    text-align: center;
   
}
  <div class="selecttier">
      <h1>4. Select Tier</h1>
      <input type="checkbox" class="<1000clicks" name="basictier" value="0.20" id="basictier" onclick="TierFunction()" >0 - 1,000 clicks<br>
      <input id="basictiertextbox" type="text" value="0.00" data-total="0" style="display:none;" />
      <input type="checkbox" class="1000-5000clicks" name="standardtier" value="0.30">1,000 - 5,000 clicks<br>
      <input type="checkbox" class=">5000clicks" name="premiumtier" value="0.40">5,000+ clicks<br>
  </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

Parallel Bootstrap vertical grid layout

Struggling to articulate my objective, but bear with me. I am in the process of creating two bootstrap grids to house expanding text boxes. Refer to the image below for a visual representation; Desired Outcome Despite my efforts, the output from my code ...

Is it possible to achieve partial text stylization in Vue using Nuxt.js?

We're currently developing a Vue application Is there a way to style text partially, similar to this example? Although creating exact constraints from ellipses may not be possible, the functionality could still be achieved procedurally. ...

Is Bootstrap revolutionizing the world of flex-box layouts?

I'm currently in the process of creating a Simon game, and everything is going smoothly until I introduce Bootstrap into the mix. Suddenly, my "Simon board" starts acting strangely. Here's the troublesome code: HTML: <div class="simon"> ...

How can I force an element to overflow without being affected by its parent's overflow style in CSS/HTML/JS, even if the parent is

I've come across many inquiries on this subject, but the proposed solutions never seem to work when dealing with ancestors that have absolute positioning. Take this example: <div id='page'> <div id='container' style= ...

Is it deemed as an anti-pattern to establish directives for styling?

Currently, I am in the midst of developing a specialized component UI library for a specific project I'm involved in. This library will consist of unique stylized components with elements that are reused throughout. As I work on this project, I find ...

Tips for changing the size and color of SVG images in a NextJS application

Looking to customize the color and size of an svg image named "headset.svg". Prior to this, I used next/image: <Image src={'/headset.svg'} alt='logo' width={30} height={30} className='object-contain' /> The s ...

Is there a way to verify the location of an element?

Currently, I am attempting to monitor the position of the 'car'. When the car reaches a top offset of 200px, I would like to apply a specific property to the 'phone' element. The concept is that as you scroll down, the car will move, an ...

One common query is how to access an HTML element from JavaScript using its ID, particularly when the element belongs to a different HTML document

Following up on the question title: In addition, the HTML document is loaded into the parent element using AJAX in the following way: $.ajax({ url: 'calender.aspx', cache: false, dataType: "html", success: functi ...

Overflowing Bootstrap navbar problem

After adjusting the navbar height, the overflowing issue was resolved, but I specifically want it to be set at 5rem. Currently, the content of the navbar exceeds the 5rem height and mixes with the main body content, regardless of what modifications I make. ...

Restricting the height of a column in Bootstrap 4 to create a more compact design

Currently, I have an element that contains a few columns. The issue is that all columns end up being the same height as the tallest one. What I want is to set a minimum height for one specific column, #colB, and have the other two columns scroll vertically ...

Twitter Bootstrap Dropdown PHP link fails to function

Recently, I designed a dropdown menu to be used on a PHP page. The main button, which contains the dropdown links, needs to still function as a link to another page. Despite following all the instructions provided on the Bootstrap website, the main button ...

How can a div be utilized to create a footer with two columns?

Is there a way to design a two-column footer using only divs and no tables? The dimensions of the footer are set at 980px and it needs to include... Copyright 2010 xyz.com (to be placed on the left side) About us | privacy | terms (to be placed on the r ...

Can someone explain this lengthy string of CSS coding?

While working on a framework file, I stumbled upon an extensive CSS string that I have never encountered before. html body .ow_button:hover .ow_ic_attach{background-image: url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width% ...

Angular error: Unable to access the 'toLowerCase' property of an undefined value

I've been working on creating my own custom filter pipe by following the instructions in this video tutorial, but I encountered an error message stating, "Angular, TypeError: Cannot read property 'toLowerCase' of undefined". I have already i ...

"Error occurred: Unable to execute bgColor as a function" on HTML select onchange event

I am having an issue with my HTML switch that has an onchange tag triggering the JavaScript function bgColor with the argument this. However, every time I attempt to use this, I receive an error message: Uncaught TypeError: bgColor is not a function. Can a ...

Ensure the main page occupies the entire screen without the need for scrolling

Can someone help me with my Vue app? I need to figure out how to make the start page full screen without any scroll bars in the main window at any scale. The scrollbar should be located within "div class="bottom-center". <script setup> import Comp fr ...

Tips for relocating anchor elements to less desirable locations

My website has an issue with anchor elements appearing too frequently and not in the desired location. I need someone to help fix this so there are only two anchors displayed. After checking my code, it seems like there are not more than the specified num ...

Where is the appropriate location to insert a <script> tag in NextJs?

I am facing a challenge in my NextJs application when trying to include the <script> code. I attempted to add it in a .js file but it did not work as expected. In traditional React applications, we typically use the index.html file to incorporate sc ...

Adjust the position and size of an image by hovering over it with your

I'm currently working on implementing a button menu for my website, but I'm facing an issue with the hover effect on images. You can check out what I've done so far at http://jsfiddle.net/tNLUx/ What I want to achieve is when hovering over ...

Looking to conceal an <hr> element using jQuery's slideToggle function and then reveal it when clicked once more

I recently embarked on my journey to learn jQuery. I encountered a challenge with a simple button that triggers the slideToggle function to show/hide a div. Directly above this toggled div, there is an break line which I want to hide when the button is in ...