开发者

text-align justify, cannot override

开发者 https://www.devze.com 2023-02-22 04:27 出处:网络
I have some text with text-align: justify; the property is inherited in links and other nested inline elements i开发者_StackOverflown that text. I want to set other text-align mode to some of them, fo

I have some text with text-align: justify; the property is inherited in links and other nested inline elements i开发者_StackOverflown that text. I want to set other text-align mode to some of them, for example text-align: center;, but I cannot. Firebug shows that the parent style is overridden, but the nested inline element is still justified. I observed that strange behavior in many different browsers and obviously they are doing this by spec? Is that really by design? How to work around it?

EDIT: Some example code. The span with ID=span1 cannot define its own text align. It should be right aligned, but it is centered instead. While I was experimenting I noticed that inline elements cannot define any text alignment at all. Very strange.

<html>
<head>
<style type="text/css">
#cubic { width: 495px; height: 200px; background-color: green; text-align: center}
#span1 {text-align: right; color: red}
</style>
</head>

<body>
<p id="cubic">
<span id="span1">This is span 1</span>
</p>
</body>
</html>


Correct; inline elements cannot have a text-align set, unless they're set to display block.

Simple example:

<html>
<style>
    #spanInline {text-align:right;}
    #spanBlock {text-align:right;display:block;}
    #divBlock {text-align:right;}
    #divInline {text-align:right;display:inline;}
</style>
<body>
<span id="spanInline">asdf</span><br />
<span id="spanBlock">asdf</span><br />
<div id="divBlock">asdf</div><br />
<div id="divInline">asdf</div><br />
</body>
</html>

If you throw borders around the elements, you'll see more information on why this doesn't work.


Actually, text-align only applies to block elements, and span is not a block element. Please try the following code:

<p id="cubic">
<div id="span1">This is div 1</div>
</p>

and check if it fits to your needs.


Ran into this issue myself and a quicker fix is to use an inline-block:

#span1 { display: inline-block; text-align: right; color: red}

It isn't good to set your spans to a plain block element because block elements will add line breaks before and after the element. If you have spans inside a paragraph, it'll completely break the paragraph.

0

精彩评论

暂无评论...
验证码 换一张
取 消