如何根据父元素的高度设置子元素的上外边距?

huangapple go评论104阅读模式
英文:

How to set a child's margin-top based on parent's height?

问题

我在一场旧大学考试中遇到了问题。基本上它要求:

使用以下 JSON 文件:

  1. [
  2. {"colore": "#FF0080", "pos_orizz": 10, "pos_vert": 30},
  3. {"colore": "#800080", "pos_orizz": 30, "pos_vert": 40},
  4. {"colore": "#808000", "pos_orizz": 50, "pos_vert": 50},
  5. {"colore": "#408080", "pos_orizz": 60, "pos_vert": 60},
  6. {"colore": "#C0C0C0", "pos_orizz": 30, "pos_vert": 50}
  7. ]

创建一个函数,使用 JSON 文件中包含的数据,在 "main"(父元素)中绘制正方形 div 元素。

这些 div 元素的尺寸是视口的 10% x 10%;
位置和背景颜色在 JSON 文件中指定(位置以主元素大小的百分比表示)。

我已经完成了一切,但当我为我的 div 元素设置样式规则时,margin-top 的百分比是相对于父元素的宽度... 这导致了各种溢出问题。

以下是你的函数,我知道肯定有一些方法可以让它正常工作,希望我已经清楚地表达了问题:

  1. async function crea(){
  2. const response = await MyFetch();
  3. const main = document.querySelector("main");
  4. response.forEach(element => {
  5. let div = document.createElement("div");
  6. div.style.width = "10vh";
  7. div.style.height = "10vh";
  8. div.style.backgroundColor = element.colore;
  9. div.style.marginTop = element.pos_vert+"%";
  10. div.style.marginLeft = element.pos_orizz+"%";
  11. main.appendChild(div);
  12. });
  13. }
英文:

I'm having problems on an old university exam.
Basically it asks to:

take this json file

  1. [
  2. {"colore": "#FF0080", "pos_orizz": 10, "pos_vert": 30},
  3. {"colore": "#800080", "pos_orizz": 30, "pos_vert": 40},
  4. {"colore": "#808000", "pos_orizz": 50, "pos_vert": 50},
  5. {"colore": "#408080", "pos_orizz": 60, "pos_vert": 60},
  6. {"colore": "#C0C0C0", "pos_orizz": 30, "pos_vert": 50}
  7. ]

create a function that, using the data contained in the json file, draws square divs in the "main" (the parent).

The dimensions of the divs are: 10% x 10% of the viewport;
position and background colour are specified in the json file (positions are in percentage of the size of the main)

I did everything but, of course, when i give my divs the style specs, the margin-top percentage is relative to the parent's width...and that cause every sort of overflow problems

  1. async function crea(){
  2. const response = await MyFetch();
  3. const main = document.querySelector("main");
  4. response.forEach(element => {
  5. let div = document.createElement("div");
  6. div.style.width = "10vh";
  7. div.style.height = "10vh";
  8. div.style.backgroundColor = element.colore;
  9. **div.style.marginTop = element.pos_vert+"%";**
  10. div.style.marginLeft = element.pos_orizz+"%";
  11. main.appendChild(div);
  12. });
  13. }

That's my function and i know for sure there's something i can do to make that work, i hope i've been clear in the exposition of the problem

答案1

得分: 0

  1. div {
  2. height: 300px;
  3. width: 300px;
  4. background: gray;
  5. }
  6. div div {
  7. position: absolute;
  8. left: 50px;
  9. top: 50px;
  10. width: 100px;
  11. height: 100px;
  12. background: orange;
  13. }
英文:

This is an example snippet that demonstrates the CSS to use draw square divs on a parent div. In this demo I have set the position in CSS, but you will need to do this in JavaScript

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. div {
  2. height: 300px;
  3. width: 300px;
  4. background: gray;
  5. }
  6. div div {
  7. position: absolute;
  8. left: 50px;
  9. top: 50px;
  10. width: 100px;
  11. height: 100px;
  12. background: orange;
  13. }

<!-- language: lang-html -->

  1. &lt;div&gt;A&lt;div&gt;B&lt;/div&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月10日 23:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定