HTML/CSS在代码中将
移到其自己的行时会产生不同的效果。

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

HTML/CSS acts differently if I move the div into it's own line in the code

问题

I understand that you don't want code translation, so I'll provide a summary of the issue you're facing.

The issue you're describing is related to the placement of the <div class="im-content"> element within the HTML structure. You have two blocks of HTML and CSS code, and in the first block, the im-content div is on the same line as the im-container im-image-holder div, while in the second block, it's indented. The difference in indentation shouldn't affect the behavior, but it appears to be causing the image not to resize correctly in the second block.

Without the actual code and complete context, it's challenging to provide a precise solution. However, code formatting and indentation usually do not affect the functionality of HTML/CSS.

To resolve the issue, check for any other changes or CSS rules that might be affecting the layout, and ensure that the CSS and HTML are properly structured. It's possible that some other part of your code or external factors might be causing the behavior difference between the two blocks.

英文:

I have the following HTML/CSS structure:

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

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

.im-wrapper {
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: hidden auto;
  top: 0;
  left: 0;
}

.im-backdrop {
  background-color: var(--darkblack);
  opacity: 80%;
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: fixed;
  outline: 0;
}

.im-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  padding: 0 8px;
  box-sizing: border-box;
  text-align: center;
}

.im-container:before {
  content: &#39;&#39;;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

.im-wrapper .im-content {
  opacity: 100%;
  box-shadow: 0 0 8px rgb(0 0 0 / 80%);
  min-width: 50%;
  margin-right: 40px;
  margin-left: 40px;
}

.im-image-holder .im-content {
  max-width: 100%;
}

.im-content {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  margin: 0 auto;
  text-align: left;
  max-width: 100%;
}

.im-figure {
  background: var(--black);
  line-height: 0;
  cursor: pointer;
}

.im-close {
  color: white;
  cursor: pointer;
  padding-right: 45px;
  right: 0;
  top: 0;
  height: 45px;
  width: 45px;
  line-height: 45px;
  position: absolute;
  text-decoration: none;
  opacity: 65%;
  font-size: 28px;
  font-weight: bold;
}

button.im-close {
  overflow: visible;
  background: 0 0;
  border: 0;
  display: block;
  outline: 0;
  padding: 0;
  box-shadow: none;
}

.im-wrapper .im-content .im-figure .im-img {
  padding: 0;
}

img.im-img {
  width: auto;
  max-width: 80%;
  height: auto;
  display: block;
  line-height: 0;
  box-sizing: border-box;
  padding: 40px 0;
  margin: 0 auto;
}

.im-bottom-bar {
  max-height: 20%;
  background-color: var(--cyan);
  top: 100%;
  left: 0;
  width: 100%;
  cursor: auto;
  padding: 0 15px;
}

.im-title {
  text-align: left;
  line-height: 18px;
  word-wrap: break-word;
  color: var(--black);
}

.im-collapsed {
  padding: 0 15px;
}

.im-left-bar {
  font-size: 0.8em;
}

.im-bottom-bar h2 {
  border-bottom: 1px solid var(--black);
  font-size: 1rem;
  padding-bottom: 2px;
}

.im-bottom-bar .im-left-bar.im-collapsed .row&gt;div {
  border-right: 1px solid var(--black);
}

.im-bottom-bar .im-left-bar.im-collapsed .row&gt;div:last-of-type {
  border-right: none;
}

.im-bottom-bar .im-left-bar {
  padding: 10px;
}

.im-counter {
  position: absolute;
  top: 0;
  right: 0;
  color: #ccc;
  font-size: 12px;
  line-height: 18px;
}

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;div class=&quot;im-wrapper&quot;&gt;
  &lt;div class=&quot;im-container im-image-holder&quot;&gt;&lt;div class=&quot;im-content&quot;&gt;
      &lt;div class=&quot;im-figure&quot;&gt;
        &lt;button title=&quot;Close&quot; type=&quot;button&quot; class=&quot;im-close&quot;&gt;&#215;&lt;/button&gt;
        &lt;figure&gt;
          &lt;img class=&quot;im-img&quot; src=&quot;https://media.istockphoto.com/id/185235465/photo/white-toast.jpg?s=1024x1024&amp;w=is&amp;k=20&amp;c=3UrLtuMZhHW6AfLbpM-87W0pW2oQHKe7End6hfgG0g0=&quot; style=&quot;max-height:1010px&quot;&gt;
        &lt;/figure&gt;
        &lt;figcaption&gt;
          &lt;div class=&quot;im-bottom-bar&quot;&gt;
            &lt;div class=&quot;im-title&quot;&gt;
              &lt;div class=&quot;row&quot;&gt;
                &lt;div class=&quot;col-12 im-collapsed im-left-bar&quot;&gt;
                  &lt;div class=&quot;row&quot;&gt;
                    &lt;div class=&quot;col-lg-6 text-left&quot;&gt;
                      &lt;h2&gt;Credits&lt;/h2&gt;
                      &lt;div class=&quot;mb-1&quot;&gt;stuff by waa&lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;col-lg-6 text-left&quot;&gt;
                      &lt;h2&gt;Characters&lt;/h2&gt;
                      &lt;div class=&quot;mb-1&quot;&gt;:3&lt;/div&gt;
                    &lt;/div&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&quot;im-counter&quot;&gt;&lt;/div&gt;
          &lt;/div&gt;
        &lt;/figcaption&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

As you can see, the image content resizes along with the window. However, before I had the exact same code, but with the &lt;div class=&quot;im-content&quot;&gt; not in the same line as &lt;div class=&quot;im-container im-image-holder&quot;&gt;. If I do this, the image doesn't properly resize anymore and jumps down.

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

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

.im-wrapper {
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: hidden auto;
  top: 0;
  left: 0;
}

.im-backdrop {
  background-color: var(--darkblack);
  opacity: 80%;
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: fixed;
  outline: 0;
}

.im-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  padding: 0 8px;
  box-sizing: border-box;
  text-align: center;
}

.im-container:before {
  content: &#39;&#39;;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

.im-wrapper .im-content {
  opacity: 100%;
  box-shadow: 0 0 8px rgb(0 0 0 / 80%);
  min-width: 50%;
  margin-right: 40px;
  margin-left: 40px;
}

.im-image-holder .im-content {
  max-width: 100%;
}

.im-content {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  margin: 0 auto;
  text-align: left;
  max-width: 100%;
}

.im-figure {
  background: var(--black);
  line-height: 0;
  cursor: pointer;
}

.im-close {
  color: white;
  cursor: pointer;
  padding-right: 45px;
  right: 0;
  top: 0;
  height: 45px;
  width: 45px;
  line-height: 45px;
  position: absolute;
  text-decoration: none;
  opacity: 65%;
  font-size: 28px;
  font-weight: bold;
}

button.im-close {
  overflow: visible;
  background: 0 0;
  border: 0;
  display: block;
  outline: 0;
  padding: 0;
  box-shadow: none;
}

.im-wrapper .im-content .im-figure .im-img {
  padding: 0;
}

img.im-img {
  width: auto;
  max-width: 80%;
  height: auto;
  display: block;
  line-height: 0;
  box-sizing: border-box;
  padding: 40px 0;
  margin: 0 auto;
}

.im-bottom-bar {
  max-height: 20%;
  background-color: var(--cyan);
  top: 100%;
  left: 0;
  width: 100%;
  cursor: auto;
  padding: 0 15px;
}

.im-title {
  text-align: left;
  line-height: 18px;
  word-wrap: break-word;
  color: var(--black);
}

.im-collapsed {
  padding: 0 15px;
}

.im-left-bar {
  font-size: 0.8em;
}

.im-bottom-bar h2 {
  border-bottom: 1px solid var(--black);
  font-size: 1rem;
  padding-bottom: 2px;
}

.im-bottom-bar .im-left-bar.im-collapsed .row&gt;div {
  border-right: 1px solid var(--black);
}

.im-bottom-bar .im-left-bar.im-collapsed .row&gt;div:last-of-type {
  border-right: none;
}

.im-bottom-bar .im-left-bar {
  padding: 10px;
}

.im-counter {
  position: absolute;
  top: 0;
  right: 0;
  color: #ccc;
  font-size: 12px;
  line-height: 18px;
}

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;div class=&quot;im-wrapper&quot;&gt;
  &lt;div class=&quot;im-container im-image-holder&quot;&gt;
    &lt;div class=&quot;im-content&quot;&gt;
      &lt;div class=&quot;im-figure&quot;&gt;
        &lt;button title=&quot;Close&quot; type=&quot;button&quot; class=&quot;im-close&quot;&gt;&#215;&lt;/button&gt;
        &lt;figure&gt;
          &lt;img class=&quot;im-img&quot; src=&quot;https://media.istockphoto.com/id/185235465/photo/white-toast.jpg?s=1024x1024&amp;w=is&amp;k=20&amp;c=3UrLtuMZhHW6AfLbpM-87W0pW2oQHKe7End6hfgG0g0=&quot; style=&quot;max-height:1010px&quot;&gt;
        &lt;/figure&gt;
        &lt;figcaption&gt;
          &lt;div class=&quot;im-bottom-bar&quot;&gt;
            &lt;div class=&quot;im-title&quot;&gt;
              &lt;div class=&quot;row&quot;&gt;
                &lt;div class=&quot;col-12 im-collapsed im-left-bar&quot;&gt;
                  &lt;div class=&quot;row&quot;&gt;
                    &lt;div class=&quot;col-lg-6 text-left&quot;&gt;
                      &lt;h2&gt;Credits&lt;/h2&gt;
                      &lt;div class=&quot;mb-1&quot;&gt;stuff by waa&lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;col-lg-6 text-left&quot;&gt;
                      &lt;h2&gt;Characters&lt;/h2&gt;
                      &lt;div class=&quot;mb-1&quot;&gt;:3&lt;/div&gt;
                    &lt;/div&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&quot;im-counter&quot;&gt;&lt;/div&gt;
          &lt;/div&gt;
        &lt;/figcaption&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I made no other changes. I'm very confused by this behavior, as I believed that the formatting of which I write my code has no effect on the behavior, as long as I close the tags properly and such. Can anyone explain why this happens?

答案1

得分: 2

伪元素引发布局问题。

(我已经按照楼主的邀请将我之前的评论转化为答案。)

问题是由这个使用伪元素 before 的 CSS 规则引起的:

.im-container:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

伪元素破坏了容器内的内容 div 的布局。问题并不明显,因为伪元素是不可见的。它有高度,但没有宽度。它对布局的影响受到视口大小和 div 之间的空白的影响。

<div class="im-wrapper">
  <div class="im-container im-image-holder">
    <div class="im-content">
    
      未显示的内容

    </div>
  </div>
</div>

最简单的解决方案是移除伪元素(通过 CSS),因为它似乎没有用于任何事情。或者,也可以只是移除 inline-block 属性,以给元素一些宽度。

英文:

pseudo-element causes layout problems

(I've converted my earlier comment to an answer at OP's invitation.)

The problem is caused by this css rule that uses the pseudo-element before

.im-container:before {
  content: &#39;&#39;;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

The pseudo-element breaks the layout of the content div within the container. The problem is not obvious because the pseudo-element is invisible. It has a height, but no width. And how it affects the layout is influenced by the viewport size and whitespace between divs.

&lt;div class=&quot;im-wrapper&quot;&gt;
  &lt;div class=&quot;im-container im-image-holder&quot;&gt;
      &lt;div class=&quot;im-content&quot;&gt;
      
       content not shown

      &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

The easiest solution is to remove the pseudo-element (via css) since it doesn't appear to be used for anything. Alternatively, one could also just remove the inline-block attribute to give the element some width.

huangapple
  • 本文由 发表于 2023年3月4日 05:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631983.html
匿名

发表评论

匿名网友

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

确定