Tips for preventing the background image from spilling out of bounds

I've been struggling to prevent the image from overflowing and expanding my webpage. I attempted using overflow:hidden in various places, but it hasn't solved the issue.

To avoid the text from zooming as well, I included a ::after element.

.mainScreen {
  display: flex;
  color: white;
  height: 90vh;
  z-index: 2;
}

.mainScreen::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 90vh;
  background-image: url('https://images.pexels.com/photos/9707969/pexels-photo-9707969.jpeg?auto=compress&cs=tinysrgb&w=600');
  background-size: cover;
  z-index: 1;
  animation: zoom-in-zoom-out 30s ease infinite;
}

@keyframes zoom-in-zoom-out {
  /*This is the animation zoom-in-zoom-out effect*/
  0% {
    transform: scale(1, 1);
  }
  50% {
    transform: scale(1.1, 1.1);
  }
  100% {
    transform: scale(1, 1);
  }
}
<div class="mainScreen">
</div>

Answer №1

Consider utilizing the background-size property instead and applying it to the main element.

* {
  margin: 0;
  padding: 0;
  min-width: 0;
  min-height: 0;
  box-sizing: border-box;
}

 ::before,
 ::after {
  box-sizing: inherit;
}

.mainScreen {
  display: flex;
  color: white;
  height: 90vh;
  z-index: 2;
  overflow: hidden;
  background-image: url('https://images.pexels.com/photos/9707969/pexels-photo-9707969.jpeg?auto=compress&cs=tinysrgb&w=600');
  background-size: 100% auto;
  background-position: top center;
  animation: zoom-in-zoom-out 30s ease infinite;
}

@keyframes zoom-in-zoom-out {
  /*This is the animation zoom-in-zoom-out effect*/
  0% {
    background-size: 100% auto;
  }
  50% {
    background-size: 110% auto;
  }
  100% {
    background-size: 100% auto;
  }
}
<div class="mainScreen"></div>

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

Scrollbar issue and splitting line in half

I am writing some CSS code below. I am trying to achieve horizontal scrolling, but if the sentence is too long, it breaks and displays on the next line. In the image, I want "aaaa.." to be displayed on only one line and "bbb.." on the second line, ...

The body's width is more compact compared to one of my containers in HTML/CSS

After completing my code for a beginner's HTML/CSS project, I realized that the main parent container holding two smaller containers needed to be set at specific widths. The two inner containers had to be exactly 650px and 270px wide, while the main p ...

Attempting to conceal a section with CSS styling

Here's a simple question for you: which one of these options is correct? I am attempting to hide this particular div: <div class="notice important-message"> .notice .important-message { display: none } Or should the classes be joined tog ...

Matching CSS attribute values with another attribute

Can CSS be used to target elements based on the value of another attribute? For instance: <div data-attr1="abc" data-attr2="def"></div> <div data-attr1="abc" data-attr2="abc"></div> I am looking for something like this (although i ...

What methods can I use to identify the selector for this element?

After pinpointing the specific element in Google Chrome using inspect element, what kind of path or syntax should I use to apply a method to this element? The hierarchy for the element is outlined below: html body div#contentDiv div#searchFormDiv ...

Developing a feature that allows users to switch between different sets of information

I'm currently exploring a new project and attempting to design a toggle that switches between monthly and annual payments based on the user's selection, much like the functionality found here: . At present, I have implemented two sets of price c ...

Ways to ensure your page stays confidential

Concept: Exploring the idea of making specific pages on a website private. For example, having 4 navigation links where one link leads to a private page that requires a username and password for access. Currently, my practice website has 7 links directing ...

How do I easily show/hide a submenu in AngularJS with a toggle menu?

After browsing some Stacks, I've come to the realization that I need to create separate directives in order to hide or toggle a menu from its sub-menu items. I want the menu to be hidden when any of the sub-menu items are clicked. However, I can&apo ...

Place in the Middle of a Bootstrap Container

Struggling with aligning the elements of a Bootstrap well in the center? The badge is off to the side instead of below the arrows, and the vote buttons are not centered within the well. Check out the code snippet below: HTML: <div class="col-md-1 ...

Concealing an Automatically Updated Section when Devoid of Content -- Ruby on Rails Version 4

I have come across various solutions to this problem, but none of them seem to be effective for my specific project. In my application, the user's previous choices are displayed in multiple divs. Initially, all the divs are empty. As the user progress ...

Issue with file path reaching into the public directory

Here is the image of the folder path:- https://i.sstatic.net/GESWX.png The CSS file seems to not be working in the exercises folder. It is only importing the header-ejs file without any styles, but it works fine for chapters and other files. How can I re ...

When designing a webpage using HTML and CSS, the size of HTML blocks decreases as the page is made smaller

Having an issue with the following: I hope you can help me identify my problem here and I'm not sure why it's occurring. https://i.sstatic.net/C7VzU.png I believe the problem lies in the header and nav sections I initially thought it might be rel ...

Ways to create a collapsing navigation with a fixed top navbar and scrolling functionality

How can I create a collapsing navigation bar that scrolls with the page using navbar-fixed-top in Bootstrap 4? <!DOCTYPE html> <html lang="en>"> <head> <meta charset="UTF-8"> <title>Document</title> <script src= ...

Count the start and end rows of a table within the <tfoot> using pdfHTML/iText 7

Currently, I am working on a project where I am developing a document generator that utilizes pdfHTML 3.0.3 and iText 7.1.14. The document includes a table displaying 'items'. It's worth noting that these item rows will likely span across mu ...

Container containing sliding content underneath

I've designed a grid with separate boxes that reveal related content when clicked. The display of this content is achieved using jQuery's slideToggle feature. The setup functions smoothly, with each row containing clickable boxes that unveil the ...

Ensuring that a paragraph text wraps neatly inside a div container

I recently came across a post discussing the issue of text wrapping in IE 9 here. My main goal is to have my text wrap only once in this particular browser. The content consists of just two lines, and I need it to smoothly transition from the first line to ...

Methods for Expanding a Div within an Oversized Row, Despite Height Constraints

I have a situation where I need to adjust the height of a cell in a table. One of the cells contains a larger element that causes the row to expand vertically. I also have another cell with a div element set to 100% of the cell's height. However, I wa ...

Improve loading speed of thumbnail and full images in HTML

I'm struggling with slow loading images on my blog website. Can anyone help me figure out how to improve loading speed and: Is it possible to use a separate thumbnail image that is smaller in size instead of the full image for both thumbnails and th ...

Using Rails to assign a page-specific CSS class within a shared layout

Is there a way to apply unique CSS classes to specific tags within a common layout? In my application.html.erb layout file, the application.css.scss is loaded using <%= stylesheet_link_tag "application". . . %>, which then includes all CSS files in ...

What is the best way to replace a CSS Background that is already marked as important?

Attempting to change the background of a website using Stylish, but encountering issues. The existing background css on the website includes an !important rule which is preventing Stylish from taking effect. Here is my code: body { background-image: n ...