Tips for positioning items within div in relation to its parent div

After successfully positioning the "choose" button inside a div, I encountered an issue where it only functions properly when hovered over. This occurred while experimenting with hover and positioning on the div element. Without hovering, the 3 buttons appeared in a specific spot that seemed out of place.

If anyone has insights into this problem, I would greatly appreciate your assistance. Thank you for your help!

I apologize for any confusion in the code provided, as I am relatively new to this. Here is the code snippets.

<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
      header {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      ul {
        display: flex;
      }

      li {
        list-style: none;
        padding: 15px;
      }

      #header-img {
        display: flex;
        width: 50px;
        height: 50px;
      }

      @media screen and (max-width: 600px) {
        #video {
          width: fit-content;
        }
      }

      #container {
        width: 80%;
        margin: 200px auto 200px auto;
        display: flex;
        justify-content: space-around;
      }

      .choices {
        background-color: rgb(241, 241, 111);
        width: 250px;
        height: 400px;
        box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.7);
      }

      .choices .pilih {
        position: absolute;
        left: 40%;
        top: 350px;
      }

      .choices:hover {
        transform: scale(1.5);
        box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5)
      }

    </style>
    <title>Document</title>
  </head>

  <body>
    <header id="header">
      <img src="https://png.pngtree.com/element_pic/16/05/30/11574bb301599cc.jpg" alt="" id="header-img">
      <nav id="nav-bar">
        <ul>
          <li><a href="#video" class="nav-link">Features</a></li>
          <li><a href="#video" class="nav-link">How it works</a></li>
          <li><a href="#video" class="nav-link">Pricing</a></li>
        </ul>
      </nav>
    </header>
    <div id="container">
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
    </div>
  </body>

</html>

Answer №1

  1. Setting an element's position to absolute makes it relative to the body by default.
  2. In order to position it relative to a specific container, you must set its parent (which has the class choices) as "position: relative"

 header {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      ul {
        display: flex;
      }

      li {
        list-style: none;
        padding: 15px;
      }

      #header-img {
        display: flex;
        width: 50px;
        height: 50px;
      }

      @media screen and (max-width: 600px) {
        #video {
          width: fit-content;
        }
      }

      #container {
        width: 80%;
        margin: 200px auto 200px auto;
        display: flex;
        justify-content: space-around;
      }

      .choices {
        background-color: rgb(241, 241, 111);
        width: 250px;
        height: 400px;
        box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.7);
        position: relative;
      }

      .choices .pilih {
        position: absolute;
        left: 25%;
        top: 50%;
      }

      .choices:hover {
        transform: scale(1.5);
        box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5)
      }
<header id="header">
      <img src="https://png.pngtree.com/element_pic/16/05/30/11574bb301599cc.jpg" alt="" id="header-img">
      <nav id="nav-bar">
        <ul>
          <li><a href="#video" class="nav-link">Features</a></li>
          <li><a href="#video" class="nav-link">How it works</a></li>
          <li><a href="#video" class="nav-link">Pricing</a></li>
        </ul>
      </nav>
    </header>
    <div id="container">
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
    </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

Having Trouble with Centering in Bootstrap 4

I'm a bit puzzled about how to center the h2 element. This is my first time using Bootstrap 4, transitioning from Bootstrap 3, so maybe there's a difference that I'm not aware of? Any guidance on this matter would be greatly appreciated. Tha ...

Issue with Font-Awesome icons failing to display properly when using the BootstrapCDN

Trying to integrate Font-Awesome icon fonts using the BootstrapCDN link with what seems like the latest version: <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> The link has been added to the <hea ...

The right floating behavior with <li> elements seems to be malfunctioning

I am currently dealing with an issue in my code where it works perfectly in Firefox and IE 8, but in IE 7 the second list item is displaying with some extra space at the top instead of being on the same line. <LI style="PADDING-LEFT: 20px"> ...

Design all buttons, except for the two specific buttons

.button , button{ border:solid black 2px !important; } Using this code, I have added a border to all buttons. But now, I need to exclude the buttons "searchsubmit" and "single_add_to_cart_button" from having a border. How can I achieve that? ...

Conceal email address within HTML code

Similar Inquiry: What is the best way to hide an email address on a website? I've been contemplating ways to keep my email address secure on my website, including using AJAX to request it from the server after key validation and then simulating H ...

Concealing a button within a bootstrap template

I am attempting to conceal radio buttons in bootstrap using JavaScript. You can view the code on jsfiddle. setTimeout(function () { $("input[value='other']").parent().hide(); }, 1000); The hiding functionality works correctly, but there is ...

Transfer the address information to the billing address fields

I need assistance with copying the user's address to the billing address fields based on a checkbox value. Currently, when the checkbox is checked, only the last input field value is being copied over to the billing address section. It is crucial that ...

Creating a hexagon shape with CSS is a challenge that many designers face

Does anyone know how to create a hexagon shape using an image without setting it as the background image using CSS? I tried the code below and it worked perfectly, but there's an issue when viewing it in emails. The background image doesn't displ ...

The selector By.PARTIAL_LINK_TEXT in Selenium is failing to meet the expected condition

I am trying to store the friend list of a Facebook ID. When I click on the Friends tab on the profile, the friend list should be opened if it's not blocked; otherwise, something else should open. To determine if the friend list is opened or blocked, ...

Angular JS Profile Grid: Discover the power of Angular JS

I came across an interesting example that caught my attention: http://www.bootply.com/fzLrwL73pd This particular example utilizes the randomUser API to generate random user data and images. I would like to create something similar, but with manually ente ...

Challenges with HTML form text area

I am having an issue with my HTML form where the word "message" is displaying at the bottom-left of the textarea. How can I adjust it to appear on the top-left of the textarea? See image here - <form name="reg_form" method="post" actio ...

My .scss styles are getting jumbled up by Webpack

As I work with webpack, npm, and react.js, my style.scss stylesheet manages the styles of my project. However, I have been struggling to properly use classes and ids with it. The issue became clear when I noticed the transformation in my style.scss sheet: ...

Invoke a node.js function from within an HTML document

Seems like a recurring question. The closest solution I found was on this page: execute a Nodejs script from an html page? However, I'm still having trouble grasping it. Here's the scenario: I have an express server set up with the following ...

Utilizing a loop for setting variable values in JavaScript

Using both JavaScript and JQuery. Let's imagine there is an array called ListArray with various sentences inside. Sounds easy enough. Is there a way to achieve this? var List = for (var i = 0; i < 10; i++) { //iterate over an array here to c ...

Interactive email with a dynamic dropdown feature

I am currently working on creating an HTML email that can display data depending on the selection made from a dropdown menu within the email itself. Despite my research, I have not come across a suitable solution for this specific scenario. My current app ...

Center text within a div container

I am facing an issue with a nested div element: <div id="footer"> <div id="footer_text"> <p>My foooo text</p> </div> </div> The CSS code for the nested div is as ...

Resetting the quiz by utilizing the reset button

Hello everyone, I'm new to this platform called Stack Overflow. I need some help with my quiz reset button. It doesn't seem to be working as intended. According to my code, when the reset button is clicked at the end of the quiz, it should bring ...

struggling to locate the file index (I've attempted numerous solutions from various forums with no success...)

Apologies for asking a question that may have been asked numerous times before, but I'm encountering some issues and need your help... Thank you in advance! Here is the HTML form I am working with: <form action="action/actionindex.php" method="po ...

Tips for maintaining row position while zooming your browser above 100% with Bootstrap

I am experiencing a small issue with the bootstrap grid system. I have set up a row with two sections using col-md-6. However, when I zoom in my browser to over 100%, the Close label and input move to the next line. I want them to stay in the same place re ...

Utilize Tailwind CSS to design a triangular shape positioned in the lower right corner of the parent div

Is there a way to position a triangle in the bottom right corner of a parent div without it extending beyond the boundaries? I attempted to use tailwind css for this, but the sides of the triangle are crossing over the parent div. Any suggestions on how to ...