Adjust text to fit within div as browser is resized

Can text inside a div adjust to fit the size of the DIV while the browser window is resized?

I came across this and this and several other resources about fitting text to a DIV, but none of them provide a definitive answer for resizing text when the browser window changes.

<div id="wrapper">
    <div class= "tfz">MAKE THIS TEXT FIT</div>
</div>

Check out this jsFiddle link

Answer №1

Give this a try -

word-wrap: break-word;

Then, adjust the width using %. Hopefully, this will resolve the issue.

For instance

<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

Customize the Style

div {
    width: 500px;
    word-wrap: break-word;
    font-size: 2vw;
}

Take a look here for more information

Answer №2

When specifying the width in pixels, you won't be able to resize it unless you incorporate some javascript/jquery. If it is crucial for you to mention the width in px, then you can only achieve it with jquery/javascript.

However, if you are open to mentioning it in percentage, then you can easily manage it through CSS like this:

#wrapper{
    position: relative; //no need to change this
    width: 50%;
    word-wrap: break-word; //apply this.
    height: auto;
}

Check out the DEMO here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Email the jQuery variable to a recipient

I'm facing an issue with sending a jQuery variable containing HTML and form values via email using a separate PHP file with the @mail function. My attempt involves using the jQuery $.ajax function on form submit to send this variable, but unfortunate ...

Is TypeScript checking in VSCode failing to detect await functions?

I have been working on an app that retrieves weather data based on a user's location, and everything seems to be functioning correctly. However, during the coding process, I keep encountering errors that are being flagged, even though the code runs sm ...

How can the issue of v-slot missing in Vue2.7 be resolved?

After executing the given code, the results displayed are captured in Google Chrome. Below is a snippet of the code used: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-e ...

Loading a Threejs model: "The CORS policy has blocked access to XMLHttpRequest from origin 'null' - How can I test this locally? Or should I simply upload it?"

Experimenting with three.js locally on a single HTML page, I am interested in exploring loading and manipulating 3D object files. Here is the code snippet that I am currently using: var loader = new THREE.AMFLoader(); loader.load( '. ...

Reactjs: Prop Type Error - Issue with retrieving the values from props during data submission

Currently, I am developing a social app where users can post screams, like posts, and add comments. The issue I'm encountering is that I am not receiving the props value when posting a new scream initially, but after refreshing the page, everything wo ...

Using Thymeleaf in a Spring Boot application, the modal automatically closes after submission

How can I keep the modal open when I click the submit button? I want to reload an existing modal form when I click submit. The code form is in fragment/header, and when I click the "login" button, the modal shows up. The issue is that in views/loginForm, ...

Tips on transferring a Rails variable to Ajax

Struggling to integrate a progress bar (Javascript) into my Ruby on Rails application, I am facing difficulties in passing values from the controller to JS for updating the bar. In my initial attempt, I used a basic global variable ($count), but it remain ...

Are there any available tools or scripting methods for accommodating various vendor prefixes?

My preference is to use standard CSS rules for different styles that include various vendor prefixes. To test, I would start with the following: border-radius: 5px; box-shadow: 0px 0px 4px 0px #fff; transform: rotate(90deg); For the final version, I woul ...

Javascript returning unexpected characters in image URL from ajax response

Within my Codeigniter web application, I am utilizing an ajax function to fetch data from the database in order to display it on the view. The data retrieved includes an image URL along with other fields. The issue arises when I receive the data within th ...

The flow of browsing the web and executing scripts in a web browser

I'm really struggling to grasp the logic behind this function I'm working on. public void PortalLogin(AutoResetEvent signal) { // Navigate to portal string portalUrl = "website_name"; ...

Seamlessly adaptive HTML5 video playback

Has anyone figured out how to make an HTML5 video in an absolutely positioned <video> element resize to fit the window width and height without ever getting cropped? Many solutions I've come across rely on using the <iframe> tag, which is ...

Would it be considered improper to apply ID's to DOM elements for the purpose of automation, as it may transform their CSS classes into individual entities?

Just thinking about the best approach for Automation (such as Selenium). I've been advised against using IDs on elements, as it could result in JS errors (in case of duplicate IDs) and make CSS classes unique. While I see the point, not having IDs can ...

Angular File Sorting Error in Gulp detected

After including .pipe(angularFilesort()) in my gulp script, the task runs wiredep but never proceeds to the default task. It just stops after executing wiredep. However, if I remove .pipe(angularFilesort()), the script works perfectly fine. Can someone h ...

Displaying database rows with PHP errors

<?php mysqli_connect('localhost','example','example'); $result = mysqli_query("blakesdatabase` LIMIT 0, 30 "); $num_rows = mysqli_num_rows($result); // Display the results echo $num_rows; ?> I am in the process o ...

Fullcalendar JSON data sources

Currently, I am working on creating dynamic calendar events that trigger a controller function whenever the date or month changes. The controller then filters values received from an Ajax call with start and end parameters. Below are the functions involved ...

What kind of data structure is suitable for a MediaStream when passing it as a prop in a Vue component

Currently, I have set up a mediastream (multi WebRTC) and plan on placing each MediaStream into the child component that contains a video tag. The stream type is MediaStream, however there is no corresponding type for a Vue prop. Refer to the Vue documenta ...

Create an ASP button with drag and drop functionality

Is there a way to enable drag and drop functionality for an asp:Button element? Also, how can I retrieve the coordinates of the button after implementing jquery drag and drop? My goal is to store these coordinates in a database so that when I revisit the ...

NodeJS: Issue when implementing try/catch with async/await causing SyntaxError

As a newcomer to Node Js, I am struggling to understand why the code below is throwing a syntax error with catch(). I recently updated to Node JS V14. Any assistance would be greatly appreciated. async function demoPromise() { try { let message ...

Personalized form outlines

I'm designing a Valentine's Day theme for my website and I was wondering if it's possible to create custom form borders, like a heart shape, without having to hard-code everything. I could always insert an image where needed, but that seems ...

Gather JSON information through a combination of JQuery ajax(), PHP scripting, and MySQL database interaction within a

I've been searching high and low with no luck. Here's what I'm dealing with: Utilizing Custom PHP MVC and attempting to achieve the following: I want to replace the id="ShowName" in userView.php with JSON data fetched via an Ajax call using ...