英文:
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: '';
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;
}
<!-- language: lang-html -->
<div class="content">
<div class="chatsContainer">
<div id="chatList">
/* here generated divs with messages */
</div>
</div>
</div>
<!-- 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: '';
position: absolute; /* changed this */
top: -5px;
left: -60px; /* changed this */
width: 50px !important;
height: 50px;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Amateur-made_Na%27vi.jpg/440px-Amateur-made_Na%27vi.jpg');
/* 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 -->
<div class="content">
<div class="chatsContainer">
<div id="chatList">
<div class="message">
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.
</div>
<div class="message">
Lorem ipsum
</div>
<div class="message">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</div>
</div>
</div>
</div>
<!-- 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:
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:
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: '';
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;
}
For your reference: https://developer.mozilla.org/en-US/docs/Web/CSS/position
Cheers!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论