css - 使用 CSS 修复文本和文章图像的对齐方式
问题描述
我希望文本与右侧的文章图像很好地对齐。
我希望必须更改 .watch-listen-link 才能以正确的方式对其进行更改。
- 我尝试向该类添加一个 top-margin 属性,但没有结果。接下来我应该尝试什么?
.article-side-image{
float: left;
width: 140px;
margin-left: 8px;
margin-right:4px;
margin-top: 8px;
}
.watch-listen-link {
text-decoration: none;
color: black;
font-weight: bold;
font-size: 18px;
}
.watch-listen-link:hover{
color: #1167a8;
}
.side-article {
float: right;
width: 250px;
position: relative;
top: -13px;
}
.no-border{
border-left: none;
padding: 0;
}
.border-right{
border-right: 1px solid #CCCCCC;
}
</style>
body section:
img class="article-side-image" src="images/article3.png">
<div class= "side-article">
<p><a class= "watch-listen-link" href=""> SpaceX rocket explodes during landing </a></p>
<p> <img class="clock" src="images/Clock-image.png"> <span class= "date border-right"> 19 January 2016 </span> <br> <a class="topic-link no-border" href=""> Science & Environment </a> </p>
</div>
解决方案
您正在尝试构建的东西看起来很像一个媒体对象。这种模式在整个网络上都使用。
你可能不想用float
这个。诸如 CSS网格或Flexbox 之类的最新添加使创建媒体对象变得更加容易。
我改编了我之前提到的关于媒体对象的文章中的配方:
.media {
display: grid;
grid-template-columns: fit-content(200px) 1fr;
grid-template-rows:1fr auto;
grid-template-areas:
"image content"
"image footer";
grid-gap: 20px;
margin-bottom: 4em;
}
.img {
grid-area: image;
}
.content {
grid-area: content;
}
<div class="media">
<div class="img">
<img src="https://mdn.github.io/css-examples/css-cookbook/balloon-sq2.jpg" alt="Balloons">
</div>
<div class="content">
<p>
<a>SpaceX rocket explodes during landing</a>
</p>
<p>
<img class="clock" src="images/Clock-image.png">
<span class= "date border-right"> 19 January 2016 </span> <br>
<a class="topic-link no-border" href=""> Science & Environment </a>
</p>
</div>
推荐阅读
- node.js - 转义 PostgreSQL 字符串值
- scala - 如何使用 DataFrame 在 Scala 中进行成对字数统计
- python - 急切执行 - 内部错误:找不到节点名称的有效设备:“Sqrt”
- javascript - 如何为自定义用户调查创建交互式表单?(使用 Javascript)
- java - Libgdx 将 2 个以上的精灵数组组合成 1 个精灵数组
- facebook - 如何从 Facebook 机器人中删除“开始”按钮
- ios - 如何将用户在应用程序中拍摄的图像保存到 UIImages 数组中?斯威夫特 4
- java - 将网格从文件读取到 Java 中的特定类
- javascript - 在浏览器当前窗口滚动条中同时使用多种颜色
- c++ - 析构函数总是命中 Null