Cannot get the "middle" div in CSS to properly wrap around its content

Click here to view the issue with the "middle" div not wrapping its content. I have been attempting to make it automatically wrap the entire content of the table, resulting in a neat white 10 pixel padded border surrounding it. Despite trying various methods such as adjusting display modes, floats, clears, and overflows, I have not been successful. Can someone pinpoint where I might be making a mistake?

#outer {
    height : 200px;
    width : 200px;
    background : red;
    overflow : auto;
    padding : 10px;
}
#middle {
    background : white;
    padding : 10px;
}
#inner {
    border : 1px solid purple;
}
td {
    background : cyan;
    padding : 5px;
    border : 1px solid blue;
}
<div id="outer">
  <div id="middle">
    <table id="inner">
      <tr>
        <td>this is some random text</td>
        <td>this is some random text</td>
        <td>this is some random text</td>
        <td>this is some random text</td>
      </tr>
    </table>
  </div>
</div>

Answer №1

Insert

display:inline-block;

into the styling for the element with the ID of #middle.

For more information on the difference between display: inline and display: inline-block, check out this helpful resource.

Take a look at the updated example for reference.

Answer №2

To ensure fixed table layout, simply add table-layout:fixed to your CSS. Here is an example:

#content {
    border: 1px solid blue;
    table-layout: fixed;
    width: 100%;
}

Feel free to view the implementation in this JSFiddle demo.

Answer №3

If you want the parent container to maintain a fixed size with no overflowing, unfortunately, tables are not the way to go. They lack flexibility in this aspect, with a minimum size that extends beyond the boundaries of the parent container by default. The solution is to adjust the size of the parent container accordingly.

    #outer {
        height : 200px;
        width : 300px;
        background : red;
        overflow : auto;
        padding : 10px;
    }

For more details, check out this link: http://jsfiddle.net/tMaCv/

Answer №4

Tables can be tricky because they always try to display all their content, which can be a problem when you have repeated words like "random" taking up a lot of space. This can cause the parent element to not resize properly. There are actually 4 different solutions to this issue:

  1. Utilize JavaScript to calculate the actual width of your table
  2. Consider using a wider table if possible
  3. Apply overflow:hidden to prevent layout issues
  4. Use min-width instead of width for the outer element

Answer №5

To resolve this issue, inform the #inner element that its width should be equal to the width of #outer minus the padding value (e.g. 20px in this case).

However, if you intend to showcase content effectively, consider increasing the size of the #outer element and monitoring the sizes of the inner elements.

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

Lock the first row and a few lines in place for an HTML table

I'm facing a layout challenge on my webpage. I have a header line followed by a table, and I want both the header line and the top row of the table to be frozen while allowing the rest of the table to scroll. Despite trying to use 'position:fixed ...

Sliced Text Transformation

Recently, I inquired about a pesky text effect that was bothering me. Thanks to the assistance of fellow users, I discovered that the culprit behind the effect was the background color's edge. Eliminating this problem should be simple, but even with ...

Custom Bootstrap design layout where the right column wraps under the left column

Need assistance with my bootstrap layout. Prior to the 980px breakpoint, the right column wraps under the left column. I want it to remain in its position without wrapping. The challenge is ensuring the left column has a fixed width while allowing the ri ...

Setting the border of a different element when focus is on an input control: tips and tricks

I've created a form with different input controls structured like this: <div class="form-group mb-3"> <div class="input-group"> <input asp-for="Email" class="form-control" ...

Animating Brightness and Contrast in CSS

I come from a strong background and I am looking to create a dynamic loop that changes the brightness using CSS specifically on Chrome. The goal is to have the background color match the user's profile, providing a unique experience each time. For ex ...

JQuery for Seamless Zoom Functionality

I am working with some divs that have images as backgrounds, and I have added a zooming effect on hover. However, the zoom effect is not smooth. Is there a way to modify the script to make the zoom effect smoother? Here is an example of my HTML structure: ...

Challenges with CSS in Google Chrome web browser (Input field and Submit button)

Having trouble with the alignment of a textbox and button on Chrome, they're not displaying correctly. Here are examples from different browsers; notice how the textbox in Chrome is misaligned. Internet Explorer/Firefox https://i.stack.imgur.com/mf ...

Variations between <div/> and <div></div>

When using Ajax to load two divs, I discovered an interesting difference in the way they are written. If I write them like this: <div id="informCandidacyId"/> <div id="idDivFiles"/> Although both divs are being loaded, only one view is added ...

CSS animation is activated solely upon its initial creation

Exploring the world of Vuejs for the first time with my debut project. The core of my project lies in the main component where I have an array as a data variable. Here's an example snippet: export default { name: 'example-component', ...

Exploring the Layout Options of Twitter Bootstrap 3

Can someone help me achieve this specific layout design using Bootstrap 3? --------------------------------------------------------- | | | | | ...

Transform black and white image to vibrant colors and add various hover effects

There are three images displayed on this webpage: This website is built on WordPress and utilizes the WP Bakery plugin for designing layouts. The images here are set to change from color to grayscale, and back to color upon mouseover. The following CSS c ...

Adjusting the background element of a fullpage.js layout during resizing and customization

Is there a way to make the background image responsive on a fullpage.js page, specifically for mobile and tablet devices? How can I ensure that a specific part of the image stays at the center of the page when resizing? For instance, in the provided imag ...

Updating a string's value in Angular based on user input

I am currently developing a custom offer letter template that will dynamically update key data points such as Name, Address, Role, Salary, etc based on the selected candidate from a list. The dynamic data points will be enclosed within <<>> in ...

What is causing justify-content to not function properly in React?

Struggling to center two divs within a container using flexbox. Flex-wrap and align-items are working fine, but justify-content just won't cooperate. :( <div className="container"> <div className="card- ...

The button is not centered within the grid container

I need assistance centering the button within a Grid layout using Material-UI <Grid container style={{backgroundColor:'green'}} alignItems="center" justify="center" > <Grid alignItems="center" justify=&q ...

Adjust Sidebar Height to Match Document Height (using React Pro Sidebar)

Having an issue with the height of a sidebar component in Next.js using React Pro Sidebar. It seems to be a JavaScript, HTML, and CSS related problem. I've tried several suggested solutions from Stack Overflow, but none of them seem to work. Surprisin ...

modify the inherent CSS property

I am working with a component that I have inherited, including its CSS style, and I need to modify one of its properties. Current CSS: .captcha-main-image { width: 100%; height: auto; } <img class ="captcha-main-image" id="captchaImage" src= ...

Unable to get compatibility mode IE to work as intended

I am facing an issue where I need to showcase a webpage in compatibility mode. Despite using the code below: <meta http-equiv="X-UA-Compatible" content="IE=11"> It seems like this code only functions if compatibility mode is already enabled. In tha ...

Transforming nested JSON files into nested jQuery divs

Is it possible to iterate through two JSON files that have a parent-child relationship based on simple ID primary and foreign keys? Can we then display the data in a list of divs with the following characteristics: Hierarchical - child divs should only a ...

Align your content perfectly on a full-screen slick slider

Attempting to center content over a full-screen slick slider from kenwheeler.github.io/slick/, I used a flexbox but the content remains at the edge of the viewport. It seems like an issue with the position tag in the slick CSS, but I can't figure out ...