Dynamically Adjusting Size of a Text Block Featuring an Embedded Image

Struggling with a design issue on my desktop-first responsive webpage for the past few days. I'm trying to create a layout where a paragraph element and an image element are displayed side-by-side, emulating one cohesive item. The paragraph should take up 30% of the container and the image 70%, with border-radius applied to the paragraph's left top/left bottom corners and the image's right top/right bottom corners. However, due to varying screen sizes, achieving equal height for both elements is proving to be a challenge. When resizing beyond a certain point, the height of these elements fluctuates.

My initial plan was to maintain equal height until the 1024px breakpoint, where they would revert back to block elements. But given the dynamic nature of the paragraph's resizing, I'm unsure if this approach is feasible. Any suggestions on improving the presentation of these elements would be greatly appreciated. I might have approached this concept incorrectly, so any guidance or assistance in resolving this matter would be immensely helpful. I've exhausted all my efforts over the last couple of days without success!

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial scale=1">
  <link href="./style.css" type="text/css" rel="stylesheet">
</head>

<body>

   <div class="container">
   <section class="home-main">    
     <p class="main-para">aliquip sunt aut efflorescere fore praesentibus tamen quae eram officia 
       illustriora est tempor o id quis adipisicing appellat sed multos ea lorem 
       sempiternum qui doctrina ab dolor senserit tractavissent an probant 
       instituendarum elit culpa qui magna tempor aliquip quamquam minim voluptate 
       legam arbitrantur graviterque eu</p>  
      <img class="main-image" src="http://placehold.it/300x200">
    </section>        
  </div>

</body>

</html>


* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  font-family: Arial;
  text-align: center;
}

.container {
  width: 95%;
  max-width: 80%;
  margin: 0 auto;
}

img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0;
}

/*  HOME-MAIN
============================================= */

@media (min-width: 1024px) {
  .main-para {
    float: left;
    width: 40%;
    background-color: #000;
    color: #fff;
    line-height: 1.5em;
    font-size: 1.4em;
    padding: 2em 1em 2.15em;
    margin: 0 0 1em;
    border-top-left-radius: 1em;
    border-bottom-left-radius: 1em;
  }

  .main-image {
    border-top-right-radius: 1em;
    border-bottom-right-radius: 1em;
    float: right;
    width: 60%;
  }
}

Answer ā„–1

To maintain a 70/30 ratio between the placeholder image and text, you can make some adjustments to the CSS code. Start by removing the max-width:80% from the container class to prevent the content from appearing cramped on mobile devices when resized.

.container {
  width: 95%;
  margin: 20px auto;
  padding: 10px;
}

Next, utilize display: flex and other selectors as outlined below to ensure elements wrap nicely within the flex container:

.home-main {
  max-width: 100%;
  display: flex;
  flex-direction: row;
  flex-flow: nowrap; 
}

Lastly, add some margin and padding to the text box and adjust the image width to occupy 70% of the outer container:

.main-para {
  padding: 10px;
  text-align: left;
}

img {
  max-width: 70%;
  height: auto;
  display: block;
  margin: 0;
}

Check out the plunk here

Answer ā„–2

One effective way to achieve this is through the use of Flexbox. By instructing Flexbox to organize child elements within a parent element based on specific criteria, such as setting one child to a fixed size and another to occupy the remaining space while also allowing for stacking when needed.

Additionally, Flexbox eliminates the need for using cumbersome float properties.

You can view an example of this concept in action on this Pen.

The changes made involved adjusting the @media queries within your CSS from the original code snippet:

@media (min-width: 1024px) {
.main-para {
float: left;
width: 40%;
background-color: #000;
color: #fff;
line-height: 1.5em;
font-size: 1.4em;
padding: 2em 1em 2.15em;
margin: 0 0 1em;
border-top-left-radius: 1em;
border-bottom-left-radius: 1em;
}

.main-image {
border-top-right-radius: 1em;
border-bottom-right-radius: 1em;
float: right;
width: 60%;
}
}

To the updated version:

@media (min-width: 1024px) {
.home-main{
display: flex; /* NEW */
}
.main-para {
flex: 4; /* NEW */
background-color: #000;
color: #fff;
line-height: 1.5em;
font-size: 1.4em;
padding: 2em 1em 2.15em;
margin: 0 0; /* NEW */
border-top-left-radius: 1em;
border-bottom-left-radius: 1em;
}

.main-image {
border-top-right-radius: 1em;
border-bottom-right-radius: 1em;
flex: 6; /* NEW */
}
}

Answer ā„–3

Here is a little something I created for you, although I must admit that resizing an image may not yield the best results. Nevertheless, feel free to give it a try!

I have made some adjustments by removing media queries, adding height to both the paragraph and image classes, and setting the font size to 1em. You can view the changes in this JSFiddle link.

.main-para {
   float: left;
   width: 40%;
   background-color: #000;
   color: #fff;
   line-height: 1.5em;
   font-size: 1.4em;
   padding: 2em 1em 2.15em;
   margin: 0 0 1em;
   border-top-left-radius: 1em;
   border-bottom-left-radius: 1em;
   font-size: 1em;
   height: 325px;
}

.main-image {
   border-top-right-radius: 1em;
   border-bottom-right-radius: 1em;
   float: right;
   width: 60%;
   height: 325px;
}

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

Internet Explorer fails to load stylesheets for elements loaded with .load() and JavaScript functionality is also disabled

I'm using the .load() function to load my content, as shown in this code: $(document).ready(function(){ $("nav a").click(function () { $("#main-content").load($(this).attr("href") + " #content > *"); return false; }); }); The sty ...

Discover the secret to permanently adding a video icon to your images

I am struggling to permanently place a video icon on an image without having both elements appear separately. Essentially, I want to overlay a video icon onto an image seamlessly without the need for external tools like Photoshop. More Clearly: I have ...

Creating a fixed top bar button in Ionic and iOS along with a scrollable list

In my app using Ionic with the iOS platform, I am trying to achieve a specific layout: A header A fixed bar button A scrollable list Here is what I have attempted so far: <ion-view view-title="Account"> <div class="button-bar" style="margin ...

Bottom is not adhering properly to the page (blocking content)

Hey there! I'm using Weebly as a CMS for my website, and I've encountered some issues with a custom footer. Specifically, on this page: The content seems to disappear if you're not using a large monitor and the page height is short. Upon in ...

Creating a personalized gradient using Tailwind CSS and Daisy UI framework

Iā€™m attempting to incorporate a unique gradient into my next.js application with the help of Tailwind CSS and Daisy UI. Below is the specific code snippet I plan on using: background: linear-gradient(180deg, rgba(192, 192, 192, 0.04) 0%, rgba(201, 180, ...

Refining nested list with textbox input using JQuery

I am facing a challenge with filtering a nested list of checkboxes based on textbox input and the label's value. The list is quite extensive, but here is a snippet: https://i.sstatic.net/w4D7V.jpg For reference, here is the Fiddle: https://jsfiddle. ...

Button to close on the right side of the navigation bar in Bootstrap version 4

Attempting to customize a navbar with Bootstrap 4, I have implemented the following modifications: Positioned the Collapse button on the right-hand side Separated the BrandName from the collapse area to ensure it remains on the left side Below is the co ...

Is there a way to determine if a webpage is overflowing horizontally using Selenium Driver?

I am currently working with Selenium to open a webpage. I am facing an issue where I need to determine if the page is overflowing horizontally. However, I have noticed that driver.FindElement(By.TagName("html")).Size.Width consistently returns the browser ...

Showing AngularJS Data Value

I currently have the following code in my .html file: {{data}} When rendered, it displays the JSON data below: [ { "images" : [ { "__v" : 0, "_id" : "542e57a709d2d60000c93953", "name" : "image1", "url" : "http://www.syll.co ...

Tips for avoiding the page from reloading when executing a query. I attempted using preventDefault(); which successfully prevented the reload, but it also stopped PHP

function createCounter() { var count = 0; return function () {return count += 1;} } var add = createCounter(); function updateDatabaseOnClick(){ document.getElementById("result").innerHTML = add(); } I'm currently working on implementing a co ...

Issue with checkboxes preventing submission of form

Currently working on a website project for a company that requires certain steps to be completed for deliveries. Unfortunately, I am facing issues with the submit button, and I apologize as I am still new to this. The code I have pasted is divided into tw ...

Comparing the impact of class and element selectors on CSS performance

Considering that CSS is read from right to left, I am pondering the balance between performance and readability when it comes to adding classes to HTML in order to more efficiently target elements in CSS. Additionally, I am curious about how using a gener ...

The Bootstrap modal demonstration fails to function on a local server but operates successfully on a live website environment

I am currently experimenting with the bootstrap modal feature using the official Bootstrap documentation. I have copied the code exactly from their website and conducted the following tests: Verified in the browser that the sources for js, jquery, and cs ...

Is it achievable to apply a shadow to a div using only CSS, or would images be required?

I am looking for a simple solution where the user can press something and see a shadow effect on a new div (div centered window) that appears on top of the entire page, maybe 1/4th in size. Can this be achieved using pure web-kit css? Or would a combinat ...

The DIV element with a line break tag displaying text on a new line

In my project, I am working with an XMLHTTPResponse object that contains nodes. My goal is to print the elements of one specific node (serps) in a div element, but in a formatted manner. The structure of the node is as follows: Currently, I need to create ...

Tips and tricks for ensuring images maintain their aspect ratio and height when resizing for responsiveness

I am facing an issue with my two-column layout. One column contains an image and the other column has text and buttons with a fixed height. To ensure the image is responsive, I have set the width to 100% and height to auto. However, upon resizing, the aspe ...

Issue with form not being validated

I'm attempting to disable the submit button when a user enters something in the email field, but despite validation, it is not working as expected. What could be the issue with my validation code? function validateEmail(email) { var btnSubmit = ...

Warning in Next.js: When utilizing conditional rendering, the server HTML is expected to have a corresponding <div> inside another <div>

Although similar questions have been asked on various platforms like Google, none seem to provide answers that align with my specific situation. Essentially, my goal is to have a different search bar displayed in the header based on the page I am currentl ...

Unseen components and columns with Bootstrap 4

I am attempting to hide a checkbox input in the middle of some column elements using Bootstrap 4. <div class="container"> <div class="row"> <a class="col-2"> 1 of 2 </a> <input type="checkbox" class="invisibl ...

Adjusting Grid Posts to a Consistent Size (Featuring Photo Posts with Captions Below)

I'm attempting to adjust the size of posts in grid mode on Tumblr. I've discovered that I can modify the width of each post using CSS. However, I'm struggling to locate where I can set the height of a grid post to a specific size. For inst ...