What could be causing the unexpected behavior of the flex property?

Check out the code snippet below:

@import url("https://fonts.googleapis.com/css2?family=Inter&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Lexend+Deca&display=swap");
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-color: hsl(233, 47%, 7%);
  font-size: 15px;
}

h1 {
  color: hsl(0, 0%, 100%);
  font-family: "Inter", "sans-serif";
  line-height: 1.5;
  margin-top: 2rem;
}

p {
  color: hsla(0, 0%, 100%, 0.75);
  font-family: "Lexend Deca", "sans-serif";
}

#card {
  max-width: 400px;
  background: hsl(244, 38%, 16%);
  margin: 6rem auto;
  border-radius: 15px;
}

#image {
  background: url("https://xaviour1504.github.io/stats-preview-card/images/image-header-mobile.jpg");
  width: 100%;
  height: 300px;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  position: relative;
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
}

.overlay {
  width: 100%;
  height: 100%;
  background: hsl(277, 64%, 61%);
  opacity: 0.5;
  position: absolute;
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
}

#content {
  text-align: center;
  margin: 5rem 2rem;
}

#para {
  line-height: 1.5;
  margin: 2rem;
}


/* media queries */

@media (min-width: 600px) {
  #card {
    display: flex;
    max-width: 1200px;
    flex-direction: row-reverse;
    height: 400px;
    margin: 10rem auto;
  }
  #content {
    margin: 40px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    text-align: left;
  }
  #image {
    height: 400px;
    max-width: 600px;
    border-bottom-right-radius: 15px;
    border-top-left-radius: 0px;
  }
  .overlay {
    border-bottom-right-radius: 15px;
    border-top-left-radius: 0;
  }
  .stats {
    display: flex;
    gap: 5rem;
    margin: 0 auto;
  }
  p {
    margin-bottom: 30px;
  }
  #heading {
    margin-left: 27px;
  }
  #para {
    margin-top: 20px;
  }
}
<body>
  <div id="card">
    <div id="image">
      <div class="overlay"></div>
    </div>
    <div id="content">
      <h1 id="heading">
        Get <span style="color: hsl(277, 64%, 61%)">insights</span> that help your business grow.
      </h1>
      <p id="para">
        Discover the benefits of data analytics and make better decisions regarding revenue, customer experience, and overall efficiency.
      </p>
      <div class="stats">
        <div class="box1">
          <h1>10k+</h1>
          <p>COMPANIES</p>
        </div>
        <div class="box2">
          <h1>314</h1>
          <p>TEMPELATES</p>
        </div>
        <div class="box3">
          <h1>12M+</h1>
          <p>QUERIES</p>
        </div>
      </div>
    </div>
  </div>
</body>

Why does the content div take full width and the image div shrink when I resize my screen to a smaller size? It should divide into equal space but it's not happening.

Answer №1

To create some distance between the edge of the card and the body, consider adding left and right padding to the body element:

body {
 padding: 0 15px;
}

Feel free to adjust the value (15px in this example) to suit your design needs.

If you need help arranging the elements inside the card, check out this helpful guide on using flexbox:

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Answer №2

The CSS code provided below has been successfully added to your webpage:

/********************************************************/
/* ADDED */
/********************************************************/
#image, #content{
  flex: 1 0 100%;
  max-width: 100%;
}
#content{
  margin: 0;
}
.stats{
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1rem;
}
@media (min-width: 600px) {
  #image, #content{
    flex: 1 0 100%;
    max-width: 50%;
  }
}

I have set content & image in a flexible mode with a width of 50% to ensure they maintain the same size always.

In addition, I transformed your stats section into a grid format as it was extending beyond its parent container.

DEMO

@import url("https://fonts.googleapis.com/css2?family=Inter&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Lexend+Deca&display=swap");
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
body {
    background-color: hsl(233, 47%, 7%);
    font-size: 15px;
}
h1 {
    color: hsl(0, 0%, 100%);
    font-family: "Inter", "sans-serif";
    line-height: 1.5;
    margin-top: 2rem;
}
p {
    color: hsla(0, 0%, 100%, 0.75);
    font-family: "Lexend Deca", "sans-serif";
}
#card {
    max-width: 400px;

    background: hsl(244, 38%, 16%);
    margin: 6rem auto;
    border-radius: 15px;
}
#image {
    /*background: url("./images/image-header-mobile.jpg");*/
  background:url("https://xaviour1504.github.io/stats-preview-card/images/image-header-mobile.jpg");
    width: 100%;
    height: 300px;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    position: relative;
    border-top-left-radius: 15px;
    border-top-right-radius: 15px;
}
.overlay {
    width: 100%;
    height: 100%;
    background: hsl(277, 64%, 61%);
    opacity: 0.5;
    position: absolute;
    border-top-left-radius: 15px;
    border-top-right-radius: 15px;
}
#content {
    text-align: center;
    margin: 5rem 2rem;
}
#para {
    line-height: 1.5;
    margin: 2rem;
}
/* media queries */
@media (min-width: 600px) {
    #card {
        display: flex;
        max-width: 1200px;
        flex-direction: row-reverse;
        height: 400px;
        margin: 10rem auto;
    }
    #content {
        margin: 40px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        text-align: left;
    }
    #image {
        height: 400px;
        max-width: 600px;
        border-bottom-right-radius: 15px;
        border-top-left-radius: 0px;
    }
...
       </div>
    -->
  </div>
</body>

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

HTML Tip: Increase readability and maintain responsive design with a best practice of setting a

Is there a recommended maximum width and height for the canvas element for optimal performance in a single draw operation? Consider mobile compatibility as well. Appreciate any insight! ...

Dropdown colorization

I want to create a dropdown menu in my black navbar, but the text color becomes blue and the background of the pop-up turns white, which clashes with my black background. I need it to be transparent while using Bootstrap v4.1.1 <nav className="navbar n ...

Can we relocate the captions in colorbox to the top of the box?

Currently, my project utilizes colorbox for opening modal windows. There are 5 different styles available for colorbox, and I have chosen to implement Example 5, which can be viewed at this URL: I am looking to customize the layout of colorbox.js's ...

Picture disappearing following the refresh of an AJAX iframe

Currently, I am developing a profile system where the user's profile is displayed in an iframe that reloads when the form submit button is clicked. The content updates successfully upon reloading, but there is an issue with images not displaying after ...

Accessing certain metafield data from a bulk of product uploads on Shopify

After setting up a "Product Reference" and "List of products" metafield, I can now easily populate it with multiple products. However, my goal is to showcase the images and titles of these added products individually for customization purposes. https://i.s ...

The HTML div captured with html2canvas is incomplete

I am currently developing a meme editor website utilizing the html2canvas library available at Below is the HTML code for the div I aim to capture: <div class="container"> <div id="theUserMeme"> <div class=& ...

When using WYSIWYG editors, be on the lookout for empty paragraphs and incorrect code in CSS, JavaScript, and

I am currently in the process of redesigning a website for a client. The site is already built using Joomla 1.7, but I am facing an issue with the articles section that was created by the client using a WYSIWYG editor. The problem lies in the messy code st ...

Buttons are not aligned with the rest of the row's height

I'm currently working on designing an upload layout that consists of a container with a row inside. Within the row, there are two buttons (each containing input elements) and one h3. For some reason, I am having difficulty getting them to align at the ...

Bootstrap 5 introduces a new feature where col-sm-6 will take precedence over col-md

When using Bootstrap 5, I encountered an issue where the columns were not sizing evenly in a row. Specifically, on small screens, I wanted them to be exactly size 6. In this case, the columns should have been size 3 for screens larger than md, but they end ...

Tips on adding a tooltip to the react-bootstrap tab element

How can I add a tooltip to the tabs in my React application using Bootstrap tabs? I have three tabs and I want a tooltip to appear when hovering over each tab. import { Tabs,Tab} from "react-bootstrap"; // inside return <Tabs variant="pills" ...

Tips for assigning an event to a variable in JavaScript

Here is my javascript code snippet: function drag(ev){ var x= ev.dataTransfer.setData("Text",ev.target.id); } I am trying to pass a variable within this event so that I can perform a specific action. However, the current implementation is not worki ...

Troubleshooting CSS media queries: why isn't the display: none property functioning as

I am facing an issue with my code: HTML <section id="offer"> <div class="container"> <div class="row"> <div class="col col-md-12"> <h2>Offer</h2> ...

Switching between states using JavaScript

Here is a code snippet I'm working with: <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="C:\Users\rapandey\Documen ...

Issue with Spyscroll functionality

Having some trouble getting Spyscroll to work. Can anyone help me identify the issue? I've been at it all day... I've attempted both the javascript and html+css setups, but neither seem to be functioning correctly. Manually adding the "active" c ...

How can CSS and DIVs be utilized to align two smaller content boxes alongside one larger content box?

I'm trying to use CSS to create a layout with three div tags arranged in a specific way. I want two small content boxes stacked on each other, with a small space between them, to the left of a large content box. Any suggestions on how I can achieve t ...

Using Regex named groups in JavaScript's replace() method

Currently in my JS project, I am facing a challenge while trying to create an object using the code snippet below. My difficulty lies in extracting regex named groups using the replace function provided. var formatters = { 'p': 'padding& ...

Overlap with upper picture formatting

I've been struggling to create an ion-card with two images: a main picture and a small book cover. Any ideas on how to achieve this? Note: The layout should have 2 images, one at the top and another as a small book cover. Check out this sample on St ...

Adjusting the bootstrap class styles

I'm brand new to Bootstrap and currently in the process of learning how to use it. I have a question regarding the customization of Bootstrap classes. Specifically, I would like to modify the border size and color of certain classes. In order to ac ...

Conceal the column title for order items in the WooCommerce admin panel

I've been attempting to hide certain columns within the administrative section of Woocommerce, but have had no success with the code I'm using. I'm currently stuck and my question is: Is there a way for me to conceal specific column names i ...

Dynamically add Select2 with a specific class name: tips and tricks

I need help adding a new row with a select2 instance using a class name. The new row is created with the select dropdown, but I am unable to click on it for some reason. var maxGroup = 10; //add more fields group $(".addMor ...