<style> body { font-family: Arial, Helvetica, sans-serif; font-size: 16px; } </style>
body
element to Arial, Helvetica, sans-serif
, this is a universal font family that works well in most browsers. We also set the font size to 16px
.
<p>If you want to use a specific font, you can bring it in using the @font-face
rule. For example, if you want to use the Roboto font from the Google font library in a web page, you can add the following code in the head tag:
<style> @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxK.woff2) format('woff2'); font-display: swap; } body { font-family: 'Roboto', sans-serif; font-size: 16px; } </style>
@font-face
The rules introduce the Roboto font and name it 'Roboto'
. Then, we specify the font family of the body element as 'Roboto', sans-serif
, where 'sans-serif'
is a general font family. We also set the font size to 16px. Please note that it is common for websites to charge for inclusion of this font, so please check for copyright information if you choose to use it.
<p>2. Use HTML tags to set fonts
<p>Similar to CSS styles, you can also use HTML tags to set fonts. Here is some sample code:
<body> <h1 style="font-family: Arial, Helvetica, sans-serif;">这是一个标题</h1> <p style="font-family: 'Roboto', sans-serif; font-size: 16px;">这是一个段落</p> </body>
<h1>
tag to create a title and set its font family to Arial, Helvetica , sans-serif
, this is the same as the previous CSS style setting. We also create a paragraph using the <p>
tag and set the font family to 'Roboto', sans-serif
and the font size to 16px. Note that if you use this method to set the font, you will need to add the corresponding style to each element, which may result in some duplicated code and unnecessary work.
<p>3. Use PHP functions to set fonts
<p>PHP provides several functions to set fonts, including functions used when creating images using the GD library. Here are some examples:
imagettftext()
Function: <?php header('Content-Type: image/png'); $im = imagecreatetruecolor(400, 100); $text_color = imagecolorallocate($im, 255, 255, 255); $font_file = 'arial.ttf'; imagettftext($im, 20, 0, 10, 50, $text_color, $font_file, '这是一个文字'); imagepng($im); imagedestroy($im); ?>
arial.ttf
and use the imagettftext()
function to write the text to the image. Please note that you need to install the GD library and related font files on the server to use this function.
pdf_set_font()
Function: <?php $pdf = new PDF(); $pdf->AddPage(); $pdf->SetFont('Arial', '', 14); $pdf->Cell(40, 10, '这是一个段落'); $pdf->Output(); ?>
pdf_set_font()## from the TCPDF library #Function to set font. First, we created a PDF document and created a new page using the
AddPage() method. Then, we use the
SetFont() method to set the font. Finally, we write text using the
Cell() method.
Summary<p>There are many ways to set fonts in PHP, including using CSS styles, HTML tags, and PHP functions. No matter what method you choose, setting up fonts is an important step in making your website more attractive and readable. Font setting is only a small part of website design. Taking the time to pay attention to every detail will allow you to make your website more prominent in design and generate more business value. <p>The above is the detailed content of How to set font in php. For more information, please follow other related articles on the PHP Chinese website!