I am struggling to find the right way to center those images

https://i.sstatic.net/aRkvl.jpg

Hello everyone, I'm currently attempting to set up the main content of the homepage resembling the image provided, but I'm encountering some challenges.

Every approach I take seems to cause the image to overflow the container and occupy the entire page. Rather than setting a fixed size for the image, my goal is to have it scale proportionally with the view's height and width

The code I am using at the moment looks like this:

<section class="main">
        
        <div class="main-left">
            <div class="container">
                <img src="assets/images/wine.png">
            </div>
        </div>

        <div class="main-right">
            <div class="container">
                <img src="assets/images/oil.png">
            </div>
        </div>

    </section>
 .main {
    display: flex;
    background-color: #f1eee9;
    height: 100%;
  }

  .main-left, .main-right {
    display: flex;
    flex-direction: row;
  }

  .main-left {
    background-color: #111;
      width: 50%;
  }

  .main-right {
    background-color: #1f1f1f;
      width: 50%;
  }

  .container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 80vw;
    height: 80vw;
  }

  .container img {
    display: block;
    width: 100%;
    height: 100%;
  }

I haven't incorporated any text yet, so any guidance on how to accomplish that would be greatly appreciated..

Answer №1

It is recommended to utilize: object-fit: cover; as detailed in the following resource : https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit

Based on your example, I have modified the container height to 80vh instead of vw.

.main-left, .main-right {
    display: flex;
    flex-direction: row;
  }

  .main-left {
    background-color: #111;
      width: 50%;
  }

  .main-right {
    background-color: #1f1f1f;
      width: 50%;
  }

  .container {
    width: 80vw;
    height: 80vh;
    background-color: blue;
  }

.container img {
    display: block;
    width: 100%;
    height: 100%
    object-fit: cover;
  }

Answer №2

To enhance the efficiency of your code, consider the following optimization:

<section class="main">
        
        <div class="main-left">
            <img src="assets/images/wine.png">
        </div>

        <div class="main-right">
             <img src="assets/images/oil.png">
        </div>

    </section>
.main {
    display: flex;
    flex-direction: row;

  }
.main-left , .main-right{
    flex: 1;
}
  .main-left {
    background-color: #111;

  }

  .main-right {
    background-color: #1f1f1f;
  }

.main > div img {
    display: block;
    width: 100%;
    height: 100%
    object-fit: cover;
  }

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

How to assign a value to a textarea element in an HTML form?

I am struggling to populate a text area with the value using this code snippet... <?php $message = $_REQUEST['message']; ?> <br/><b>Description</b><br/> <TEXTAREA NAME="message" COLS=40 ROWS=6 value="<?=$messa ...

Utilizing the power of SimpleHTMLDom to retrieve high-resolution images through an advanced Google search

I want to display a large image that was retrieved from Google using simple_html_dom.php include_once( "simple_html_dom.php" ); $key="unique"; $html = new simple_html_dom(); $html=file_get_html('http://images.google.com/images?as ...

The printing function for the window system can cause disruptions to the layout of the table

After creating a page with a simple table that can be filled in and printed, I noticed some issues with the table formatting after printing. The intended table design was supposed to look like this: https://i.stack.imgur.com/aAk89.png However, upon print ...

Clear out the classes of the other children within the identical parent division

I'm currently in the process of replacing my radio circles with div elements. I've successfully implemented the functionality for selecting options by clicking on the divs, as well as highlighting the selected div. However, I'm facing trou ...

Confirming the authenticity of a newly added user input with the help of jQuery

Within my current project, I have implemented validation features for text boxes. When a user clicks on a text box, the border is highlighted. If they click elsewhere without entering any value, the border turns red and an alert pop-up appears. In additio ...

I am looking for a way to add some color to the text within a div on my HTML page. Can you help me

Is there a way to apply background color only to the text content within this div, without styling the entire element? ...

Issue with Django Form not showing up on HTML after encountering an error

I have a question regarding my Django project. When a user sends a GET request, I need to display a form on the page. Here is the initial code snippet: form = SignUpForm() if request.method == 'POST': .... else: return render(request, & ...

What methods can be used to center a Google map on a specific location?

I am facing an issue with three map canvases on different divs. Despite setting the center to the same position in all three of them using JavaScript, the second and third maps do not seem to reflect that center. Does anyone have any insights on what cou ...

What could be causing some videos not to load on a Chrome page?

Make sure to click on the link using a webkit browser. Upon loading, you should see a 4x4 grid of videos, but in Chrome, only 1-3 videos tend to load. Interestingly, it works perfectly fine on Safari. Why is this discrepancy happening even though they a ...

When the button is clicked, my goal is to increase the value of the text field by one using JavaScript

I need the functionality for each button to increment the value text field located next to it <input type="number" value="0" min="0" max="5" step="1" id="qty"/> <button onclick="buttonClick()" type="button" class="btn btn-success btn-sm" id="add" ...

Generating forms dynamically with Vuetify

When working with HTML code, the structure looks like this: <v-col cols="12" md="4" v-for="(leadObj, i) in lead.data" :key="i" > <v-col v-if="leadObj.inputType !=='select'"> ...

Adjusting font size to perfectly fit within its confines

When using v-for to map out objects from an array into cards, it's important to keep the height consistent throughout the section. Manually adjusting text size with @media queries can be tedious and inelegant, requiring multiple rules for different sc ...

React is producing a collection of <td>'s

My React code is very straightforward and it runs smoothly: function Columns(){ return ( <React.Fragment> <li>Hello</li> <li>World</li> </React.Fragment> ); } function Example(){ ...

Difficulty in breaking text within flexbox styling

I'm facing an issue where the text inside a flex container does not break correctly when the horizontal space is too small to display it all. But shouldn't it still not break even in the "normal" state? What mistake have I made here? (functio ...

Is it possible for me to use AJAX to load content from a string? I am attempting to postpone the activation of certain HTML

I need help optimizing my HTML page that includes certain sections with large Javascript files and images, which are initially hidden. Even when set to "display: none," the browser still loads all the content. One solution could be moving those sections in ...

Foundation 4 Framework: Mastering the Art of Clearing CSS Floats

I'm currently in the process of developing a website, but I am facing difficulties with clearing the floats. My website is built using the foundation 4 framework. Whenever I apply the .columns class to an element, it causes the elements to float left. ...

Identifying the length of a division element following its addition

I'm facing difficulties in detecting the length and index of the dynamically appended div. I have researched extensively and found a solution involving MutationObservers, but I am unsure if it is necessary for this particular issue. Let's focus ...

Manipulating links using JQuery: avoiding the binding of new links

I need to update a website with 50 pages that currently doesn't use ajax, making it cumbersome to make edits since changes have to be made on all 50 pages individually. After removing duplicate HTML from all 50 pages, I'm facing some issues: fu ...

Difficulty achieving transformation of one div element within another div element

My aim is to create a unique hover effect for a button where it tilts slightly without affecting the text. You can view my progress here: https://codepen.io/kyannashanice/pen/QWveOMg I tried stacking the text and button in separate divs and triggering the ...

Disabling click events on a span tag in Angular 6: A step-by-step guide

Is there a way to disable the click event for deleting hours with the 'X' symbol based on a condition? Thank you in advance. <table navigatable class="<some_class>"> <tbody> <tr *ngFor="let item of ...