简体   繁体   English

防止文字在图标下换行

[英]Prevent text from wrapping under icon

I'm trying to create a list of items and each item needs to have an icon on the left. 我正在尝试创建一个项目列表,每个项目都需要在左侧有一个图标。 For long named items the text is wrapping under the icon. 对于长名称的项目,文本会在图标下环绕。 How do I prevent this from happening? 如何防止这种情况发生?

Below in the image you can see the text "Created" is going under the file icon: 在图像的下方,您可以看到文本“ Created”正在文件图标下:

在此处输入图片说明

I'm using bootstrap and this is how I'm creating each item: 我正在使用引导程序,这就是我创建每个项目的方式:

var renderActivity = function(a) {
  var html = "";
  html += "<a title='" + a.title + "' href='" + a.path +"' class='list-group-item clearfix" + a.type + "'>";
  html +=   "<span class='glyphicon glyphicon-"+a.icon+" pull-left fa-3x'></span>";
  html +=   "<div>";
  html +=       "<h3 class='list-group-item-heading'>" + a.type + ": " + a.title + "</h3>";
  html +=       "Created: " + a.created
  html +=   "</div>";
  html += "</a>";

  return html
}

You have to float: left the div after the p tag. 您必须float: leftdiv放在p标签之后。 What is currently hapening is how a floating element is expected to behave with simple text surrounding it. 当前令人发指的是, 浮动元素应如何在周围带有简单文本的情况下运行。 Just like images in newspaper are floated to left and right and the text goes sideways to the images and then passes below the image when the runs of text extends below the image. 就像报纸中的图像左右浮动一样,文本从侧面移到图像上,然后在文本行延伸到图像下方时在图像下方通过。

What you have to do is also float the div after the p tag. 你有什么做的也是floatdivp标签。 But to make it float you will have to give it specific width too. 但是要使其浮动,您也必须给其指定宽度。 The following code should solve your problem: 以下代码可以解决您的问题:

  a { display: block; float: left; max-width: 400px; border: 1px solid cyan; padding: 2px; } a > p { float: left; border: 1px dotted green; margin-right: 2px; } a > div { float: left; max-width: 350px; border: 1px dotted red; } 
  <a href=""> <p> <img src="https://i.stack.imgur.com/8lMxs.png" width="24" height="24" alt=""> </p> <div> <span> <h3>File my gcr labour relations, greiviences, improvements and desiplnies and on and on</h3> Created: monday morning for al of em. </span> </div> </a> 

PS: If you are using bootstrap then I'd suggest to use media object in these scenarios. PS:如果您正在使用引导程序,那么我建议在这些情况下使用媒体对象 They use tables layout for, may be, better old browser compatibly. 他们使用表布局来兼容更好的旧浏览器。

There are a lot of solutions. 有很多解决方案。 One way: 单程:

https://jsfiddle.net/4r9fwwys/ https://jsfiddle.net/4r9fwwys/

 .txt-part{ float: left; padding: 0 0 0 10px; display: inline-block; } .glyphicon { float: left; font-size: 30px; display: inline-block; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <a title="title" href="path" class="list-group-item clearfix type"> <span class="glyphicon glyphicon-open pull-left fa-3x"></span> <div class="txt-part"> <h3 class="list-group-item-heading"> type:title </h3> <span class="created">created</span> </div> </a> <a title="title" href="path" class="list-group-item clearfix type"> <span class="glyphicon glyphicon-remove pull-left fa-3x"></span> <div class="txt-part"> <h3 class="list-group-item-heading"> type:title </h3> <span class="created">created</span> </div> </a> 

Down side of this - can become ugly on long content or small screen. 缺点-在长内容或小屏幕上可能变得难看。 But at least for small screen You could add one more class to that icon span - hidden-xs (hidden-xs-down for Bootstrap v4) 但是至少对于小屏幕而言,您可以在该图标范围中再添加一个类-hidden-xs(对于Bootstrap v4,它是hidden-xs-down)

But I would try to place that icon in a :before style like this: 但我会尝试将图标放在:before样式中,如下所示:

https://jsfiddle.net/f5jzeksm/ https://jsfiddle.net/f5jzeksm/

 a > h3, a > span { padding-left: 15px; } .created { font-size: 80%; } h3 { poaition: relative; } h3:before { font-family: 'Glyphicons Halflings'; position: absolute; left: 2px; } h3.icon-glyphicon-open:before { content: '\\e167'; } h3.icon-glyphicon-remove:before { content: '\\e014'; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <a title="title" href="path" class="list-group-item clearfix type"> <h3 class="list-group-item-heading icon-glyphicon-open"> type:title </h3> <span class="created">created</span> </a> <a title="title" href="path" class="list-group-item clearfix type"> <h3 class="list-group-item-heading icon-glyphicon-remove"> type:title </h3> <span class="created">created</span> </a> 

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

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