Struggling to properly align div in the center

I've been struggling to properly center a div inside another div, but instead of being centered it appears shifted to the right. I've experimented with different positioning combinations and techniques from various sources, yet I haven't found a solution. Here is how it currently appears: https://jsfiddle.net/L13qa1tr/

content_page.html:

<div id="wrapper">
      <div id="main-content" class="container">
          <h1 class="text-center">This is a test, remove before release.This is a test, remove before release.This is a test, remove before release.</h1>
      </div>
</div>

Relevant CSS, content_page.css:

body {
  font-family: 'Lora', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  position: relative;
  top: 61px;
  width: 100%;
  height: 100%;
  color: white;
  background-color: black;}

html {
  width: 100%;
  height: 100%;}

h1 {
  font-family: 'Cabin', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-weight: 700;
  margin: 0 0 35px;
  letter-spacing: 1px;
  text-transform: uppercase;}

#wrapper{
    background-color: #555555;
    width: 100%;
    height: auto;
    padding: 5px;
    margin: 0 auto;}
#main-content {
    position: relative;
    top: 32px;
    left: 88px;
    right: 88px;
    bottom: 32px;
    background-color: #E1E1E1;
    height: auto;
    box-sizing: border-box;
    margin-right: auto;
    margin-left: auto;}

If any additional information is required, please let me know. I'm open to suggestions or guidance on how to resolve this issue. Additionally, I find it puzzling that the inner div's width should be set to 100%, yet it doesn't occupy the entire space as expected.

Answer №1

After some cleanup, the code is now simplified with the unnecessary parts removed.

To center the content properly, I moved the margin: 0 auto; rule inside the #main-content instead of the #wrapper. I also adjusted the width to demonstrate its functionality.

body {
  font-family: 'Lora', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  position: relative;
  top: 61px;
  color: white;
  background-color: black; }

h1 {
  font-family: 'Cabin', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-weight: 700;
  margin: 0 0 35px;
  letter-spacing: 1px;
  text-transform: uppercase; }

#wrapper{
    background-color: #555555;
    padding: 5px;
}
#main-content {
    position: relative;
    top: 32px;
    background-color: #E1E1E1;
    box-sizing: border-box;
    margin: 0 auto;
    width: 50%;
}
<div id="wrapper">
      <div id="main-content" class="container">
          <h1 class="text-center">This is a test, remove before release.This is a test, remove before release.This is a test, remove before release.</h1>
      </div>
</div>

Answer №2

If you want to center the #main-content division on your page, here's how you can achieve it.

#main-content {
    position: relative;
    width: 80%;
    width:calc( 100% - 88px - 88px);
    margin-left: auto;
    margin-right: auto;
    background-color: #E1E1E1;
    height: auto;
}

The calc method involves using specific left and right parameters. You could opt to remove this and stick with using percentages, which will still centrally align the content.

To center a division, you must specify a width and apply automatic margins on both the left and right sides:

margin-left: auto;
margin-right: auto

Check out this updated fiddle for reference.

Answer №3

One issue that was causing problems was when you were trying to move the div by using

position:relative and adding top,left,right, and bottom.

Instead, all you need to do is add margin:auto. Reducing the width helps keep the content centered.

#main-content {
  width:600px;
  margin:auto;
  background-color: #E1E1E1;
}

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

Tips for displaying HTML tags in a chatbot using JavaScript or jQuery

I'm currently working on revamping an older chatbot project I stumbled upon online. It serves as a basic "command bot" that provides specific responses based on user input. However, the chatbot is mainly text-based and does not support rendering HTML ...

Event dedicated to the selection of mobile options

I am facing an issue with binding an event to the options of a select tag on mobile devices. The code inside the click event handler doesn't seem to be working and I'm unsure of the reason behind it. My assumption is that perhaps the click event ...

Implementing bootstrap columns while displaying individual components one after the other

Here is the html code I am currently working with: <label>Search:</label> <input type="text" id="list_search" class="form-control col-8"> <button class="btn btn-info col-2">Search</button> It's interesting to note that ...

Is there a way to dynamically adjust the carousel slide indicators based on the changing viewport size?

My carousel is inspired by the one at the following link: I am looking to make a modification where, as the screen size decreases, the bottom links disappear and are replaced with dot indicators for switching between slides. ...

Problem with onblur and onchange events not being triggered in input tag

After encountering this issue, I came to the realization that the events onblur and onchange function properly. However, I noticed that if your page contains only ONE <input type="text" onblur="loadXMLDoc()"> Change Content</input> The behav ...

What is the best way to resize my hamburger menu and achieve a flawlessly circular border for it?

I've been struggling to reduce the size of my hamburger menu (both height and width) for some time now. I managed to make it a bit smaller, but I can't seem to figure out how to shrink it further. Additionally, I'm having trouble creating a ...

Tips for creating a curved shape using fabric.js

I am encountering an issue while trying to draw an arc using a circle with a start and end angle in my code. Here is the snippet I am working with: var circle = new fabric.Circle({ radius: 30, left: 20, top: 20, fill: " ...

Tips for creating equal height columns in Bootstrap 4

Working with Bootstrap 4 Beta in an attempt to create two columns that maintain full height regardless of screen size. The code below functions perfectly, ensuring both columns maintain full height across all devices. However, upon inserting the bootstra ...

Achieving equal heights for div boxes while aligning list items inside

I have 8 green boxes on my site, each containing an image and either 3 or 4 list items with text only. The issue I'm facing is that the boxes are not of equal height due to two reasons: (1) One box may have an extra list item pushing the rest down, or ...

Angular 6 issue: Bootstrap 4 carousel indicators not responsive to clicks

I am currently working on incorporating a Bootstrap 4 carousel into my application, but I seem to be encountering issues with the indicators. Ideally, clicking on any of them should navigate you to the corresponding photo, similar to this example: https:// ...

Can MDBootstrap be integrated with Bootstrap for a cohesive design?

After using Bootstrap for many years, I recently started a project where I incorporated the Bootstrap CDN. However, I realized that I also needed certain features and components from MDBootstrap. When I added MDBootstrap, it caused conflicts with the Boots ...

The text exceeds the width of the element

Currently, I am facing an issue with displaying notifications where the text exceeds the element width. I am looking for a solution to make the text wrap to the next line if it is too long. https://i.sstatic.net/5NcNs.png To handle notifications, I am ut ...

Display <div> exclusively when in @media print mode or when the user presses Ctrl+P

Looking for a way to create an HTML division using the div element that is only visible when the print function is used (Ctrl+P) and not visible in the regular page view. Unfortunately, I attempted the following method without success. Any advice or solut ...

Align boxes in the center within a DIV container

Here is the box design that I have created: https://i.sstatic.net/3rtcn.jpg The green color boxes are generated dynamically inside a "col-md-10" div. If there are less than 3 boxes in the second row, I would like to center align them. For example, in thi ...

Having issues with my code. Attempting to interact with a specific element on a webpage using its ID, but encountering difficulties

Is there a way to automatically click on an element on a webpage based on its ID in the HTML code? The ID for this particular button is "input-optionXXX" where XXX represents a 3-digit number ranging from 100 to 400. I need a python script that can scan ...

What is the most effective method for dynamically creating an HTML report from scratch?

I need help figuring out the best way to convert a substantial amount of program output into an HTML report. Right now, I am manually piecing together the report by extracting relevant information from the extensive logging in my large Python program and f ...

Rails: jQuery - page refreshes unexpectedly during event execution

After successfully implementing a jQuery script for my custom dropdown menu on a standalone webpage, I encountered issues when integrating it into my Rails application. I am unsure if the problem is related to Turbolinks or if there is another underlying c ...

Extracting information from an external website in the form of XML data

Trying to retrieve data from an XML feed on a remote website , I encountered issues parsing the XML content and kept receiving errors. Surprisingly, fetching JSON data from the same source at worked perfectly. The code snippet used for XML parsing is as f ...

Applying CSS to Align List Items to the Left with Word Wrap

Over at my blog page qavalidation.com, I am faced with an issue related to the alignment of vertical menus with links. When word wrap is applied, there seems to be a misalignment in the left indentation. Can someone provide guidance on the correct CSS sty ...

Execute a function only after the completion of another

I am trying to implement a function where the .hidden class is added to a div only when it has scrolled to the top. I know I can use SetTimeout, but I want to ensure that the div disappears only when it has reached the top of the scroll. $(".more").on(" ...