简体   繁体   English

无法覆盖text-align:使用text-align进行调整:保留在简单HTML中

[英]Unable to override text-align: justify with text-align: left in simple HTML

I have an odd issue with the text-align: left not working after the element has inherited a justify property. 我对text-align: left有一个奇怪的问题text-align: left在元素继承了justify属性后, text-align: left不起作用。 I even tried using the !important keyword and it's still not working. 我什至尝试使用!important关键字,但仍无法正常工作。 The odd thing is, if I use developer mode in Chrome, it says text-align: left is active, text-align: justify is inactive, but it is NOT rendering the left align. 奇怪的是,如果我在Chrome使用开发人员模式,它 text-align: left是活动的, text-align: justify是无效的,但它并不是渲染左对齐。

Here is my code: https://jsfiddle.net/h0vx9sLc/3/ (updated Fiddle with "fix") 这是我的代码: https : //jsfiddle.net/h0vx9sLc/3/ (用“修复”更新了小提琴)

 body { width: 100px; } p:nth-child(1) { } p:nth-child(2) { text-align: justify; } p>span { text-align: left !important; } 
 <p> <span>Testing Blah Blah Blah Blah Blah</span> </p> <p> <span>Testing Blah Blah Blah Blah Blah</span> </p> 

Based on the CSS priority there really is no chance the justify property has a higher priority than left . 基于CSS优先级, justify属性的优先级确实没有比left更高的机会。 I tested this in Chrome, Firefox, and IE and all are doing the same thing. 我在Chrome,Firefox和IE中对此进行了测试,并且它们都在做同一件事。

Here is the screen shot of Chrome claiming the left property is active, but it isn't. 以下是Chrome浏览器的屏幕截图,其中声称left属性处于活动状态,但实际上并非如此。

在此处输入图片说明

Ok, this is a VERY bizarre property of CSS. 好的,这是CSS的一个非常奇怪的属性。

I figured it out with the help of this question: text-align justify, cannot override 我借助以下问题弄清楚了: 文本对齐对齐,无法覆盖

Apparently an inline element IGNORES text-align CSS, but it can INHERIT them. 显然,内联元素IGNORES文本对齐CSS,但是可以继承它们。 So my SPAN was inheriting the justify, but CSS rendering IGNORES the text-align CSS of an inline element. 所以我的SPAN继承了理由,但是CSS渲染了IGNORES,它是内联元素的文本对齐CSS。

To fix it, I add display: inline-block; 为了解决这个问题,我添加了display: inline-block; to my span. 到我的范围。 Really, really bizarre. 真的,真的很奇怪。 I'm not sure what to make of the Chrome developer issue. 我不确定该如何解决Chrome开发者问题。 I guess it should technically be a bug? 我想从技术上讲应该是个错误?

You can add a class to override the justify value or is there something I missed? 您可以添加一个class来覆盖justify值,还是我错过了一些事情?

Update: <span> s are inline elements and text-align applies to blocks of text. 更新: <span>是内联元素,并且text-align适用于文本 So you have to apply display: block or inline- block on the <span> s or apply a class , an id , or an inline style to the <p> s. 因此,您必须在<span>上应用display: blockinline- block ,或者将classid或内联样式应用于<p>

 body { width: 100px; } p~p { text-align: justify; } p>span { text-align: left !important; } .left { text-align: left !important; } 
 <p class="left"> <span>Testing Blah Blah Blah Blah Blah</span> </p> <p class="left"> <span>Testing Blah Blah Blah Blah Blah</span> </p> 

Your selector is throwing you off i think. 我认为您的选择器会让您失望。 The following from teachbrij.com explain well. 以下来自breachbrij.com的解释很好。

~ Sign: 〜标志:

It is general sibling combinator and similar to Adjacent sibling combinator. 它是通用的同级组合器,类似于相邻的同级组合器。 the difference is that the second selector does NOT have to immediately follow the first one means It will select all elements that is preceded by the former selector. 区别在于第二个选择器不必立即跟随第一个选择器,它将选择前一个选择器之前的所有元素。

Here is the W3 rundown. 这是W3概要。 Same info... 相同的信息...

I have attached your updated fiddle in which I removed the the left justify. 我附上了您更新的小提琴 ,其中删除了左对齐。 You will see that all of the multiple spans get thrown EXCEPT the first even though there is no 您会看到,除了没有第一个跨度之外,所有跨度都被抛出

align: left

set. 组。

Interesting question for sure. 当然有趣的问题。 Hope this helps. 希望这可以帮助。

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

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