如何在不改变按钮大小的情况下将文本包装在bootstrap按钮中?

时间:2017-08-09 12:44:27

标签: html css twitter-bootstrap twitter-bootstrap-3

我试图在不更改按钮大小的情况下在自举按钮内包装或调整文本大小。我有几个必须对齐的按钮

我使用过这个类,文字是换行但按钮的大小会影响与其他按钮的对齐

 .btn-wrap-text {
    white-space: normal !important;
    word-wrap: break-word !important;
}

有示例代码,只需调整视图大小: https://jsfiddle.net/mrapi/3yv314dx/1/

感谢

3 个答案:

答案 0 :(得分:2)

您可以更改字体大小。如果您不想更改字体大小,请使用以下代码

.btn-wrap-text {
  overflow: hidden;
  white-space: nowrap;
  display: inline-block;
  text-overflow: ellipsis;
}

答案 1 :(得分:2)

您可以使用解决方案https://jsfiddle.net/3yv314dx/3/



$('[data-toggle="tooltip"]').tooltip(); 

.btn-outline {
    background-color: transparent;
    color: inherit;
    transition: all .5s;
}

.btn-wrap-text {
    overflow: hidden;
  white-space: nowrap;
  display: inline-block;
  text-overflow: ellipsis;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
    <div class="row">
    <div class="col-sm-6">
    	        <div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE                        
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                       ARTICLE                  
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                         ARTICLE WITH LONGER NAME
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                         ARTICLE       
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE               
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                       ARTICLE             
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE WITH LONGER NAME
                    </button>
                </div>
				<div class="col-sm-4 col-md-4"> 
                    <button class="btn btn-success btn-sm btn-block btn-outline btn-wrap-text" data-toggle="tooltip" title="ARTICLE WITH LONGER NAME">
                       ARTICLE WITH LONGER NAME
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE                        
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE                         
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE                        
                    </button>
                </div>
               <div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE                        
                    </button>
                </div>
				<div class="col-sm-4 col-md-4">
                    <button class="btn btn-success btn-sm btn-block btn-outline">
                        ARTICLE                        
                    </button>
                </div>
            </div>
			
			
           </div>  
  </div>
&#13;
&#13;
&#13;

在解决方案中,我使用椭圆来截断额外的字符&amp;显示使用tooltip

的整个文字

答案 2 :(得分:2)

不确定为什么所有复杂的解决方案。

只需将以下内容添加到.btn css:

即可
white-space: normal;

因此,如果您的全局css文件中已有.btn,请执行以下操作:

.btn {
    white-space: normal;
}

或者,如果您的全局css文件中没有任何内容,只需内联,例如:

<button type="button" class="btn btn-primary" style="white-space: normal;">This is some button with text that should wrap</button>
  

注意:此方法可能不适用于IE的古老版本