Arranging 2 divs within a parent div using CSS placement

In my design, I have a main div with 100% width, containing two inner divs. One of the inner divs has a fixed width of 200px, and I want the other inner div to fill up the remaining space, like this:

  -----------------this is main div -------------
 |                                               |
 | ----subdiv1 (200px)---- --subdiv2-----------|
 ||                    |  |                   ||
 |------------------------ -------------------- |
 |----------------------------------------------|

I have been trying to figure out how to make subdiv2 take up the rest of the empty space while ensuring that subdiv1 stays at 200px. I searched on Google for a solution but couldn't find one.

Answer №1

Check out this solution I came up with by using a float: left rule for the div on the left, and a margin-left rule for the div on the right: http://jsfiddle.net/P4xMj/

Sample HTML:

<div id="container">
    <div id="left">
        Content here
    </div>
    <div id="right">
        More content goes here on the right side. Let's make this text really long to test wrapping more than one or two lines. Additional text to fill up space.
    </div>
</div>

Sample CSS (background colors used for clarity):

#container {
    background: #FF0;
    overflow: auto;
    padding: 10px;
}

#left {
    background: #0F0;
    float: left;
    width: 200px;
}

#right {
    background: #F00;
    margin-left: 210px;
}

Answer №2

To achieve the desired layout, make sure to include float:left; in the styling of your subdiv1. Below is the code snippet that demonstrates how this can be implemented:

<div>
  <div style="float:left;width:200px;background:#0F0">
  SUBDIV1
  </div>
  <div style="background:#F00;">
  SUBDIV2
  </div>
</div>

In essence, remember to apply float:left; to your subdiv1.

Answer №3

To align the left div to the left and create a 200px margin on the right div, you can use CSS properties like float: left and margin-left: 200px.

Check out this JSFiddle for a live example: http://jsfiddle.net/SpxH9/

Here's the HTML structure:

<div id="container">
    <div id="left">left</div>
    <div id="right">right</div>
</div>

And here's the CSS code:

#container {
    overflow: hidden;
}
#left {
    float: left;
    width: 200px;
}
#right {
    margin-left: 200px;
}

Another technique that can be used is replacing margin-left with overflow: hidden. This method eliminates the need to specify dimensions twice and makes adapting to changes easier.

For instance, with 10px borders applied: http://jsfiddle.net/SpxH9/1/

#container {
    overflow: hidden;
}
#left {
    float: left;
    width: 200px;
}
#right {
    overflow: hidden;
}

If you attempt the same approach with manual calculation of dimensions, it can be challenging as demonstrated here: http://jsfiddle.net/SpxH9/2/ (fixed version: http://jsfiddle.net/SpxH9/3/)

Lastly, using overflow: hidden on #container helps contain the floats, but an alternative method like clearfix could also be considered.

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

Having trouble with bootstrap carousel malfunctioning on Firefox browser?

I'm experiencing an issue with the carousel slider on my website. It seems to only be working in Chrome and not in Mozilla Firefox. You can view the live site at: Below is the code I have used for the carousel: <header id="myCarousel" class="car ...

Safari (on both mobile and Mac) has experienced a change in the hitbox for buttons

I'm experiencing an issue with a simple HTML button on my website. While it works perfectly on Google Chrome, users running Safari on macOS and iOS are facing a problem where the hitbox of the button is offset by about 50px above the actual button are ...

The information from the textarea and select option is not getting through to the email

Despite following suggestions, I am still unable to figure out why my form is not functioning properly. The form I have collects various information such as name, email, phone number, a select option, and a message in a textarea input. I dynamically change ...

Flip an image by analyzing the colors beneath it

Looking for a way to invert the color of specific areas under a mask (PNG) for a floating menu. Rather than inverting all at once, I want only certain parts to be inverted underneath the mask. Is this achievable, or is there another approach I should consi ...

Crafting a Javascript Email Event: A Step-by-Step Guide

Currently working on a website design project for a friend who requires a "Contact Us Page". I am utilizing Bootstrap Studio and have successfully created a page where users can enter their name and email. However, I am facing difficulties in configuring ...

Image floating next to a table

Trying to achieve something that I assumed would be straightforward, but CSS always has its surprises! The issue I'm facing is with an image floated to the left. Next to the image, I have a title, and below the title, but still alongside the image, I ...

How to use CSS animations to remove an element from the DOM

My website has a structured HTML layout with product containers and individual products. I also have a JavaScript function that can remove any product from the page. <div class="products-container"> {foreach from=$cart.products item=product} & ...

Angular application experiencing issues with fixed headers not scrolling correctly

I've been working on implementing a fixed header for one of my pages in an Angular application, but I'm having some trouble getting it to work as expected. Currently, when the user expands the accordions on the page and scrolls down, the headers ...

Adjust the color of the button upon hovering with CSS

I'm having trouble changing the text color from white to black when hovering over the button. The code seems fine, but the text color isn't changing. Can anyone help me figure out how to make it work? .main__btn { font-size: 1.2rem; back ...

What is causing my HTML file path to function on my server, but not when I access the HTML file directly?

My file structure is organized as follows: The main file is located in the folder "HTML-Project/index.html". Within this folder, I have my style.css file and two other folders: one for pictures (HTML-Project/pictures) and another for categories (HTML-Proje ...

Having trouble retrieving JSON data from backend server in Svelte framework

My goal is to retrieve JSON data from the Backend, specifically information about music videos. It should be a simple task, but I am encountering some difficulties. I attempted to follow a tutorial provided at: . The tutorial demonstrated fetching data wi ...

The content in an app using Ionic and Angular is being obstructed by the bottom tabs, preventing users

I am facing an issue with my Ionic and Angular app. When I place tabs at the bottom of my page, outside the ion-content, the scrolling stops working inside the ion-content and the fab button, which is located inside the ion-content, ends up covering the ta ...

Creating a versatile PHP script capable of managing multiple forms

Each time I develop a website for a client, I find myself in the same cycle of writing form HTML and then creating a PHP script to manage the data. Dealing with lengthy forms or multiple forms can make the process messy. Before diving into crafting a dyna ...

Looking for assistance with verifying the winning criteria for a Tic-Tac-Toe game coded in JavaScript

I need help understanding how to check for the winning conditions in my code. Can someone kindly explain it to me step by step? I'm quite new to coding. prntscr.com/m06ew1 <!DOCTYPE html> <html> <head> <title>Tic-Tac-Toe ...

Initiate the React application with the given external parameters

I have created a React app that is embedded within a webpage and needs to start with specific parameters obtained from the page. Currently, I am passing these parameters in the index.HTML file within a div element. The issue arises when these parameters ar ...

Developing an HTML table for room reservations using PHP - a comprehensive booking system

I'm currently working on creating an HTML table for a room booking system, but I've run into a problem trying to figure out how to insert data into cells. Right now, I'm retrieving data from a database (start time and end time for the bookin ...

Repositioning elements within the web browser

I am looking to create a web page that features: a variety of objects with distinct shapes (such as squares, rectangles, and more) the ability for users to connect these objects using arrows (see image link below) Furthermore, users should be able to mo ...

Utilizing Javascript to Extract Data from Twitter Json Files

Can someone provide assistance with parsing JSON feed text retrieved from Twitter? I am looking to access and apply style tags to elements like the link, created date, and other information. Any tips on how I can achieve this task successfully would be g ...

The Bootstrap 4 carousel link takes precedence over any other links on the page

Is anyone else experiencing this issue? I have three different link colors. .custom-links > a, a:link, a:hover, a:visited { color: black; } .carousel-caption > a, a:link, a:hover, a:visited, a:active { color: white; } .rss-links > a, a: ...

Adjust size of item within grid component in VueJS

I have a grid container with cells and a draggable item in a Vue project. I am trying to figure out how to resize the box inside the grid component (refer to images). https://i.stack.imgur.com/q4MKZ.png This is my current grid setup, and I would like the ...