Page alignment cannot be achieved using the body selector, but can be achieved with the div page wrap

Hey there, currently working on a HTML site and encountered an issue with centering the page. The code looks like this:

HTML:

 <body>
--Rest of code-- 
</body>

CSS:

    body{
    width:70%;
    margin-left:auto;
    margin-right:auto;
    }

Unfortunately, this approach doesn't center the page as expected. However, when I modify it to include a page wrap div like this:

    <body>
    <div class="page-wrap">
    --Rest of code--
    </div>
    </body>

and update the CSS:

.page-wrap{
width: 70%;
margin-left:auto;
margin-right:auto;
}

Everything works perfectly now.

I'm puzzled conceptually why targeting the body in CSS didn't work initially. Is the content under body different from the one enclosed by the div tag?

Similarly, another CSS quirk that baffles me is when styling 'Hello' within the following structure:

<div class="A">
<div class"container">
<p>Hello</p>
</div>
</div>

In order to style 'hello', I have to use

.A .container

whereas using just

.A 

doesn't have any effect, even though both .A and .container encompass the same content.

If anyone can shed light on these concepts, I would greatly appreciate it.

Answer №1

Consider adding the following code snippet:

body { max-width: 800px; }

This will set a maximum width of 800 pixels for the body element.

Remember to enclose your entire code within the <body> tag as well.

Run the code on JS Fiddle

Answer №2

Surprisingly, both of the provided codes are effective! View code here

body{
    width:70%;
    margin-left:auto;
    margin-right:auto;
}

.A {
    color: red;
    background: silver;
}

Answer №3

/* styling block elements with relative positioning */
.position-relative {
    margin-left: auto;
    margin-right: auto;
    width: {some-width};
}

/* styling block elements with absolute positioning */
.position-absolute {
    left: 0;
    right: 0;
    margin-left: auto;
    margin-right: auto;
    width: {some-width};
}
.parent-of-position-absolute {
    position: relative;
}

Keep in mind that by default, block elements have an automatic width (not 100%) which fills the parent element's width. Setting width to 100% may cause issues, especially in IE where it refers to the window width.

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

Creating CSS boxes in a dual column layout with a step-by-step approach

Currently, I am in the process of developing a user interface for a menu that looks something like this: https://i.sstatic.net/GuzRm.png I am exploring different implementation methods for this design. In this project, I am utilizing Bootstrap framework. ...

Incorrectly parsing data in ReactJS leads to errors

After making significant progress, the focus of this question has strayed too far. Therefore, it will not be continued. *===== The issue at hand revolves around fetching data from an API, receiving results, and encountering difficulties when attempting t ...

"Retrieve JSON data from MongoDB without any extra spaces or formatting, also known as unpretty

In my current setup with mongodb 2.2.0, I am working on a way to display json data in a single line format instead of the "pretty" printing typically achieved with printjson() or find().pretty(). Essentially, I want the documents to be listed in json forma ...

Tips for customizing the ion-alert header in Ionic framework

I have recently created an alert controller and am struggling to customize the appearance of the header within the alert popup. Despite going through the documentation, I have not been able to find a suitable solution. async customizeAlert(header, subHea ...

What could be causing my CSS not to update properly in the web browser?

During the process of coding my website, I have noticed a peculiar issue. Whenever I save my HTML and CSS documents and refresh my browser, I am able to see the changes in the HTML right away. However, the changes in the CSS do not seem to take effect imme ...

Using CSS transitions to animate elements with specified opacity values

My goal is to transition buttons from opacity 0 to opacity 1 with a delay, but I am facing an issue. Some of these buttons have the "selected" class which gives them an opacity of 0.35. However, when the transition starts, the buttons with the "selected" c ...

Uploading multiple chunks with form data using Blueimp

I am looking to upload multiple files with form data in chunks. The goal is to save the form data to a database and the images to a specific folder. Currently, I am utilizing blueimp upload and you can view my code on this Fiddle. Here is the JavaScript ...

Troubleshooting Event.target Problem in Firefox 6

When working in Firefox 6, I encountered an issue while trying to identify the target element on which the event occurred. Instead of displaying the desired element, it was showing as undefined in the alert message. Utilizing the Firebug tool for debugging ...

dispatch a WebSocket message within a route using Express.js

The Objective: Imagine a bustling marketplace with multiple shops. I'm working on a dedicated page localhost:3000/livePurchases/:storeId for shop owners to receive real-time notifications whenever they make a new sale. https://i.stack.imgur.com/VdNz ...

Learn how to execute shell commands on a Linux server from a Node.js application by utilizing Socket.io for establishing a connection. This includes tasks such as running "ls -ltr", changing

After successfully establishing a connection with my Linux server, I aim to execute shell commands for automation purposes such as changing directories and creating new folders. The main objective is to connect to the Linux server through a webpage, wher ...

Is there a way for me to retrieve the current value of a star rating when I click on it?

Looking for assistance with a ReactJS rating stars component that behaves oddly. The starting value is set to 5 stars, but each click on the stars seems to return the previous value rather than the current one. import React, {useState} from "react&quo ...

Every day, I challenge myself to build my skills in react by completing various tasks. Currently, I am facing a particular task that has me stumped. Is there anyone out there who could offer

Objective:- Input: Ask user to enter a number On change: Calculate the square of the number entered by the user Display each calculation as a list in the Document Object Model (DOM) in real-time If Backspace is pressed: Delete the last calculated resul ...

Extracting Job Titles from LinkedIn Profiles

Currently, my code is designed to search for job titles on LinkedIn (e.g. Cyber Analyst) and gather all the links related to these job postings/pages. The goal of the code is to compile these links into a list and then iterate through them to print out th ...

"Is it possible to load the jQuery dform plugin from a JSON file

I have been using the JQuery dForm plugin to construct a form and it has been functioning well with the provided code snippet: $("#myform").dform({ "action" : "index.html", "method" : "get", "html" : [ ...

Transforming a dynamic background image into dynamic HTML elements

Having trouble parsing a background image to HTML elements. The images are retrieved from a database and the HTML elements are dynamically created. However, the image is not displaying. I attempted to include JavaScript in the while loop but encountered ...

Combining Ionic 3 with epubJS

Greetings. I've been attempting to access ePub files in my Ionic 3 test app without success. epubjs has been installed via npm. Here is the package.json contents: { "name": "app_name", "author": "Author", "homepage": "http://example.com/", ...

How can I ensure my custom CSS stylesheet takes precedence over Inline CSS when working with Bootstrap?

Looking to enhance the visual appeal of my website by incorporating CSS properties for background changes. Take a peek at my code: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta ...

Is there a way to determine the total number of colors present in an image that has been uploaded, without taking into consideration any anti

Can we determine the exact number of colors in an uploaded image, excluding any compression artifacts and anti-aliasing effects? I'm developing a design tool that enables users to upload images and get real-time pricing based on the colors detected i ...

angular-recaptcha: Adjusting language based on the website's language update

My website offers three different languages that users can switch between. The language switch functionality is implemented on the client side using JavaScript (AngularJS). I have integrated reCAPTCHA 2 into my website and now I need to update the languag ...

Bootstrap 5 utilizes an 8-column system specifically designed for tablets, while defaulting to a 12-column system for both desktop and mobile versions

Currently, I am utilizing the Bootstrap grid system with its 12-column setup, which is working well. However, my designer has requested that I make the Tablet view an 8-column system, with each column taking up 100% of the width and no gutters in between. ...