What is the best way to adjust the spacing based on the width of the screen?

Seeking assistance with coding for a website, I have encountered an issue where I need the background to be relative to the user's screen width. However, I am struggling to find the right approach.

I have attempted using relative units and media queries as demonstrated in the code below. Additionally, I experimented with a classic container which somewhat worked, but as the page shrinks, it eventually reaches the browser window's borders. My objective is to create a small white space between the end of the browser window and the "background-dark-white" that adjusts according to the user's page width.

<section id="jobs">
    <div class="container-fluid background-dark-white pt-4 pb-3 shortest">
        <div class="container-fluid shorter-inner">
            <div class="row">
                <div class="col-md-6 col-lg-4 mt-3 mb-0 mx-auto">
                    <div class="card">
                        <img src="../images/painting.jpg" alt="painting-image" class="card-img-top">
                        <div class="card-body background-dark-white">
                            <h4 class="card-title text-capitalize">painting</h4>
                            <p class="card-text">
                            <h5 class="text-grey font-weight-light pb-3 pt-1">Draw an artwork.</h5>
                        </div>
                    </div>
                </div>
                <!--End of technology card-->
                <!--Marketing card-->
                <div class="col-md-6 col-lg-4 mt-3 mb-0 mx-auto card-width-longer">
                    <div class="card">
                        <img src="../images/engineering.jpg" alt="engineering-image" class="card-img-top">
                        <div class="card-body background-dark-white">
                            <h4 class="card-title text-capitalize">engineering</h4>
                            <p class="card-text">
                            <h5 class="text-grey font-weight-light pb-3 pt-1">Assemble the parts together.</h5>
                        </div>
                    </div>
                </div>
                <!--End of marketing card-->
                <!--Design card-->
                <div class="col-md-6 col-lg-4 mt-3 mb-0 mx-auto card-width-longer">
                    <div class="card">
                        <img src="../images/computing.jpg" alt="computing-image" class="card-img-top">
                        <div class="card-body background-dark-white">
                            <h4 class="card-title text-capitalize">computing</h4>
                            <p class="card-text">
                            <h5 class="text-grey font-weight-light pb-3 pt-1">Write an amazing computer program.</h5>
                        </div>
                    </div>
                </div>
                <!--End of design card-->
                <!--Product card-->
                <div class="col-md-6 col-lg-4 mt-3 mb-0 mx-auto card-width-longer">
                    <div class="card">
                        <img src="../images/advertising.jpg" alt="advertising-image" class="card-img-top">
                        <div class="card-body background-dark-white">
                            <h4 class="card-title text-capitalize">advertising</h4>
                            <p class="card-text">
                            <h5 class="text-grey font-weight-light pt-1">Know advanced advertising tactics.</h5>
                        </div>
                    </div>
                </div>
                <!--End of product card-->
                <!--Service card-->
                <div class="col-md-6 col-lg-4 mt-3 mb-0 mx-auto card-width-longer">
                    <div class="card">
                        <img src="../images/servicing.jpg" alt="servicing-image" class="card-img-top">
                        <div class="card-body background-dark-white">
                            <h4 class="card-title text-capitalize">servicing</h4>
                            <p class="card-text">
                            <h5 class="text-grey font-weight-light pt-1">Provide quality services worldwide.</h5>
                        </div>
                    </div>
                </div>
                <!--End of service card-->
                <!--Selling card-->
                <div class="col-md-6 col-lg-4 mt-3 mb-0 mx-auto card-width-longer">
                    <div class="card">
                        <img src="../images/writing.jpg" alt="writing-image" class="card-img-top">
                        <div class="card-body background-dark-white">
                            <h4 class="card-title text-capitalize">writing</h4>
                            <p class="card-text">
                            <h5 class="text-grey font-weight-light pt-1">Write a book and express yourself.</h5>
                        </div>
                    </div>
                </div>
                <!--End of selling card-->
                <!--End of the benefits section-->
            </div>
        </div>
    </div>
</section>

Included below are all CSS adjustments implemented:

.shortest {
  max-width: 100rem;
  width: 100%;
  max-width: 90rem;
}
.shorter-inner {
  max-width: 100rem;
  width: 100%;
  max-width: 76rem;
}

@media only screen and (min-width: 1200px) {
  .shortest {
    max-width: 100rem;
  }
}
@media only screen and (min-width: 992px) {
  .shortest {
    max-width: 70rem;
  }
}
@media only screen and (min-width: 768px) {
  .shortest {
    max-width: 50rem;
  }
}
@media only screen and (min-width: 600px) {
  .shortest {
    max-width: 40rem;
  }
}

In need of guidance - could my issue stem from incorrect application of media queries? Grateful for any support provided!

Answer №1

When utilizing rem, keep in mind that it is relative to the font-size of the root element.
Using % is relative to the parent element, so be mindful of this distinction.

To obtain the 'screen width size', consider using vw (Viewport Width), which provides you with 1% of the viewport width.


For more information, feel free to check out: link.

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 can I make an absolutely positioned div slide down from a specific area on the page using jQuery's slideDown() function?

I have a website project in progress where the main image on the homepage is of a house with windows and a door. The idea is that when a user clicks on a window, a <div> should appear with some text. Similarly, clicking on the door should trigger a & ...

Showing the heading again after a new page starts

Currently, I am using WeasyPrint to generate a document and facing an issue with long sections that may span multiple pages. When a section breaks across pages, the name of the section does not display after the page break as intended. The following examp ...

The area surrounding inline content, both above and below

Can you explain the space above and below inline content? This phenomenon can be seen here: http://jsbin.com/vertical-spacing-weirdness/ If you look closely, there is a white area between the div/table and the span even though the span does not have ...

Validation does not occur when the submit button has an ng-click event attached to it

This is the form I created: <form action="" ng-controller="forgotCtrl as forgot" name="forgotForm"> <div class="form-group row"> <label for="email" class="col-sm-2 form-control-label">Email address</label> ...

"Dynamic navigation bar that remains in place even while scrolling past Adsense advertisements

Looking to implement a sticky menu for my blog using jQuery. The menu is functioning as desired, however, the issue arises when I scroll down on a page that contains adsense - the menu ends up going under the ad. What could be causing this problem? Below ...

Retrieve specific form data with the help of jQuery

In a unique situation, I am dealing with a webpage that contains multiple forms and I am attempting to retrieve the data submitted in each form. Here is an example of the HTML code: <form name="form1"> <input type="text"> <input ty ...

techniques for accessing HTML source code through an AJAX call

I am trying to retrieve the HTML source of a specific URL using an AJAX call, Here is what I have so far: url: "http://google.com", type: "GET", dataType: "jsonp", context: document.doctype }).done(function ...

The jQuery plugin functions smoothly when running on localhost, but it causes issues with the background image when deployed on the web

I created a compact plugin to show a cookie message. While testing it on my local machine, everything functioned smoothly without affecting any web pages. However, once I transferred the files to the server, I encountered issues such as: A background ima ...

Show the output of JQuery in the text input area

Can anyone help me figure out how to display the value of #response in an input field instead of a div? I've tried using the code below, but it didn't work: <div id="response"></div> I attempted to use an input field like this, ...

Enhance data table by displaying a set number of rows that do not completely fill the table's height

I'm currently attempting to implement a v-data-table with fixed header and footer. However, I've encountered an issue where if I set the table height to be larger than the row height, the table takes on the defined height and the footer ends up b ...

iOS 8 home screen web apps with status bar overlay and footer bar integration

After installing a web application to 'home' and working with it, I noticed that a recent update to iOS has made the usual black status bar transparent and float above the web content below. In addition, there is an unseen horizontal bar at the ...

Verify the information retrieved from the JSON document

I have a JSON file containing 10 records, and I am continuously adding these records to a list as the page is scrolled. This results in the data being appended dynamically. My goal is to stop appending more data once all the records from the JSON file hav ...

Implementing jQuery functionality on elements that are generated dynamically

I'm facing a challenge when working with dynamic elements in jQuery and I really could use some help. Let me provide an example from my current project: main.js $(function () { renderPlaceList(places); setupVoting(); } The two functions are ...

Tips for modifying and refreshing data in a live table with JQuery

One challenge I'm facing is figuring out how to transfer the data from a dynamic table back into input fields when clicking on the edit button of a specific row. Additionally, I need to update that particular row based on any changes made to the value ...

Looking to deactivate a particular checkbox in a chosen mode while expanding the tree branches

I encountered an issue with a checkbox tree view where I needed to disable the first two checkboxes in selected mode. While I was able to achieve this using the checked and readonly properties, I found that I could still uncheck the checkboxes, which is no ...

Tips for enhancing the fluidity of animations after removing an item from a list

I'm working on a project where I have a list of elements that are removed with an animation when clicked. However, I noticed that when one element is removed, the rest of the elements just jump up instead of smoothly transitioning. Is there a way to m ...

Issue with strange symbols appearing in Safari on Mac

Hey there! I have this website built with asp.net and I have a text-box for filling in quantities. Sometimes, I notice this strange icon that looks like a human appearing on the text box. It only seems to show up when using Mac Safari browser. Check out th ...

Customize the default tab appearance in bootstrap version 5.2

Is there a way to customize the appearance of these tabs? I'm looking to change the color to match the primary color of Bootstrap. https://i.sstatic.net/6HaUO.png instead of https://i.sstatic.net/qHUZ9.png <!DOCTYPE html> <html> & ...

Is it possible to combine the output of a loop using Liquid?

My goal is to display something like: data-tags="[tag1, tag2, tag3]" but I'm currently seeing data-tags:[tag1tag2tag3]. Is my usage of join incorrect? Here is the code: data-tags="{% for tag in subtask.tags %}{{tag.title | slugify | join ', &ap ...

Next.js fails to properly render external links when utilizing the Link component

It was quite surprising to discover that a basic Link component doesn't function as expected in Next.js when attempting to use it with an external URL and HTML Button tag within. Here is my attempt at solving this issue: Attempt 1: <Link href="h ...