Div not centered after translation by 50% on the Y-axis

My HTML and body have a height of 100vh.

Inside my body, I have a square that is 100px by 100px.

When I apply top: 50% to the div, it moves down 50%. But when I apply translateY(50%) to the div, it does not move down 50%. Why is this?

Please note that you need to open the HTML in a full page view to see that the div does not move down by 50%.

* {
  padding: 0;
  margin: 0;
}


html, body {
  height: 100vh;
  width: 100vw;
}

body {
  position: relative;
  background-color: black;
}

div {
  height: 100px;
  width: 100px;
  background-color: orange;
  position: absolute;
/*   top: 50%; */
  transform: translateY(50%);
}
<div></div>

Answer №1

When you set <strong><em>top: 50%</em></strong>, the top of an element is positioned at the halfway point of <strong><em>the container's height</em></strong>, effectively centering it vertically within its parent.</p>
<p><code>By using <strong><em>transform: translateY(50%)</em></strong>, you move the element down by 50% of <strong><em>its own height. For instance, if the element is 100px tall, it will shift down by 50px</em></strong>.</p>
    </div></answer1>
<exanswer1><div class="answer" i="98462501" l="8.5" c="1618183147" a="Y29tYXk=" ai="689535">
<p><code>Setting top to 50%
places the top of the element at 50% of the container's height, positioning it at the vertical center of the parent.

Using transform to translate by 50%
offsets the element downward by half of its own height. If the element is 100px in height, it moves downwards by 50px.

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 a dynamic dropdown menu using JQuery that does not automatically submit the form when a value is

Upon selecting a background color from the dropdown menu, I am generating a dynamic dropdown for text colors. The Text Color dropdown is populated correctly based on the selection of Background Color. Although the functionality works as intended, I encoun ...

Display or conceal DIVs with multiple attribute values according to the selected value in a dropdown menu using the jQuery Filter Method

I am attempting to display or hide multiple services, each with a custom attribute called data-serviceregion, which may have multiple values based on the selected option from the dropdown. <form class="book-now"> <select name="region" id="region" ...

Numerous animated elements within A-frame

Is there a way to incorporate animation into an A-Frame glTF model, causing it to smoothly move 5 spaces to the left and then 5 spaces forward? Below is my current code: <a-gltf-model color="#FFFFFF" width="1" height="1" de ...

What is the method to retrieve the color of a linearGradient in an SVG at a particular

I am working on a unique progress bar that uses a linear gradient and a small ellipse to indicate the completion percentage. The AngularJS binding updates the completion percentage and a function is called. My challenge is to change the color of the ellip ...

Animated Gradient Header with CSS

I've been attempting to add an animated gradient to my header class, but so far I haven't been successful. I want the gradient to apply only to the header, not the body. Here's the link I'm using for reference: Can anyone offer some i ...

Is it better to choose AJAX and JQuery for their speed and complexity, or stick with the simplicity of GET requests?

When a user attempts to log in and fails, an error message should be displayed on the page. There are two primary methods that can be used to achieve this: one involves using a form action on the HTML page and handling incorrect login information in the PH ...

Tips for updating the content of a div with the click of another div

Is there a way to dynamically change the content of a div upon clicking on a menu item? Here is the current layout for reference: https://i.stack.imgur.com/nOnlQ.png Below is the CSS and HTML code snippet: body { background-color: #555657; ...

Is it possible to rotate an image with a random angle when hovering in Angular?

I'm currently working on a photo gallery project and my goal is to have the images rotate when hovered over. However, I am experiencing difficulties in passing values from TypeScript into the CSS. HTML <div class="back"> <div cl ...

Parsing a Table in Python with HTMLParser

I am trying to convert an HTML table into a 2D array (rows and columns) using only the HTMLParser library in Python, without relying on BeautifulSoup or other non-standard libraries. This task is part of a personal project that I am working on for fun :) ...

The integration of Express server with static ejs files, CSS, and JavaScript is experiencing technical difficulties

I am currently working on a website using node.js and have created two routes: /home and /contact. Everything was functioning properly until I added CSS and JS files to both routes. For some reason, the second call does not work as expected. When I access ...

Minimizing the use of line breaks in HTML to enhance visual design characteristics

While working with Bootstrap and HTML to construct a website, I have frequently resorted to using line breaks in the code for design purposes. However, I believe there must be a more professional approach to achieve the desired layout. For instance, on ...

What is the purpose of including a viewport meta tag on my website if I already have CSS media queries in place?

As someone who is just starting out with building websites, I have been delving into the world of meta tags. However, even after conducting thorough research, I am still unsure about the necessity of a meta viewport tag when I am already utilizing CSS me ...

What is the purpose of Firebug adding <tbody> elements to <table> tags?

While analyzing the HTML source code, I noticed that the <tbody> tag is missing. However, when inspecting the code using Firebug in the HTML tab, the <tbody> tag suddenly appears. What could be the reason behind this discrepancy? ...

Loop through multiple levels of elements using jQuery

I have a snippet of HTML that resembles the following structure: <div class="block"> <div id="item1"></div> <div id="item2"></div> </div> <div class="block"> <div id="item3"></div> </di ...

What is the best method for retrieving text that does not contain tags (such as text

My challenge involves storing headings and paragraphs into separate arrays, but I am struggling with handling text between <br>. Below is my HTML code and Python snippet: <p><strong>W Ognisku</strong><br>W londyńskim Ognisku ...

Ways to customize the alignment of nav-link items in Bootstrap 4

I am using Bootstrap 4 and have a set of three nav-link elements: <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link active" href="#">Item1</a> </li> <li class="nav-item"> <a class="nav ...

Is there a way to achieve a straightforward three-column stacked layout in Bootstrap 5 that spans 100% of the viewport height?

Today I decided to try out Bootstrap 5 for the first time. However, I am facing a challenge in creating a 3 stacked column layout with box-1, box-2, and box-3 on top of each other, taking up 100vh. I have shared a redacted version of the code below, wher ...

The only functional button in the table is the one located on the top row

My MVC Application is encountering an issue with only the first item in the table working perfectly, while the rest are not calling the method. Despite my extensive research, I have been unable to resolve this issue. Below is the code on my view. Can someo ...

Challenges with arranging tables properly in React using Material UI

I am struggling to make this inner table span the full width and merge with all 8 columns. I have been unable to get the colspan attribute to work with material UI. The inner table is collapsible, which is working correctly, but the formatting of the table ...

Using Javascript to dynamically swap images within a div as the user scrolls

Can someone help me achieve a similar image scrolling effect like the one on the left side of this website: I am looking to change the image inside a div as I scroll and ensure that the page doesn't scroll further until all the images have been scrol ...