简体   繁体   English

在CSS中使用不同样式的文本具有相同的宽度

[英]Making text with different styles the same width in CSS

I was wondering if there was a way to make an html element with the same text in it but in different styles take up the same amount of space without giving it a fixed width. 我想知道是否有一种方法可以使html元素具有相同的文本,但是采用不同的样式占用相同的空间而又不给其固定的宽度。

Here's a jsfiddle of what I'm talking about. 是我在说的东西。

I want both divs to have the same width despite having slightly different styles. 我希望两个div具有相同的宽度,尽管风格略有不同。 The text can change so I can't give the div a fixed width or use fixed padding or margins to achieve this either. 文本可以更改,所以我不能给div一个固定的宽度或使用固定的填充或边距来实现这一点。 Any ideas? 有任何想法吗?

HTML: HTML:

<div class="normal">Lorem ipsum dolor sit amet.</div><br />
<div class="disabled">Lorem ipsum dolor sit amet.</div>

CSS: CSS:

div {
    padding:5px;
    display:inline-block;
    border:1px solid black;
    background-color:lightgray;
}

.normal {
    font-weight:bolder;
}

.disabled {
    font-weight:lighter;
    font-style:italic;
    color:#666666;
}

EDIT: The two different styled divs will not be next to each other in its application. 编辑:两个不同风格的div在其应用程序中不会彼此相邻。 They're the same div, I just want the size of the div not to change when the different style is applied. 它们是相同的div,我只想在应用不同的样式时不改变div的大小。 Here's a better example: http://jsfiddle.net/cbargren/4b8KQ/3/ 这是一个更好的例子: http//jsfiddle.net/cbargren/4b8KQ/3/

Wrap them in a div with display: inline-block , and remove display: inline-block from the child div s. display: inline-block将它们包装在一个div ,然后从子div删除display: inline-block

http://jsfiddle.net/thirtydot/4b8KQ/1/ http://jsfiddle.net/thirtydot/4b8KQ/1/

HTML: HTML:

<div class="foo">
    <div class="normal">Lorem ipsum dolor sit amet.</div>
    <div class="disabled">Lorem ipsum dolor sit amet.</div>
</div>

CSS: CSS:

.foo {
    display: inline-block;
}
.foo div {
    padding:5px;
    border:1px solid black;
    background-color:lightgray;
}

.normal {
    font-weight:bolder;
}

.disabled {
    font-weight:lighter;
    font-style:italic;
    color:#666666;
}

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

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