css - 如何仅使用 css 进行布局?
问题描述
我想做这里描述的布局
https://github.com/robberfree/frontend-problem/tree/master/img-with-text
是否可以仅使用 CSS 完成任务?我尝试了一些方法。但是很难在 img 和 text 之间保持固定的边距。
对于 flex 布局。跨度的宽度可能大于文本实际宽度,如下所示:
.img-text {
padding: 0;
display: flex;
align-items: center;
justify-content: center;
}
.img-text img {
width: 32px;
object-fit: contain;
margin-right: 8px;
}
.img-text span {
text-align: center;
}
<p class="img-text">
<img src="https://github.githubassets.com/images/modules/logos_page/Octocat.png" />
<span>unexpected happened,Something unexpected happened
Something unexpected happened,Something unexpected happened,Something unexpected happened
</span>
</p>
解决方案
使用flexbox进行对齐。
.img-text {
padding: 0;
display: flex;
align-items: center;
justify-content: center;
}
.img-text img {
width: 32px;
object-fit: contain;
margin-right: 8px;
}
.img-text span {
text-align: center;
}
<p class="img-text">
<img src="https://github.githubassets.com/images/modules/logos_page/Octocat.png" />
<span>
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. It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like
Aldus PageMaker including versions of Lorem Ipsum.
</span>
</p>
推荐阅读
- javascript - 不在应用程序中时,电子自动打字机打字很慢
- vba - 有 sub 调用内置的“插入矩形”功能吗?
- telegram - 获取电报贴纸的所有表情符号
- java - Jenkins - 当我单击它们时,添加 JDK 和添加 Maven 按钮会消失
- python - 数据框的日平均值
- html - 如何在 SVG 上添加链接
- eclipse - org.testng.TestNGException:实例化类Tests.LoginTest时发生错误:org/apache/poi/xssf/usermodel/XSSFWorkbook
- c++ - 较新的 Windows SDK 导致 api-ms-win-core-file-l1-2-2.dll 在 Windows 7/8.1 上丢失
- angular - NgRx 选择器正在返回 Store 对象
- r - 尽管未超过内存限制,但 shinyapp 内存不足