The text alignment is set to center, however, the wrapper, input boxes, and button are not aligning themselves in the center. The logo

Within this webpage, there exists a logo image file with a specified height to ensure proper fit. The logo positions itself beautifully within its designated div, with the wrapper div containing text-align: center, resulting in perfect alignment. Additionally, there are two text input boxes accompanied by a search button, enclosed in a separate div from the logo's section. However, upon page load, this particular div appears on the left side of the screen.

If I were to eliminate the searchArea div and solely wrap the button and input boxes within the main wrapper, everything aligns perfectly but becomes distorted due to width properties being affected.

The challenge at hand is how to centrally position all elements contained within the searchArea div beneath the logo without compromising the defined styles for the searchArea class???

HTML:

<div id="logo" class="centerWrapper">
    <img id="logo" class="logo" src="img/TestAPICropped.png" alt="logo">
</div>

<div id="search" class="centerWrapper">
    <div id="searchArea" class="searchArea">
        <input type="text" class="inputBox" name="charName" placeholder="Character Name"><br>
        <input type="text" class="inputBox" name="realmName" placeholder="Realm Name"><br>
        <button type="button" class="searchButton">Search</button>
    </div>
</div>

CSS:

.centerWrapper {
    text-align: center;
}
.logo {
    height: 46px;
}
.searchArea{
    margin-top: 0.8%;
    height: 125px;
    width: 340px;
}
input.inputBox {
    background-color: #0B122F;
    border-color: #877055;
    color: #4A4E5A;
    margin: 5px 0px;
    padding: 5px 10px 5px 10px;
    width: 72%;
}

Answer №1

If you want to center block-level elements like block-level elements such as <div>, you need to apply the margin-left and marign-right properties, both set to auto.

Although the shorthand margin: 0 auto is commonly used for this purpose, in your case where there's a margin-top of 0.8%, you must manually specify both for your .searchArea selector.

You can implement this through:

.centerWrapper {
  text-align: center;
}

.logo {
  height: 46px;
}

.searchArea {
  margin-top: 0.8%;
  height: 125px;
  width: 340px;
  margin-left: auto;
  margin-right: auto;
}

input.inputBox {
  background-color: #0B122F;
  border-color: #877055;
  color: #4A4E5A;
  margin: 5px 0px;
  padding: 5px 10px 5px 10px;
  width: 72%;
}
<div id="logo" class="centerWrapper">
  <img id="logo" class="logo" src="img/TestAPICropped.png" alt="logo">
</div>

<div id="search" class="centerWrapper">
  <div id="searchArea" class="searchArea">
    <input type="text" class="inputBox" name="charName" placeholder="Character Name"><br>
    <input type="text" class="inputBox" name="realmName" placeholder="Realm Name"><br>
    <button type="button" class="searchButton">Search</button>
  </div>
</div>

I hope this explanation proves useful! :)

Answer №2

If you want to center block elements horizontally, remember to use margin: 0 auto; However, don't forget about the margin-top property as well, which should be set to 0.8%; You can combine these two like this:

.searchArea {
    margin: 0.8% auto 0;
}

Best of luck!)

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

I require the flexbox children to be arranged in a regular manner

How can we set flexbox children to default layout behaviors? Let's make them behave like the disobedient kids they are. .wrapper { This should be a row } .column { Make all heights equal display: flex; } .child { Back to regular display...wit ...

Steps for displaying a website within a specific Div using HTML

I'm trying to set up a website to open within a specific <div> tag, like the example shown in this link: Responsive. Can anyone spot what I'm doing incorrectly? <html> <head> <script> function mobile320() { ...

Show the user's username after they have successfully signed in

Hello, I am trying to display the username after a user logs in. Here is the code for the page where I would like to show it: index.php <?php require_once 'classes/Membership.php'; $membership = New Membership(); $membership->confirm_Me ...

The never-ending delay of an infinite fade in and out CSS3 animation

I am currently working on the following: Fiddle Code This is the HTML structure being used: <div id="animation"> <ul> <li>this is</li> <li>CSS3 looped</li> <li>animation</li> </ul> &l ...

Creating an image slider that pauses on mouse hover

Here is a code snippet that I'd like to share <body> <div id="slideshow"> <div> <img src="assets/images/home-banner.jpg" width="995" height="421" alt=""/> </div> <div&g ...

Adding javascript code to HTML using Express

Currently, I am in the process of building a web application utilizing the Express framework to create Impress.js presentations and implementing a visual editor for them. I have configured the app.js to only fetch the .html file, but my objective is to inc ...

invoking a JavaScript function from an HTML file

I want to create a JavaScript server on Raspbian that not only serves .html content to the browser but also allows for user input through events like button clicks. Both my .html and .js files are located in the same directory, and I have used absolute pat ...

Hide the parent div element if the nested unordered list does not contain any items

I transformed an HTML template into a typo3 template using automaketemplate. Within the HTML code, there is a structure of divs like this <div class="bigPostItWrap">1 <div class="postit">2 <div class="p ...

Mixing strings with missing slashes in links

console.log(mylink) When using the console to log mylink, the expected result is a normal link like http://example.com/something.jpg. I tried concatenating it as shown below: var html = '<div id="head" style="background:linear-gradient(rgba(0, 0 ...

Is there a way to automatically activate the "Add" button when I hit the enter key in the text box?

Being someone who relies on to-do lists, I implemented a system where you can input tasks into a textbox and click the add button to see them listed below. However, I found it cumbersome to keep clicking the add button every time I wanted to quickly add mu ...

separating kids with selectors

What is the recommended method for adding borders between children of various elements within a div? In the example below, there should be borders between p,div and div,img. <div id="list"> <p>child 1</p> <div>child 2</ ...

HTML element resizing unexpectedly due to browser interactions

I recently created a sleek HTML transparent header bar using the following CSS: #head-bar{ position:fixed; top: 0px; width:100%; left:0px; min-height:25%; z-index:2; background-color:#000; background: rgba(0, 0, 0, 0.5); } ...

Can the appearance of the Chrome browser be altered to resemble a printed page?

Simply put, I'm tackling a massive project right now. While my print.css is functioning perfectly, the task of constantly previewing the print page has become incredibly frustrating. What's more, it's impossible to inspect the page effectiv ...

The jQuery method serializeArray does not include the values of HTML textareas and checkboxes

Hello, I'm looking to send data to the server from an HTML form. The server I'm using is CodeIgniter and I'm utilizing the AJAX method. Here's the HTML form: <div id="fileupload"> <form id="upload-media-form" class="uploa ...

Show links in the sidebar menu when the primary navigation links are hovered over

I am trying to create a dynamic side menu that displays different links depending on which link is hovered over in the top navigation bar. Here is what I have so far: HTML: <ul class="nav navbar-nav"> <li class="nav"><a class="toggle" hr ...

Limit Content to be contained within a Bootstrap Container/Div

How can I shrink the content inside a container without distorting images? When the screen size decreases, all the images and content inside start to lose their alignment and sizes begin to change. I am aware that this may make the content harder to see. ...

The integration of jQuery in PHP is experiencing some technical challenges

There's one issue driving me crazy: I created a left-floating file tree view and included it in my website. However, when I include my website in the file treeview, it becomes unclickable in Chrome, and in IE 9, the thumbnail part for viewing PDF docu ...

AngularJS's support for html5mode on GitHub pages is now available

Is it feasible for GitHub pages to accommodate AngularJS in html5mode? I came across a source online suggesting that it can be done with a fallback page for 404 errors. However, this solution seems flawed as it may result in multiple 404 errors, which wou ...

The Youtube Subscribe Embed feature is causing my image to lose its corners

When I use the official embed code from Google Developers to embed my YouTube channel, it cuts off the corners of my image and leaves white corners. This doesn't look good on my website with a black background. <script src="https://apis.google ...

Interactive <li></li> element for news ticker

Hello there! I am currently retrieving JSON data from a Wordpress site and converting the titles into list items as shown below: <?php $get=file_get_contents("https://www.thekashmirmonitor.net/wp-json/wp/v2/posts?categories=192"); $var=json_decode($get ...