How to set font-size

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Idiot88
New php-forum User
New php-forum User
Posts: 19
Joined: Thu Jan 25, 2018 2:56 pm

Thu Feb 01, 2018 5:42 am

I’m trying to set the font size in html so that my PHP code will output the new font size. This is my script:


<p style="font-size:30px">This line is printed ok.</p>
<p style="font-size:8px">
<?php
echo ‘This is printed but not in 8px font.’;
?>


What am I doing wrong?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 720
Joined: Mon Feb 22, 2016 5:52 pm

Thu Feb 01, 2018 8:22 am

What am I doing wrong?
Using inline style. It is considered bad practice and has been considered bad practice for many years.

Use a stylesheet and set classes if you really must keep changing font sizes in the same element type; which is quite unlikely.

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Fri Feb 02, 2018 4:41 am

Hello.

If you really need the code to work within the HTML itself then you can simply create a <style> tag and place all your classes inside it.
Honestly, I would recommend using stylesheet, sometimes I can't torture my eyes to see the mixed codes of HTML, PHP, and CSS. :?
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

Idiot88
New php-forum User
New php-forum User
Posts: 19
Joined: Thu Jan 25, 2018 2:56 pm

Fri Feb 02, 2018 8:11 am

Hi
Thanks for your replies. I would like to do it the right way but I don’t know how. Please point me in the direction of a simple style sheet example.

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Fri Feb 02, 2018 9:52 am

Hello.

Firstly, you can create a <link> tag on in your <head> tag ( or just place it at top, like after important tag such as <!DOCTYPE html> or <html> itself ), example;

<link rel="stylesheet" type="text/css" href="style.css" />

Which, you can store all of your classes inside the style.css file instead, just to distinguish your coding style between HTML and PHP with CSS.
Any changes of your CSS can be done through the 'style.css'. Not just that, you can narrow the troubleshoot process by doing these separate files. :)
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

Idiot88
New php-forum User
New php-forum User
Posts: 19
Joined: Thu Jan 25, 2018 2:56 pm

Fri Feb 02, 2018 10:12 am

Do you mean I should copy my class from the .php file to my .css file?

Thanks.

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Fri Feb 02, 2018 10:24 am

Yes! Indeed!

It will be easier. Let's have a look on your case inside the style.css file you can make it simple and specific.

For your <p> tag you can make both of them to hold different class or id which will specifically tells CSS to only make changes on the class that you chose;

On top of your HTML

Code: Select all

<link rel="stylesheet" type="text/css" href="style.css">
style.css will always be with the HTML file you included it with. So if you use XAMPP (well, I did) you place your HTML file inside 'htdocs' then your style.css should be inside the same folder as the HTML file. Somehow, that's based on everyone decision as if I would create a new folder 'css' and place all the css file inside it. So, it will be;

<link .... href="css/style.css">

Inside HTML

Code: Select all

<p class="class1">Hey look at me. I am 12px!</p>
<p class="class2">No! Look at me. I am 28px!</p>
While in style.css file you can just edit both of them, specifically.

Code: Select all

p .class1
{
	font-size : 12px;
}

p .class2
{
	font-size : 28px;
}
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Fri Feb 02, 2018 11:01 am

After reading your question, again.

I think I understood wrong at first. You really mean to style the output from PHP right?

Well, this is just theory, I have't tried it yet. So..

Code: Select all

<?php echo "<p style='font-size:8px;'>I might be in 8px</p>" ?>
You can test the code Here
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

Idiot88
New php-forum User
New php-forum User
Posts: 19
Joined: Thu Jan 25, 2018 2:56 pm

Sat Feb 03, 2018 8:12 am

Thank you Zaim.

Your example works and has helped me.

Post Reply