When two div tags are set to float and add up to 100% width, the second div will be forced

New to the world of coding.

I am faced with a dilemma involving two div tags, one with 80% width and the other with 20%. Despite totaling up to 100%, the second div insists on moving to the next line.

The goal is to utilize the full line while keeping both divs on the same row.

<!DOCTYPE html>
<html>
<head>
<style>

.border {
border-style: dotted;
}

</style>
</head>
<body>

<div class="border" style="width: 80%;">thing</div>
<div class="border" style="width: 20%; float: left;">other thing</div>

</body>
</html>

Answer №1

Give this a shot:

Here is the CSS code snippet:

.table_format {
  display: flex;
  width: 100%;
}
.border {
  border: 1px solid #ddd;
}

And here is the corresponding HTML:

<div class="table_format">
  <div class="border" style="width: 80%; float: left; background: #dd0d0d;">thing</div>
  <div class="border" style="width: 20%; float: left; background: #4abdac;">other thing</div>
</div>

For further reference, here is the link to jsfiddle.

Answer №2

It is important to take into account the border width when setting the overall width of an element. The border will add to the total width, so adjusting the width value accordingly is necessary for achieving the desired result.

.border {
    border-style: dotted;
}
<div class="border" style="width: 80%; float: left; background: #dd0d0d;">thing</div>
<div class="border" style="width: 20%; float: left; background: #4abdac;">other thing</div><br>

<div style="width: 80%; float: left; background: #dd0d0d;">thing</div>
<div style="width: 20%; float: left; background: #4abdac;">other thing</div><br>

<div class="border" style="width: 79%; float: left; background: #dd0d0d;">thing</div>
<div class="border" style="width: 19%; float: left; background: #4abdac;">other thing</div>

Answer №3

Give it a shot,

<div class="frame" style="width: 80%; float: left;">object</div>
<div class="frame" style="width: 20%; float: left;">another object</div>

Answer №4

To ensure the border is included in the width of an element, you can apply box-sizing:border-box;:

Check out the code snippet below:

.border {
    border-style: dotted;
}

* { box-sizing: border-box; -webkit-box-sizing: border-box; }
<div class="border" style="width: 80%; float: left; background: #dd0d0d;">thing</div>
<div class="border" style="width: 20%; float: left; background: #4abdac;">other thing</div><br>

Answer №5

It's important to remember that block level elements, like the div in your case, always stretch from one side of the page to the other. When you set a width, you're actually controlling the width of the content inside the block, not the block itself as a whole. To achieve your desired layout, you may need to float or reposition the first div so it doesn't take up the full width.

Additionally, keep in mind that borders are outside the content width and padding and margin also affect the overall size. By setting a width of 80% and 20% for your div elements, you must consider how this interacts with the parent element (in this case, the body) and any border thickness which adds extra pixels to the total width.

If you want to maintain the borders, you could adjust the widths of the div elements to accommodate them or alternatively remove the borders and float the first div instead to achieve your desired layout.

In response to a question in the comments, you can use the CSS calc() function to calculate the width by subtracting the necessary pixels corresponding to the border width. Remember to account for any additional space taken up by the border when using this method.

UPDATE: It's worth noting that the width of certain border styles, such as "dotted", may exceed 1px. Adjust your calculations accordingly to accommodate the specific border style being used.

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

A guide to showcasing tabs as a navigation menu based on media size using AngularJS

Help needed with creating a navigation menu that includes nav-icons on specific media screen sizes. The goal is to display all tabs as a list when clicking on the nav-icon. An attempt was made to implement a navbar, but it interfered with the tab styling. ...

Keep the navigation bar in motion as the page is scrolled

I am currently designing a webpage with Bootstrap and have a fixed top navbar along with a side navbar that uses scrollspy for content navigation. However, as I scroll down the page, the side navbar remains static and does not move along. I would like both ...

The width of the flexbox child item is too narrow for the content it contains

I am facing an issue with the Flexbox child element not aligning correctly with the content width. Here is the code snippet: https://i.sstatic.net/jtCnSuhF.png .pw-event-options { -webkit-box-flex: 0; -webkit-flex: 0 0 280px; -moz-box-flex: 0; ...

Modify the input class once an image has been chosen (But not yet submitted)

Looking for a solution to update background image when file is selected <input type="file" class="imgupload" name="file" /> I've created image uploader with a hidden form element inside a padded div, displaying a background image. Want to chan ...

The custom pagination feature in Addsearch UI always default to displaying the first page of search

I am currently using addsearch-ui version 0.7.11 for my project. I have been attempting to integrate a customized pagination function based on this example template. However, I have encountered an issue where the arrow buttons always navigate to the first ...

div that adjusts its max-width to always perfectly match the width of its contents

Check out this jsfiddle for more information: http://jsfiddle.net/tN6QE/2/ The CSS code we are using is as follows: div { display: inline-block; border: solid 1px black; padding: 10px; max-width: 200px; } We are dealing with two scenarios here. ...

Move the button to the following line and center it. Ensure that the background image remains at full size

.home{ background-image:url("https://cdn.pixabay.com/photo/2016/12/27/21/03/lone-tree-1934897_960_720.jpg"); background-repeat: no-repeat; height: 100vh; display: flex; align-items: center; justify-content: center; } .hero-text{ fon ...

Unique WordPress page with distinct logos

How can I display a different logo for a specific page on my WordPress site? I am using the default Logo theme, but I need to replace the logo for a particular page. Below is the code that controls the logo display in the theme file: <a href="<?ph ...

Changing the color of Bootstrap Carousel Indicators for a single slide

I am currently facing an issue with the default bootstrap carousel. One of my slides has a white background, causing the carousel indicators to disappear on that specific slide. Although I have a Javascript/jQuery solution in place, it appears to be slig ...

React-router is failing to redirect properly within a specific component

I have configured react router to redirect from the "/" route to a custom route "/:city/posts", but the redirect is not working as expected. When the button is clicked, the page refreshes. This setup is working fine in other components, so I'm not sur ...

Tips on styling div elements to act as table cells

Is there a way to style divs with ids 1&2 and have the inner divs act like a table? Specifically, I want the first column to display a label, the second column to display an input field, and the third column to display a button. I attempted to adjust ...

Is there an easy method for customizing scrollbars?

I'm looking to include an "overflow:scroll" in a navigation div, but the default scrollbar on windows is unattractive. Is there a simple way to customize the scrollbar without having to download extra JavaScript libraries, APIs, and so on? ...

Styling CSS for Centering a Heading on an Image's Center

Struggling to center an h3 heading in the middle of an image within a grid? Look no further, as I have 3 images across one row - four columns per image/heading. Check out the desired layout https://i.stack.imgur.com/8VFRt.png I've managed to center t ...

Attempting to obtain a score value from the input, but unsuccessful in doing so

Prior to posing this question, I had already managed to successfully retrieve the score value. Below is my original script: <script> $.ajax({ type: "POST", url: "https://example.com/paylist.php?acc=useraccount&json=1", ...

adding content to div is becoming less speedy

Currently, I'm developing a drawing board using only html/css/jquery and the drawing functionality is working smoothly. The approach I've taken involves capturing the mousemove events and placing a dot (div) at each point where the event occurs, ...

What's the best way to integrate Bootstrap into my HTML document?

Hey there, I'm new to the coding world and just started learning. I could use some assistance with including Bootstrap v5.1 in my HTML code. The online course I'm taking is using an older version of Bootstrap, so I'm having trouble finding t ...

Implementing a striking image background for your Bootstrap website header

I am trying to add a background image to a main header that contains a navbar, but I am having trouble getting the image to display correctly. Here is the jsfiddle link for reference: http://jsfiddle.net/Mwanitete/fgkq759n/8/ Here is the HTML code snippe ...

Is it possible to generate an HTML element by utilizing an array of coordinates?

I have a set of 4 x/y coordinates that looks like this: [{x: 10, y: 5}, {x:10, y:15}, {x:20, y:10}, {x:20, y:20}] Is there a way to create an HTML element where each corner matches one of the coordinates in the array? I am aware that this can be done usi ...

Utilizing Jquery for draggable/droppable functionality in combination with custom overflow styles

Encountering a challenge with a drag and drop system that utilizes CSS overflow properties to enable vertical scrolling of a list of values. The issue arises when dragging an item between selected and unselected areas, causing the dragged item to disappear ...

The JavaScript onclick attribute of the button cannot be seen in the html inspect tool

I created a code where I made a button called "Clear Cart" and passed it to the HTML code using JavaScript. I included the attribute 'onclick' on the button, but when I checked the page, the button appeared without the 'onclick' attribu ...