【问题标题】:can't get image and text to align with CSS/flexbox under the navbar无法让图像和文本与导航栏下的 CSS/flexbox 对齐
【发布时间】:2021-01-07 19:00:43
【问题描述】:

我一直在尝试将文本与图像的右侧对齐,但无论我做什么——当文本行变得太长时——它一直显示在图像的正下方,而不是继续出现在下一个图像旁边的线。我试图为文本添加显示内联,但它并没有改变任何东西。我也尝试将图像向左浮动,但最终将我的图像切成两半。

p {
    text-align: right;
    color: white;
    font-size: 20px;
    font-family: orpheuspro, serif;
    font-style: normal;
    align-items: center;
    line-height: 2.5;
    text-transform: capitalize;
    font-size: 30px;
    justify-content: right;
    float: right;
    padding-right: 100px;
    padding-bottom: 90px;
    overflow: auto;
}

img {
    padding-left: 90px;
    opacity: 0.55;
    height: auto;
   
}

div {
    position:relative;
    background-color: #361833;
    min-height: 40vh;
} 

HTML 文件:

<div>
<div>
  <br>
  <img src='../../assets/img/image0.jpeg' alt="wj" width="500"/>
  <p>some loooooooooooooooooooooooooooong text here shdfklshjfsldfhskdjfzf</p>
</div>
</div>

任何指导都将不胜感激,因为我对 CSS 很陌生!谢谢!

【问题讨论】:

  • @RayeesAC 没有改变任何东西:/
  • 您可以使用word-break:break-word; 来对齐文本下一行。
  • 你提到了 flex 但你没有使用它只是添加了 display flex;

标签: html css display text-alignment text-align


【解决方案1】:

尝试使用 flex property 而不是 float

同时添加到 p 标签 word-break:break-word; 以对齐下一行​​

结果桌面视图:

移动端视图:

p {
    text-align: right;
    color: white;
    font-size: 20px;
    font-family: orpheuspro, serif;
    font-style: normal;
    align-items: center;
    line-height: 2.5;
    text-transform: capitalize;
    font-size: 30px;
    justify-content: right;
    padding-right: 100px;
    padding-bottom: 90px;
    overflow: auto;
    word-break:break-word
}

img {
    padding-left: 90px;
    opacity: 0.55;
    height: auto;

}

.nav {
    position:relative;
    background-color: #361833;
    min-height: 40vh;
    display:flex;
    flex-wrap:nowrap;
}

@media only screen and (max-width:768px){
  .nav {
      display:flex;
      flex-direction:column;
  }
}
<div>
  <div class="nav">
    <img src='https://i.stack.imgur.com/amhjm.png' alt="wj" width="500"/>
    <p>some loooooooooooooooooooooooooooong text here shdfklshjfsldfhskdjfzf</p>
  </div>
</div>

【讨论】:

  • 谢谢!这在大多数情况下都有效。唯一的问题是当线条的长度变得太长时,它似乎会挤压图像——关于如何解决这个问题的任何想法?
【解决方案2】:

最终通过为我的文本设置宽度来解决这个问题!

【讨论】:

  • 我强烈建议您仍然好好看看 Rayees CSS 代码,因为这是包括响应性在内的最好的方法之一,即使不是最好的。
  • @LaurentC rayees 的 css 代码会在文本过长而这不是我的目标时挤压图像
  • 你能提供一个我可以使用的codepen吗?谢谢你
猜你喜欢
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-15
相关资源
最近更新 更多