简体   繁体   English

覆盖Bootstrap 3 CSS样式

[英]Overriding Bootstrap 3 CSS styles

I have this style: 我有这种风格:

.form-style{
   margin: 50px auto;/* also tried margin: 50px auto !important; */
   /* other styles */
 }

And I use it in my div element like this: 我在div元素中这样使用它:

<div class="row">
<div id="myDiv" class="form-style col-md-6 col-md-offset-3">
<p>This is a text.</p>
</div>
</div>

If I don't use form-style my div is appear at the center. 如果我不使用form-style div会出现在中间。 But I want to use form-style and when I use it, the margin property of the form-style will prevent the bootstrap col-md-offset-3 to make my div center. 但是我想使用form-style ,当我使用它时, form-style的margin属性将阻止bootstrap col-md-offset-3使我的div居中。 How can I override the parent margin so that it haven't been set for my div? 如何覆盖父边距,以便尚未为div设置父边距?

If I remove the margin from form-style it works fine. 如果我从form-style删除页margin ,它将正常工作。 But I can't remove the margin since it is used in other parts of my project. 但是我无法删除边距,因为它已在我的项目的其他部分中使用。

Not sure why you want something like this as it seems a hack but your issue is that your element is floating that's why the margin auto is not working, so you need to remove the floating to make it working. 不知道为什么想要这样的东西,因为它看起来很hack,但是问题是您的元素是浮动的 ,这就是margin自动功能不起作用的原因,因此您需要删除浮动以使其起作用。 (but it's not a good idea as it will make bootstrap behave strange) (但这不是一个好主意,因为它会使引导程序表现得很奇怪)

 .form-style { margin: 50px auto!important; float: none!important; /* other styles */ } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <div class=container> <div class="row"> <div id="myDiv" class="form-style col-xs-6 col-xs-offset-3"> <p>This is a text.</p> </div> </div> </div> 

From your question is seems that you don't want to replace the offset value of left to right, but want the margin set to the top and bottom. 从您的问题来看,您似乎不想替换从左到右的偏移值,而是希望将margin设置为顶部和底部。 If this is the case this will work without any override of Bootstrap values. 如果是这种情况,它将在没有任何Bootstrap值覆盖的情况下起作用。 Also, adding the div id will not affect any other places that form-style is used. 此外,添加div id不会影响使用form-style任何其他位置。

 #myDiv.form-style { margin-top: 50px; margin-bottom: 50px; border: 1px solid; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" /> <div class="row"> <div id="myDiv" class="form-style col-md-6 offset-md-3"> <p>This is a text.</p> </div> </div> 

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

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