这个问题与此相似(将文本放置在图像旁边),但是关于使用“ display:flex”的情况。
我们有一个包含头像图像和其旁边文本的评论。注释应该出现在图形表达的右侧,并且在文本较短时会显示注释,否则它将跳转到图形表达下方:
我想设置头像的宽度,并用剩余的可用宽度发表评论。
a.inner-anchor {
cursor: default;
}
div.github-comments {
width: 70vw;
margin: auto;
border: 1px solid #e1e4e8;
border-radius: 6px;
padding: 40px;
}
#gh-comments-list {
list-style: none;
list-style-type: none;
}
#gh-comments-list li {
margin: 0;
padding: 1.5em 0;
display: flex;
flex-wrap: wrap;
border-top: 1px solid #aaa;
}
#gh-comments-list li>a.user {
width: 2.5em;
height: 2.5em;
overflow: hidden;
padding: 3px;
}
#gh-comments-list a.user, #gh-comments-list a.comment-url {
text-decoration: none !important;
box-shadow: none !important;
border-bottom: none !important;
}
#gh-comments-list li>a.comment-url {
width: 100%;
text-align: right;
font-size: .6em;
opacity: .5;
}
#gh-comments-list li .comment-content {
padding: 0 1.5em;
}
#gh-comments-list li>a.user img {
max-width: 100%;
height: auto;
border-radius: 50%;
border: 3px solid #fff;
box-shadow: 0 0 3px #aaa;
box-sizing: border-box;
}
<div class="github-comments">
<h2>Comments</h2>
<ul id="gh-comments-list">
<li>
<a href="https://github.com/" class="user">
<img class="avatar" src="https://avatars3.githubusercontent.com/u/45234211?v=4">
</a>
<div class="comment-content">
<p>A Comment.</p>
</div>
<a href="https://github.com/" class="comment-url">#616068412 - 2020-04-19T07:44:35Z</a>
</li>
<li>
<a href="https://github.com/" class="user">
<img class="avatar" src="https://avatars3.githubusercontent.com/u/45234211?v=4">
</a>
<div class="comment-content">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
</div>
<a href="https://github.com/" class="comment-url">#616068412 - 2020-04-19T07:44:35Z</a>
</li>
<li>
<a href="https://github.com/" class="user">
<img class="avatar" src="https://avatars0.githubusercontent.com/u/45391682?v=4">
</a>
<div class="comment-content">
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
</div>
<a href="https://github.com/" class="comment-url">#618986438 - 2020-04-24T12:44:51Z</a>
</li>
</ul>
<p id="no-comments-found">No comments found for this article.</p>
<p id="loading-comments" style="display: none;">Loading comments...</p>
<p id="next-comments-page" style="display: none;">next comments page</p>
<p id="leave-a-comment">Join the discussion for this article on <a href="https://github.com/">this ticket</a>. Comments appear on this page instantly.</p>
</div>
或这里是一个小提琴:https : //jsfiddle.net/otmjrks6/
您非常接近,您需要做的就是添加flex:1;
到用于注释文本的类中:
#gh-comments-list li .comment-content {
padding: 0 1.5em;
flex: 1; /* This lets the element grow or shrink to fit */
}
这意味着元素可以增长或收缩以适应空间。参考:Flex的MDN Web文档
工作示例:
a.inner-anchor {
cursor: default;
}
div.github-comments {
width: 70vw;
margin: auto;
border: 1px solid #e1e4e8;
border-radius: 6px;
padding: 40px;
}
#gh-comments-list {
list-style: none;
list-style-type: none;
}
#gh-comments-list li {
margin: 0;
padding: 1.5em 0;
display: flex;
flex-wrap: wrap;
border-top: 1px solid #aaa;
}
#gh-comments-list li>a.user {
width: 2.5em;
height: 2.5em;
overflow: hidden;
padding: 3px;
flex-basis: 2.5em;
}
#gh-comments-list a.user, #gh-comments-list a.comment-url {
text-decoration: none !important;
box-shadow: none !important;
border-bottom: none !important;
}
#gh-comments-list li>a.comment-url {
width: 100%;
text-align: right;
font-size: .6em;
opacity: .5;
}
#gh-comments-list li .comment-content {
padding: 0 1.5em;
flex:1;
}
#gh-comments-list li>a.user img {
max-width: 100%;
height: auto;
border-radius: 50%;
border: 3px solid #fff;
box-shadow: 0 0 3px #aaa;
box-sizing: border-box;
}
#no-comments-found {
display: none;
}
#next-comments-page {
cursor: pointer;
display: none;
}
<div class="github-comments">
<h2>Comments</h2>
<ul id="gh-comments-list">
<li>
<a href="https://github.com/" class="user">
<img class="avatar" src="https://avatars3.githubusercontent.com/u/45234211?v=4">
</a>
<div class="comment-content">
<p>A Comment.</p>
</div>
<a href="https://github.com/" class="comment-url">#616068412 - 2020-04-19T07:44:35Z</a>
</li>
<li>
<a href="https://github.com/" class="user">
<img class="avatar" src="https://avatars3.githubusercontent.com/u/45234211?v=4">
</a>
<div class="comment-content">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
</div>
<a href="https://github.com/" class="comment-url">#616068412 - 2020-04-19T07:44:35Z</a>
</li>
<li>
<a href="https://github.com/" class="user">
<img class="avatar" src="https://avatars0.githubusercontent.com/u/45391682?v=4">
</a>
<div class="comment-content">
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
</div>
<a href="https://github.com/" class="comment-url">#618986438 - 2020-04-24T12:44:51Z</a>
</li>
</ul>
<p id="no-comments-found">No comments found for this article.</p>
<p id="loading-comments" style="display: none;">Loading comments...</p>
<p id="next-comments-page" style="display: none;">next comments page</p>
<p id="leave-a-comment">Join the discussion for this article on <a href="https://github.com/">this ticket</a>. Comments appear on this page instantly.</p>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句