I need assistance getting a <div> perfectly centered on the page while managing the bottom and right margins

I created a design resembling the French flag and I want to maintain the same margin from the edge of the browser window. However, the ratio of the container may change. The image below illustrates what I have implemented.

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

Despite my intention for margin: 0 0; to keep all margins at zero, only the top and left margins behave as desired. Unfortunately, the right and bottom margins do not cooperate.

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

Is there a way to set all margins equally? Below is the complete code snippet:

#wrap {
  /*I've attempted adjustments here*/
  position: absolute;
  width: 90%;
  height: 90%;
  /*height: 500px;*/
  margin: 0 0;
  border: 4px solid #000;
}
#wrap div {
  width: 33.3333333333%;
  height: 100%;
  display: inline-block;
}
#wrap div:first-child {
  margin-right: 33.3333333333%;
  background: #003153;
}
#wrap div:first-child + div {
  background: #cc3333;
}
<!DOCTYPE HTML>
<html lang="ko">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
  <title>Document</title>
</head>

<body>
  <div id="wrap">
    <div></div>
    <div></div>
  </div>
</body>

</html>

Answer №1

The default margin set by browsers for the body was causing the issue, so I adjusted it to margin:0 for body. Additionally, I changed the width in #wrap to be 100%.

body
        {
            margin:0;
        }

 #wrap{ 
                    position: absolute;
                    width:100%;
                    height:90%;
                    /*height: 500px;*/
                    margin: 0 0;
                    border:4px solid #000;}

Answer №2

When styling your wrap, remember to apply left: 5%;

Additonally, simply use margin: 0; instead of margin: 0 0;

Answer №3

When approaching this task, I would start by making changes to the HTML structure as follows:

<div id="container">
  <div class="blue"></div>
  <div class="white"></div>
  <div class="red"></div>
</div>

Next, I would style the blocks with the following CSS properties:

.blue {
   background: #003153;
 }

.red {
   background: #cc3333;
 }

.white {
   background: #FFF;
 }

For the container elements, I would use the following CSS rules:

#container {
    width: 90%;
    height: 500px;
    margin: 0 auto;
    border: 4px solid #000;
    font-size: 0; /* necessary for removing space between divs */
  }

  #container div {
    width: 33.3333333333%;
    height: 100%;
    display: inline-block;
  }

Check out this example on CodePen

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

Customize Text Area's Font Based on Selected Option in Dropdown List

I'm currently working on an HTML file that includes a dropdown list and a text area. Here's the code snippet: <select id='ddlViewBy' name='ddlViewBy' onchange='applyfonttotextarea()'> <option value = ' ...

Using static typing in Visual Studio for Angular HTML

Is there a tool that can validate HTML and provide intellisense similar to TypeScript? I'm looking for something that can detect errors when using non-existent directives or undeclared scope properties, similar to how TypeScript handles compilation er ...

When attempting to transfer data from an Ajax HTML form to a Flask template in Python upon clicking a button, encountered difficulties

I am a beginner with Flask and HTML. I need some help as I am struggling to retrieve parameter values from the HTML form (index1.html). Here is my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

Creating Responsive Headers Using CSS for Mobile Devices

I'm looking to make my header font size of 60px more responsive for mobile devices so that the text doesn't get cut off. Any suggestions on how to achieve this? Thank you! Here's the code snippet: h1 { font-size: 4.286em; /* 60px */ margi ...

What is the best way to distinguish elements in the same HTML using Angular Material?

Currently, I am working on a project using Angular material, where I have a component.html file that defines a form and a table with data. In the first image of my project, you can see the tab title, a form to add new records, and the table displaying exi ...

Is there a way for me to insert a new column into a table using jinja?

As a beginner in the final project of CS50x, I am working on creating a webpage that allows users to input weights into a database table and then view those weights along with weight gain/loss information. I am using jinja and python to render a query resu ...

What is the method for determining the width of a Mat-Table once it has been displayed?

When utilizing Angular Material Mat-Table in conjunction with Angular 8, I am passing the dataSource dynamically. The number of rows and columns varies each time. Is there a method to calculate the width of the table once it is rendered on the screen? &l ...

What is the method for writing to an HTML file with the use of expressjs?

Alright, I have an interesting idea here. I am looking for a way to allow users to push a button and have a new p element permanently added to the HTML file. This means that even after reloading the page, everyone should be able to see this new element. An ...

Ion-List seamlessly integrates with both ion-tabs and ion-nav components, creating a cohesive and dynamic user interface

On my homepage, there is an ion-list. Sometimes (not every time), when I select an item in this list or navigate to the register page using "this.app.getRootNav().push("ClienteCadastroPage")", and then select an input in the registerPage or descriptionPage ...

Issue with MaterializeCSS: Unable to add new options to select menu

I have a function that appends options to a dropdown based on the onchange event. The options are being appended successfully, but they are not visible when clicking the dropdown. $(document).ready(function(){ $('select').formSelect(); }) ...

Use the Arrow Keys to guide your way through the Page

I am looking to enhance user experience by allowing them to easily navigate through my webpage using the arrow keys on their keyboard. The goal is for users to be able to move from one section to the next in a seamless manner, unless they are focused on an ...

The cascade of CSS elements arrangement

I have two unique boxes set up like this: <div id="box1"></div> <div id="box2"></div> The second box, box2, has a border and I'm looking to cover the top border with box1. To achieve this, I've applied a negative margin- ...

JavaScript: Understanding the concept of closure variables

I am currently working on an HTML/JavaScript program that involves running two counters. The issue I am facing is with the reset counter functionality. The 'initCounter' method initializes two counters with a given initial value. Pressing the &a ...

Once you address a block using jQuery

$(function(){ $(window).scroll(function () { var scrollVal = $(this).scrollTop(); var adscrtop =$(".header").offset().top // 在 RWD 767以下不作動 if(window.screen.width>767){ if(sc ...

Building Dynamic Forms with React.js and Bootstrap for Easy Input Field Management

In the process of developing a web application using React.js and react-bootstrap, I encountered an interesting challenge. On one of the form pages, users should be able to input symptoms of an illness they are dealing with. The key functionality required ...

What are the advantages of implementing HTML5 canvas for animation?

I'm a beginner in html5 and I've recently been experimenting with the canvas element. Can anyone provide insight on when the canvas would be most beneficial or practical to use? Essentially, what are some scenarios where it is recommended to util ...

Tips for Improving the Naming Conventions of CSS Modules

I currently have the following setup in my webpack.config.js: { test: /\.css$/, use: [ {loader: "style-loader"}, { loader: "css-loader", options: { modules: true, importLoaders: 1, s ...

Another component's Angular event emitter is causing confusion with that of a different component

INTRODUCTION In my project, I have created two components: image-input-single and a test container. The image-input-single component is a "dumb" component that simplifies the process of selecting an image, compressing it, and retrieving its URL. The Type ...

Organizing an angular expansion panel

I am currently dealing with an expansion panel that has a lot of content. The layout is quite messy and chaotic, as seen here: https://ibb.co/Y3z9gdf It doesn't look very appealing, so I'm wondering if there is a way to organize it better or ma ...

Text located in the bottom right corner of the window with the use of JavaScript

Is there a way to replace text at the bottom right of the window, as opposed to the page, so that it remains fixed in the corner of the window? I'm looking to create a "share" link that is always visible. ...