Alter the size of an element's width dynamically while maintaining a predetermined width

Having a fixed width div:

// CSS

@media only screen
.column.small-centered:last-child, .columns.small-centered:last-child {
  float: none;
}
.lesson_lt_mc_div .small-centered {
  margin-top: 15px;
  margin-bottom: 15px;
  width: 296px;
}
foundation.css:1@media only screen
.column.small-centered, .columns.small-centered {
  margin-left: auto;
  margin-right: auto;
  float: none;
}
.text-left {
  text-align: left !important;
}

// HTML

<div class="small-3 small-centered text-left columns">
    <input type="radio" name="lt_mc_ans" value="1" id="lt_mc_ans_1"><span> 1. </span>
    <label for="lt_mc_ans_1">I</label>
    <br>
    <input type="radio" name="lt_mc_ans" value="2" id="lt_mc_ans_2"><span> 2. </span>
    <label for="lt_mc_ans_2">rsr rs rsrs rsrs r rrrs</label>
    <br>
    <input type="radio" name="lt_mc_ans" value="3" id="lt_mc_ans_3"><span> 3. </span>
    <label for="lt_mc_ans_3">Very</label>
    <br>
    <input type="radio" name="lt_mc_ans" value="4" id="lt_mc_ans_4"><span> 4. </span>
    <label for="lt_mc_ans_4">She</label>
</div>

The purpose of having a fixed width is to center the div while aligning the text to the left. However, it causes an issue when the text wraps due to its length, making the options appear distorted.

Is there a way to adjust the div width dynamically using jQuery or JavaScript? (While considering any possible CSS alternatives).

Answer №1

To create this layout, you'll need an outer and inner element.

The text in the outer element should be aligned in the center. The inner element should be displayed as inline-block so that it only takes up the width of its content.

.outer {
  text-align: center;
  background: lightblue;
}
.inner {
  background: lightgreen;
  display: inline-block;
  text-align: left;
}
<div class="outer">
  <div class="inner">
    Some example text
    <br />and more text
    <br/>and even more
  </div>
</div>

Alternatively, instead of using an inner div, consider using your form to achieve a similar effect.

Edit

Below is your revised HTML with the addition of the form tag:

.outer {
  text-align: center;
  background: lightblue;
}
.inner {
  background: lightgreen;
  display: inline-block;
  text-align: left;
}
<div class="outer small-3 small-centered text-left columns">
  <form class="inner">
    <input type="radio" name="lt_mc_ans" value="1" id="lt_mc_ans_1"><span> 1. </span>
    <label for="lt_mc_ans_1">I</label>
    <br>
    <input type="radio" name="lt_mc_ans" value="2" id="lt_mc_ans_2"><span> 2. </span>
    <label for="lt_mc_ans_2">rsr rs rsrs rsrs r rrrs</label>
    <br>
    <input type="radio" name="lt_mc_ans" value="3" id="lt_mc_ans_3"><span> 3. </span>
    <label for="lt_mc_ans_3">Very</label>
    <br>
    <input type="radio" name="lt_mc_ans" value="4" id="lt_mc_ans_4"><span> 4. </span>
    <label for="lt_mc_ans_4">She</label>
  </form>
</div>

Answer №2

To center the small-centered class, apply display: table; margin: auto; and eliminate the width property...

Answer №3

If you adjust your width attribute to a min-width attribute and then configure the width attribute as shown below:

width: intrinsic;
width: -moz-max-content;
width: -webkit-max-content;

You should see the desired behavior. For more information on the 'max-content' option, visit this link.

Edit: See a demonstration using your html structure on CodePen: here

Answer №4

Why not use min-width on the div in the CSS instead of setting a fixed width, allowing it to grow with the text?

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

Using AJAX to search through paginated pages is quite simple and effective

Currently, I have developed a JavaScript script that filters names within a list. However, a problem has arisen with pagination - as the list paginates, I am unable to retrieve names without refreshing the page. Is there a way to use AJAX to access and dis ...

Pausing and then resuming an interval function within the same function

I'm struggling with an $interval function that runs every second. The function retrieves user credentials from local storage and checks if they have expired. If they have, it refreshes them with new ones. Otherwise, it does nothing. This is what my ...

Retrieve coordinates of the clicked location with RichMarker in the Google Maps JavaScript API v3

I followed the solution provided here for richmarker.js After implementing it, when I use my_rich_marker.addListener('click', function(event) { console.log(event); //undefined } What I am trying to achieve is to access event.latLng of the ...

Arranging JavaScript array in alphabetical order

Just a quick question - does the .sort method in JavaScript automatically sort items in an array using ASCII order? I've been looking at examples, but have yet to find a definitive answer. Are there any alternative methods for alphabetically sorting a ...

A guide to Embedding a Variable Template Within an Anchor Tag in Django Templates

Just starting out in web development, Django, python, html, you name it. Currently, I have a simple Django app that lists publication titles stored in the database. It's working fine. Now, my goal is to turn each publication title into a clickable li ...

Is there a foolproof method to verify if a user truly has visibility of a div element?

When I search online, most solutions only consider the viewport and/or the first parent container that is scrollable when trying to determine if a div is visible. However, I am wondering if there is a foolproof method to check if a div is truly visible t ...

Struggling with Selenium as I attempt to click multiple "Remove" buttons, constantly encountering the StaleElementReferenceException error

I am facing a challenge when trying to remove multiple elements on a webpage. My current approach involves locating all the "Remove" buttons and clicking them one by one. However, I keep encountering a 'StaleElementReferenceException' as I remove ...

Using a parameter as optional in an Arrow Function

My handler function looks like this: handleSelect = (k0, k1, v) => { ... } }; I am looking for a way to make the k1 parameter optional in this function. Is there a recommended approach to achieve this? ...

Passing parameters as an array in Angular can be done by using the format: ?category[]=1&category[]=2&category[]=3,

Struggling to send an array using the $http.get() method in AngularJS. Here's my current approach: $http.get('/events.json', {params: {category_id: [1,2]}}); While I anticipate the request to be sent as /events.json?category_id[]=1&cat ...

Real-time Feedback: Providing live updates to users about the current connection status

My task requires pulling data from multiple datasources which can be time-consuming. To enhance user experience, I want to provide real-time information about the progress by displaying messages like "Currently retrieving data from table1" and "Now fetchin ...

Transforming JSON/XML into a hierarchical display

I've come across the following XML file: <Person attribute1="value1" attribute2="value2"> value3 <Address street="value4" city="value5">value6</Address> <Phone number="value7" type="value8">value9</Phone> </Pers ...

Creating a form that seamlessly integrates with the content using a combination

As I continue to explore the depths of WordPress and delve into learning HTML/CSS, my latest project involves embedding an email signup form on my website using Klaviyo. The basic code provided by their form creator initially had everything condensed into ...

Fixing the PHP AJAX issue with loading additional content

Hello, I've created an app where users can click a "Load More" button to display data from a database. However, the problem arises when all the data has been fetched and the button continues to appear. If the user clicks the button again, the same dat ...

VueJS ensures that instance properties are reactive

Currently, I am in the process of developing a VueJS plugin that utilizes instance properties. I find myself needing to reintroduce some reactive values back into VueJS. After conducting my research: I have been struggling to grasp the concept behind Obj ...

Ways to develop tests for functions specific to my scenario

Currently, I am attempting to implement a timeout function test within my application. Within the controller: $scope.$watch('toy',function(toyVar){ if(toyVar == 1) { //perform actions } else { $timeout(f ...

Changing the size of a div using jQuery

I'm attempting to adjust the size of a div element. You can view my progress in this code fiddle: https://jsfiddle.net/bj3m24ks/31/ Kindly refrain from sharing links to other tutorials as I have already explored them all. I attempted to utilize the $ ...

Creating dynamic grids in React.js by utilizing HTML

Currently, I am tackling one of the projects on FCC which is the Game of Life. Prior to diving in, my focus is on figuring out how to render a grid on the page. I aim to modify the table dimensions while ensuring it fits neatly within its container. The ...

Unleashing the Power of Node.js: A Step-by-Step Guide to Crafting

I am developing a console interface that prompts the user with questions and receives input through the console. Some questions require the user to provide a limited number of inputs. I have researched ways to obtain console input in node js, but I haven&a ...

Prevent certain individuals from selecting a checkbox within HTML forms

Currently, I have a form where users need to fill in their information. However, I want certain parts of the form to be preventable based on the level of access the user has. For example, I have successfully implemented a code snippet onfocus='blur() ...

What's the process for setting a value in selectize.js using Angular programmatically?

Currently, I am implementing the AngularJS directive to utilize selectize.js from this source: https://github.com/kbanman/selectize-ng In my scenario, I have two dropdowns and my goal is to dynamically populate one of them called selectizeVat based on the ...