将元素和fontawesome图标放在同一行中(Placing element and fontawesome icon in same line)

编程入门 行业动态 更新时间:2024-10-12 08:19:16
元素和fontawesome图标放在同一行中(Placing element and fontawesome icon in same line)

将元素放在一行中时遇到问题。

<div class="container"> <ul id="suggestion_list"> <li class="mp_search_suggest_element"> <span > <b>some bold text</b> here <i class="fa fa-bed fa-fw" aria-hidden="true"></i> </span> </li> <li class="mp_search_suggest_element"> <span > <b>some bold text</b> some long text here <i class="fa fa-bed fa-fw" aria-hidden="true"></i> </span> </li> </ul> </div>

我通过容器div中的ajax动态加载ul。 我想让文字在左边对齐,右边的图标在一行中。 因此,该行拉伸容器div以适应并且从不以两行显示文本或图标。

css:

.container { display: inline-block; position: absolute; width: auto; min-width: 350px; max-width: 600px; padding: 10px 0; } ul{ list-style-type: none; } li{ width: 100%; white-space: nowrap ; }

所以这就是我到目前为止所拥有的。

I have a problem placing elements in one line.

<div class="container"> <ul id="suggestion_list"> <li class="mp_search_suggest_element"> <span > <b>some bold text</b> here <i class="fa fa-bed fa-fw" aria-hidden="true"></i> </span> </li> <li class="mp_search_suggest_element"> <span > <b>some bold text</b> some long text here <i class="fa fa-bed fa-fw" aria-hidden="true"></i> </span> </li> </ul> </div>

I load the ul dynamically via ajax in the container div. I'd like to have the text aligned to the left, the icon to the right, in one line. So the line stretches the container div to fit in and never displays the text or the icon in two lines.

The css:

.container { display: inline-block; position: absolute; width: auto; min-width: 350px; max-width: 600px; padding: 10px 0; } ul{ list-style-type: none; } li{ width: 100%; white-space: nowrap ; }

So this is what I have so far.

最满意答案

如果您愿意,也可以使用flexbox执行此操作。 使li成为flexbox容器。

看例子:

.container {
  display: inline-block;
  position: absolute;
  width: auto;
  min-width: 350px;
  max-width: 600px;
  padding: 10px 0;
  background: #333;
}

ul {
  list-style-type: none;
  color: #eee;
}

li {
  width: 100%;
  white-space: nowrap;
  display: flex;
  justify-content: space-between;
}
li span:nth-child(2){
  padding-right: 10px;
} 
  
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css ">
<div class="container">
  <ul id="suggestion_list">
    <li class="mp_search_suggest_element">
      <span><b>some bold text</b> text here</span>
      <span><i class="fa fa-bed fa-fw" aria-hidden="true"></i></span>

    </li>
    <li class="mp_search_suggest_element">
      <span><b>some bold text</b> some long text here</span>
      <span><i class="fa fa-bed fa-fw" aria-hidden="true"></i></span>
    </li>
  </ul>
</div> 
  
 

You can also do this with flexbox if you like. make the li a flexbox container.

see example:

.container {
  display: inline-block;
  position: absolute;
  width: auto;
  min-width: 350px;
  max-width: 600px;
  padding: 10px 0;
  background: #333;
}

ul {
  list-style-type: none;
  color: #eee;
}

li {
  width: 100%;
  white-space: nowrap;
  display: flex;
  justify-content: space-between;
}
li span:nth-child(2){
  padding-right: 10px;
} 
  
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css ">
<div class="container">
  <ul id="suggestion_list">
    <li class="mp_search_suggest_element">
      <span><b>some bold text</b> text here</span>
      <span><i class="fa fa-bed fa-fw" aria-hidden="true"></i></span>

    </li>
    <li class="mp_search_suggest_element">
      <span><b>some bold text</b> some long text here</span>
      <span><i class="fa fa-bed fa-fw" aria-hidden="true"></i></span>
    </li>
  </ul>
</div> 
  
 

更多推荐

本文发布于:2023-08-08 00:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:放在   图标   元素   fontawesome   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!