如何在整个HTML文档中应用全局字体?
有时我们希望为所有元素设置一个基础属性,并允许更具体的选择器根据需要为某些元素进行调整。也有一些情况,尤其是在进行简短的临时测试时,我们希望字体应用于所有文本,无论如何。
设置全局字体的方法
您可以使用以下方法将全局字体设置为整个HTML文档。
开始使用这些方法
在这里,您将看到上面提到的每种方法的示例,以将全局字体设置为整个HTML文档。
使用通用选择器
我们可以使用CSS通用选择器(*)来选择整个HTML文档并定义字体系列。因此,整个HTML文档将遵循所有元素上定义的字体系列。
示例
在下面的示例中,我们将使用上述方法。
<!DOCTYPE html> <html> <head> <title>Online HTML Editor</title> <style> * { font-family: Verdana !important; } </style> </head> <body> <h1>Online HTML Editor</h1> <p>This is real time online HTML Editor</p> </body> </html>
输出
使用!important关键字
如果您不小心为某个元素定义了任何字体系列,并且该字体系列可能被此!important关键字覆盖,则需要CSS !important关键字。
示例
在这个示例中,我们将使用通用属性应用字体系列,并为特定元素定义另一个字体系列。
<!DOCTYPE html> <html> <head> <title>Online HTML Editor</title> <style> /* Has priority over all other font-family declarations in the document */ * { font-family: Verdana !important; } /* This won't apply due to !important */ p { font-family: Arial; } </style> </head> <body> <h1>Online HTML Editor</h1> <p>This is real time online HTML Editor</p> </body> </html>
输出
结论
现在您已经知道如何广泛地为整个文档设置属性,记住何时这样做是至关重要的。HTML/CSS 最重要的功能之一是能够根据标签、ID等对元素的外观进行精细控制。虽然可以使用 * 来为整个页面设置字体,但您应该谨慎使用,因为它可能会破坏您以后更精确地更改字体的能力。!important 非常强大,但它会削弱其余 CSS 的多功能性。
- 相关文章
- 如何在 JavaScript 中获取整个文档 HTML 作为字符串?
- 在 HTML 文档中应用颜色的不同方法
- 如何在 Vue.js 中添加或应用全局变量?
- 如何为整个 Android 应用程序设置默认字体系列?
- 如何在 XML 文档上应用 XSL 转换?
- 如何在替换整个文档的同时更新 MongoDB 文档?
- 如何创建一个 HTML 文档?
- 如何在 HTML 中设置字体颜色?
- 如何在 HTML 中更改字体大小?
- 如何在 HTML 中更改文本字体?
- 如何在 HTML 中使用小字体?
- 如何使用
标签定义 HTML 文档标题?</span></a></li> <li><a href="https://tutorialspoint.com/How-to-include-Modernizr-in-HTML-document"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M218.101 38.101L198.302 57.9c-4.686 4.686-4.686 12.284 0 16.971L353.432 230H12c-6.627 0-12 5.373-12 12v28c0 6.627 5.373 12 12 12h341.432l-155.13 155.13c-4.686 4.686-4.686 12.284 0 16.971l19.799 19.799c4.686 4.686 12.284 4.686 16.971 0l209.414-209.414c4.686-4.686 4.686-12.284 0-16.971L235.071 38.101c-4.686-4.687-12.284-4.687-16.97 0z"></path></svg><span>如何在 HTML 文档中包含 Modernizr?</span></a></li> <li><a href="https://tutorialspoint.com/how-to-add-emoji-in-html-document"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M218.101 38.101L198.302 57.9c-4.686 4.686-4.686 12.284 0 16.971L353.432 230H12c-6.627 0-12 5.373-12 12v28c0 6.627 5.373 12 12 12h341.432l-155.13 155.13c-4.686 4.686-4.686 12.284 0 16.971l19.799 19.799c4.686 4.686 12.284 4.686 16.971 0l209.414-209.414c4.686-4.686 4.686-12.284 0-16.971L235.071 38.101c-4.686-4.687-12.284-4.687-16.97 0z"></path></svg><span>如何在 HTML 文档中添加表情符号?</span></a></li> <li><a href="https://tutorialspoint.com/how-to-define-the-root-of-an-html-document"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M218.101 38.101L198.302 57.9c-4.686 4.686-4.686 12.284 0 16.971L353.432 230H12c-6.627 0-12 5.373-12 12v28c0 6.627 5.373 12 12 12h341.432l-155.13 155.13c-4.686 4.686-4.686 12.284 0 16.971l19.799 19.799c4.686 4.686 12.284 4.686 16.971 0l209.414-209.414c4.686-4.686 4.686-12.284 0-16.971L235.071 38.101c-4.686-4.687-12.284-4.687-16.97 0z"></path></svg><span>如何定义 HTML 文档的根?</span></a></li> </ul></div> <div class="library-cta"> <div> <h5 class="library-cta__title">启动你的<span class="text-yellow-400">职业生涯</span></h5> <p>完成课程获得认证</p> <a target="_blank" href="https://tutorialspoint.com/latest/certifications" class="library-cta__button button button--yellow">开始学习</a> </div> <img src="/static/images/library-cta.svg" alt=""> </div> <div class="library-page-bottom-nav "> <button class="button button--blue" id="print-page"> <svg fill="white" xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512"><path d="M112 160V64c0-8.8 7.2-16 16-16H357.5c4.2 0 8.3 1.7 11.3 4.7l26.5 26.5c3 3 4.7 7.1 4.7 11.3V160h48V90.5c0-17-6.7-33.3-18.7-45.3L402.7 18.7C390.7 6.7 374.5 0 357.5 0H128C92.7 0 64 28.7 64 64v96h48zm16 208H384v96H128V368zm-16-48c-17.7 0-32 14.3-32 32H48V256c0-8.8 7.2-16 16-16H448c8.8 0 16 7.2 16 16v96H432c0-17.7-14.3-32-32-32H112zm320 80h48c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64V368c0 17.7 14.3 32 32 32H80v80c0 17.7 14.3 32 32 32H400c17.7 0 32-14.3 32-32V400z"></path></svg>打印页面</button> <div class="flex-group"> <a href="/articles/index.php"> <button class="button button--neutral"> <svg xmlns="http://www.w3.org/2000/svg" width="10" height="16" viewBox="0 0 10 16" fill="none"><path d="M1.03117 8.48836C0.64065 8.09783 0.64065 7.46467 1.03117 7.07414L7.39514 0.710183C7.78566 0.319658 8.41883 0.319658 8.80935 0.710183C9.19987 1.10071 9.19987 1.73387 8.80935 2.1244L3.15249 7.78125L8.80935 13.4381C9.19987 13.8286 9.19987 14.4618 8.80935 14.8523C8.41882 15.2428 7.78566 15.2428 7.39513 14.8523L1.03117 8.48836ZM3.12109 8.78125L1.73828 8.78125L1.73828 6.78125L3.12109 6.78125L3.12109 8.78125Z" fill="black"></path></svg> 上一篇 </button> </a> <a href="https://tutorialspoint.com/How-to-include-Modernizr-in-HTML-document"> <button class="button ">下一篇 <svg xmlns="http://www.w3.org/2000/svg" width="10" height="16" viewBox="0 0 10 16" fill="none"><path d="M8.87117 8.48836C9.26169 8.09783 9.26169 7.46467 8.87117 7.07414L2.50721 0.710183C2.11668 0.319658 1.48352 0.319658 1.09299 0.710183C0.70247 1.10071 0.70247 1.73387 1.09299 2.1244L6.74985 7.78125L1.093 13.4381C0.702471 13.8286 0.702471 14.4618 1.093 14.8523C1.48352 15.2428 2.11668 15.2428 2.50721 14.8523L8.87117 8.48836ZM6.78125 8.78125L8.16406 8.78125L8.16406 6.78125L6.78125 6.78125L6.78125 8.78125Z" fill="white"></path></svg> </button> </a></div> </div> <div class="error-success" id="error-success" style="display: none;"></div> <div class="error-warning" id="error-warning"></div> <script src="/articles/js/article.js?v=2.6"> </script> <div class="bottom-library-ads mt" style="margin:5px;"> <div class="google-bottom-ads" id="google-bottom-ads" style="height:450px;"> <div>广告</div> <div> <div id="ezoic-pub-ad-placeholder-131"></div> <div id="ezoic-pub-ad-placeholder-135"></div> <script> ezstandalone.cmd.push(function() { var width = window.innerWidth; if( width <= 768 ){ ezstandalone.showAds(135); document.getElementById("ezoic-pub-ad-placeholder-131").remove(); document.getElementById("google-right-ads").remove(); }else{ ezstandalone.showAds(131); document.getElementById("ezoic-pub-ad-placeholder-135").remove(); } }); </script> </div> </div> </div> </div> <div> <div class="data-sticky" id="google-right-ads"> <div class="google-right-ad" style="margin: 0px auto !important;margin-top:5px;min-height:280px!important"> <div id="ezoic-pub-ad-placeholder-127"></div> <script> ezstandalone.cmd.push(function() { ezstandalone.showAds(127); }); </script> </div> <div class="google-right-ad" style="margin-top:16px;min-height:280px!important"> <div id="ezoic-pub-ad-placeholder-128"></div> <script> ezstandalone.cmd.push(function() { ezstandalone.showAds(128); }); </script> </div> <div class="google-right-ad" style="margin-top:16px;margin-bottom:15px;min-height:600px!important"> <div id="ezoic-pub-ad-placeholder-129"></div> <script> ezstandalone.cmd.push(function() { ezstandalone.showAds(129); }); </script> </div> </div> </div> </div> </div> </main> <footer class="footer bg-neutral-800"> <div class="container"> <div> <h5>TOP TUTORIALS</h5> <ul> <li><a href="/python/index.htm" title="Python Tutorial">Python Tutorial</a></li> <li><a href="/java/index.htm" title="Java Tutorial">Java Tutorial</a></li> <li><a href="/cplusplus/index.htm" title="C++ Tutorial">C++ Tutorial</a></li> <li><a href="/cprogramming/index.htm" title="C Programming Tutorial">C Programming Tutorial</a></li> <li><a href="/csharp/index.htm" title="C# Tutorial">C# Tutorial</a></li> <li><a href="/php/index.htm" title="PHP Tutorial">PHP Tutorial</a></li> <li><a href="/r/index.htm" title="R Tutorial">R Tutorial</a></li> <li><a href="/html/index.htm" title="HTML Tutorial">HTML Tutorial</a></li> <li><a href="/css/index.htm" title="CSS Tutorial">CSS Tutorial</a></li> <li><a href="/javascript/index.htm" title="JavaScript Tutorial">JavaScript Tutorial</a></li> <li><a href="/sql/index.htm" title="SQL Tutorial">SQL Tutorial</a></li> </ul> </div> <div> <h5>TRENDING TECHNOLOGIES</h5> <ul> <li><a href="/cloud_computing/index.htm" title="Cloud Computing Tutorial">Cloud Computing Tutorial</a></li> <li><a href="/amazon_web_services/index.htm" title="Amazon Web Services Tutorial">Amazon Web Services Tutorial</a></li> <li><a href="/microsoft_azure/index.htm" title="Microsoft Azure Tutorial">Microsoft Azure Tutorial</a></li> <li><a href="/git/index.htm" title="Git Tutorial">Git Tutorial</a></li> <li> <a href="/ethical_hacking/index.htm" title="Ethical Hacking Tutorial">Ethical Hacking Tutorial</a></li> <li><a href="/docker/index.htm" title="Docker Tutorial">Docker Tutorial</a></li> <li><a href="/kubernetes/index.htm" title="Kubernetes Tutorial">Kubernetes Tutorial</a></li> <li><a href="/data_structures_algorithms/index.htm" title="DSA Tutorial">DSA Tutorial</a></li> <li><a href="/spring_boot/index.htm" title="Spring Boot Tutorial">Spring Boot Tutorial</a></li> <li><a href="/sdlc/index.htm" title="SDLC Tutorial">SDLC Tutorial</a></li> <li><a href="/unix/index.htm" title="Unix Tutorial">Unix Tutorial</a></li> </ul> </div> <div> <h5>CERTIFICATIONS</h5> <ul> <li><a href="/certification/business-analytics-certification-2023/index.asp" title="Business Analytics Certification">Business Analytics Certification</a></li> <li><a href="/certification/java-prime-pack/index.asp" title="Java & Spring Boot Advanced Certification">Java & Spring Boot Advanced Certification</a></li> <li><a href="/certification/data-science-advanced-certification/index.asp" title="Data Science Advanced Certification">Data Science Advanced Certification</a></li> <li><a href="/certification/cloud-computing-and-devops-advanced-certification/index.asp" title="Cloud Computing And DevOps">Cloud Computing And DevOps</a></li> <li><a href="/certification/advanced-certification-in-business-analytics/index.asp" title="Advanced Certification In Business Analytics">Advanced Certification In Business Analytics</a></li> <li><a href="/certification/artificial-intelligence-and-machine-learning-certification/index.asp" title="Artificial Intelligence And Machine Learning">Artificial Intelligence And Machine Learning</a></li> <li><a href="/certification/devops-certification/index.asp" title="DevOps Certification">DevOps Certification</a></li> <li><a href="/certification/game-development-prime-pack/index.asp" title="Game Development Certification">Game Development Certification</a></li> <li><a href="/certification/frontend-developer-certification/index.asp" title="Front-End Developer Certification">Front-End Developer Certification</a></li> <li><a href="/certification/aws-prime-pack/index.asp" title="AWS Certification Training">AWS Certification Training</a></li> <li><a href="/certification/complete-python-prime-pack/index.asp" title="Python Programming Certification">Python Programming Certification</a></li> </ul> </div> <div> <h5>COMPILERS & EDITORS</h5> <ul> <li><a href="/online_java_compiler.php" title="Online Java Compiler">Online Java Compiler</a></li> <li><a href="/online_python_compiler.php" title="Online Python Compiler">Online Python Compiler</a></li> <li><a href="/execute_golang_online.php" title="Online Go Compiler">Online Go Compiler</a></li> <li><a href="/compile_c_online.php" title="Online C Compiler">Online C Compiler</a></li> <li><a href="/compile_cpp_online.php" title="Online C++ Compiler">Online C++ Compiler</a></li> <li><a href="/online_csharp_compiler.php" title="Online C# Compiler">Online C# Compiler</a></li> <li><a href="/execute_php_online.php" title="Online PHP Compiler">Online PHP Compiler</a></li> <li><a href="/execute_matlab_online.php" title="Online MATLAB Compiler">Online MATLAB Compiler</a></li> <li><a href="/execute_bash_online.php" title="Online Bash Compiler">Online Bash Compiler</a></li> <li><a href="/execute_sql_online.php" title="Online SQL Compiler">Online SQL Compiler</a></li> <li><a href="/online_html_editor.php" title="Online Html Editor">Online Html Editor</a></li> </ul> </div> </div> <ul class="footer__list container"> <li><a href="/about/index.htm" title="ABOUT US">ABOUT US</a> | </li> <li><a href="/about/about_team.htm" title="OUR TEAM">OUR TEAM</a> | </li> <li><a href="/about/about_careers.htm" title="CAREERS">CAREERS</a> | </li> <li><a href="/job_search.php" title="JOBS">JOBS</a> | </li> <li><a href="/about/contact_us.htm" title="CONTACT US">CONTACT US</a> | </li> <li><a href="/about/about_terms_of_use.htm" title="TERMS OF USE">TERMS OF USE</a> | </li> <li><a href="/about/about_privacy.htm" title="PRIVACY POLICY">PRIVACY POLICY</a> | </li> <li><a href="/about/return_refund_policy.htm" title="REFUND POLICY">REFUND POLICY</a> | </li> <li><a href="/about/about_cookies.htm" title="COOKIES POLICY">COOKIES POLICY</a> | </li> <li><a href="/about/faq.htm" title="FAQ'S">FAQ'S</a></li> </ul> <div class="footer__socials container"> <img class="footer__logo" src="https://tutorialspoint.com/static/images/logo-footer.svg" alt="tutorials point logo"> <div> <a rel="nofollow" target="_blank" href="https://#/tutorialspointindia" title="Follow us on Facebook"><i class="fab fa-2x fa-facebook"></i></a> <a target="_blank" href="https://twitter.com/tutorialspoint" rel="nofollow" title="Follow us on Twitter"><i class="fab fa-2x fa-x-twitter"></i></a> <a target="_blank" href="https://www.youtube.com/channel/UCVLbzhxVTiTLiVKeGV7WEBg" rel="nofollow" title="Follow us on Youtube"><i class="fab fa-2x fa-youtube"></i></a> <a target="_blank" href="https://www.linkedin.com/company/tutorialspoint/" rel="nofollow" title="Follow us on LinkedIn"><i class="fab fa-2x fa-linkedin"></i></a> <a target="_blank" href="https://www.instagram.com/tutorialspoint_/" rel="nofollow" title="Follow us on Instagram"><i class="fab fa-2x fa-instagram"></i></a> </div> <div class="flex-group"> <button class="button-reset"><a href="https://play.google.com/store/apps/details?id=com.tutorialspoint.onlineviewer" target="_blank" title="Download Android App" rel="nofollow"><img src="https://tutorialspoint.com/static/images/googleplay.svg" alt="Download Android App"></a></button> <button class="button-reset"><a href="https://itunes.apple.com/us/app/tutorials-point/id914891263?ls=1&mt=8" target="_blank" title="Download IOS App" rel="nofollow"><img src="https://tutorialspoint.com/static/images/appstore.svg" alt="Download IOS App"></a></button> </div> </div> <div class="footer__legal-wrapper"> <div class="footer__legal ff-nunito"> <p> </p><p class="">Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical and non-technical subjects.</p> <p></p> <p class="footer__copyright ">© Copyright 2024. All Rights Reserved.</p> </div> </div> </footer> <script src="/static/js/lib-script.js?v12.64"></script> <script src="https://#/gsi/client" async="" defer=""></script> <script> function addParagraphs(ind) { let techLinks =[["Python","JavaScript","Spring Boot","Java","Linux/Unix","C#","Data Science","MySQL","Artificial Intelligence","SQL"], ["https://tutorialspoint.com/certification/complete-python-prime-pack/index.asp?utm_source=tutorialspoint&utm_medium=python_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/javascript-prime-pack/index.asp?utm_source=tutorialspoint&utm_medium=javascript_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/ultimate-guide-to-java-and-spring-boot-for-2022/index.asp?utm_source=tutorialspoint&utm_medium=java_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/ultimate-guide-to-java-and-spring-boot-for-2022/index.asp?utm_source=tutorialspoint&utm_medium=spring_boot_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/linux-system-administrator-certification/index.asp?utm_source=tutorialspoint&utm_medium=unix_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/chash-and-net-prime-pack/index.asp?utm_source=tutorialspoint&utm_medium=csharp_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/data-science-prime-pack/index.asp?utm_source=tutorialspoint&utm_medium=data_science_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/mysql-technologies-pack/index.asp?utm_source=tutorialspoint&utm_medium=mysql_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/artificial-intelligence-and-machine-learning-certification/index.asp?utm_source=tutorialspoint&utm_medium=artificial_intelligence_tutorial_3p&utm_campaign=internal", "https://tutorialspoint.com/certification/mysql-technologies-pack/index.asp?utm_source=tutorialspoint&utm_medium=sql_tutorial_3p&utm_campaign=internal"]] var p2 = ''; if(ind>=0){ p2 = p2 + '<a style="text-decoration:none;" href="' + techLinks[1][ind] +'" target="_blank" ><p class="prmt_ad">Learn <strong>'+ techLinks[0][ind] + '</strong> in-depth with real-world projects through our <strong>' + techLinks[0][ind] + ' certification course</strong>. Enroll and become a certified expert to boost your career.</p></a>'; } else{ p2 = p2 + '<a href="https://tutorialspoint.com/latest/courses?utm_source=tutorialspoint&utm_medium=tutorials_3p&utm_campaign=internal" style="text-decoration:none;" target="_blank"><p class="prmt_ad">Explore our <strong>latest online courses</strong> and learn new skills at your own pace. Enroll and become a certified expert to boost your career.</p></a>'; } (p2).insertBefore(('.tutorial-content h2').eq(2)); } $(document).ready(function() { var url= window.location.href.toLowerCase(); if(url.indexOf('python') > -1){ addParagraphs(0); } else if(url.indexOf('javascript') > -1){ addParagraphs(1); } else if(url.indexOf('spring_boot') > -1){ addParagraphs(2); } else if(url.indexOf('java') > -1){ addParagraphs(3); } else if((url.indexOf('unix') > -1)||(url.indexOf('linux') > -1)){ addParagraphs(4); } else if((url.indexOf('csharp') > -1)||(url.indexOf('chash') > -1)){ addParagraphs(5); } else if(url.indexOf('data_science') > -1){ addParagraphs(6); } else if(url.indexOf('mysql') > -1){ addParagraphs(7); } else if((url.indexOf('artificial_intelligence') > -1)||(url.indexOf('artificial-intelligence') > -1)){ addParagraphs(8); } else if(url.indexOf('sql') > -1){ addParagraphs(9); } else{ addParagraphs(-1); } }); </script> <script> if(getCookie('user_id') == '' || getCookie('user_id') == null){ window.onload = function() { initializeGoogleOneTap(); }; } </script> <script src="https://tutorialspoint.com/fontawesome/js/all.min.js?v2.9"></script> <script src="https://#/cmp.min.js" data-cfasync="false"></script> <script async="" src="/static/js/ezoic-ad-inserter.js?v4.2"></script> <!-- Google tag (gtag.js) --> <script async="" src="https://#/gtag/js?id=G-E4SW76VCNP"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-E4SW76VCNP'); </script> <!-- New Facebook Pixel Code --> <script> !function(f,b,e,v,n,t,s) {if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)}; if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t,s)}(window,document,'script', 'https://#/en_US/fbevents.js'); fbq('init', '854536859149047'); fbq('track', 'PageView'); </script> <!-- End facebook Pixel Code --> <footer style="margin: 2em 2em 4em; font-size: small; text-align: center;"> © <script>document.write(new Date().getFullYear() + ' ' + location.host)</script>. All rights reserved. </footer></body></html>