Diving Div Floating

I've been working on this code snippet for quite some time, but I can't seem to find a solution that keeps the two div elements horizontal when resizing the browser window.

<!DOCTYPE html>
<head>
    <style>
        #ONE {
            float: left;
            border: 1px solid red;
            width: 500px;
            height: 50px;
        }
        #TWO {
            float: left;
            border: 1px solid yellow;
            width: 500px;
            height: 50px;
        }
    </style>
</head>
<body>

<header>

    <div id="ONE"></div>
    <div id="TWO"></div>

</header>

</body>
</html>

Every attempt I make results in the "TWO" div dropping below "ONE" on resizing. How can I achieve my goal of keeping them side by side without depending on screen width changes?

Visit this link for the original code and problem.

Answer №1

Moreover, to build upon @connexo's response for more up-to-date browsers that have flexbox support.

* {
  box-sizing: border-box;
}
header {
  display: flex;
}
#ONE,
#TWO {
  height: 50px;
  flex: 0 0 500px;
}
#ONE {
  border: 1px solid red;
}
#TWO {
  border: 1px solid green
}
<header>
  <div id="ONE"></div>
  <div id="TWO"></div>
</header>

This setup will trigger a scrollbar when the width is below 1004px (or 1000px if using box-sizing:border-box).

JSFiddle Demo

A few benefits of this approach.

Firstly, the default behavior of flexbox is nowrap so you don't need to explicitly specify it.

Secondly, it avoids the white-space problem that typically requires a font reset workaround.

Note: Additionally, you can combine both techniques and flexbox will take precedence over inline-block if the browser supports it....progressive enhancement!

JSfiddle Demo (both)

Answer №2

If you're looking to keep two divs on the same line with fixed widths, try using a combination of display: inline-block; and white-space: nowrap;.

This will ensure that your divs stay in one line, even though a horizontal scrollbar may appear if the viewport width shrinks below 1004px.

header {
    font-size: 0; /* eliminates space between #ONE and #TWO */
    white-space: nowrap; /* prevents wrapping of inline-block children */
}
#ONE, #TWO {
    display: inline-block;
    font-size: 14px; /* adjust font size as needed */
    height: 50px;
    width: 500px;
}
#ONE {
    border: 1px solid red;
}
#TWO {
    border: 1px solid yellow;
}

Check out this JSFiddle for an example.

Answer №3

Check out this JSFiddle demo

body{font-size: 16px;}
        
#apple {
            color: red;
            margin-left: 10px;
            font-weight: bold;
}
#orange {
            color: orange;
            margin-left: 20px;
            font-style: italic;
}

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

Struggling to insert HTML into an element using JavaScript or jQuery within a function

I'm currently experimenting with implementing smooth page transitions using Barba. The following is my code snippet for adding some HTML when a new page is loaded. Barba.Dispatcher.on('newPageReady', function(currentStatus, oldStatus, conta ...

Is it possible to place Angular Material components using code?

Currently, I'm in the process of creating a responsive Angular application. Is there any way to adjust the height and position of the <mat-sidenav-content></mat-sidenav-content> component in Angular Material programmatically without relyi ...

When using the Column width property in React Material UI, it may not automatically expand

I've been playing around with React Material UI Tables and I'm having trouble making the columns auto-expand without wrapping. I tried setting the 'width: auto' property in the <Table size="small" style={{ width: auto}}> ...

Is there a downside to concealing a checkbox off-screen using position: absolute for a clever workaround?

I recently came across a clever trick known as the checkbox hack, which involves hiding checkboxes on web pages by positioning them off-screen. The example provided on CSS Tricks demonstrates this technique with the following code: position: absolute; top ...

Chrome is throwing a syntax error with an unexpected token in jQuery replaceWith

jQuery('div#top').replaceWith('<div id="top"> </div>') When I try to replace the entire content of the top div using jQuery, Chrome gives me an error: "Uncaught SyntaxError: Unexpected token" on the first line. I'm no ...

When the mouse reaches the far left portion of the screen, an action will be triggered

Is there a way to trigger an action when the mouse reaches the far left or right of the screen using HTML, CSS, jQuery, or any other method? I'm specifically trying to show my navbar when the mouse reaches the left end of the screen. Any assistance wo ...

Adding a certain number of days to a date within an existing ng-module in AngularJS

Hey everyone, I'm new to using AngularJS and I am currently attempting to incorporate one ng-module value into another ng-module date input within AngularJS. For example: if the date is 17-08-2016 and we add 20 days, the expected result should be 03-0 ...

How can I use TailwindCSS in NextJS to remove the "gap" className from a component?

Currently, I am working on button components in NextJS with Tailwindcss and I am encountering a problem with some variants. Everything works fine, except when I remove the children (button text), I notice something strange that I can't quite figure ou ...

Ajax TabContainer mysteriously vanishes during Postback

I'm currently working on a GIS web application using C# ASP.net. Within my project, I have an Ajax TabContainer that houses multiple TabPanels containing various elements like a map window and scale bar from the ESRI WebAdf toolkit. Below is a simpl ...

Notification of failed fetch in backbone

We are experiencing a problem with our backbone application. Our goal is to show a notification to the user when a fetch fails (due to timeout or general error). Instead of displaying an error message on a new page, we want to overlay a dialog on top of th ...

Struggling with determining the perfect transition speed for the sidemenu display

I'm a newcomer to web development and facing an issue with setting the transition speed for opening and closing this side menu. Despite adding transitions in the CSS and specifying duration in the Javascript, the menu continues to open instantly. I di ...

Start by prioritizing items with a higher click count when looping through the

I am creating a component to display recent search results. The API provides these results, including a value called "click_count". Each time a user clicks on a positive search result from the recent searches, this click_count value will increment by one. ...

Continuous clicking on navigation items with Angular JS ui.router causes looping navigation

In our current project, we have implemented ui.router for routing purposes. However, we have encountered an unusual problem where clicking on navigation items in quick succession causes all the pages to enter into a loop and never stops until it crashes. ...

Achieve text fading effects with JavaScript for a dynamic user experience in an ASP.Net C# application

In my WebForm ASP.Net application, I have implemented a feature where I change the text on the web page. To draw user attention to this change, I want to fade out the old text completely, replace it with new text, and then fade in the new text. Currently, ...

Creating a striking Materialize Jumbotron: A step-by-step guide

Recently, I came across a sign up page that caught my eye. You can view it here. Upon inspecting the source code, I discovered that it was built using angular material design. This led me to wonder how I could achieve a similar look using Materialize.css ...

Obtaining the height of a jQuery element after it has been appended to another div may result in

I am struggling to make a div scrollable only when it reaches a certain height after adding text to it. The jQuery code I wrote to check the height keeps returning zero. Can anyone offer guidance or suggestions? HelpOverlay.prototype.buildContent = functi ...

"Modifications made to Magento's JavaScript and CSS are not showing up

Recently, I encountered an issue where I added custom JavaScript code and it appeared to be included, but when I made changes to the content, it didn't seem to have any effect. It continued running the older JavaScript file despite clearing the cache ...

The filtering function using jQuery and ajax

Hello, I'm currently working on a website that compares the prices of domain names. I could use some assistance with a specific function that I'm trying to implement. At the moment, there is a section on the website where users can click on dif ...

Creating dynamic SVG animations through CSS instead of relying on the <animateMotion> tag offer greater flexibility and control

I am attempting to position an arrow at the midpoint of a bezier curve. The method outlined in this StackOverflow question, which involves using <animateMotion> to move a <path> representing the arrow and freezing it at the center of the curve, ...

Error occurs when SRCSET loads the wrong size because "sizes" parameter is missing

According to the guidelines, when sizes is not specified in the image attribute, the browser should automatically calculate the required size based on CSS rendering. In this scenario, my image is 300px and the browser should select the 300px image. Howeve ...