Ways to position variable width blocks so they appear to float

I'm facing an issue with floating multiple DIVs and allowing them to break out of a containing div. Below is an example of my code:

<!DOCTYPE HTML>
<html lang="en-GB">
    <head>
        <meta charset="utf-8" />
        <style>
            * {
                -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
                -moz-box-sizing: border-box;    /* Firefox, other Gecko */
                box-sizing: border-box;         /* Opera/IE 8+ */
            }

            div.outer {
                width:300px;
                height:300px;
                border:3px solid #000;
                overflow:visible;
                padding:10px;
            }

            div.mid {
                overflow:hidden;
                border:1px solid #000;
                padding:0;
                width:485px;;
            }

            div.inner {
                width:150px;
                height:100px;
                background:#EDEDED;
                border:1px solid #C0C0C0;
                float:left;
                margin:5px;
            }

        </style>
    </head>

    <body class="landingPage">
        <div class="outer">
            <div class="mid">
                <div class="inner"></div>
                <div class="inner"></div>
                <div class="inner"></div>
            </div>
        </div>
    </body>
</html>

While this setup works well, the problem arises when I don't know the number or width of "inner" DIVs in advance. Therefore, I can't specify the "width" property in the div.mid CSS. Removing the width causes the inner DIVs to wrap, which is not desirable.

Can anyone provide a solution to this? It's important to note that the box-sizing property must be retained.

Here is the fiddle

Answer №1

To adjust the size of the inner div, try setting width: auto; and max-width: 150px;. This will ensure a maximum size limit without knowing the exact dimensions in advance.

Answer №2

When faced with encapsulating content of an unknown size, I typically follow a few steps:

  • Incorporate max-width along with width
  • Opt for percentages over fixed widths for internal elements
  • Experiment with box-sizing: content-box as opposed to border-box

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

Replace the custom styles in Internet Explorer

Having trouble with IE overriding a user-defined stylesheet. Font awesome icons and glyph icons disappear when user loads their own style sheet for accessibility. Tried using !important in relevant places, also attempted adding a JS function for inline sty ...

Creating a logo slider using CSS - A step-by-step guide

I attempted creating a logo slider using Owl carousel. Unfortunately, the carousel isn't showing up. Here's how I envision my carousel to appear: https://i.sstatic.net/scDtT.png This is the HTML code I used: <div class="owl-carousel"> ...

Tips for customizing bootstrap's fixed navbar-default to ensure that the list items align downwards:

Is it possible to customize the bootstrap fixed navbar-default so that the li elements align downward instead of at the top? If you have any corrections or custom solutions, I'd love to see them! Feel free to share your code on platforms like CodePen, ...

Tips for preventing tables from breaking their borders when the page is being split?

I am looking to enhance the design of my website by incorporating CSS borders on cards displayed in columns of 3. However, I want to ensure that the cards are not split between columns. Below is an excerpt from my CSS code: <style> .card { borde ...

Saving Bootstrap Data - Modals, Tags and Notifications

Check out my code snippets on this example/demo page. /* Messages Modal */ $(document).ready(function(){ var informer = $("#messageInformer a"); var refreshBadge = function(messageCount) { var badge = informer.find(".badge"); ...

Achieving CSS responsiveness by transforming the divider from vertical to horizontal when the window is resized

Struggling with creating the footer to match the image? As a beginner in CSS and Bootstrap, I admit I lack expertise in this area. My aim is to replicate the footer design for mobile view only, while having a different layout for desktop. The horizontal pa ...

The imported font used in Firefox is displaying with a striking underline text-decoration

I am currently using the Cardiff font in a project and attempting to apply the style text-decoration:underline to it. While this works perfectly in Chrome (Version 35.0.1916.114), in Firefox (Version. 29.0.1) the underline appears to be crossing through t ...

Detecting the physical screen size based on the user agent in PHP and JavaScript

Is it possible to accurately detect the screen size of mobile browsers using both javascript and php? Given how mobile devices come in all shapes and sizes these days, relying solely on pixels may not be enough. I'm looking for a method that can speci ...

What steps can I take to avoid random divs from overlapping on smaller screens when dealing with elements created in the created() hook?

I encountered an issue with the code I'm working on (the circles overlap in the fiddle provided, but display correctly on my PC - possibly due to pixel discrepancies in the fiddle). You can find the code here. TestCircle.vue: <template> <d ...

What is the best way to horizontally align tiles in Bulma?

For my portfolio website, I have chosen Bulma as the framework. Within the "how to reach me" section, I want to display my social media profiles for contact information. Ideally, I would like these profile tiles to be centered horizontally on the page. How ...

Novice problem with the <div> tag

I am currently in the process of developing a website, and I have encountered an issue with the title not staying within the designated div box at the top of the page. <div style="width:1000px;height:40px;background:grey;border:3px solid;border-radiu ...

Ensuring the mobile menu button is always visible on Bootstrap 4, regardless of screen size

Is there a way to consistently display the mobile menu button regardless of screen size? I want the links to only appear when someone clicks on the mobile button. Thank you! I am aware of a solution for Bootstrap 3, but it does not seem to work with Boots ...

Difficulty encountered while using CSS to customize a vue template

I'm currently working on a login page and experiencing difficulty styling Vue templates using CSS. Here is the code that works without Vue: HTML <body class="text-center"> <form class="form-signin"> <h1 class="h3 mb-3 fon ...

Adjusting image size within floating containers

I am working on a design with two floating divs, one on the left and one on the right, both nested within a main div. Each floating div contains an image that needs to be resized to the maximum size possible without differing sizes. This is what I curren ...

Animate the entire paragraph with CSS hover effect

I'm seeking ideas on how to achieve a specific effect without the need to wrap individual lines in inner elements like span or a. Check out this example <div class="m-linkitem"> <h1>Hover Below</h1> <a href="#">Lorem ...

Implementing Dynamic Parent Node Manipulation with Button Clicks in JavaScript

I am currently working on dynamically creating an input field using the append child method along with add and delete buttons to form a tree-like structure. The goal is to be able to create child nodes with add and delete buttons upon clicking the add butt ...

Guide to incorporating text with a hover overlay image using HTML, CSS, and JavaScript exclusively

Here is some code for displaying hidden information on shape hover, but there are a couple of issues: 1. When the font size is reduced to 24px, the dark grey background does not cover the entire shape. 2. I want to include text that appears with the hover ...

Customize Pagination Style with Bootstrap Label Alignment

Seeking assistance with aligning a pagination element alongside other buttons and labels within the same row. I would like to have a label displaying current and total item counts right before the pagination elements, ensuring that everything is middle-ali ...

Preventing parent properties from overriding descendants is a key part of maintaining a hierarchical

I found a css file online that I'm using for a website I'm building, but I am only incorporating certain components from it. The issue is that the global styles in this css file are overriding all the global styles on my website. My solution was ...

Customize Bootstrap 4 borders within a row

I am currently facing an issue with my code, where there is a border at the bottom of every row. Click here to view the code Unfortunately, I have noticed that the border at the bottom of each row does not extend across the full width. This occurs even wh ...