admin管理员组文章数量:1336631
If I have the following 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. Is there a way I can reduce the font-size of div1
uniformly? That is, take the font size defined in div2
and div3
and reduce them by say 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. Any suggestions?
If I have the following 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. Is there a way I can reduce the font-size of div1
uniformly? That is, take the font size defined in div2
and div3
and reduce them by say 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. Any suggestions?
- is javascript an option or are you wanting to solely use CSS to acplish this? – Borophyll Commented Jul 11, 2012 at 2:36
- @Borophyll: Sure. I don't see why not. Just edited my question. – Legend Commented Jul 11, 2012 at 2:37
5 Answers
Reset to default 4I think that this is what you want:
Demo: http://jsfiddle/SO_AMK/JjutY/
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:
$(".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. With the following example if you change the div1 font-size it will effect both div2 and div3. See this article for a parison of the font-size unit types CSS FONT-SIZE: EM VS. PX 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 bees trivial as 100% = 10px. This also makes using the jQuery ui library a lot easier as they use this same font-size setup.
If you set your inner divs' font-sizes in ems
it's trivial. 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.
I don't think you can do that with css but if someone knows how kindly educate me as well.
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.
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:
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. 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');
and then set the style on the child nodes
本文标签: javascriptHow to change the fontsize uniformlyStack Overflow
版权声明:本文标题:javascript - How to change the font-size uniformly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372817a2462568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论