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

Can you explain the significance of the <%= %> HTML tag?

I've recently been tackling a project with Webpack. For those not familiar, Webpack is a bundler that combines all your files into one final product. One task I encountered was trying to insert one HTML file into another, similar to an import or requ ...

Display a section of the picture at maximum width

Can anyone help with displaying only a portion of an image at full width (100%) without any overflow issues? The code I've tried is not working: .intro_sea { position: absolute; width: 101%; clip-path: inset(30% 50% 0 0); margin: 0; paddi ...

Adjusting the size of HTML checkboxes for creating PDF documents

Currently facing a dilemma. When it comes to resizing checkboxes in HTML, the common recommendation is to use JavaScript to prompt the browser to adjust the size of the elements. However, in our scenario, we rely on wkhtmltopdf, a command line tool that u ...

Vertical centering menu adjustment

Can anyone help me with centering an image on my website? I've tried the following code: top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); You can see what's happening on my site here: ...

Font and div placement varies on a mac within a WordPress environment

www.oihanevalbuenaredondo.be After styling my post titles in CSS using font-family: adobe arabic, I noticed that while they display correctly on my Windows machine, they appear in a basic font on Mac OS, Safari specifically. Why is it that Safari cannot r ...

The functionality of Jquery datatables seems to be faulty when navigating to the second page

While utilizing the jQuery datatables plugin, I encountered an issue where the event click function only worked on the first page and not on subsequent pages. To address this problem, I discovered a helpful resource at https://datatables.net/faqs/ Q. My ...

Tips for discovering the exact positions of child divs that are Nth siblings within a parent div

I have designed a new calendar interface with dynamic functionality. When the user clicks on any time slot displayed in IMAGE1, a span element is created dynamically to represent 8 hours starting from that clicked time. My question is, how can I access th ...

Using `overflow: hidden` on a table will result in the bottom and right borders disappearing

I am facing an issue where the border of my table is being cut off when using overflow: hidden. Below is an example code snippet illustrating this problem. <style> table { border-collapse: collapse; } table { ...

The mysterious behavior of CSS3 transforms on intricate rotations

When a div is viewed in perspective and has the transformations rotateY(-90deg) rotateX(-180deg), adding rotateZ(-90deg) to it results in a rotation of +270 degrees instead of -90 degrees. The new style of the div changes from transform: rotateY(-90deg) ...

The unequal division of space between two flexed child divs is caused by varying image sizes

In my parent container, I have set the display property to flex. Inside, there are two divs with both having flex:1 set. However, the first child div contains an image and takes up twice as much space as the second div. If the screen is 1000px in height, ...

Creating a Bootstrap 4 DIV element with horizontal scrolling functionality

I've been struggling to implement a horizontal scroll for a DIV on my site, but none of the common solutions like overflow-x or white-space:nowrap are working for me. This is the section of my website where I need a horizontal scroll bar If you want ...

When the button is clicked, I would like to use JavaScript to toggle the visibility of a div, allowing it to open and

I am attempting to toggle a div using JavaScript when the open and close button is clicked. However, I have encountered an issue where the div disappears when the button is clicked, possibly due to post-back. var toggle = function () { var mydiv = d ...

Aligning an image in a way that adjusts to different screen sizes

Struggling with centering an image horizontally in a responsive manner while using Bootstrap v3.3.5? Here's my code: <div class="container"> <div style="margin:0 auto;"> <img src="images/logo.png" alt="logo" /> ...

Guide on incorporating the WHERE clause into an insert statement in PHP version 5.1.6

What is the correct way to incorporate a where clause into an insert statement in PHP 5.1.6? In my code, I have declared a global variable $module and I am trying to use a where clause to refine my search results. Specifically, I want to include where mod ...

Adding elements to a roster

Can anyone assist me with appending data from a csv file to an unordered HTML list? The csv file I have contains both single and grouped data for the list, as seen below: Test Group Name A,Test Link Name 1,Test URL 1 Test Group Name A,Test Link Name 2,Tes ...

Ways to display or conceal information depending on the dropdown choice

In my Angular project, I am dealing with a dropdown menu that is followed by some data displayed in a div element. component.html <select class="form-control" id="power" required> <option value="" disabled selected ...

Mobile view causes malfunction in Bootstrap Toggle Burger Menu functionality

<nav class="navbar navbar-expand-lg navbar-dark px-2 " style="background-color:#E53935;"> <a class="navbar-brand" href="#">FoodFusion</a> <button class=&quo ...

The Iframe contains its own scroll feature within the body

I am facing an issue with displaying an Iframe inside a modal. The challenge is that the content within the Iframe varies in height, making it difficult to set a fixed height for the Iframe itself. When I try setting the height to 100%, the result is a 150 ...

JS Issue with Generating Content

Introduction( kind of :) ) Starting with the process of generating 5 coffee beans for each cup of coffee. The coffee class includes a strength attribute, and my goal is to visually distinguish the coffee beans which have a strength greater than the select ...

Adjust the placement of a div within another div based on various screen sizes dynamically

Currently, I am working on an Ionic 2 app where the user is required to select specific points on the screen. These coordinates will then be utilized on another screen with a different size. My initial attempt at using rule of three/cross multiplication pr ...