Using CSS to ensure that a div element gets the remaining width within its parent container

Looking to create a responsive design similar to the one in this https://i.sstatic.net/8NvkQ.png Where the image maintains its width on smaller screens, while allowing the text to fill up the remaining space and reach 100% of the parent container.

I've attempted the following code but it's not producing the desired outcome.

<div class="comment">
   <div class="image"><img>Image here</img></div>
   <div class="text">Contents</div>
</div>

.comment {
   width: 100%;
}
.image {
   width: 50px;
   float: left;
}
.text {
   float: left;
}

While I'm open to using jQuery for a solution, my preference is for a CSS-only approach. Any assistance would be greatly appreciated! :)

Answer №1

Replace float with overflow: hidden like so:

.text {
  overflow: hidden;
}

.comment {
  width: 100%;
}
.image {
  margin-right: 15px;
  width: 50px;
  float: left;
}
.text {
  overflow: hidden;
}
<div class="comment">
   <div class="image"><img src="" alt="Image here"></div>
   <div class="text">Contents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes hereContents goes here</div>
</div>

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

The Bootstrap datepicker functions properly when used in plain HTML, but encounters issues when implemented within the .html()

Using HTML: <input class="m-ctrl-medium date-picker" size="16" type="text" id='lateETD1' name="dateRecv" value=""/> Not working with script: var ETD = $("#ETD"); ETD.html("<input class='m-ctrl-medium date-picker' id='la ...

Does 1 CSS point amount to the same as 75% of a CSS Pixel?

Can anyone help clarify the relationship between CSS pixels and CSS points in modern web design? I've come across information stating that one CSS point is equivalent to 3/4 of a pixel. Is this accurate? Also, is it true that we can disregard the old ...

Tips for implementing the .nex() property from jQuery in CSS styling

When I add the class "playing", then I will apply custom CSS to the next li tag. Please review my code and see if you understand. Please take a look at my code. <ul> <li class="playing">li (sibling)</li> <li id="head">li ...

What is the best way to align an HTML block (<ul>) immediately below an <input> using jQuery?

Here is the HTML code snippet: <html> <body> <div id="pgContent"> <div id="studyOverlay"> <div id="studyContainer"> <div id="studyTest"> ...

Create two grid components for Material-UI and use Flexbox to ensure that both items have equal height

I'm currently working on ensuring that two grid items have the same height using flex in Material UI, but I've hit a roadblock. It seems like my approach with the Grid element might be incorrect. I've experimented with adjusting the height a ...

krajee implement image insertion with screen capture

I am currently utilizing the Krajee file input plugin to upload images to a server. I am also looking to incorporate the ability to add files through screen capture. The concept is that when an image is captured, it will be displayed in the file upload pre ...

The removal of a JQuery element can result in causing the webpage to become unresponsive and lead to

When attempting to create a loop in JQuery to remove elements from my HTML, I encountered an issue where the function caused my browser to hang and become unresponsive. Here is the JQuery code I used: function removeElement(){ var i =0; ...

Hovering over text can change the width of the background color

I'm currently facing a roadblock while working on my website. I have successfully adjusted the height of the list items on my navigation bar, but now I am struggling to expand the width so that when hovering over it, the background color covers a slig ...

Is there a way to prevent the Title and Button from shifting upward when hovering over the button?

This video showcases the issue I am looking to implement a hover animation where an element slides to the right, but without moving the button and title upwards upon hover. Button Code: button{ width: 200px; padding: 15px 0; text-align: center; ...

Is it possible to utilize a tracking pixel (invisible GIF) as a background image within a stylesheet?

I'm curious to know if adding a tracking pixel (a transparent GIF with server logging) as a background-image in a linked stylesheet will have the same impact as if it were a regular <img> on a webpage. After conducting a quick test using Chrome ...

Showing content in a single line causes it to drop down to the next

Check out my code snippet here: http://jsfiddle.net/spadez/aeR7y/23/ I'm curious about how I can make my header list align on the same line as the header, without dropping down. Here's the CSS code I have for the list: #header li { display: ...

Is the column-fill property malfunctioning in Chrome?

column-fill: balance; is said to "equally divide content between columns". It appears to be functioning correctly in Firefox and Edge. However, Chrome (and Opera) seem to have a strong aversion to the second column, leaving it mostly empty. It seems to be ...

The back button on the page does not reset the CSS styling

I have a button that activates navigation to a different page. When this button is clicked, it changes the current section/page's display settings to none and sets the new section/page's display to block. <div class="listLeft">&l ...

Is there a way to ensure that my if statement functions properly within this script?

Yesterday, I sought advice on my question, but everyone recommended using the BeautifulSoup library. Unfortunately, I am restricted from utilizing any external libraries for this class, although I have managed to make some progress. The goal of my code is ...

Having odd PHP problems again, where my code seems to be completely ineffective. It's really bizarre

My code is not working as expected. The data.txt file is not being updated and I'm unsure of how to proceed. Any assistance would be greatly appreciated. <?php $txt = "data.txt"; if (isset($_POST['field1'])){ $fh = fopen( ...

Numerous asynchronous AJAX requests accompanied by various loading indicators

My ASP.net MVC page is utilizing ajax calls to load data for each of the six divs. To handle the loading indicator, I have created two methods as shown below. $("#divId").loading(); $("#divId").stopLoading(); Here is an example of one of my ajax calls ( ...

Show the elements of an array within a div when a button is clicked, utilizing pure vanilla JavaScript

I'm looking for help with printing the elements of an array onto a div element. Can anyone provide some guidance? var myArray=["stone","paper","scissors"] <button onclick="printnumbers()"></button> <div id="result"> </div> ...

ways to run php script based on screen resolution or size

I have 2 php files that I need to execute based on screen size. include_once('large.php'); include_once('small.php'); The condition is to run large.php when the screen size is greater than 992px, and small.php when the screen size is ...

After the JavaScript calculation is completed, the following display may show a value of NaN

Check out my JavaScript calculation in action: Live Preview I've encountered an issue with the calculation script. After the initial calculation, it shows a NAN value on subsequent calculations without reloading the page. Is there a way to enable rep ...

Achieving the perfect button using a combination of material-ui and styled-components

Utilizing the ToggleButton and ToggleButtonGroup components from material-ui, starting with material-ui's gatsby template. To prevent common material-ui errors with production builds, I am attempting to incorporate styled-components as well. The foll ...