What is the best way to ensure that a parent element with a nowrap property can inherit the width of

It's proving difficult for me to make a parent element inherit the width of its children. Here is the link to the jsfiddle http://jsfiddle.net/4mk3S/

Currently, I only have an outer div with

    white-space: nowrap;

and child divs with

    display: inline-block;

I want the outer div to automatically adjust its width based on the total width of its child elements. Any assistance would be greatly appreciated. Thank you!

Code :

<div class="outer">
    <div class="inner"></div>
    <div class="inner"></div>
    <div class="inner"></div>
</div>

.outer {
    white-space: nowrap;
    padding: 10px;
    background: green;
}

.inner {
    display: inline-block;
    width: 1000px;
    height: 100px;
    background: red;
}

Answer №1

make sure to include display: inline-block; in the parent element as well

inline-block makes the container wrap around all its contents, whereas block (the default for div) takes up the full width of the parent element

Check out the updated fiddle here

Here is the code that needs to be adjusted

.outer {
    white-space: nowrap;
    padding: 10px;
    background: green;
    display:inline-block;
}

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

Aligning the last element within a list

I'm having a major issue with this code. Despite reading all related posts, I still can't get it to work. Seems like I might be missing something obvious. LOL. Here is the site (best viewed in Chrome): and here is what I believe is the simplifi ...

Unable to use column formatting on a row using flexbox

I am working on creating a layout with rows and columns where the content in each column is vertically aligned. Here is an example of what I am trying to achieve: Currently, I am using react and have multiple components. The approach I believe will work ...

What is the best way to design versatile div containers that combine the float property with fluid dimensions?

I am attempting to achieve a specific layout where the width of the content_container extends up to the right side of the screen and dynamically adjusts based on whether the expandable pane is collapsed or expanded. Applying width: 100% to .content_contain ...

Determining the repetition and placement of background images when employing multiple backgrounds

Is it possible to apply two background images to the same div? I want one image to repeat along the x-axis and the other to appear only once. I've tried using multiple background images, but I'm not sure how to target specific rules for each bac ...

Unique background image is assigned to each div sharing the same class

Seeking a way to assign unique random backgrounds to each div with the .item class. Initially attempted using PHP and CSS, for example: <?php $bg = array('bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg ...

Creating uniform-sized flex items with varying image sizes

When setting flex: 1 1 0 on flex items, it does not work as expected especially when the flex items contain images of varying sizes. In this scenario, the flex-item containing the image ends up becoming significantly wider than the others. Below is the H ...

Implementing alternating row shades using Bootstrap

I am attempting to apply shading to every other row in a table. Here is the code snippet: dt:nth-child(4n+1) ,dt:nth-child(4n+1) +dd{ background-color:silver; } <div class="col-sm-2 tabularData"> <dl class="dl-horizontal"> <dt> ...

JavaScript is unable to post content or access elements

Check out the following code: <div class="col-2"> <div class="input-group"> <label class="label">Name</label> <i ...

JavaScript function that reveals a block of text when clicked for multiple Div Class elements

I am attempting to create an Onclick event that will trigger when any of my div elements with the class "Clicker" is clicked. I am not proficient in JavaScript and need assistance on how to achieve this. I was able to make it work using document.getElement ...

What is the best way to retrieve the targeted style directly from CSS, rather than relying on the computed style of the element?

Although similar questions have been posed before, such as this one, I haven't found a working solution. Is there a way for me to retrieve the width attribute value from a CSS class and then write it to a div? I'm looking to access the exact va ...

"Bootstrap 4 with the ability to display multiple content sections under a

Currently, I am experimenting with bootstrap tabs and my goal is to have a single tab with multiple content divs. I attempted to achieve this by using two data-target values like data-target=".etab-p1, .etabi-img1". However, the solution only seems to work ...

Creating a shadow effect within a CSS mask: A simple guide

Is it possible to create a shadow within an SVG mask cutout? Here is the code snippet I am currently using index.html <div class="content"> <section id="home-section"> </section> ... </div> styles. ...

What are the steps to showcase a multiplication chart based on user-inputted rows and columns using jQuery?

I'm currently facing some challenges with coding a multiplication table using jQuery. I already have the code to display the multiplication table based on inputted rows, but I need help in modifying it to allow for inputting both rows and columns. An ...

Ways to display an icon in Angular 10 only when it is hovered over

Just getting started with Angular and still learning the concepts. Trying to figure out how to show an icon only when it's hovered over. Can anyone assist me? Sorry, can't share the code because of company rules. The icons are for sorting and ...

Enhance User Experience with a Responsive Website Dropdown Menu

Currently, I am focused on enhancing the responsiveness of my website and I realized that having a well-designed menu for mobile view is essential. To address this need, I added a button that only appears when the screen size is 480px or lower, which seems ...

Strategies for sending arguments to a TypeScript function from an HTML document

When I fetch my list of users as a JSONArray, I want to display the data of each user when their name is clicked. However, I am encountering difficulty passing the user's ID into the function. <ng-container *ngFor = "let user of users"> ...

Send a string to directive via HTML

Trying to implement a "clipboard" directive following this example. In my case, I need to dynamically compute the string to be copied to the clipboard. The goal is to pass the output of a function that generates the string to the directive. Currently, I ...

When an element is transferred to a different <div>, it does not automatically inherit its new CSS styles

There's an element with existing behavior that needs to be moved to meet new requirements without losing its original styles. The challenge is applying new styles to the element after it's been moved. For example: <div id="existingStructure" ...

Uploading images in PHP

I need assistance with uploading an image to a server using PHP, saving it inside a directory, and then retrieving the image URL. Here is my HTML code: <form method="post> <fieldset> <div class="form-group col-md-6 col-lg-6 col-sm-1 ...

Encountering issues with loading styles in Vue single file components

While working on my project at this link, I encountered an issue with displaying a styled modal. Despite trying to import the styles using: <style scoped> @import "../styles/modal-style.css"; </style> and even directly pasting the co ...