Avoiding line breaks for a div positioned on the right within a flexible container

My current design consists of a red bar inside a container that is positioned between two black boxes. The size of the boxes remains fixed, while the red bar and container are both based on percentages.

I am aiming to decrease the size of both the container and the red bar without causing the right black box to drop down to the next line. I managed to address this issue using custom JavaScript calculations, but I prefer to keep functionality and design separate. I believe there should be a CSS solution that doesn't require hacks or additional div tags.

Any suggestions on how to achieve this?

.container {
  width: 80%;
  height: 40px;
  background: grey;
}
.box {
  height: 50%;
  border: 15px solid black;
  border-top: none;
  border-bottom: none;
  float: left;
}
.bar {
  width: 80%;
  height: 100%;
  background: red;
  float: left
}
<div class="container">
  <div class="box"></div>
  <div class="bar"></div>
  <div class="box"></div>
</div>

JSFiddle

Answer №1

CSS3 introduces a new flex display style that is now supported by all major browsers.

.container {
  display: webkit-flex;
  display: flex;
  height: 40px;
  background: grey;
}
.box {
  height: 50%;
  border: 15px solid black;
  border-top: none;
  border-bottom: none;
}
.bar {
  width: 100%;
  height: 100%;
  background: red;
}
<div class="container">
  <div class="box"></div>
  <div class="bar"></div>
  <div class="box"></div>
</div>

When setting the box elements to a specific width, it is recommended to use min-width instead of width.

Answer №2

Utilize the calc() function in your CSS styling. This feature is a part of CSS3 and is widely supported by all major browsers, including IE9.

.bar {
    width: calc(100% - 60px);
}

.container {
  width: 80%;
  height: 40px;
  background: grey;
}
.box {
  height: 50%;
  border: 15px solid black;
  border-top: none;
  border-bottom: none;
  float: left;
}
.bar {
  width: calc(100% - 60px);
  height: 100%;
  background: red;
  float: left
}
<div class="container">
  <div class="box"></div>
  <div class="bar"></div>
  <div class="box"></div>
</div>

Answer №3

Consider using a "table" layout approach

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        .container {
            width: 80%;
            height: 40px;
            background: grey;
            display: table;
        }
            .container > div {
                display: table-row;
            }

                .container > div > div {
                    display: table-cell;
                    vertical-align: top;
                }
        .box {
            height: 50%;
            margin: 0 7px;
            border: 15px solid black;
            border-top: none;
            border-bottom: solid 1px black;
            /*float: left;*/
        }

        .bar {
            width: 80%;
            height: 100%;
            background: red;
            /*float: left*/
        }
    </style>
</head>
<body>
    <div class="container">
        <div>
            <div>
                <div class="box"></div>
            </div>
            <div class="bar"></div>
            <div>
                <div class="box"></div>
            </div>
        </div>
    </div>
</body>
</html>

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

Effortlessly glide to the form and center your attention on the textarea

Upon clicking the link, I aim to accomplish three tasks: Implement smooth scrolling down to #form Automatically focus on the textarea for immediate message writing Add extra top margin to account for a fixed top bar on the website. <a class="link" ...

Adjust the color of the text within a div element when hovering over and moving the mouse away

Is there a way to change the text color on mouse hover and mouse out, even with multiple nested div elements? I'm having trouble getting it to work, so any help would be appreciated. <div class="u860" id="u860" style="top: 0px;"> <div id ...

Misplacement of HTML head tags

Is it a grave error in this layout that H1 isn't the first rendered head element? Is there any way to correct this issue? Given that both columns have variable lengths, I am struggling to find a workaround. Reference I was requested to provide a ref ...

Adjust the CSS of a dynamically generated jQuery checkbox button in real-time

I am working on a project where I am creating a series of jQuery checkboxes dynamically within a loop. Here is how I am doing it: var checkbox = $('<input>').attr({type: 'checkbox', id: checkbox_id); panel.append(checkbox); panel ...

How can we transition a line of code from PHP to Smarty?

I'm currently working on converting a php line of code to Smarty. I've managed to convert the variable successfully, but I am struggling with incorporating the small syntax before it. Below are both sets of syntax - how can I include link_it with ...

Opting for CSS3 transitions over jQuery animate

Currently, I am working on animating a block of HTML that involves fading in (opacity) and slightly moving from the left (margin-left)... $('.latest-stats').delay(1000).animate({ opacity: 1, marginLeft: '55px' }, 1000, function () { ...

Height of Hover Background Color in WordPress Menu

Greetings! Welcome to my website I'm in the process of modifying the hover color for the top menu, and I've managed to change it successfully. However, I would like the hover effect to cover the entire height of the menu, as right now it's ...

Is there a unique shape element, such as a true circle, available in HTML5?

There doesn't seem to be any documentation on this, suggesting they may not exist. However, it's always worth investigating. I am in search of a perfectly circular (or any polygon shape other than a rectangle) element. I can create a circle in c ...

Position the div element beside the image, rather than below it

I am having an issue with Bootstrap where the card I want to display on the right of an image is appearing below it instead. I attempted to use the display: inline-block property, but unfortunately, that did not solve the problem. I also explored other so ...

Creating a full-screen background image in React and updating it with a button

This issue has been consuming a lot of my time, and though I know the answer must be out there somewhere. My problem is quite similar to what's being discussed here. Essentially, I am trying to dynamically change the background image on button click b ...

Enter information to accompany your images in the description box

After browsing through numerous websites tonight, I stumbled upon this code. My goal is to create a photo upload page on Google Drive and set up a dropbox for it. I'm facing an issue where the information inputted into my placeholder box (city and ye ...

The content spills out of the container

I'm currently working with a div that displays scrolling text when it overflows. I am wondering if there is a way to hide the scroll bars until the text actually overflows? Additionally, is there a method to make the overflow occur only vertically and ...

Display the query results on a separate blade

In my original attempt, I intended to display the results in a modal but later decided to show them in a different blade. My goal is to fetch all comments related to a post using its post_id. However, I encountered the following error: The GET method is no ...

What is the best way to transmit a response from PHP to Ajax?

A JavaScript function is used here that utilizes the POST method to send form data to PHP. The PHP script then checks this data in a database to verify its authenticity. However, there seems to be confusion on how to convey the response from PHP back to Ja ...

Determination of Vertical Position in a Displayed Table

I am trying to incorporate infinite scrolling with an AJAX-based loader in the body of an HTML table. Here is a snippet of my HTML code: <table> <thead> <tr><th>Column</th></tr> </thead> <tbody> ...

Troubleshooting: Issue with multiple file input elements on page - only the first input is functional and not displaying preview images

Having some trouble with loading multiple file inputs and their preview images. I've tried using getElementsByClassName but I suspect that I'm not iterating over them correctly. I want to avoid using unique IDs for the elements because they will ...

Learn the process of playing a video in an HTML5 video player after it has finished downloading

// HTML video tag <video id="myVideo" width="320" height="176" preload="auto" controls> <source src="/server/o//content/test2.mp4" onerror="alert('video not found')" type="video/mp4"> Your browser does not support HTML5 vid ...

Switch out 2 Bootstrap columns for 2 concealed columns with just a click. Utilizing Rails 4 and Bootstrap

Using Twitter Bootstrap 3 for a column system showcasing four similar advertisements at the bottom of the page. Code Snippet: <div class="row similar"> <% @recomended_ads.each do |advertisement| %> <div class="col- ...

Struggling to properly position my dropdown menu without causing the div content to shift downward

<div id="header"> <a href="#" id="logo"></a> </div> <div id="nav_cont"> <ul id="nav"> <li id="main_btn"><a class="mainlink" href="#">Parent 01</a></li> <li id="communities_btn">< ...

What is the best way to continuously run a series of functions in a loop to create a vertical news ticker effect?

I'm in the process of creating a vertical latest news ticker, and although I'm new to javascript, I'm eager to learn and build it myself. So far, I've come up with this, but I want the news cycle to restart once it reaches the end. ...