Display content in two columns. If the width of the left column is too narrow, move the content in the right column

In my layout, there is a wrapper div with two child elements: left div and right div. The left div contains text which can be lengthy, while the right div holds an image loaded asynchronously without known width:

<div id="wrapper">
    <div id="left">TEXT TEXT TEXT TEXT</div>
    <div id="right">IMAGE</div>
</div>

The desired display for this structure is as follows:

#wrapper{
    width: 600px;
    display: inline-block;
}
#left{
    float: left;
    width: 400px;
}
#right{
    float: left;
    width: 200px;
}

Now, the goal is to make this layout responsive. If the image on the right surpasses 400px in width, it should move below the left div, which will then expand to fill the entire width of the wrapper like so:

#left.width = #wrapper.width - #right.width;
if (#left.width < 200px) {
  #left.width = 100%;
  #right move to new line;
} else {
  display #left div on left side and #right div next to #left div
}

Answer №1

I have made adjustments by applying float:left to both of your div elements. You can view the updated code at http://jsfiddle.net/fxWg7/1866/

#wrapper {
height: auto;
overflow: hidden;
}

#right {
width: auto;
float: right;
background: #aafed6;
}

#left {
float: left; 
background: #e8f6fe;
width: auto;
overflow: hidden;
min-width: 200px;
max-width:300px;
}

Answer №2

Here is the CSS code with some highlighting that addresses your question: http://jsfiddle.net/differentusername/abcd1234/. Feel free to test it out and let me know if it meets your requirements.

/* Responsive design approach:
1) No fixed widths are applied to #container, #left, and #right divs as they naturally span 100%.
2) The #left section, being higher in the DOM order, will naturally appear on top */
#container{
overflow: auto; /* ensures floating elements stay within bounds */
}
#left{}
#right{}


/*
Targeting small viewports:
We set a "max-width: 100%" for "#right img" so it maintains its natural width unless the layout becomes smaller */
@media (max-width: 599px) {  
#right img{
max-width: 100%; /* maintain image width or scale down based on viewport size */
}
}


/* Larger viewports:
Beyond this breakpoint, define the container width and ensure the right image adjusts responsively */
@media (min-width: 600px) {
#container{
margin: 0 auto; /* horizontally center all elements */
width: 600px;
}

/* Assign percentage widths to #left and #right */
#left{
float: left;
width: 60%; 
}
#right{
float: left;
width: 40%; 
}
#right img{ 
width: 100%; /* image occupies full width of parent element */
height: inherit; /* maintain aspect ratio */
}
}

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

Implementing script loading within the Angular scope

I'm attempting to load a custom script from the database based on client-side logic. I am having trouble figuring out how to make it function properly. Here is my controller code: 'use strict'; angular.module('youshareApp') . ...

Are there any improved methods for optimizing the CSS in my Next.js project?

Currently, I'm immersed in a project using Next.js and styling it with CSS in JS (ReactJSS). Upon inspecting the page source, I noticed that there are over 4000 lines of CSS for a single page, which is not ideal for performance. To provide context, l ...

Displaying checkbox values with JavaScript

How can I display multiple checkbox values when checked in HTML code? Whenever I check a checkbox, I want its value to be shown alongside the previously checked values. Right now, my code only displays one value at a time. Any suggestions on how to achie ...

Activate the Chrome Extension that allows you to open a link in a new tab with just a middle click or regular click, without closing the popup

When I try to click a link in my extension popup and open it in a new tab using "middle click -> open link in a new tab", the popup closes. Is there a way to keep the popup open so I can click on multiple links from my extension without interruption? Any ...

Tips for validating html:options collection in Struts 1.3?

A Treemap is populated with values from a database using the following Java code: Map<String, String> treeMap = new TreeMap<String, String>(map); Iterator mapIterator = mapSet.iterator(); while (mapIterator.hasNext()) { Map.Entry mapEntry = ...

The search functionality on my website, powered by webapp2 and HTML, is not functioning properly

Upon examination, I noticed that my search term is being transmitted to my handler as an empty string. Here are the key components: This is my HTML form: <form class="navbar-search" action="/search"> <input type="text" class="se ...

What are the steps to switch multiple CSS styles for creating a dark mode?

I'm currently using a combination of HTML, CSS, and JavaScript to construct my website. One feature I'd like to implement is a Darkmode switch button that toggles between Dark and Light mode when clicked. However, the issue I'm facing is tha ...

Comparing v-show(true/false) and replaceWith: Exploring the best practice between Vue and jQuery

Currently, I am in the process of learning vue.js and have a question regarding the best approach to implement the following scenario: The main query is whether it is acceptable in HTML to write <span></span> followed by a <textarea>< ...

Hide elements in hidden divs until the page is fully loaded to reduce initial page

www.prismasites.com I am creating a unique world map by using SVG DIVs shaped like Continents. Upon clicking on a continent on the world map, it conceals the world map DIV and displays the respective continent DIV with SVG. I have successfully achieved ...

Craft a CSS triangle shape from scratch

Looking at the image of a blue triangle, I am curious about how to recreate it using CSS. Would using a background image be the best approach, or is there a way to achieve this with CSS transforms? I noticed the triangle is on the left side but not on the ...

Looking to extract specific information from a webpage using VBA

I'm trying to extract the text 'Catalog Manager/ Sales' & 'EOL (product/component)' from the view source tab using VBA. Here is an excerpt of the view source code: <tr> <td nowrap="true" valign="top" width="165px" cl ...

Modify styling of complete list upon clicking in React

For my latest project, I developed a basic todo application using React. One of the key features is that when a user clicks on a checkbox, the corresponding todo item's CSS changes to show a strike-through effect. Additionally, a button appears on hov ...

Ways to collect email address or name from an email message

Suppose I send an email to someone with a link at the bottom. The text of the link might be something like click me. When the user clicks on this link, they will be directed to a webpage. On this webpage, a message saying "Thank you" will be displayed a ...

What is the best way to ensure one div expands to full width while simultaneously hiding another div on the page?

Hey there, I'm looking for a way to expand the width of a clicked div while making the other div disappear from the screen simultaneously. It should also be toggleable, so clicking the button again will shrink the div and bring back the other one. For ...

What is the reason for the disparity between CSS box-sizing content-box and border-box?

Consider the following example where I only changed the CSS box-sizing property from content-box to border-box. The resulting output difference is significant. Give this code a try: .box1 { width: 300px; height: 100px; border: 1px solid blue; pa ...

Aurelia-powered DataTable plugin for effortless data updating

I'm currently utilizing the DataTables and DatePicker plugins along with Aurelia in my project. The goal is for the user to select a date, which will then prompt the data table to display the corresponding data for that specific date. However, I' ...

center items alignment with hidden tags

I'm currently facing an issue with the CSS property display: flex, align-items: center Here is a snippet of my HTML and CSS files: * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Kumbh Sans', sans-serif; } .na ...

Error TS2307: Module './tables.module.css' or its type declarations could not be located

Currently utilizing CSS modules within a create-react-app project and encountering an error within Google Chrome browser: https://i.stack.imgur.com/0ItNM.png However, the error appears to be related to eslint because I am able to close the warning modal i ...

The size of the cursor varies depending on the line it's placed on

I have a content editable div where three lines are separated by BR tags. When I click on the second line, the cursor becomes bigger than that in the first line. .content { line-height: 35px; } <div class="content" contenteditable="true"> ...

Get rid of any fonts that are causing rendering delays on amp pages

Encountering issues with Google Lighthouse and AMP Pages I have attempted various solutions but none seem to be working <link rel="preload" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,600,700,900&display=swap ...