How can you prevent a div from wrapping around its content?

I know this question has probably been asked numerous times before, so I apologize for bringing it up again. However, none of the existing solutions have worked for me after spending a frustrating two hours trying to figure out my issue.

Within a section, I have two nested divs. My goal is to make the left div take up 100% of the width minus 200px, with the right div filling in the remaining space. Unfortunately, no matter what I attempt, the divs keep wrapping around their content.

To make things more concise, I've created a jsfiddle link:

http://jsfiddle.net/nttzV/1/

Here's the necessary CSS snippet:

section {
width: 100%;
height: 100%;
border: 2px solid green;
}
.box1 {
float: left;
width: calc(100% -200px);
border: 2px solid red;
display: block;
}
.box2 {
float: left;
margin-left: 5px;
width: 180px;
border: 2px solid blue;
display: block;
}

Answer №1

To properly subtract in the calc function, make sure to include a space after the minus sign:

width: calc(100% - 200px);

Answer №2

If you want the "left div to be 100% - 200px and the right div whatever is left over," consider adjusting your approach. Instead of using the calc() function, which has limited browser compatibility, try setting the width of the right <div> to 200px and letting the left <div> take up the remaining space.

You can achieve this layout with the following code:

<section>
    <div class="boxR"></div>
    <div class="boxL"></div>
</section>
section {
    min-width:300px;
}

div.boxR,
div.boxL {
    min-height:200px;
}

div.boxR {
    float:right;
    width:180px;
    background-color:#CCC;
}

div.boxL {
    margin-right:200px;
    background-color:#555;
}
  

For a working example, check out this JSFiddle demo.

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

Concealing forms within an Angular 5 application

I'm currently working on displaying the terms of use on the initial screen along with two buttons. If the user clicks the accept button, they will be directed to the authentication form. However, if they click refuse, the "Refused Terms" screen will a ...

Tips for stopping table column width growth when an icon is inserted into a cell dynamically

In the table I have, the header is clickable and when clicked, it triggers sorting. Clicking on the header title adds an icon (ascending or descending) to the right of the title, causing the column width to increase based on the size of the icon. Refer to ...

What is the best way to include a CSS stylesheet in a Wordpress theme?

Is there a way for me to properly load my custom CSS in Wordpress so that the dynamically inputted content on my pages matches the styling of my header and footer? In my functions.php file, I currently have the following code, but it only prints the code ...

Two adjacent div tags spanning the full width of the page, with differing widths (40% and 60%) that resize to 100% width and stack on top of each

I've been experimenting with a layout challenge and running into some obstacles. Two div tags are at the heart of the issue. Div A features an image with a default size of 768 x 400, while Div B displays an image at 1152 x 400. These images are meant ...

What is causing Safari to block styling for <em> elements in RESET.CSS only?

I am utilizing Eric Meyer's reset.css file, which can be found at this link: Interestingly, my <em> definition in my main stylesheet is working perfectly fine on all browsers except Safari. Somehow, only in Safari, the styling for italics is no ...

Connected selection menu

I have noticed several discussions on this topic, but many of them rely on JavaScript or focus solely on a standard drop-down list. I have developed a PHP function that generates drop-down menus based on select queries in my database. Currently, this part ...

Stylish CSS menu design

I am struggling to set up a top menu bar using float: left. How can I achieve the desired behavior for a top menu bar? The code snippet below demonstrates what I have tried so far: * { margin:0; padding: 0; } #menu { background-color: yell ...

CSS styling for the dropdown list in the content/option section

Can someone help me with a simple question I've been researching for hours without success? I'm trying to figure out how to style the content/option/popup of a dropdownlist on the right side of the 'open button' like in this screenshot ...

What is the process for assigning a CSS class to a div element that is generated by react's render() function?

In React, I am encountering an issue where the css class I want to set on the div returned from render is being removed. This problem even persists when I try nesting another div inside the first one and setting the class on the enclosed div. Has anyone ...

Text that disappears upon clicking on show/hide按钮

Could someone please help me figure out how to prevent the "See more" text from disappearing when clicked in the example below? I want it to stay visible. Thank you! ...

The input[type="submit"][disabled] is malfunctioning on IE8

My input submit button has 3 different states: Active : Orange button Mouse down : Dark gray button Disabled : Light gray button <input type="submit" id="savebtn" class="save_button"/> CSS: .save_button { background: url('/Images/save_but ...

Is it possible to have a text box that is partially read-only and partially editable?

<form> <div class="col-sm-2" style="margin-top: 5px;color:#357EBD;font-weight:bold;" id="sub-ticketid"><?php echo 'Ticket 12345#'; ?></div> <input type="text" class="form-control" style="background:#fff;" name="sub-hold ...

Tips for arranging 3 cards in a straight line across the horizontal axis

I'm new to coding and I've recently started using HTML, CSS, and Bootstrap 4. Currently, I'm working on a webpage that includes a navigation bar, 4 cards, and a footer with information. I utilized Bootstrap to create rows and columns, but I& ...

Trouble with displaying vertical scroll bar in React app when height is set to 100%

I am developing a React application where I need the content to always occupy at least 100% of the height to style the background accordingly. I have attempted to achieve this by setting the CSS height to 100% at the top level, which works fine. However, t ...

I'm struggling to find the correct media query to fix the problem with resizing my window

Looking to create a layout where two large icons are displayed side by side horizontally, but then switch to a vertical layout when the window size is reduced (such as on a mobile phone). I know that media queries are necessary for this, but I am unsure of ...

Utilizing the Bootstrap 4 jumbotron classes to create an interactive image carousel

I've made good progress on my mockup website by laying out the basics. #hero { background: url("https://picsum.photos/1280/710") no-repeat center; background-size: cover; } .book { height: 100px; width: 100px; bottom: 9vh; } &l ...

What is an alternative method for creating a horizontal line without the need for the <hr> tag?

Is there a way to create a slim horizontal line without using the <hr> tag ? This is what I attempted: .horizontal-line{ border-top: 1px solid #9E9E9E; border-bottom: 1px solid #9E9E9E; } While it does function, I am hoping for a thinner line. ...

Error: Unable to change image -- TypeError: Cannot assign value to null property 'src'

As I work my way through the textbook for one of my classes, I am practicing by building an image swapping page. The concept is simple - clicking on a thumbnail swaps out the main image and enlarges it as if it were linking to another web page. Despite fol ...

What is the process for setting up custom HTML tags in Resharper?

I am looking to customize my HTML files by using custom tags to incorporate knockout components [1]: <like-widget params="value: userRating"></like-widget> To enable the tag in VisualStudio's HTML formatting settings, I made the followin ...

What is the best approach for optimizing CSS changes across multiple pages in WordPress?

I am looking to customize the background images for different groups of pages on my WordPress site. Currently, I am manually assigning the background images to individual pages by using PageID in the code snippet below. With over 1000 pages, this method i ...