简体   繁体   English

CSS:在div顶部垂直对齐文本

[英]CSS : vertical align text at top of div

How to align the <span> text at top of the <div> if float:right is applied to the <span> . 如果将float:right应用于<span> ,如何在<div>顶部对齐<span>文本。

Here is my code: 这是我的代码:

 span { float: right; } 
 <div>Lorem ipsum dolor sit amet, <br>consectetuer adipiscing elit, <br> sed diam nonummy nibh dolore <span>Align me</span> </div> 

I tried vertical-align:text-top but its not working. 我尝试了vertical-align:text-top但是它不起作用。 I can't use negative margin and can't even make any changes to HTML. 我不能使用负边距,甚至不能对HTML进行任何更改。

You can align it to the top right using position: absolute : 您可以使用position: absolute将它对齐到右上角:

  p { position: relative; } span { position: absolute; top: 0; right: 0; } 
 <p>Lorem ipsum dolor sit amet, <br>consectetuer adipiscing elit,<br> sed diam nonummy nibh dolore <span>Align me</span></p> 

Please see this and let me know. 请看此并让我知道。

span {
  float: right;
  position: relative;
  bottom:40px;
}

You can use Flexbox and set align-items: flex-start . 您可以使用Flexbox并设置align-items: flex-start

 p { display: flex; align-items: flex-start; justify-content: space-between; } 
 <p>Lorem ipsum dolor sit amet, <br>consectetuer adipiscing elit,<br> sed diam nonummy nibh dolore <span>Align me</span></p> 

here is the example since you cannot edit the HTML you can make a css change example: 这是示例,因为您无法编辑HTML ,因此可以制作一个css更改示例:

    span#mySpan {
    background-color:yellow;
    vertical-align:middle;
}

I think position:absolute is best now. 我认为立场:绝对是最好的。
Like, 喜欢,
position:absolute; right:0; top:0;}
just try 你试一试

Translating 🔝 : 翻译🔝:

span {
  float: right;
  transform:translateY(-200%)
}

CSS grid 📐 : CSS网格📐:

p {
  display: grid;
}

span {
  float: right;
  grid-column-start:2;
  text-align: right;
}

Hope this will help you to solve your problem: 希望这可以帮助您解决问题:

<div>Lorem ipsum dolor sit amet,
  <br>consectetuer adipiscing elit,
  <br> sed diam nonummy nibh dolore
  <span>Align me</span>
</div>


div {  
   border: 1px solid gray;
   position: relative; 
   display: flex;
    }

span {
  display: inline-block;     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM