css - 如何控制独立于图像的跨度的垂直放置
问题描述
我希望能够垂直移动跨度而不干扰图像
<hgroup>
<img src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/cropped-45795044_561214090992995_7744636018275385344_n.jpg" class="header-image" width="25%" alt="sustainablewestonma">
<span style="display:inline-block;width:65%;text-align: left;color:#538232;font-size: 3.5vw;"> Educate ● Initiate ● Collaborate</span>
</hgroup>
解决方案
有几种方法可以得到你想要的!
- Flexbox:
正确和最简单的方法之一是使用 flexbox,通过添加
display: flex;
到 span 的父元素并使用align-items
属性及其相关值(例如center
orflex-start
或)对齐它flex-end
。我只是用下面的代码片段与你分享(别担心,我只是删除了内联 CSS 并将其添加为一个类以获得更好的分辨率)。
.group{
display: flex;
align-items: flex-start;
}
.span{
display:inline-block;
width:65%;
text-align: left;
color:#538232;
font-size: 3.5vw;
}
<hgroup class="group">
<img src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/cropped-45795044_561214090992995_7744636018275385344_n.jpg" class="header-image" width="25%" alt="sustainablewestonma">
<span class="span"> Educate ● Initiate ● Collaborate</span>
</hgroup>
- 绝对位置:
您也可以
position: absolute;
像下面显示的那样添加到 span 元素中,并通过top
属性控制 span 的垂直位置,您可以使用百分比或其他单位(如px )设置它。
.span{
display:inline-block;
width:65%;
text-align: left;
color:#538232;
font-size: 3.5vw;
position: absolute;
top: 20px;
}
<hgroup>
<img src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/cropped-45795044_561214090992995_7744636018275385344_n.jpg" class="header-image" width="25%" alt="sustainablewestonma">
<span class="span"> Educate ● Initiate ● Collaborate</span>
</hgroup>
注意:请注意,当您要使用position: absolute;
以避免其他元素溢出并突然进入您的<hgroup>
分区时,您应该为您<hgroup>
的图像高度设置一个最小高度。
- 转换:最后一种方法是使用
transform
属性,translateY(*value*)
其中值可以是任何数字,相关单位如px,您也可以在下面的代码片段中看到这一点。
.span{
display:inline-block;
width:65%;
text-align: left;
color:#538232;
font-size: 3.5vw;
transform: translateY(-20px);
}
<hgroup>
<img src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/cropped-45795044_561214090992995_7744636018275385344_n.jpg" class="header-image" width="25%" alt="sustainablewestonma">
<span class="span"> Educate ● Initiate ● Collaborate</span>
</hgroup>
推荐阅读
- php - Codeigniter HMVC Cron 作业 (CLI) 失败
- javascript - Angular js:一切正常,但在 html 中创建的按钮在 compile 不起作用
- symfony - 如何在 Sonata Media Bundle 中显示每个模块的所有上下文列表?
- javascript - 即使我正确填写了prompt(),我的if语句怎么能不起作用
- javascript - 基本反应本机应用程序中的高内存消耗
- python - 如何使用循环来计算nan的数量
- clojurescript - 任何人都可以给我一个建议,让我在 CLJS 或其他使用 Clojure 的仪表板文本编辑器中使用 QuillJS?
- sql - 将同一表中的行分组为同一表中的一行
- ruby-on-rails - 设计测试助手无法登录
- java - Maven Archetype 动态包名