html - figure 和 div 标签之间绝对定位的奇怪差异

标签 html css sass css-position

我已经实现了一些 css 规则来练习。好吧,但我遇到了绝对定位元素 left:5% 的百分比偏差问题。我观察到当我使用 figure 标签时它不能正常工作,而是与 div 元素一起工作。为了理解您必须调整大小的问题,以缩小页面。两个版本与视口(viewport)左侧的偏差不同。我想了解为什么以及如何解决这个问题(在图形版本中)。 我发布了两个版本的代码和 jsfiddle。

<html lang="it">

<head>
 <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
 <!--  <meta name="viewport" content="width=device-width, initial-scale=1.0"> Non si mette perchè non ho istruzioni reponsive precise come media query-->
<meta name="description" content="Sito con intestazione con sfondo. Esercizio si rifa al libro css master, esercizio finisched-example.html del capitolo 5">
<meta charset="UTF-8">
<title>Cat Page</title>

<style type="text/css">
  body {
    padding: 0;
    margin: 0;
    font-family: "Helvetica Neue", Arial, sans-serif;
  }

  .sfondo-intro {
    position: relative;
    /*margin:0; servirebbe per h1 che ha un margine di default,non per header*/
    height: 600px;
    /*perche senza contenuto h1 non visualizza lo sfondo*/
    background-image: url(https://imagizer.imageshack.com/img923/4457/MeXAJj.jpg);
    background-position: 50% 30%;
    /*per mantenere il gatto centrato quando ridimensiono la finestra*/
    background-size: cover;


  }

  .profile-box {
    width: 160px;
    background-color: #FFF;
    position: absolute;
    left: 5%;
    bottom: -60px;
    -webkit-border-radius: .5em;
    border-radius: .5em;
    padding: .5em;
  }

  .profile-box img {
    max-width: 100%;
    /*max-width a differenza di width fa si che l'immagine sia larga al massimo quanto è larga l immagine. Prova a mettere width:400px ad esempio in .profile-box per vedere che succede (l immagine non si espande fino a 400px)*/
    height: auto;
    /*Altezza settata in modo da continuare a rispettare l aspect ratio dell immagine*/

  }

  .didascalia {
    text-align: center;
    font-size: 1.2em;
    color: #555;
    font-weight: 600;
  }

</style>

 </head>



  <body>

<header class="sfondo-intro" role="banner">
  <div class="profile-box">
    <img src="https://imagizer.imageshack.com/img924/606/oMPbxW.jpg" alt="Charles The Cat" />
    <figcaption class="didascalia">@CharlesTheCat</figcaption>
  </div>

  </header>
  </body>

</html>

Code with div

这里还有其他版本用 figure 元素代替 div 元素

  <!DOCTYPE html>

  <html lang="it">

 <head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<!--  <meta name="viewport" content="width=device-width, initial-scale=1.0"> Non si mette perchè non ho istruzioni reponsive precise come media query-->
<meta name="description" content="Sito con intestazione con sfondo. Esercizio si rifa al libro css master, esercizio finisched-example.html del capitolo 5">
<meta charset="UTF-8">
<title>Cat Page</title>

    <style type="text/css">
    body {
    padding: 0;
    margin: 0;
    font-family: "Helvetica Neue", Arial, sans-serif;
  }

  .sfondo-intro {
    position: relative;
    /*margin:0; servirebbe per h1 che ha un margine di default,non per header*/
    height: 600px;
    /*perche senza contenuto h1 non visualizza lo sfondo*/
    background-image: url(https://imagizer.imageshack.com/img923/4457/MeXAJj.jpg);
    background-position: 50% 30%;
    /*per mantenere il gatto centrato quando ridimensiono la finestra*/
    background-size: cover;


  }

  .profile-box {
    width: 160px;
    background-color: #FFF;
    position: absolute;
    left: 5%;
    bottom: -60px;
    -webkit-border-radius: .5em;
    border-radius: .5em;
    padding: .5em;
  }

  .profile-box img {
    max-width: 100%;
    /*max-width a differenza di width fa si che l'immagine sia larga al massimo quanto è larga l immagine. Prova a mettere width:400px ad esempio in .profile-box per vedere che succede (l immagine non si espande fino a 400px)*/
    height: auto;
    /*Altezza settata in modo da continuare a rispettare l aspect ratio dell immagine*/

  }

  .didascalia {
    text-align: center;
    font-size: 1.2em;
    color: #555;
    font-weight: 600;
  }

   </style>

 </head>



<body>

<header class="sfondo-intro" role="banner">
  <figure class="profile-box">
    <img src="https://imagizer.imageshack.com/img924/606/oMPbxW.jpg" alt="Charles The Cat" />
    <figcaption class="didascalia">@CharlesTheCat</figcaption>
  </figure>

</header>
</body>

</html>

Second version with figure

最佳答案

一些标签带有来自 use-agent 的默认样式,并且 figure 元素有一些边距。您可以使用像谷歌开发者控制台这样的检查器(在 Chrome、Windows 或 Linux 上使用 ctrl+i)来检查元素应用了哪些样式并调试此类内容,但通常当相同的 css 对不同的标签有不同的工作方式时,通常是这种情况。

在您的图形中添加 margin: 0 可以解决这种情况。

关于html - figure 和 div 标签之间绝对定位的奇怪差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58436152/

相关文章:

jquery 表头克隆问题

python - Django:如何向多个页面添加元标签?

jquery - HTML5 视频缓冲区

javascript - 我怎样才能使这段代码更好地工作?

html - DIV css 背景图像不会在 IE 8 中显示

html - 使用 CSS 响应式网格生成器,数学是如何工作的?

css - Sass 多重选择器重构

css - 安装和配置 sass 和 compass 以与基础一起使用

javascript - 如何在文件系统中获取最后修改日期和时间?

java - 将图表导出为 html - 在应用程序中打开 html 时丢失图像。如何设置图片的绝对路径