简体   繁体   English

如何在同一行上对齐两个元素html

[英]How to align two elements on the same line html

I have an example like this jsfiddle where I have a list of <a> element. 我有一个像jsfiddle这样的示例,其中有一个<a>元素列表。 Inside each <a> element I have a <span> and a <button> . 在每个<a>元素中,我都有一个<span><button> What I would like to do is I want to align the button on the right, I do this by adding the class pull-right and it worked fine 我想做的是我想将右边的按钮对齐,我通过添加class pull-right做到这一点,并且效果很好

<button class=" btt pull-right glyphicon glyphicon-chevron-right"></button>

, and I want to align the span and the button on the same line, but if I have a very long Name inside the span then the button is no longer on the same line with the span. ,并且我想将spanbutton对齐在同一行,但是如果跨度内有一个很长的Name,则按钮将不再与跨度在同一行。 How can I fix this problem? 我该如何解决这个问题?

Use position: absolute; 使用position: absolute; in your styling and set the buttons right styling to 0 . 在样式中,将right样式按钮设置为0 This will place your button at an absolute position, according to your parental div . 根据您的父母div ,这会将您的按钮置于绝对位置。

.btt {
  position: absolute;
  right: 0;
}

Live example 现场例子

You can set width and display property to the span elements like this: 您可以像这样设置span元素的width和display属性:

.surveySummaryList a span {
      width: calc(100% - 40px);
      display: inline-block;
      word-break: break-all;
}

and add rule for your buttons width like this: 并为您的按钮宽度添加规则,如下所示:

.btt{
    width: 30px;
}

I updated the FIDDLE 我更新了FIDDLE

This is an issue I have faced a few times and usually what I do is decide the maximum length I will allow this value to be and then truncate after that with ellipsis. 这是我遇到过的几次问题,通常我要做的是确定允许的最大长度,然后用省略号将其截断。

It is up to you but I think this is a good choice to make as it is defensive coding to keep your display neat no matter what the value. 这取决于您,但是我认为这是一个不错的选择,因为它是防御性编码,可以使显示保持整洁,无论其价值如何。

Here is an example CSS class you can add to your span: 这是一个示例CSS类,您可以将其添加到跨度中:

.truncate {
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

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