Create a stylish layout with two div elements positioned side by side using flexbox for a responsive design

Struggling with my portfolio website, I encountered a challenge. I attempted to place two divs side by side, each containing text and an image. While the initial setup was simple, trying to make it responsive using flexbox has proven to be quite frustrating.

Here is the desired layout achieved using flexbox in plain HTML and CSS.

The current code being used (without flexbox implementation due to difficulty grasping it) is as follows:

#best_cases {
  height : 800px;
}

#first_case {
  width         : 650px;
  height        : 577px;
  float         : left;
  margin        : 95px 0 0 211px;
  border-radius : 5px 5px 0px 5px;
  background    : #1f2930;
}
/* Additional CSS properties omitted for brevity */     
<div id="best_cases">
<div id="first_case">
<h2>T3Qvi3w</h2>
<p>Shop voor het kopen van <br />
smartphone accessoires.</p>
<img src="img/TeQview_small.png" alt="" width="580" height="auto" />
</div>

<div id="second_case">
<h2>Studieplaen</h2>
<p>De nieuwste manier om <br />
te plannen.</p>
<img src="img/Studieplanner_small.png" alt="" width="580" height="380px" />
</div>
</a>
</div>

Your assistance in resolving this issue would be greatly appreciated. Thank you!

Answer №1

Using flexbox for layout can make your design fluid, but be cautious when applying fixed pixel widths to elements as it may cause the layout to break. It is recommended to use percentages for padding and margin, and utilize flex properties for controlling widths to maintain a fluid layout.

For example, in the code snippet below, we have set up a basic layout using flexbox with percentages for padding and margin, and flex properties for alignment, justification, and sizing.

When implementing this layout in production, consider incorporating media queries to handle font sizing adjustments. This is especially important for side-by-side layouts, as content may overflow the containers at smaller screen sizes causing display issues.

#best_cases {
  display: flex;
  justify-content: center;
  flex-wrap: no-wrap;
  width: 100%;
}

#first_case,
#second_case {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  margin: 0;
  padding: 0;
  flex-grow: 0;
  background: #1f2930;
  border-radius: 5px;
}

#first_case {
  text-align: left;
  margin-right: 1%;
}

#second_case {
  text-align: right;
  margin-left: 1%;

}

#first_case h2,
#second_case h2,
#first_case p,
#second_case p {
  color       : #ffffff;
  font-family : montserrat bold,
                arial,
                verdana;
  font-size   : 2.5em;
  padding: 2% 10% 5% 10%;
  /* 40/16 */
}

#first_case h2,
#second_case h2 {
  font-size: 2.5em;
  /* 40/16 */
}

#first_case p,
#second_case p {
  font-size: 1.125em;
  line-height: 26px;
  /* 18/16 */
}

#first_case img,
#second_case img {
  max-width: 585px;
  height: auto;
}

#first_case img {
  margin-left: 10%;
  width: 90%;
  border-bottom-right-radius: 5px;
}


#second_case p {
  text-align: right;
}

#second_case img {
  margin-right: 10%;
  width: 90%;
  border-bottom-left-radius: 5px;
}
<div id="best_cases">
  <div id= "first_case">
    <h2>Abcdefg</h2>
    <p>Shop for buying smartphone accessories.</p>
    <img src="http://placehold.it/580x380" alt="" />
  </div>
  <div id="second_case">
    <h2>Hijklmn</h2>
    <p>Shop for buying smartphone accessories.</p>
    <img src="http://placehold.it/580x380" alt="" />
  </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

Comparing iPad and Desktop Devices

Working on the responsive site sandbox.mercomcorp.com, I encountered an issue where the CSS meant for an iPad device is affecting the desktop CSS. Shouldn't the iPad have its own separate media query from the desktop? Here's a snippet of the CSS: ...

I'm experiencing difficulties with JS on my website. Details are provided below – any suggestions on how to resolve this issue

Can someone help me with a web project issue I'm facing? Everything was going smoothly until I tried to add some JS for dynamic functionality. However, when I attempt to access my elements by tag name, ID, or class, they always return null or undefine ...

Is it possible for CSS to automatically style paragraphs based on the preceding heading class?

Looking to create some unique CSS rules for formatting interview transcripts. The format I have in mind is as follows: <h2 class="interviewer">Alice: [00:00:00]</h2> <p>Is it ok if I ask you a question now?</p> <h2 class="inter ...

The problem of removing issue divs persists despite Jquery's efforts

Just yesterday, I successfully created a Commentbox using PHP, HTML, and ajax. The beauty of the ajax part is that it allows me to delete a comment without having to refresh the page. To achieve this, I assign a unique id to each comment (div) through the ...

What is the best way to loop through <div> elements that have various class names using Python 3.7 and the Selenium webdriver?

I am currently in the process of creating a Reddit bot that provides me with a detailed report about my profile. One task I need to accomplish is identifying which of my comments has received the most likes. Each comment on Reddit is wrapped in elements w ...

Does the downloading of images get affected when the CSS file has the disabled attribute?

Is it possible to delay the download of images on a website by setting the stylesheet to 'disabled'? For example: <link id="imagesCSS" rel="stylesheet" type="text/css" href="images.css" disabled> My idea is to enable the link later to tri ...

Toggling with Jquery when an image is clicked

I'm trying to wrap my head around the functionality of jquery toggle. My goal is to toggle to the next anchor element with the class plr-anchor when an image with the class go_down is clicked. The information is being populated using maps. Javascript ...

Why is it that when the form is submitted, the value becomes unclear?

This is a form with HTML and TypeScript code that I am working on. <form> <div class="form-group"> <input class="form-control" ([ngModel])="login" placeholder="Login" required> </div> <div class="form-group"> &l ...

Converted my Flask code into a specialized software package, encountering a perplexing TemplotNotFound error

After reorganizing my Flask code into a Python package structure, I am facing an issue where none of the HTML templates can be found when I run the code. Let me outline the simplified structure of my current Flask package. The run.py file is responsible f ...

Can you reveal a table with the click of a button?

Hi there! I'm a newcomer to the world of Ruby on Rails and I’m currently working on creating a page that includes a date field. Once a specific date is selected, I’d like to generate a table displaying reports from that chosen date. controllers/r ...

How can you add padding to an HTML page using CSS?

I've been attempting to use padding-bottom:90px; in order to shift Tweets by ‎@StackOverflow (the entire tweets section) downwards on the page. I want to move it further down using the above padding, but it doesn't seem to be working for me. H ...

Is there a way to determine if a browser is operating in compatibility mode?

Is there a way to determine if the browser is in compatibility mode or not? I need to apply different CSS depending on the mode. ...

Unable to transmit HTML emails

I've been encountering an issue while trying to send HTML emails using Gmail SMTP from CI. It seems that my emails are being rejected if they contain any tables. Interestingly, there is no error message provided; the emails simply do not show up in my ...

Tips on creating reusable scoped styles for 3 specific pages in Vue.js without impacting other pages

Our project includes global classes for components that are utilized throughout the entire system. <style lang="scss" scoped> .specialPages { padding: 0 10px 30px 10px; } /deep/ .SelectorLabel { white-space: nowrap; al ...

Unable to access or click on Wordpress links or site titles

On my website, www.website.com, I have a main Wordpress installation and a test site at www.website.com/test. The main site functions correctly, but the links on the test site are behaving strangely. I have set the post title as the permalink structure (w ...

PHP - Unable to store the input content

I've encountered an issue with my website where I need to create a form for users to submit new information or news. Below is the code snippet that I have: <?php include "0begin.php"; $title=$_POST["title"]; isset($title) or $title=$_GET["tit ...

Position two div elements and an hr tag to create a step progress bar

Check out my progress bar design How can I center the 'created' and 'assigned' divs below the circle while ensuring the 'hr' line touches the circle, and it is responsive across different screen sizes? Any tips on how to cr ...

Encountering an issue trying to insert multiple objects into a Laravel session

Whenever I attempt to add an item to the cart, it only adds it once. If I try to do it again, it ends up overwriting the existing item and the counter for the items in the cart remains at 1. I've experimented with switching between a button and a lin ...

How can I assign a specific class to certain elements within an *ngFor loop in Angular?

I have a situation where I am utilizing the *ngFor directive to display table data with the help of *ngFor="let record of records". In this scenario, I am looking to assign a custom CSS class to the 'record' based on specific conditions; for exam ...

What could be the reason for Chrome breaking up this straightforward bootstrap header into two lines?

Why is it that the code below displays correctly in one line in both FF and IE, but Chrome for some reason is showing it on two lines as if both span and button elements had "display:block;"? Even though the button has a computed display of "inline-block," ...