Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to adapt font size to div width with CSS?

I have a div with 70% width, and here’s what I want to do:

  • Put some words in that div
  • Adapt font size so that those words occupy all div width


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!

like image 984
Engkus Kusnadi Avatar asked Jun 29 '16 03:06

Engkus Kusnadi


People also ask

How do I automatically adjust font size in CSS?

Syntax: font-size-adjust: number|none|initial|inherit; Below are the examples that illustrates the use of font-size-adjust property.

How do I change the font size inside a div?

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.

How do I change the font width in CSS?

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.

How do you change the font size based on container width?

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.

How to set a Div size according to its content using CSS?

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.

Is there a way to resize font size according to Div size?

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.

What is font-size-adjust in CSS?

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.

What is the default font size for text in HTML?

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.


2 Answers

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>
like image 166
Marina Avatar answered Oct 22 '22 16:10

Marina


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>
like image 5
Anthony Astige Avatar answered Oct 22 '22 16:10

Anthony Astige