What causes the inner div to be perfectly aligned within the containing div while the button remains off-center?

https://i.sstatic.net/lw6tx.png

body {
  color: white;
  background-color: #1E1B1B;
  border-color: grey;
}

.nav-col {
  background-color: transparent;
  margin: 10px;
  padding: 10px;
  font-size: 30px;
  border: 1px solid grey;
  height: 700px;
  width: 150px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="nav-col">
  <div style="text-align:center"><i class="fa fa-home"></i></div>
  <button style="text-align:center;"><i class="fa fa-home"></i></button>
</div>

Answer №1

The div and the button are not aligned in the center.

Both the content of the div and the button are centered within their respective elements.

The div takes up the full width of its container (can be seen by setting a background color), while the button adjusts to the minimum required width.

body {
  color: white;
  background-color: #1E1B1B;
  border-color: grey;
}

.nav-col {
  background-color: transparent;
  margin: 10px;
  padding: 10px;
  font-size: 30px;
  border: 1px solid grey;
  height: 700px;
  width: 150px;
}

.nav-col > div {
  background: red;
}
<div class="nav-col">
  <div style="text-align:center">contents</div>
  <button style="text-align:center;">contents</button>
</div>

text-align only affects the position of inline content, not the element itself.

If you want the button to be centered, apply text-align to the button's parent container.

body {
  color: white;
  background-color: #1E1B1B;
  border-color: grey;
}

.nav-col {
  text-align: center;
  background-color: transparent;
  margin: 10px;
  padding: 10px;
  font-size: 30px;
  border: 1px solid grey;
  height: 700px;
  width: 150px;
}

.nav-col > div {
  background: red;
}
<div class="nav-col">
  <div style="text-align:center">contents</div>
  <button style="text-align:center;">contents</button>
</div>

Answer №2

The default width of the div element is 100%, while the button element's width is similar to width: fit-content (meaning it adjusts based on its content).

To center the button, you can use the following CSS code:

button {
      display: block;
      margin: 0 auto;
}

I recommend avoiding inline styles and instead, keeping your CSS in a separate stylesheet for better organization.

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

Adding CSS styles to an HTML page using JavaScript

Unfortunately, I do not have the ability to access the HTML directly as it is generated dynamically by a program. However, I do have access to the JS page that is linked to it. As an example, I am able to manipulate elements using JavaScript like so: ...

Widget for tracking attendance - incorporating HTML, CSS, and Bootstrap styling

I recently created a widget named attendance to track monthly data, from January to December. I spent time designing and coding it using HTML, CSS, and Bootstrap, as shown in the image below https://i.sstatic.net/FtFJv.png However, I am now faced with t ...

Interactive sidebar component with navigation and animated section markers

For weeks, I've been attempting to create a navigation sidebar similar to the ones shown in these images: Even though getbootstrap.com/components offers appealing navigation sidebars, I have not found a built-in component in their library. This has m ...

How do I effectively replace content with a banner or alert message for users experiencing issues with the website on Internet Explorer?

mysite seems to be struggling with displaying 3D content on Internet Explorer compared to Chrome. Is there a way I can replace the content with a banner or alert informing users that they will have a better experience using Chrome or other supported browse ...

Code snippet in Python using Selenium where Google Maps is not visible in the page source

Currently, I am attempting to extract GPS coordinates data from a property listing: https://www.primelocation.com/new-homes/details/55357965?search_identifier=d10d089a335cc707245fb6c924bafbd2 The specific GPS coordinates can be found on the "Map & Nearby" ...

Adding labels to a JavaScript chart can be done by using the appropriate methods

https://i.stack.imgur.com/uEgZg.png https://i.stack.imgur.com/y6Jg2.png Hey there! I recently created a chart using the Victory.js framework (check out image 1) and now I'm looking to incorporate labels similar to the ones shown in the second image ab ...

Concealing text in multi-select option of Bootstrap when overflowing horizontally

I am attempting to create a multi-select feature with only one horizontal scroll bar using bootstrap. However, I do not want each option to have its own horizontal scrollbar. When I choose the longest option (123456789012345678901234567890), only part of ...

Can CSS be altered dynamically in Laravel blade?

Is there a way to dynamically change CSS? I am trying to set the class=sheet padding-top: 28mm; when the size of $anArray is less than 30. If the array has more than 30 elements then apply padding-top: 28 * 2 mm;. Finally, if the array exceeds 60, use pad ...

What are the steps to reveal the second element once the first one has vanished?

Is there a way to delay the appearance of the second square until after the first square has disappeared? For example, I want the first square to appear after 3 seconds and then disappear, followed by the second square becoming visible after 11 seconds. ...

What is a way to require the user to choose only one checkbox using HTML?

I am currently constructing an HTML quiz where respondents must select checkboxes to answer. Here is a sample snippet that demonstrates this: <h3>Question 1:</h3> <input type="radio" name="answer1" id="c01" valu ...

What is the best way to send information using HTML attributes following a mapped array in React without relying on additional components?

I am struggling to pass data using an HTML attribute in my handleClick function without the need for another component. Unfortunately, I'm having trouble handling this - can anyone offer some assistance, please? const handleLiClickFirst = (airport) =& ...

Issue with alignment of divs causing them to stack in a strange manner instead of floating left and

Check it out in action right here: All I want is for the .news div boxes to gracefully float left and right using the handy cycle helper in Rails. However, the current output isn't quite what I had in mind: This is how I envision the layout should l ...

What is the best way for me to include a list of image links in multiple dynamically created divs using PHP?

After diving into the world of PHP, I recently discovered a way to automatically generate content for 40 divs in a product gallery using multiple arrays. Utilizing a for loop, each iteration not only retrieves information from the arrays but also construct ...

Tips on positioning an image at the center of an HTML page

When designing a website, I encountered an issue where only a portion of the image was displaying in the slider due to the fixed height of the div being set to 500. I want the full image to be displayed centered within the same height. <div id="myCar ...

Retrieving JSON data and transforming it into HTML

After reading several examples, I am still struggling with a particular issue. I am receiving a feed from an API that is structured like this: { total:123, id: "1234", results: [ { id: "1234", name: "bobs market", ...

Printing values of an object in an Angular/HTML script is proving to be quite challenging for me

In my Angular project, I have created a service and component for listing objects. The list is functioning correctly as I have tested it with 'console.log()'. However, when I try to display the list on localhost:4200, nothing appears. <table&g ...

extract the text content from an object

I am trying to add an object to the shopping cart. The item contains a key/value pair as shown in the following image: https://i.stack.imgur.com/5inwR.png Instead of adding the title with its innerText using p and style, I would like to find another ...

Effortlessly navigate between Formik Fields with automated tabbing

I have a component that validates a 4 digit phone code. It functions well and has a good appearance. However, I am struggling with the inability to autotab between numbers. Currently, I have to manually navigate to each input field and enter the number. Is ...

Choose the value of the adjacent text input with jQuery

I am working with dynamic checkboxes and corresponding text inputs. My goal is to alert the value of the text input when a checkbox is checked. However, I am having trouble retrieving the correct value of the text input associated with each checkbox. Addit ...

How to center align table headers in Angular Material 2?

When md-sort-header is added to md-header-cell in md-table, it defaults to left alignment. Is there a way to center-align specific header cells, like "name"? <md-header-cell *cdkHeaderCellDef md-sort-header style="text-align:center"> Name &l ...