在:before伪元素中设置背景图片的静态宽度的选项是什么?

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

Is option to make static width for background-image for :before?

问题

My program adding to div a random message generated and pushed by JS to .message:before contains the default background image.png
如果消息很长,图像宽度为50px,但如果消息较短,则图像比应该更扁平。
如果有任何选项可以设置图像大小,而不管消息大小如何?

.message {
  display: flex;
  padding: 5px;
  margin: 5px 65px;
  height: min-content;
  min-width: 10px;
  max-width: 400px;
  background-color: rgb(98, 117, 180);
  border-radius: 20px;
}

.message::before {
  content: '';
  position: relative;
  top: -5px;
  right: 60px;
  width: 50px !important;
  height: 50px;
  background-image: url('default-avatar.png');
  /* for see result, will generate by js personalised for every user */
  background-position: center;
  background-repeat: no-repeat;
  background-size: 50px 50px;
  border-radius: 50%;
  z-index: 99;
}
<div class="content">
  <div class="chatsContainer">
    <div id="chatList">
      <!-- here generated divs with messages -->
    </div>
  </div>
</div>

I deleted max-width from .message and then every image printing good, but I need to set message width shorter than 50% - paddings and margins to set div at left side of .chatList

英文:

My program adding to div a random message generated and pushed by JS to .message:before contains the default background image.png
If the message is long, the image is 50px wide, but if it is shorter, the image is flatter than it should be.
Is there any option to set one image size regardless of message size?

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

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

.message {
  display: flex;
  padding: 5px;
  margin: 5px 65px;
  height: min-content;
  min-width: 10px;
  max-width: 400px;
  background-color: rgb(98, 117, 180);
  border-radius: 20px;
}

.message::before {
  content: &#39;&#39;;
  position: relative;
  top: -5px;
  right: 60px;
  width: 50px !important;
  height: 50px;
  background-image: url(&#39;default-avatar.png&#39;);
  /* for see result, will generate by js personalised for every user */
  background-position: center;
  background-repeat: no-repeat;
  background-size: 50px 50px;
  border-radius: 50%;
  z-index: 99;
}

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

&lt;div class=&quot;content&quot;&gt;
  &lt;div class=&quot;chatsContainer&quot;&gt;
    &lt;div id=&quot;chatList&quot;&gt;
      /* here generated divs with messages */
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I deleted max-width from .message and then every image printing good, but I need to set message width shorter than 50% - paddings and margins to set div at left side of .chatList

答案1

得分: 1

你可以将图像绝对定位到消息容器中。我在代码中已经注释了我的更改。

英文:

You could position the image absolute to the message container. I commeted my changes in the code.

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

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

.message {
  position: relative; /* changed this */
  display: flex;
  padding: 5px;
  margin: 5px 65px;
  min-height: 70px; /* changed this */
  min-width: 10px;
  max-width: 400px;
  background-color: rgb(98, 117, 180);
  border-radius: 20px;
}

.message::before {
  content: &#39;&#39;;
  position: absolute; /* changed this */
  top: -5px;
  left: -60px; /* changed this */
  width: 50px !important;
  height: 50px;
  background-image: url(&#39;https://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Amateur-made_Na%27vi.jpg/440px-Amateur-made_Na%27vi.jpg&#39;);
  /* for see result, will generate by js personalised for every user */
  background-position: center;
  background-repeat: no-repeat;
  background-size: 50px 50px;
  border-radius: 50%;
  z-index: 99;
}

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

&lt;div class=&quot;content&quot;&gt;
  &lt;div class=&quot;chatsContainer&quot;&gt;
    &lt;div id=&quot;chatList&quot;&gt;
      &lt;div class=&quot;message&quot;&gt;
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 
      &lt;/div&gt;
      &lt;div class=&quot;message&quot;&gt;
        Lorem ipsum
      &lt;/div&gt;
      &lt;div class=&quot;message&quot;&gt;
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译的内容:

我看到的情况与您描述的相反。当消息很长时,您的图像会被压缩,而当消息很短时,情况还好。

这是因为您的::before被设置为position: relative。这意味着它包括在.message元素的大小计算中。如果您在浏览器中打开开发工具并检查.message元素,您应该看到类似以下内容:

一种解决方法是在::before元素上使用position: absolute,因为它会将其从文档流中移除。您还需要在.message选择器上设置position: relative,因为绝对定位的元素将与最近的定位祖先一起工作。

之后,只需在::before选择器中将right: 60px更改为left: -60px,您应该会看到如下结果:

这是我用于您参考的CSS:

.message {
    display: flex;
    padding: 5px;
    margin: 5px 65px;
    height: min-content;
    min-width: 10px;
    max-width: 400px;
    background-color: rgb(98, 117, 180);
    border-radius: 20px;
    position: relative;
}

.message::before {
    content: '';
    position: absolute;
    top: -5px;
    left: -60px;
    width: 50px !important;
    height: 50px;
    background-image: url('avatar.jpg');
    background-position: center;
    background-repeat: no-repeat;
    background-size: 50px 50px;
    border-radius: 50%;
    z-index: 99;
}

供您参考:https://developer.mozilla.org/en-US/docs/Web/CSS/position

祝好运!

英文:

What I'm seeing is the opposite of what you describe. With a long message your image gets squished, and with a short message it is okay.

The reason for this is because your ::before is set to position: relative. This means it is included in the size calculation for the .message element. If you open your dev tools in the browser and inspect the .message element you should see something like this:
在:before伪元素中设置背景图片的静态宽度的选项是什么?

One way to resolve this is to use position: absolute on the ::before element as it will remove it from the flow of the document. You'll also need to set position: relative on the .message selector because absolutely positioned elements will work with the closest positioned ancestor.

After that, just change the right: 60px to left: -60px in the ::before selector and you should see a result like this:

在:before伪元素中设置背景图片的静态宽度的选项是什么?

Here's the css I used for your reference:

.message {
    display: flex;
    padding: 5px;
    margin: 5px 65px;
    height: min-content;
    min-width: 10px;
    max-width: 400px;
    background-color: rgb(98, 117, 180);
    border-radius: 20px;
    position: relative;
}

.message::before {
    content: &#39;&#39;;
    position: absolute;
    top: -5px;
    left: -60px;
    width: 50px !important;
    height: 50px;
    background-image: url(&#39;avatar.jpg&#39;);
    background-position: center;
    background-repeat: no-repeat;
    background-size: 50px 50px;
    border-radius: 50%;
    z-index: 99;
}

For your reference: https://developer.mozilla.org/en-US/docs/Web/CSS/position

Cheers!

huangapple
  • 本文由 发表于 2023年5月7日 05:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191283.html
匿名

发表评论

匿名网友

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

确定