简体   繁体   English

CSS 特性问题

[英]CSS specificty issue

I have a nested div like this我有一个这样的嵌套 div

<div class="myDiv">
   <div class="myOtherDiv">

In my CSS I want myOtherDiv to have margin: 0 auto; but not myDiv在我的 CSS 中,我希望myOtherDivmargin: 0 auto; but not myDiv margin: 0 auto; but not myDiv

If I write如果我写

.myDiv{
   margin: 0 auto;
}

It applies to both with the specificty of 0,1,0它适用于两者的特异性为 0,1,0

But

 .myDiv .myOtherDiv{
   margin:0 auto;
}

Nothing happens.没发生什么事。 Which is weird, visual code reports the specificity of this to be 0,2,0 which is higher so should it not apply?这很奇怪,视觉代码报告这个的特异性为 0,2,0 更高,所以它不应该适用吗?

As you have not provided all of the CSS code it is hard to tell.由于您没有提供所有的 CSS 代码,所以很难说。 However,然而,

Issues with margin: 0 auto; margin: 0 auto;问题margin: 0 auto; are you usually a result of the element not being a block element or contain a width .你通常是元素不是元素或包含宽度的结果

Since div is a block element naturally, it takes up the entire width available even if you only have 1 letter inside that div(Ie <div>A</div>由于div自然是块元素,因此即使该 div 内只有 1 个字母,它也会占用整个可用宽度(即<div>A</div>

You must declare a width before centering your div items.在居中 div 项目之前,您必须声明一个宽度。

So for example:例如:

.myDiv {
width: 50%;
margin: 0 auto;
}

Im regards to specificity ,我关于特异性

If the parent <div> doesn't have a width assigned margin:0 auto will not do anything.如果<div>没有分配宽度的margin:0 auto将不会做任何事情。 However, if you assign a width then the block will not occupy all the page's space, and then the block itself is centered, but the items of the child div will not be centered or affected.但是,如果分配宽度,则块不会占据页面的所有空间,然后块本身居中,但子div的项目不会居中或受影响。

If you assign both the child and parent div margin:0 auto without either having a set width it will produce a null effect without affecting positioning at all because each block is just taking up all the space possible within the page.如果您同时分配子和父 div margin:0 auto而没有设置宽度,它将产生空效果而根本不会影响定位,因为每个块都只占用页面内的所有可能空间。

If you want the child div block centered, then assign a width to the child div, and leave the parent div alone.如果您希望子 div 块居中,则为子 div 分配一个宽度,而不要理会父 div。 Since the parent occupies all the page's width space, the child div will be centered on the page using margin:0 auto;由于父 div 占据了页面的所有宽度空间,因此子 div 将使用margin:0 auto;居中在页面上margin:0 auto; and the width you assigned.以及您分配的宽度。

.myDiv .myOtherDiv {} or just .myOtherDiv {} should be good to target myOtherDiv. .myDiv .myOtherDiv {} 或只是 .myOtherDiv {} 应该适合定位 myOtherDiv。

Did you give the div a width?你给div一个宽度吗? Block level elements like div's default to 100% widht of their parent.像 div 这样的块级元素默认为它们父级的 100% 宽度。 So you need to set a specific smaller width, or use display: table;所以你需要设置一个特定的较小的宽度,或者使用display: table; so the content decides the width.所以内容决定了宽度。 See below:见下文:

 .myDiv{ background: blue; } .myOtherDiv{ margin: 0 auto; display: table; background: red; }
 <div class="myDiv"> <div class="myOtherDiv">content </div> </div>

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

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