The element is not occupying the full width within the div container

I'm working with a div that contains some elements. My goal is to have these elements span the entire width of the container div.

.container {
  height: 100px;
  width: 100px;
  display: flex;
  flex-direction: column;
  overflow: scroll;
  border: 1px solid black;
}

.inner {
  width: 100%;
  background-color: gray;
  white-space: nowrap;
 }
<div class="container">
  <span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
  <span class="inner">XXX</span>
  <span class="inner">XXX</span>
</div>

When you horizontally scroll, you'll notice that the background does not fully extend to 100%.

Answer №1

Make sure to update your code according to the following guidelines:

.container {
  height: 100px;
  width: 100px;
  display: flex;
  flex-direction: column;
  overflow: scroll;
  border: 1px solid black;
  align-items:flex-start; /* remove stretch alignment */
}

.inner {
  min-width: 100%; /* use min-width instead of width */
  background-color: gray;
  white-space: nowrap;
 }
<div class="container">
  <span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
  <span class="inner">XXX</span>
  <span class="inner">XXX</span>
</div>

If you prefer all elements to have full coloration, update your code as shown below:

.container {
  height: 100px;
  width: 100px;
  overflow: scroll;
  border: 1px solid black;
}

.container>div {
  min-width: 100%;
  display: inline-block;
}

.inner {
  display:block;
  background-color: gray;
  white-space: nowrap;
}
<div class="container">
  <div>
    <span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
    <span class="inner">XXX</span>
    <span class="inner">XXX</span>
  </div>
</div>

Answer №2

Take a look at this clever flex approach.

Simply swap out flex-direction: column for flex-flow: column wrap in the .container class. Also, eliminate width: 100% from the .inner class.

By making these adjustments, you will achieve the desired outcome.

.container {
  height: 100px;
  width: 100px;
  display: flex;
  /*flex-direction: column;*/
  flex-flow: column wrap;
  overflow: scroll;
  border: 1px solid black;
}

.inner {
  /*width: 100%;*/
  background-color: gray;
  white-space: nowrap;
}
<div class="container">
  <span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
  <span class="inner">XXX</span>
  <span class="inner">XXX</span>
</div>

Answer №3

UPDATE: After some exploration, I have devised a solution without using flexbox (Driven by curiosity)

Initially, the question was misinterpreted
.

SOLUTION 1: Introducing a wrapper div around the spans.

.container {
  height: 100px;
  width: 100px;
  border: 1px solid black;
  overflow-x:scroll;
}

.wrapper{
  background:orange;
  height:auto;
  width:max-content;
}
.inner{
  display:block;
  min-width:100px;
}
<div class="container">
  <div class="wrapper">
    <span class="inner">LONG TEXT LONG TEXT LONG TEXT LONG TEXT LONG TEXT</span>
    <span class="inner">XXX</span>
    <span class="inner">XXX</span>
  </div>
</div>

Answer №4

Make sure to set the span elements' display property to block instead of inline to adjust their behavior accordingly.

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

Issue with Angular 2 NgFor Pattern Error Message Display Absence

I am attempting to incorporate inputs with a regex requirement within an ngFor loop, but I am not receiving the expected error message when entering something that does not match the required pattern. Even when I input an incorrect pattern, "Test" remains ...

Injecting AngularJS directives and styling elements into the body section of a Twig template using the {% block body %} tag

I'm currently in the process of constructing a Rest API using Symfony 3 and Fosresbundle, with AngularJS responsible for fetching JSON data within twig templates. However, I've encountered an issue where I need to specify angularJS directives an ...

Utilizing Cookies within an HTML Page

My current code is functioning perfectly, accurately calculating the yearly income based on the input "textmoney." I have a link to a more advanced calculator for a precise prediction. My goal is to find a way for the website to retain the data input from ...

Having trouble getting your jQuery/JavaScript function to detect the property "-webkit-clip-path"?

Exploring the clip-path feature of CSS has been a fun experiment for me. I have successfully implemented it in my project and am now trying to incorporate JavaScript to dynamically change the start/stop positions of the clip-path. Below is a snippet of my ...

What is the best way to select and style individual HTML elements using CSS?

Having trouble editing a Validations Form in Ionic 4 with CSS. It seems that only the form elements are targeted after clicking on the form group. Attached below are the form states before and after click. I'm unable to style the form elements before ...

What is the proper way to structure an Xpath search?

Recently, I've been working with Selenium in QA Test automation. When it comes to selecting/writing Xpath for an element, I've noticed that there are different formats that can be used. For example, protected final String LOGIN_BUTTON_LOCATOR = ...

Display the Angular UI grid containing data from the textboxes once the information has been submitted

I recently started working on an angularjs application and I am facing some challenges in finding the solution. Any advice or suggestions would be greatly appreciated. My goal is to dynamically display an angular UI-grid based on the text selected in the ...

Use the .htaccess file to redirect any non-existing page to a custom HTML page while maintaining the

I am trying to set up a custom error page (error-404.html) for non-existing files and directories on my website. The goal is to seamlessly redirect users to the error page without changing the URL that they entered. RewriteCond %{REQUEST_FILENAME} !-f Rew ...

A dynamic image carousel featuring multiple images can be enhanced with fluid movement upon a flick of

I am trying to enhance this image slider in HTML and CSS to achieve the following objectives: 1. Eliminate the scroll bar 2. Implement swipe functionality with mouse flick (should work on mobile devices as well) 3. Make the images clickable .slider{ ove ...

Struggling to use the innerHTML method to update a div within another div element?

<!DOCTYPE html> <html> <head> <title>Business Information Card</title> <script type="text/javascript"> window.onload = init; function init(){ var button = document.getElementById("populate ...

The Angular Observable continues to show an array instead of a single string value

The project I am working on is a bit disorganized, so I will try to explain it as simply as possible. For context, the technologies being used include Angular, Spring, and Maven. However, I believe the only relevant part is Angular. My goal is to make a c ...

Restrict Text Input Field to Accept Only Specific Domain

Hey there! I've set up a text input box for users to link their Tripadvisor page to their profile. I want to make sure that when they paste the URL in, it is checked for the correct format. For example, if someone pastes: or , then allow the link. Ho ...

Creating a multi-step progress bar in Yii2 that showcases completed steps using jQuery

Is there a way to automatically transition from one state to another once the order status changes in Yii2? var i = 1; $('.progress .circle').removeClass().addClass('circle'); $('.progress .bar').removeClass().addClass(&a ...

Python Automation with Selenium Web Scraping

I attempted to scrape the content of Crédit Suisse's page as an exercise. After creating this script, I encountered difficulties in retrieving the data. Initially, I suspected it may be due to an iframe issue or an AngularJS website structure, but u ...

Is there a way to adjust the brightness of specific sections within an image using html, css, and js?

I am working on a project where I have an image with tags underneath that correspond to different parts of the image. For example, if the image is of a dog, one of the tags could be 'nose', indicating the portion of the image around the dog' ...

The desired outcome is not displayed following the Ajax post request

I have created an app that utilizes asynchronous AJAX requests to enable seamless chat functionality without refreshing the page. However, I'm facing an issue where the messages are not being displayed. When I inspect the network tab and navigate to t ...

Deactivate the selection box feature while keeping the existing value in the post

Within my current asp.net-mvc project, there is a specific page where users can make selections from a dropdown box that triggers a post request to update multiple values. To prevent any confusion caused by delays in the postback process leading to uninten ...

React is producing a collection of <td>'s

My React code is very straightforward and it runs smoothly: function Columns(){ return ( <React.Fragment> <li>Hello</li> <li>World</li> </React.Fragment> ); } function Example(){ ...

What could be causing my iframe to not adjust its size according to its content?

My attempt at resizing a cross-site iframe is not working as expected, despite following the guidance provided here. The iframe on my page seems to remain unaltered in size, and I can't figure out what mistake I might be making. The current location ...

Adjusting the opacity of the background image in the section - focus solely on the background

This section is what I'm working on. <section ID="Cover"> <div id="searchEngine">hello</div> </section> I am trying to achieve a fade in/out effect specifically for the background image of the Cover section. T ...