Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the maximum size for a <h1> element?

Tags:

html

css

I have a <h1 style="font-size:2.5vw;">Example</h1> in my HTML file. I've been testing how it looks in different viewport sizes using 'Chrome DevTools' (Google Chrome's developer tools).

I found that the header is too large compared to the rest of the content in my HTML when the device is set to 'iPad Pro' (1024x1366).

In order to resolve this issue, I was wondering if there was a way to set a maximum size for the header element whilst incorporating "font-size:2.5vw;" (I need it to still be responsive with respect to the size of the viewport)?

I tried adding max-width://size into the style field of the element, but this did not make a difference.

NOTE: My meta element looks like this: <meta name="viewport" content="initial-scale=0.41, maximum-scale=1.0" />

like image 895
bgmn Avatar asked Apr 25 '20 21:04

bgmn


People also ask

How do I change the size of an H1 tag?

How to Change Font Size in HTML. To change font size in HTML, use the CSS font-size property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.

Can we change size of H1 tag in HTML?

Yes, from a technical point of view you can use whatever font size you like for whatever element you like.

What font size would the H1 elements be?

H1: 32 pt (30–34pt) H2: 26 pt (24–28pt) H3: 22 pt (20–24pt) H4: 20 pt (18–22pt)

How do I set maximum font size in HTML?

font-size: 3vw; means that the font size will be 3% of the viewport width. So when the viewport width is 1200px - the font size will be 3% * 1200px = 36px. So a max-font-size of 36px can be easily implemented using a single media query to override the default 3vw font-size value.


Video Answer


2 Answers

What you are looking for is clamp

p { font-size: clamp(1rem, 2.5vw, 1.5rem); }
<p>
If 2.5vw is less than 1rem, the font-size will be 1rem.
If 2.5vw is greater than 1.5rem, the font-size will be 1.5rem.
Otherwise, it will be 2.5vw.
</p>

https://developer.mozilla.org/en-US/docs/Web/CSS/clamp

like image 162
The Fool Avatar answered Oct 11 '22 05:10

The Fool


CSS does not have a built-in way for doing max-font-size or min-font-size, but some work-arounds does the job.

One way is using media queries:

h1 {
  font-size: 30px;
}
@media screen and (min-width: 1600px) {
  h1 {
     font-size: 50px;
  }
}

Another way is using max() or min() functions with the viewport vw unit:

font-size: min(max(30px, 3vw), 50px);
like image 41
Ahmed Hammad Avatar answered Oct 11 '22 06:10

Ahmed Hammad