I have a div with 70%
width, and here’s what I want to do:
Is this possible with only css? Here is my code:
.parent {
width:70%;
height:auto;
min-height:100px;
background:red;
font-size:10vw;
}
Please help me to change the font-size dynamically to the parent class
<hr>
<div class="parent">
This Text
</div>
Note: the div size is responsive, this is important. Thanks in advance!
Syntax: font-size-adjust: number|none|initial|inherit; Below are the examples that illustrates the use of font-size-adjust property.
To change the font size in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-size.
The font-stretch property allows you to make text narrower (condensed) or wider (expanded). Note: Some fonts provide additional faces; condensed faces and expanded faces. For these fonts, you can use the font-stretch property to select a normal, condensed, or expanded font face.
The font size can be set with vw (viewport) unit, which means the viewport width. The viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.
Using inline-block property: Use display: inline-block property to set a div size according to its content. making web pages presentable. CSS allows you to apply styles to web pages. More each web page. Using fit-content property in width and height: In this method, we set the width and height property to fit-content value.
I found a way of resizing font size according to div size, without any JavaScript. I don't know how much efficient it's, but it nicely gets the job done. Embed a SVG element inside the required div, and then use a foreignObject tag inside which you can use HTML elements.
For example, a developer might use the Bold Comic Sans font for titles and Roboto font for the body. However, this might make your website look cluttered, as many browsers do not support all the fonts. CSS font-size-adjust property can avoid such a situation by making the font size auto-adjust.
Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em). Tip: If you use pixels, you can still use the zoom tool to resize the entire page. To allow users to resize the text (in the browser menu), many developers use em instead of pixels.
You just have to add display: inline;
in your code
.parent {
width: 70%;
height: auto;
min-height: 100px;
background: red;
font-size: 10vw;
display: inline;
}
<div class="parent">
This Text
</div>
Perhaps not quite what you're looking for, but something - You can make both the font and the element relative to view width.
p {
font-size: 5vw;
background-color: red;
width: 50vw;
}
<p>
I'm a P!
</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With