On the same line, there are two divs with different characteristics - one dynamically changing width and the

I am facing an issue with arranging two divs under one parent div. The parent div is set to have a width of 100%:

<div id="parent">
  <div class="left"></div>
  <div class="right"></div>
</div>

The specific conditions are as follows:

  • I need both divs to be on the same line.
  • The right div may or may not be present. When it is present, I want it to always be fixed on the right side. However, the left div should be flexible and adjust its width based on its content.

I have experimented with both float:left and display:inline-block but have not found a satisfactory solution.

If you have any suggestions or ideas, they would be greatly appreciated.

Answer №1

If you're not concerned about IE7 compatibility, I recommend @sandeep's solution using display: table-cell.

Alternatively, there is another approach, but it requires placing the "right" div first in the HTML structure.

Check out: http://jsfiddle.net/thirtydot/qLTMf/
and the same example without the "right div" included: http://jsfiddle.net/thirtydot/qLTMf/1/

#parent {
    overflow: hidden;
    border: 1px solid red
}
.right {
    float: right;
    width: 100px;
    height: 100px;
    background: #888;
}
.left {
    overflow: hidden;
    height: 100px;
    background: #ccc
}
<div id="parent">
    <div class="right">right</div>
    <div class="left">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam semper porta sem, at ultrices ante interdum at. Donec condimentum euismod consequat. Ut viverra lorem pretium nisi malesuada a vehicula urna aliquet. Proin at ante nec neque commodo bibendum. Cras bibendum egestas lacus, nec ullamcorper augue varius eget.</div>
</div>

Answer №2

@Yijie; Perhaps this link could be what you're looking for: http://jsfiddle.net/sandeep/NCkL4/7/

UPDATE:

http://jsfiddle.net/sandeep/NCkL4/8/

ALTERNATIVELY, YOU CAN VIEW THE SNIPPET BELOW

#parent{
    overflow:hidden;
    background:yellow;
    position:relative;
    display:table;
}
.left{
    display:table-cell;
}
.right{
    background:red;
    width:50px;
    height:100%;
    display:table-cell;
}
body{
    margin:0;
    padding:0;
}
<div id="parent">
  <div class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
  <div class="right">fixed</div>
</div>

Answer №3

Here is an example of HTML:

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

(The div with the class "right" must come before the div with the class "left" in the HTML markup)

And here is some corresponding CSS:

.right {
float:right;
width:200px;
}

Answer №4

When it comes to adjusting the left div style based on the presence of the right div, a CSS selector doesn't readily come to mind.

One possible solution would be to dynamically add a class either server-side or through JavaScript to the parent div or left div element.

<div id="parent twocols">
  <div class="left"></div>
  <div class="right"></div>
</div>

Alternatively,

<div id="parent">
  <div class="left"></div>
</div>

The style for the right div remains constant:

.right {
    float: right;
    width: 200px; /* adjust as needed */
    /* margin and padding can be customized */
}

For the left div, the following rules apply:

.parent.twocols .left {
    margin-right: 200px; /* accounting for right div width, margin, and padding */
}

Answer №5

My experience has shown that utilizing white-space: nowrap; for the outer container, display: inline-block; for the inner containers, and then applying white-space: normal; to the inner ones (especially if you want the second one to word-wrap) can lead to successful outcomes.

Answer №6

It seems like this might be what you're looking for:

<html>
<head>
<style type="text/css">
#parent 
{width:100%;
height:100%;
border:1px solid red;
}
.left
{
float:left;
width:40%;
height:auto;
border:1px solid black;
}
.right
{
    float:left;

width:59%;
height:auto;
border:1px solid black;
}
</style>
</head>
<body>
<div id="parent">
  <div class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
  <div class="right">This is the right side content</div>
</div>
</body>
</html>

Check out the live demo here:http://jsfiddle.net/anish/aFBmN/

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

How to use CSS to make a child element's width automatically fill the width of its

Is there a way to make the child div info fill the parent div video-featured width while remaining absolute, allowing it to overlap the parent div without using a fixed width? <div class="video-featured col-md-4"> <div class="video"> < ...

Utilizing DIV elements within table cells

Within this particular table, it is not only cell B that contains both a heading and content, but cells A and C as well. My effort to establish the heading and content using DIV elements has been somewhat successful. While I have achieved the desired font ...

"Utilize HTML to make inline-block elements appear small in size while giving the illusion of being

I have a group of inline-block elements (4 total), all arranged neatly in a single row. As the window size changes, the elements wrap to the next line accordingly. The page does not display scrollbars at the bottom until the browser width reaches the minim ...

Concealing the resize handle icon using CSS

Consider a scenario where there is a table structure: <table> <tr> <th><div>Title1</div></th> <th><div>Title2</div></th> </tr> <tr> <td>< ...

Is there a way to retrieve the modal's viewport height in Angular?

Is it possible to determine the viewport height of my ng bootstrap modal within my Angular application? Here is what I currently have: I have a modal with CSS styling as shown below: .modal-xxl { width: 95% !important; max-height: 90% !important; ...

What steps do I need to take to retrieve data in a JSON array based on its

Hello, I could really use your assistance with a problem I'm having. Here is the scenario: I have a JSON array that looks like this: "category" : [ { id: 1, product: [{id : product_1, type : ball}] }, { id : 2, product :[{id : prod ...

The fixed left div and scrollable right div are experiencing issues with their alignment

I am having an issue with a section where the left div is fixed and the right div is scrollable. However, the content of the right div scrolls even before the scroll top reaches the section. My goal is for the right div to scroll only when it reaches the t ...

Navigational elements, drawers, and flexible designs in Material-UI

I'm working on implementing a rechart in a component, but I've encountered an issue related to a flex tag. This is causing some problems as I don't have enough knowledge about CSS to find a workaround. In my nav style, I have display: flex, ...

When the mouse hovers over DIV2, the background image of DIV1 will be replaced

I have a full-screen background div with the ID #background-change. Within that full-screen background, I have 3 Divs named .fullscreen-column-1, .fullscreen-column-2, etc. My goal is to change the background image of #background-change when the mouseove ...

modern marquee

I have implemented a marquee tag that displays news from my database. In order to keep the marquee up-to-date without refreshing the entire page, I utilized ajax (update_panel + timer). However, I am encountering an issue where the marquee does not continu ...

What could be causing the div to be wider than the content inside of it?

I am having an issue creating a webpage with a 20-60-20 flex fill layout. The div in the center, which should occupy 60% of the page, is wider than its content, causing it to appear off-center. https://i.stack.imgur.com/WwCJy.png Here is my home.componen ...

Different methods of transferring PHP post information to JavaScript

Is there a cleaner and more elegant way to pass posted PHP variables to JavaScript for manipulation, rather than using inline JavaScript? I currently have a simple PHP form that posts data to specific variables and then uses inline JavaScript to handle the ...

The evolving impact of the HTML5 <video> element

I've been attempting to find information on this topic, but unfortunately my search has not been very successful. I vaguely recall coming across a tutorial that covered something like this. Does anyone happen to know how to alter the perspective of a ...

simulate the act of clicking on a download link by utilizing a secondary click

adown is a link that allows users to download a file from my webpage. It functions properly on the page. However, btndown is a button designed to simulate clicking on the adown link, but unfortunately, it does not work as expected. When the btndown button ...

Tips for obtaining the entire date and time on one continuous line without any breaks or separation

Is there a way to retrieve the current date and time in the format of years, months, days, hours, minutes, seconds, and milliseconds like this? 201802281007475001 Currently, I am getting something like: 2018418112252159 This is my code so far: var dat ...

Inject data from ajax into the body of the html document

Is it possible to retrieve values from an ajax call and insert them into the body of an HTML document? Here is an example: function check() { $.ajax({ url : '/check', datatype : 'json', async ...

Incorporate a vertical scrollbar in the tbody while keeping the thead fixed for smooth vertical scrolling

I'm seeking a solution to implement horizontal and vertical scroll for my table. Currently, the horizontal scroll is working fine, but when trying to add vertical scroll, the table header also moves along with it. Is there a way to keep the table hea ...

Stop tooltips from appearing on hyperlinks in Internet Explorer

Is there a solution to disabling the pesky tooltips that appear in the bottom left corner of IE when hovering over links? Alternatively, is there a method to customize the text that appears instead of the default tooltip when hovering over a link (I attem ...

Attempting to automate the process of clicking a button on a webpage using cefsharp

Hello, I am currently working with cefsharp and vb.net to develop a code that will help me navigate to the next page of a specific website. The website link is: https://www.recommendedagencies.com/search#{} My goal is to extract the list of company names ...

Guidelines for retrieving a class name using jQuery when hovering over a div

I need assistance in fetching the class name by hovering over a div. Both divs have the same id but slightly different class names. Here is an example: <div id="1-someid" class="1-example-class border cz"> ...more elements go here.... </div> ...