Utilize CSS to occupy the rest of the container's width

I have a header bar with three elements - a left section, a middle section, and a right section. I want the middle section to automatically fill up the remaining space in the header. How can I achieve this?

header {
  background: red;
}

#middle {
  background: orange;
  display: inline-block;
}

#right {
  background: green;
  display: inline-block;
}
<header>
  <img src="https://picsum.photos/100"/>
  <div id="middle">222</div>
  <div id="right">333</div>
</header>

Answer №1

Try using the calc function for precise width adjustments!

Check out this link for a live example

Here is an example of how to use it in your HTML and CSS:

<div class="left">
  This div is set to be exactly 100 pixels wide!
  </div><!-- Notice there isn't a space between the divs! *see edit for alternative* --><div class="right">
    This div fills up the rest of the available width!
  </div>

To achieve this layout, you can style your elements with the following CSS:

.left {
  display: inline-block;
  width: 100px;

  background: red;
  color: white;
}
.right {
  display: inline-block;
  width: calc(100% - 100px);

  background: blue;
  color: white;
}

Update: If you prefer having no space between the two divs, you can also try setting font-size: 0 on the outer element as an alternative method.

Answer №2

If you want to achieve this specific layout, you can utilize CSS table-cells.

To implement this layout, make some adjustments to your HTML code:

<div id="header">
    <div class="container">
        <div class="logoBar">
            <img src="http://placehold.it/50x40" />
        </div>
        <div id="searchBar">
            <input type="text" />
        </div>
        <div class="button orange" id="myAccount">My Account</div>
        <div class="button red" id="basket">Basket (2)</div>
    </div>
</div>

You just need to remove the wrapper element around the two .button elements.

Add the following CSS styling:

#header {
    background-color: #323C3E;
    width:100%;
}
.container {
    display: table;
    width: 100%;
}
.logoBar, #searchBar, .button {
    display: table-cell;
    vertical-align: middle;
    width: auto;
}
.logoBar img {
    display: block;
}
#searchBar {
    background-color: #FFF2BC;
    width: 90%;
    padding: 0 50px 0 10px;
}

#searchBar input {
    width: 100%;
}

.button {
    white-space: nowrap;
    padding:22px;
}

Set display: table to .container with a width of 100%.

For .logoBar, #searchBar, and .button, apply display: table-cell.

Adjust the width of #searchBar to 90%, causing other elements to adjust accordingly.

Use text-align and vertical-align in the table cells as needed.

You can view a demo here: http://jsfiddle.net/audetwebdesign/zWXQt/

Answer №3

To achieve this layout, you can utilize flexbox.

Set your container element (in this case header) to use display: flex. Then, in the element you want to span the full width, apply flex-grow: 1.

header {
  background: red;
  display: flex;
}

#middle {
  background: orange;
  flex-grow: 1;
}

#right {
  background: green;
}
<header>
  <img src="https://picsum.photos/100"/>
  <div id="middle">222</div>
  <div id="right">333</div>
</header>

Answer №4

To create a visually appealing layout, you can enclose the image and search bar within their own container. Float the image to the left with a specific width to separate it from other elements on the page.

By taking the image out of the normal flow, you prevent other items from adjusting their position around it. To ensure that the search bar appears correctly positioned next to the image, add a left padding equal to the image's width plus some space.

This technique results in a fixed-width image while allowing the rest of the container block to dynamically adjust based on the search bar's content.

<div class="container">
  <img src="img/logo.png"/>
  <div id="searchBar">
    <input type="text" />
  </div>
</div>

Here is the corresponding CSS:

.container {
  width: 100%;
}

.container img {
  width: 50px;
  float: left;
}

.searchBar {
  padding-left: 60px;
}

Answer №5

Using CSS:

max-width: -moz-available;

Answer №6

If I were to create a search bar, I would approach it in this way:

<div id='search-logo-bar'><input type='text'/></div>

Using the following CSS styling:

div#search-logo-bar {
    padding-left:15%;
    background:#666 url(logo.png) no-repeat left center;
    background-size:20%;
}
input[type='text'] {
    display:block;
    width:80%;
}

To see a demo of this implementation, visit:

http://jsfiddle.net/5MHnt/

Answer №7

To simplify your search process, just insert the image into the searchBar division.

<div id="searchBar">
    <img src="images/logo.png" />                
    <input type="text" />
</div>

Answer №8

After exploring various possible solutions, I conducted a brief experiment. Here is the result:

http://jsbin.com/hapelawake

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

transforming a text input into unadorned plain text

Currently, I am in the process of creating a small HTML form that consists of a single textbox input. Once the user enters text into this textbox and clicks on a button located at the end of the page, I would like the textbox to transform into normal plain ...

The text-overflow property with ellipsis functionality appears to be malfunctioning in Internet Explorer when applied to input elements, yet functions

Having an issue with the MS IE (version 11) when using the text-overflow: ellipsis; property for an input field. It appears that this specific property is not working properly in this browser. If anyone has any suggestions or solutions, it would be greatl ...

Learn how to efficiently transfer a dynamic subtitle text value from an HTML5 video player to a database

I have successfully developed an HTML5 video player with subtitles. Currently, my goal is to allow the user to submit the subtitle text at a specific timestamp to a database. Despite trying various methods, I am facing challenges in fetching the subtitle t ...

I am experiencing issues with how the data from the database is being displayed

I'm trying to fetch all my users from the database. The code works, but I believe there is an issue with my syntax and how it's being echoed. As a result, it generates a separate table for each data entry. Check out the attached image for refer ...

When the value is inputted into the textbox and the enter key is pressed, a fresh chip displaying the entered value will materialize

Whenever I click on a textbox, a menu opens that requires me to enter a value in the textbox. After entering the value and hitting enter, a new chip with the entered value should appear. I tried creating an Onchange event to pass the values to the chip com ...

Combining multiple rows into a single row and inserting a new column

Hi there, I'm currently diving into the world of programming and tackling a project involving PHP and MySQL. However, I've encountered a bit of a roadblock. My current setup involves a table named marks, where each time a mark is added to a stud ...

HTML - Lists Not Displaying Properly in Numerical Order

My goal with HTML is to: Produce 2 Numbered Lists Nest an Unordered List within Each Ordered List and add elements inside Unfortunately, my numbering isn't displaying correctly. Instead of 1. 2. etc., I'm seeing 1. 1. Below is the code I am u ...

Utilize the apostrophe-images-widgets custom widget by calling apos.attachments.url function twice

I'm currently working on creating a unique layout using the apostrophe-images-widgets for my website. I've managed to override the html template at the project level in lib/modules/apostrophe-images-widgets/views/widget.html. The problem I'm ...

Steps to set a background image for an entire page in VueJS

I recently downloaded a Background Image to add to the Home Page of my VueJS application. Unfortunately, when the page loads, only a portion of the page is covered by the image. My goal is to have the entire page filled with the image, except for the Navba ...

sending live video from a Python server to a browser-based client

Is there a way to stream a video from a python server to an HTML page web client (Node.js)? In my Node.js web project, I am currently using Python to write frames and JavaScript to read them. However, I am facing limitations with both frame rate and resol ...

Using the <a href> tag to send emails directly from within an HTML document

I am working on creating a basic HTML page using a free web template I found online. In the "contact" section, I want to configure it so that <a href= "mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92fff7d2f5fff3fbf ...

Guide for implementing tabs using router-view in Vue.js

I've been utilizing vuesax [ https://github.com/lusaxweb/vuesax ] for managing tabs. Within vs-tabs, I have multiple router-views. I am looking to display different Vue template files for each respective tab of the router-view. Below is the code snip ...

First-character styling not applying to ID in CSS

My CSS :first-letter selector is effective on all p elements, however when applied to a span with the id #fletter, it doesn't seem to work... #fletter span:first-letter { font-weight: 600; font-size: 25px; } <span id="fletter"> The : ...

Can one intercept the window.location.replace event?

Suppose I am currently browsing the URL "example.com/somepage#somehash" and then decide to execute window.location.hash = "anotherhash", the URL will be updated to "example.com/somepage#anotherhash". This action triggers the window.hashashchange event. On ...

Restricting thumbnail scrolling within the visible area in a jQuery/CSS Gallery/Slideshow

Is it possible to create a gallery slideshow feature where the selected thumbnail's image source will be displayed in the main image container? I believe I can implement that part successfully. Currently, my challenge is defining the thumbnail scroll ...

Managing the vertical positioning of grid items: tips and tricks

Is there a way to make the hexagons in my grid responsive to the layout, so that their height and width remain fixed relative to their container? Currently, they are overflowing from the grid container as shown in this jsfiddle: https://jsfiddle.net/gv5wc1 ...

Discovering an Improved Method for Implementing a Functional jQuery Menu Bar

Currently, I have a functioning Menubar where each button on the menu triggers a secondary Menubar using jQuery. The code snippet is as follows: <script> $(document).ready(function(){ $("#homeButton1").mouseover(function(){ $(".secondMen ...

The power of Geolocation API coupled with the efficiency of AJAX requests

I'm currently working on a personal project that involves utilizing the W3C Geolocation API to fetch the user's location and then displaying the weather forecast for that specific location using the Wunderground API through an AJAX request. Here ...

Image fails to download on Safari and iPhones in PHP

Can anyone help me figure out why images are opening in a new window instead of downloading inside Chrome and Firefox on my iPhone? ...

What is the best way to distinguish Xpath when dealing with several elements that share identical attributes and text?

I need to find and click on the link labeled "Customer One". The current xPath I am using is: //div[contains(@class,'client-info') and contains(div/text(),'Customer')] When using Firepath, this xPath returns four separate elements (I ...