What's the best way to maintain a 50/50 split in flexbox containers when incorporating a large image?

Currently, I am exploring ways to set up a Flexbox configuration with two separate div elements that each occupy half of the screen width equally.

On the left side, there will be text displayed while the right side will showcase an image that will exclusively fill its designated 50% width, adjusting larger images as needed.

I'm in need of guidance on the most effective method to accomplish this task as I am still in the early stages of learning about Flexbox.

.content{
    position: relative;
    display: flex;
    width: 100%;
    height: 600px;
}
.content div{
    flex-direction: column;
    justify-content: center;
    flex: 1;
}
.text{
    background-color: pink;
}
.image{
    background-color: paleturquoise;
}
<section class="content">
    <div class="text">
        <p>text goes here</p>
    </div>

    <div class="image">
        <img src="https://i.imgur.com/rcZLIwH.jpg" alt="image">
    </div>
</section>

Answer №1

One way to achieve this is by setting the .image element with a position: relative and ensuring the image width is set to 100%:

.content{
    position: relative;
    display: flex;
    width: 100%;
    height: 600px;
}
.content div{
    flex-direction: column;
    justify-content: center;
    flex: 1;
}
.text{
    background-color: pink;
}

.image{
    background-color: paleturquoise;
    /* make the parent position relative */
    position: relative;
}

.image img {
  /* ensure image width matches parent width */
  width: 100%;
}
<section class="content">
    <div class="text">
        <p>text goes here</p>
    </div>

    <div class="image">
        <img src="https://i.imgur.com/rcZLIwH.jpg" alt="image">
    </div>
</section>

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

What is the best way to use @foreach to make the text in the final row of a table bold?

Is there a way to apply bold styling to the text in the final row using @foreach in HTML? I am looking to emphasize the total sum of each column by displaying it in bold text. While I initially considered creating a new table below the current one solely ...

Using the last child as a parent element in Selenium with JavaScript

I am trying to access the input element of the last child in this code snippet. Specifically, I want to retrieve the text Text Reply - Delete. <div class="priority-intent-div"> <div class="row add-priority-intent-div"> ...

The PHP script is failing to load the Bootstrap and CSS files in the HTML document

I developed a PHP application that functions as a plugin within a WordPress site. The application is located in the wp-content/plugins folder and is named SW. Within this folder, there are files like index.php and related folders containing bootstrap and C ...

Solving Problems with D3.js Mouseover and Focus + Context

I am just starting to explore the world of D3.js, I've completed a few tutorials and dived straight into my initial project. My intention was to combine the concepts from the following examples with some modifications to suit my specific requirements. ...

Ways to incorporate a border line between a pair of icons within a bootstrap card

Incorporating a border line within the card-header is my current goal. My approach involves positioning two icons - one on the left side and the other on the right. Illustrative example below: https://i.sstatic.net/BgxVu.png Utilizing HMTL and CSS to ac ...

Activate on click using JavaScript

When a link with the class .active is clicked, I want to use a JavaScript function to deactivate any other active links. The structure of the code should be as follows: <ul class="product"> <li><a href="#myanmar" class="active">Mya ...

The close button is not functioning properly

I created a script to close my div element by clicking on the 'x' icon, but instead of deleting the div only, the whole page gets deleted. I'm not sure where I went wrong, can anyone help me? HTML <div class="note"> <span id ...

Arranging h1 tags within a container id

Seeking assistance with aligning specific classes in a certain way: "left" should align to the left part of #header, "links" should be centered, and "right" should align to the right of #header. Additionally, I want the icons to align vertically with the t ...

Is there a way to adjust the orientation of a <video> element without affecting the orientation of the video controls?

<video controls> </video> video { transform: scaleX(-1) } This CSS code flips the video horizontally, but it also flips the control buttons along with it. I attempted to flip the wrapper element containing the video using .wrapper {transfor ...

Struggling to Align Banner in Dynamic Layout

I am currently working on centering the banner image as the resolution size decreases, rather than having it pushed to the right. The specific page I am addressing can be found here. My approach involves utilizing a width of 100%. <!-- BEGIN T ...

Achieving a full width vertical navigation on the left side using Bootstrap's grid system: a step-by

I am looking to design an admin panel using bootstrap. To start off, I want to create a navigation menu on the left side. However, I encountered an issue where my navigation menu with a red background does not stretch the full width of the left column. Her ...

Issue with Bootstrap 4: Dropdown menu extends beyond the edge of the screen towards the right side

I'm having trouble with the dropdown items extending off the page. I've tried a few solutions from BS3 but they don't seem to be working for me. I suspect it might have something to do with the ml-auto class. (please disregard the if-else st ...

Upon transitioning to Bootstrap 4, the grid columns automatically wrap

I currently have a table-like display created using the bootstrap grid classes. The body rows on this table are filled dynamically with JavaScript. Depending on the application state, the table is either hidden or shown. I have two divs, one with an ID con ...

My confusion is running high when it comes to navigating the mobile version of my website

Creating a navigation bar for my website has been challenging. Whenever I switch to mobile view, the list in my navigation bar shifts 40px to the right side of the screen where there's nothing. Is there any way you can assist me with this issue? ...

The best way to perfectly align my gridview in HTML and CSS

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="1000px" AllowPaging="True" PageSize="8" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCanc ...

javascript method to apply styling

I'm puzzled by how these two javascript/css codes can yield different results: 1st: prev.setAttribute('style', 'position:absolute;left:-70px;opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;&apos ...

What is the best approach for establishing a consistent font size and line height for a website using em units?

What is the optimal approach to ensure consistent typography across different browsers (font-size and line height) for an entire website with a fixed width of 970px, centered layout? It's common to receive designs from clients with varying font sizes ...

Struggling with your Dropdown Menu onclick functionality in Javascript? Let me lend

I am seeking assistance with implementing a Javascript onclick Dropdown Menu. The current issue I am facing is that when one menu is opened and another menu is clicked, the previous menu remains open. My desired solution is to have the previously open menu ...

Chosen item from the dropdown menu in Bootstrap

I am utilizing Bootstrap in combination with RAILS. My main objective is: create a dropdown list with Bootstrap styling show the selected item in <p>, <div>, or <span> The JavaScript code appears as follows: $(function(){ $(".drop ...

Using Less in CSS values can sometimes involve incorporating slashes (`/`), such as in the shorthand for `font`

While working with Less and font shorthand, I came across an issue: .font(@weight: 300, @size: 22px, @height: 32px) { font: @weight @size/@height "Helvetica Neue", Arial, "Liberation Sans", FreeSans, sans-serif; } The above code snippet resulted in a ...