简体   繁体   English

div中心文本的背景宽度不是100%

[英]Center text in div with background not 100% width

I'm using Bootstrap 3. 我正在使用Bootstrap 3。

I need to center a text (h2) in a div with a background. 我需要将div中的文本(h2)居中放置在背景中。 I need that his background not 100% width. 我需要他的背景不是100%宽。

I cannot center them in their column(s) (see screenshot of wrong behavoiur). 我无法在他们的专栏中将其居中(请参见错误行为的屏幕截图)。

在此处输入图片说明

This is HTML: 这是HTML:

<div class="col-sm-6">

            <div class="category-title">

                <h2>{{ @value->category }}</h2>

            </div>

</div>

And this is the CSS: 这是CSS:

    div.category-title{display:inline-block;
background-color:#E1001A;
padding:10px;
text-align:center;
margin:0 auto}

div.category-title > h2{color:#FFF;
text-align:center;
text-transform:uppercase;
font-size:200%;}

Thank you 谢谢

Add class text-center to the parent element of category-title 将class text-center添加到category-title的父元素

 div.category-title { display: inline-block; background-color: #E1001A; padding: 10px; margin: 0 auto } div.category-title > h2 { color: #FFF; text-transform: uppercase; font-size: 200%; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="col-sm-6 text-center"> <div class="category-title"> <h2>Offerte Pala</h2> </div> </div> <div class="col-sm-6 text-center"> <div class="category-title"> <h2>Offerte Pala</h2> </div> </div> </div> 

Try text-center in div class. 在div类中尝试text-center Something like following. 像下面这样。

<div class="col-sm-6 text-center">
        <div class="category-title">
            <h2>{{ @value->category }}</h2>
 </div>

Try class="text-center" from official Bootstrap site. 从官方Bootstrap网站尝试class="text-center" http://getbootstrap.com/css/ http://getbootstrap.com/css/

<p class="text-left">Left aligned text.</p>
<p class="text-center">Center aligned text.</p>
<p class="text-right">Right aligned text.</p>

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

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