Offspring Div Protruding from Mother Div

Struggling to contain the character div within the game div in my project. Seeking advice as I am relatively new to CSS and would appreciate any tips or feedback.

* {
  padding: 0;
  margin: 0;
}

#game {
  margin: auto;
  width: 400px;
  height: 500px;
  border: 1px solid black;
  overflow: hidden;
}

#block {
  width: 50px;
  height: 500px;
  background-color: black;
  position: relative;
  left: 400px;
  animation: block 2s infinite linear;
}

@keyframes block {
  0% {
    left: 400px;
  }
  100% {
    left: -50px;
  }
}

#character {
  height: 50px;
  width: 50px;
  background-color: black;
  margin: auto;
  float: left;
}
<div id="game">
  <div id="block"></div>
  <div id="character"></div>
</div>

Experience the issue first-hand with this JSFiddle: https://jsfiddle.net/skltnboss/Ljb845h3

Answer №1

When it comes to the stick, my understanding may vary. If you wish for it to stay within the boundaries of the #game, ensure that the left property is set to a minimum of 0:

#character: {
   left: 0;
}

If you desire for it to shift left and vanish as it currently does, while staying within the confines of the #game, then set the overflow property of the #game to hidden:

#game {
   overflow: hidden;
}

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

The list in the navbar is misaligned and not centered vertically within the flex container

I've been working with flex for a while now, but I'm struggling to vertically align the content of my navbar to the center in the code. <nav className="nav navbar"> <h3> Logo </h3> <ul className= &q ...

What is the way to specify both the initial and final classes within a nested class structure?

Within my code, there is a block-border class containing two block-content classes. Specifically, I am working on compatibility with IE9 and higher. I am seeking assistance in defining the structure using Less as shown below: .block-border { &. ...

Guide to positioning an image absolutely within a div

I am struggling to position a small close image within a div without success. The goal is for the image to always appear on the left side of the div, regardless of the content. The content consists of a text label with a maximum length of 8 characters. Iss ...

What is the best way to navigate to a particular element on a webpage?

I am looking to implement functionality on my HTML page where users can 'scroll to' or 'focus on' a specific element. Traditionally, I would use an anchor tag with a href="#something". However, in this case, I am already utilizing the h ...

Enhance the appearance of a dropdown selection using CSS

I am interested in customizing the appearance of the first option (Search Clubs) to be bold, while keeping the rest with normal font weight. Currently, I am able to make the first option bold, but it only appears that way after clicking the dropdown menu ...

What is preventing these elements from being contained within particular divs?

I'm facing an issue with my HTML code and need some help unraveling what's happening. On my simple website, I have a container div, header div, and body div, but it seems like the HTML elements are not aligning properly within these divs (they a ...

Having some trouble getting this jsfiddle to function properly on my local machine. Any suggestions?

I'm having trouble with this jsfiddle. It works perfectly for me, but when I try to replicate it, nothing happens when I press the button. Can anyone help? http://jsfiddle.net/KPEGU/1850/ Here is the code snippet: <html> <head> <ti ...

relocate the image with an identical filename stored in the variable

Having trouble moving an image to a new position when clicking on its small thumbnail. I've attempted using jquery's find() function but it doesn't seem to be working... $('#slide1_controls img').click(function (event){ var sr ...

The theme for Wordpress in 2015, also known as

Is there a way to automatically resize and fit the cover picture in the twenty fifteen theme to make it a square cover instead of the default rectangular one? I've been trying to change the dimensions of the cover image container but can't seem t ...

Unusual characteristics found in a select list controlled by Angular

The article titled "How to set the initial selected value of a select element using Angular.JS ng-options & track by" by @Meligy served as my guide while working on implementing a select list (ng-options). Despite achieving the desired basic functional ...

Using JSON and Ajax to dynamically append and generate a new Div element

I am currently working on an HTML page where most of its components are static and loaded from JSON files. My goal is to create "posts" and attach "comments" to them. I have two JSON files for this purpose: posts.JSON and comments.JSON. The posts.JSON fi ...

Show vertical column lists on big screens and stacked drop-downs on mobile devices

I am looking for an efficient solution to showcase a vertical column list on desktop/large screens, while also displaying the same as a stacked dropdown list on mobile devices. This project needs expertise in html, css, and bootstrap. Please take a look a ...

Froala text area is unexpectedly visible when I attempt to cover it with a partially see-through mask

The website I'm currently developing features a semi-transparent overlay that dims the screen with a light-colored message displayed on top. This overlay and message are shown whenever there is a background process running. Everything was running smoo ...

Issue with webkit-box-reflect causing errors on Android devices

I have been attempting to incorporate a reflection effect on a mobile webview application (specifically for Android and iOS using webkit). I followed examples I found on various websites, including Stack Overflow. "-webkit-box-reflect: below -14px -webkit ...

After refreshing the page, Google Chrome finally displays the CSS styles correctly

I'm currently working on a JavaScript script to showcase images on a webpage. These images are loaded using an AJAX request and a CSS style is directly applied using jQuery. The script functions correctly on Firefox, Opera, and IE, but Google Chrome i ...

Universal compatibility for web displays

I've been conducting testing on a website across Chrome, Firefox, and Internet Explorer, utilizing the CSS code snippet below: #foot_links1, #foot_links2, #foot_links3 { position: absolute; margin-top: 55px; margin-top: 14em; color: # ...

Styling in Next.js with conditions

I am attempting to create a scenario where a link becomes active if the pathname matches its href value. function Component() { const pathname = usePathname(); return ( <div className="links"> <Link href="/"> ...

Parallax scrolling and Swiper slider, a match made in digital design

Currently facing an issue with Swiper Slider and Simple Parallax Scrolling. Whenever I switch slides, the same image is displayed repeatedly. I suspect the problem lies in the fixed position of the image within the parallax effect. Attempted to resolve b ...

Fixing the PHP AJAX issue with loading additional content

Hello, I've created an app where users can click a "Load More" button to display data from a database. However, the problem arises when all the data has been fetched and the button continues to appear. If the user clicks the button again, the same dat ...

JavaScript function for automatic scrolling to the bottom of the page is not functioning as expected

I'm working on incorporating a terminal/console feature into my website. I came across the JavaScript functions for scrolling down a page, namely window.scrollTo(0,document.body.scrollHeight); and window.scrollTo(0,document.querySelector(".fakeSc ...