When the screen is minimized, my website doesn't align to the center as expected

When the screen is maximized everything looks great, but when I minimize it, the content doesn't center. I tried using margin: auto; in the "main-div" class, but then everything shifted to the top left corner. So, I added a "wrapper-div" to contain the main div and align it, but it doesn't seem to be working as expected.

There seems to be something at the top that I can't quite figure out. Click here for image description


body {
  background-color: hsl(30, 38%, 92%);
}

table {
  border-radius: 20px;
  overflow: hidden;
  border-collapse: collapse;
}

.wrapper-div {
  width: auto;
  margin: auto;
}

.main-div {
  width: fit-content;
  margin: 135px 470px;
  position: absolute;
}
<body>
  <div class="wrapper-div">
    <div class="main-div">
      <table cellpadding="0">
        <tr class="parent-row">
          <td class="img-container">
            <img src="images/image-product-desktop.jpg" alt="image-product-desktop-version" />
          </td>
          <td class="text-container">
            <h3 class="title1">PERFUME</h3>
            <h2 class="title2"><b>Gabrielle Essence Eau De Parfum</b></h2>
            <p class="description-text">
              A floral, solar and voluptuous interpretation composed by Olivier Polge, Perfumer-Creator for the House of CHANEL.
            </p>
            <div class="both-prices">
              <h2 class="new-price">$149.99</h2>
              <h4 class="old-price">$169.99</h4>
            </div>
            <a class="cart-btn" href="#"><span></span>Add to cart</a>
          </td>
        </tr>
      </table>
    </div>
  </div>
</body>

Answer №1

To center your content on any screen, apply the following CSS properties to your main div: display:flex;, align-items:center;, and justify-content:center;.

body {
  background-color: hsl(30, 38%, 92%);
}

table {
  border-radius: 20px;
  overflow: hidden;
  border-collapse: collapse;
}


.main-div {
  display:flex;
  align-items:center;
  justify-content:center;
  height:100vh;
}
<body>
  <div class="wrapper-div">
    <div class="main-div">
      <table cellpadding="0">
        <tr class="parent-row">
          <td class="img-container">
            <img src="images/image-product-desktop.jpg" alt="image-product-desktop-version" />
          </td>
          <td class="text-container">
            <h3 class="title1">PERFUME</h3>
            <h2 class="title2"><b>Gabrielle Essence Eau De Parfum</b></h2>
            <p class="description-text">
              A floral, solar and voluptuous interpretation composed by Olivier Polge, Perfumer-Creator for the House of CHANEL.
            </p>
            <div class="both-prices">
              <h2 class="new-price">$149.99</h2>
              <h4 class="old-price">$169.99</h4>
            </div>
            <a class="cart-btn" href="#"><span></span>Add to cart</a>
          </td>
        </tr>
      </table>
    </div>
  </div>
</body>

Answer №2

Are you satisfied with this?

 body {
      background-color: hsl(30, 38%, 92%);
    }
    
    table {
      border-radius: 20px;
      overflow: hidden;
      border-collapse: collapse;
    }
    
    .wrapper-div {
      background-color:red;
      width: 80vw;
      margin:0 auto;
    }
    
    .main-div {
      width: fit-content;


    }
 <div class="wrapper-div">
    <div class="main-div">
      <table cellpadding="0">
        <tr class="parent-row">
          <td class="img-container">
            <img src="s1.jpg" alt="image-product-desktop-version" />
          </td>
          <td class="text-container">
            <h3 class="title1">PERFUME</h3>
            <h2 class="title2"><b>Gabrielle Essence Eau De Parfum</b></h2>
            <p class="description-text">
              A floral, solar and voluptuous interpretation composed by Olivier Polge, Perfumer-Creator for the House of CHANEL.
            </p>
            <div class="both-prices">
              <h2 class="new-price">$149.99</h2>
              <h4 class="old-price">$169.99</h4>
            </div>
            <a class="cart-btn" href="#"><span></span>Add to cart</a>
          </td>
        </tr>
      </table>
    </div>
  </div>

Don't forget to specify the width of your parent element, and use margin: 0 auto for horizontal alignment.

Answer №3

The issue lies within the code snippet below:

 .main-div {
      width: fit-content;
      margin: 135px 470px;
      position: absolute;
    }

Using the margin property in this way will result in a fixed spacing on the left of the element, regardless of screen size.

To address this issue, adjust the margin value to be proportional to the screen width like so:

.main-div {
  width: fit-content;
  margin: 12vw 20vw;
  position: absolute;
}

Learn more about using the vw unit in CSS: https://www.w3schools.com/cssref/tryit.asp?filename=trycss_unit_vw

Alternatively, you can utilize @media queries to set different margin values for smaller screens:

@media only screen and (max-width: 600px) {
  .main-div {
     width: fit-content;
     margin: 110px 170px;
     position: absolute;
  }
}

Note that relying solely on @media queries is not recommended as it may require multiple adjustments for various screen sizes.

For further information on @media queries in CSS, visit: https://www.w3schools.com/cssref/css3_pr_mediaquery.asp

Answer №4

Are you looking to center your CSS styling?

I recently came across some valuable information about margin styling in the Mozilla documentation

The margin property allows for one, two, three, or four values to be specified. These values can be lengths, percentages, or even the keyword 'auto'. Using negative values can bring an element closer to its neighbors than default positioning.

When using a single value, that margin applies uniformly to all sides of the element.

However, simply setting

margin: auto;

may not suffice to center your element on the page as desired.

A more specific declaration like

margin: 0 auto;

is often necessary when employing two values. The first value affects top and bottom margins, while the second influences left and right margins.

By implementing this styling adjustment, your element should appear centered on the page.

I hope you found this explanation helpful. Thank you

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

Unable to generate a navigation panel using HTML/CSS and jQuery

I recently completed the basic courses in HTML/CSS, JavaScript, jQuery, and PHP on Codecademy. I'm currently working on creating a website using HTML/CSS and jQuery in Codecademy's codebits. However, I'm facing some issues with my navigation ...

Can a linked checkbox be created?

Is it possible to create a switch type button that automatically redirects to a webpage when turned on by clicking a checkbox? I'm working on implementing this feature and would like to know if it's feasible. ...

Navigation problems resolved: The fixed-top class causes issues with navbar-right and obscures the logo

I have implemented a bootstrap Nav on this page To achieve a sticky top navigation, I used the fixed-top class. However, this caused the navbar to align left instead of right as intended. It also ended up covering my logo, which was originally aligned to ...

JavaScript Failing to Validate User Input in Form

I'm a beginner in JavaScript and I'm trying to validate a simple date of birth input, but for some reason, it seems that the JavaScript file is not working. No matter what I input, it always goes through, so I'm trying to figure out what&apo ...

Ensure that grid rows occupy the least amount of space possible

I'm relatively new to grid layout and I've encountered a challenge that has me stuck. Here's what I have so far: codepen And this is the relevant part of the grid: grid-template: 'img date' 'img head' 'img s ...

Creating dependent dropdown lists is a useful way to streamline data entry and ensure accuracy in your

I am looking to create a series of 4 connected dropdown lists, structured like this: District: <select id="district"> <option>Select a District</option> <option value="district1">dstrict1</optio ...

Conceal all div elements except for displaying the initial two

Can an entire div be hidden with only the first 2 entities visible? <div class="inline-edit-col"> <span class="title inline-edit-categories-label">Brands</span> <ul class="cat-checklist product_brand-checklist"> < ...

How to Use Laravel to Create HTML Divs Without Images

While working with a text editor, we have the ability to incorporate various HTML tags such as images and videos. However, I am only interested in displaying text fields. When I use {{$loop->content}}, it displays: <p><strong>EXAMPLE</st ...

Whenever the download bar emerges at the bottom, the slideshow content on the page suddenly shifts upwards

Each time the download bar shows up at the bottom, the slideshow content on the Homepage suddenly moves up. It returns to its original position after I close the download bar.https://photos.app.goo.gl/F482eMfkXyfEZkdA9. My assumption is that this issue is ...

When enlarging or extending the container, text starts to spill out

My issue is that the text content is overflowing the designated box or container - any ideas on how to fix this problem? I have attempted to utilize max-width and max-height, but unfortunately, I am unable to get them to function as intended. Here's ...

What is the best way to store a range object obtained from getSelection in order to recreate it on a separate page load?

My goal is to develop a web application that enables users to highlight selected text on a webpage with the click of a button. I want these highlights to persist even when the user returns to the page. So far, I have achieved: var selectedRange = documen ...

Leveraging a vacant CSS class to locate specific HTML elements

While browsing through some HTML code, I noticed the use of classes without any CSS styling attached to them. Instead of using id's, they were simply used to group or identify elements for jQuery selectors: html: <div class=someClass> </div ...

Is there a way to extract the number from a b tag using selenium with Python?

I'm attempting to extract the numbers within all the <b> tags on this particular website. I am looking for each "qid" (question id), so I believe I need to utilize qids = driver.find_elements_by_tag_name("b"). Following suggestions fro ...

What is the best way to restrict user input to specific numbers only?

I have a text input field, how can I ensure that only numeric values from 1 to 31 are allowed? <input type="number" min="1" max="31"> ...

Is there a way to automatically redirect my login page back to the original page in case of login failure?

I'm currently working on a basic login page using express.js + E.js. If the login credentials are incorrect, I have a variable badLogin that is triggered (see below). However, my goal is to redirect the website back to the login page instead of remai ...

Tips on how to retrieve the value of the second td in an HTML table when clicking on the first td utilizing jQuery

There is a specific requirement I have where I must retrieve the value of the second td in an HTML table when clicking on the first column. To accomplish this task, I am utilizing jQuery. $('.tbody').on('click','tr td:nth-child(1) ...

Tips for saving the visibility status of a <div> in your browser bookmarks?

Currently, I am in the process of developing a single webpage (index.html). The navigation bar at the top includes links to hash references (DIV IDs). If JavaScript is enabled, clicking on these links triggers a JavaScript function with the onclick attribu ...

The hamburger icon is not visible on smaller screens when using the Bootstrap 5 navbar

'The stylesheets for Bootstrap CSS and JS have been included in the file. The content shows up fine with default colors, but not when I customize them.' ''' <li class="nav-item"> <a class= ...

Tips for making jQuery maphilight function properly?

Can someone assist me with Mapilight? I have been trying to get it to work but no success so far. Here are the script links in the head section of my HTML. <script type="text/javascript" src="/js/jquery.js"></script> <script type="text/ja ...

The Jquery function .load() doesn't load HTML comment tags as expected

Utilizing the .load() Jquery function enables me to insert html tags from one file into another. Here is an example of the code: <html> <head> <script src="jquery.js"></script> <script> function loadConte ...