What is the best way to adjust the size of a <div> element when its content <span

I'm currently dealing with a div that has a max-width of 200px. Inside this div, there are some spans with the properties white-space: nowrap and display: inline-block.

.a {
  max-width:200px;
}

.b {
 display: inline-block; 
 white-space:nowrap;
}

<div class="a">
 <span class="b">A</span> <span class="b">B</span>
 <span class="b">C</span>
</div>

Currently, when the length of span A and span B exceeds the max-width of the div, span B is wrapped into the next line.

<div class="a">
 <span class="b">A</span> Empty space here
 <span class="b">B</span> Empty space here
 <span class="b">C</span> Empty space here
</div>

The issue I am facing is that the width of the div does not adjust according to the length of the span. It always remains at a width of 200px, leaving empty space after the span.

My goal is to ensure that the div resizes based on the length of the span, without any unnecessary empty space.

If anyone could provide an explanation or solution for this, it would be greatly appreciated.\

Answer №1

When utilizing the max-width property, it restricts the width of the div element to a specified value (such as 200px in this instance). However, switching to min-width instead of max-width could potentially be more beneficial...

Answer №2

.a {
  max-width:200px;
}

#sameline {
display:inline-block;
 white-space:nowrap;
}
<div class="a">
   <div id=sameline>
 <span class="b">A</span> Empty space here
 <span class="b">B</span> Empty space here
 <span class="b">C</span> Empty space here
    </div>
</div>

Answer №3

From what I gather, your issue may be related to the use of display: inline-block; on the <span> element... consider removing this style and reverting to the default display: inline; instead.

While the span is currently displaying in an inline manner, it also appears to be behaving like a block element, causing it to break when there is insufficient width available within its parent container, resulting in the creation of unwanted empty space.

UPDATE

In addition, don't forget to eliminate the white-space: nowrap; property as well...

Answer №4


.styles{
max-width:150px; 
margin:10px; 
display:inline-block; 
white-space:nowrap; 
 }

<div class="styles"> 
<span class="letters">A</span> Some space goes here 
<span class="letters">B</span> Some space goes here
<span class="letters">C</span> Some space goes here
</div>

Answer №5

.x {
  max-width:200px;
}

.y {
 display: inline-block; 
}
.z {
 white-space:nowrap;
}

<div class="x">
  <div class="z">
   <span class="y">A</span> <span class="y">B</span>
  </div>
  <span class="y">C</span>
</div>

If you want A & B to stay on the same line, try using flexbox for better control:

Consider how you want to handle overflow-x if content exceeds 100px

Here's an example using flexbox (which also handles extra whitespace):

.x {
  max-width:200px;
  display:flex;
  flex-direction:row;
}

.y {
 display: flex;
 flex: 1 1 100px;
}
</style>
</head>
<body>
<div class="x">
   <span class="y">A</span> <span class="y">B</span>
  <span class="y">C</span>
</div>

Answer №6

Within the scope of the a class, the default white-space setting is normal. This means that any empty spaces will be disregarded by the browser. To visualize this, you can add a border to the element!

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

When attempting to align buttons to the right side, encountered a surprising issue with their positioning

I need help moving the buttons above the chart in my JS Fiddle to the right-hand side. When I apply the class pefBut to the div with the id range-butts-index, the buttons disappear. However, when I remove the class, the buttons reappear on the left side. ...

Converting a single-column table into an array

After reading through numerous posts related to my query, I have yet to find a solution to my problem. Utilizing PHP and MySQLi, I am attempting to extract data from my server. Within my database, I have a single-column table and my aim is to store this ...

Centering the searchbar in the Bootstrap 4 navbar

I'm finding it difficult to center my search bar inside my navigation. I've experimented with different options like mr-auto and ml-auto or mx-auto, but the element just won't budge. I want to avoid using specific pixel values as it may affe ...

What is the best way to connect a CSS file to a jade document?

Having some trouble linking normalize.css while using socket and express. html head title= "Real time web chat" link(href='/css/normalize.css') script(src='/chat.js') ...

I struggled to insert a horizontal scrollbar into the iframe

In the process of constructing a family tree, I am utilizing lists as elements to create the structure. However, I am encountering an issue when the tree grows larger - no horizontal scrollbar is appearing. I have attempted to use iframe and CSS to manage ...

Creating evenly spaced PHP-generated divs without utilizing flexbox

My goal is to display images randomly from my image file using PHP, which is working fine. However, I am facing an issue with spacing the images evenly to fill the width of my site. This is how it currently appears: https://i.stack.imgur.com/AzKTK.png I ...

What could be the reason that my d3.tooltip is not functioning properly for these specific two lines on the chart?

I am currently experiencing issues with the d3.tool tip for my line charts. Additionally, I would like to adjust the y-axis scale to create larger gaps between the lines. Below are the codes used to generate the line charts: <!DOCTYPE html> <meta ...

What could be causing issues with the JQuery .Resize() function?

I'm attempting to create a responsive layout where the content div adjusts itself when the user resizes the page. The top and bottom divs are static, so only the content div changes its size based on the page flow. $(window).resize(function() { v ...

Prevent the text within the textarea from wrapping onto the next line

My challenge involves a textarea where text overflows onto the next line when it exceeds the maximum characters. However, I want the text to continue on the same line with a scroll bar for better visibility. Here's an illustration: text here flows on ...

Unable to hear sound - JavaScript glitch

Spent countless hours trying to figure this out - Can't get the audio file to play when clicking an HTML element. var sound = document.querySelector(".soundfile"); function playAudio() { sound.play(); } document.querySelector(".btn-hold").addE ...

Internet Explorer 11 has a problem with excessive shrinking of flex items

I have a flexbox setup with a left and right section, each of equal width. The left side displays an image and the right side contains text. While this layout works fine in Google Chrome, it does not wrap properly in Internet Explorer 11 when the width is ...

Obtain the CSRF token from a webpage using JavaScript

I am currently working on a project where I need to retrieve the csrf token from a web page built with Django using javascript. The structure of my HTML template is as follows: <div class = "debugging"> <p id = "csrf">{% csrf_token %}</p ...

Scheduled Events and revising the date navigation in the calendar

https://developer.mozilla.org/fy-NL/demos/detail/html5-calendar/launch Among the codes provided, which specific code enables the use of the browser's back/forward buttons to change the day? I'm having trouble figuring it out. Additionally, do y ...

Utilizing Flask to gather information from a leaflet map

I am currently working on creating a webpage using Flask. The webpage features a leaflet map where users can click to create a marker that opens a popup window with a link. The link's purpose is to open a new page displaying the longitude and latitude ...

Error: Collision detection in Three.js console output

I am attempting to create a collision detection system using Three.js (a WEBGL library). However, I keep encountering an error stating "cannot call method multiplyVector3 of undefined". Is there anyone who can help me identify what I may be doing incorrec ...

Utilize grids to ensure consistency in the width of every <li> element across the browser

Is there a way to make the ul element span the entire width of the webpage regardless of window size? http://jsfiddle.net/32hp1w5p/ ul { grid-column: 1/13; list-style-type: none; display: table; margin: 0 auto; } li { float: left; border: ...

Validating HTML forms using Javascript without displaying innerHTML feedback

Hey, I'm just diving into web development and I'm struggling to figure out why my innerHTML content isn't displaying on the page. Can anyone offer some assistance? I'm trying to display error messages if certain fields are left empty, b ...

The v-on:click event handler is not functioning as expected in Vue.js

I am currently learning vue.js and facing some challenges. Below is the code snippet from my HTML page: <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.jsdelivr.net ...

When the click event is triggered, the second function guess() does not execute

I am currently developing a number guessing application. It consists of two functions, the first one called startGame() works correctly (it receives the maximum number and then disappears by adding the hidden class). However, the second function that is ...

What is the best way to ensure that a Flex div and its child images stay confined within the boundaries of its parent container?

I'm struggling to create a responsive grid of images. I can't get the images to be both responsive with max-height and max-width, while also making sure the parent div takes up their full width. On the other hand, if I make the parent div the cor ...