简体   繁体   English

如何统一更改字体大小?

[英]How to change the font-size uniformly?

If I have the following HTML: 如果我有以下HTML:

<div class="div1">
   <div class="div2">
   </div>
   <div class="div3">
   </div>
</div>

div2 and div3 have their own CSS with font-sizes etc. defined. div2div3有自己的CSS,字体大小等定义。 Is there a way I can reduce the font-size of div1 uniformly? 有没有办法可以统一减少div1的字体大小? That is, take the font size defined in div2 and div3 and reduce them by say 2px ? 也就是说,采用div2div3定义的字体大小并将它们div3 2px This is similar to how you do Select All -> Ctrl + Shift + < to reduce the size of the selected text by 1 point in word processing software. 这类似于如何Select All -> Ctrl + Shift + <在文字处理软件中将所选文本的大小减少1个点。 Any suggestions? 有什么建议么?

I think that this is what you want: 我认为这就是你想要的:

Demo: http://jsfiddle.net/SO_AMK/JjutY/ 演示: http//jsfiddle.net/SO_AMK/JjutY/

HTML: HTML:

<div class="div1">
   <div class="div2">
       Some text, and more, and more, and more, and more, and more, and more, and more, and more.
   </div>
   <div class="div3">
       Some text, and more, and more, and more, and more, and more, and more, and more, and more.
   </div>
    <button class="increaseFontSize">Increase Font Size</button>
</div>​

jQuery: jQuery的:

$(".increaseFontSize").click(function(){
        var fontSize = getFontSize($(".div3"));
        var newFontSize = fontSize + 2;
        $(".div3").css("font-size", newFontSize);
        return false;
});
function getFontSize(element) {
    var currentSize = $(element).css("font-size");
    var currentSizeNumber = parseFloat(currentSize);
    return currentSizeNumber;
}

If you set the font-size with a percentage or em then yes you can, but not with pixels. 如果您使用百分比或em设置font-size,那么您可以,但不能使用像素。 With the following example if you change the div1 font-size it will effect both div2 and div3. 使用以下示例如果更改div1字体大小,它将同时影响div2和div3。 See this article for a comparison of the font-size unit types CSS FONT-SIZE: EM VS. 有关字体大小单位类型CSS FONT-SIZE:EM VS的比较,请参阅此文章 PX VS. PX VS. PT VS. PT VS. PERCENT 百分

.div1{
    font-size:16px;
}

.div2{
    font-size:80%;
}

.div3{
    font-size:90%;
}

Also here is what I use in my websites to setup the font-sizes: 这也是我在我的网站中使用的设置字体大小:

/* percentage to px scale (very simple)
 80% =  8px
100% = 10px
120% = 12px
140% = 14px
180% = 18px
240% = 24px
260% = 26px
*/

body
{
    font-family: Arial, Helvetica, sans-serif;
    font-size:10px;
}

By setting the font-size properties like that for the body element, setting the font-size for everything else becomes trivial as 100% = 10px. 通过设置body元素的font-size属性,为其他所有内容设置font-size变得微不足道,因为100%= 10px。 This also makes using the jQuery ui library a lot easier as they use this same font-size setup. 这也使得使用jQuery ui库变得更加容易,因为它们使用相同的字体大小设置。

If you set your inner divs' font-sizes in ems it's trivial. 如果你在ems设置内部div的字体大小,这是微不足道的。 If you had this... 如果你有这个......

.div2 {
    font-size: 1.8em; /*180% of inherited font-size */
}

.div3 {
    font-size: 1.4em; /*140% of inherited font-size */
}

... then if you'd change div1's font-size , .div2 and .div3's font-size s would change proportionally. ...然后,如果你改变div1的font-size ,.div2和.div3的font-size将按比例改变。

I don't think you can do that with css but if someone knows how kindly educate me as well. 我不认为你可以用css做到这一点,但如果有人知道如何善待我。

the only solution i can think of is to nest them so that elements with div2 and div3 won't get affected and this will only affect div2 and div3 that are nested under div1 elements. 我能想到的唯一解决方案是嵌套它们,以便div2和div3的元素不会受到影响,这只会影响嵌套在div1元素下的div2和div3。

    div.div2 { font-size:14px; }
    div.div3 { font-size:12px; }

    div.div1 div.div2 { font-size:12px; }
    div.div1 div.div3 { font-size:10px; }

basic idea with javascript: 用javascript的基本想法:

var divs = document.getElementsByTagName('div');
for(int i = 0; i < divs.Length; ++i) {
  var element = divs[i];
  element.style.setAttribute('font-size', '2px');  //whatever you need to set it to
}

The above will change the font size for every div. 以上将改变每个div的字体大小。 Obviously this is probably not quite going to do it for you... but you could do a few things to modify the code. 显然这可能不适合你...但你可以做一些事情来修改代码。 You could get your top-level div with document.getElementById('id'); 您可以使用document.getElementById('id');获取顶级div document.getElementById('id'); and then set the style on the child nodes 然后在子节点上设置样式

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

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