What is causing the malfunction of the position within this particular section?

On my website, I have a specific section where I want to showcase four products with arrows on both sides for navigation. However, I am facing an issue with the positioning of the elements. Can someone take a look and help me figure it out? Check out this image for reference.

Additionally, there's also an issue with the background color not filling the space properly around the items and buttons. Any insights on why this is happening?

Edit: See how I envision the layout to look like in this picture.

HTML:

<section class="one">
        <div><span class="title">New products</span></div>
        <br>
        <button class="left">
            <span class="Lmain"></span>
            <span class="Lside"></span>
        </button>
        <div class="items">
            <div class="item">
                <a href="">
                    <img class="itemImg" src="../Images/Image1.png" alt="Picture of the product">
                </a>
                <div><span class="desc">Description about that specific item that is being showen to you above this text
                        right here</span></div>
            </div>
            <div class="item">
                <a href="">
                    <img class="itemImg" src="../Images/Image2.png" alt="Picture of the product">
                </a>
                <div><span class="desc">Description about that specific item that is being showen to you above this text
                        right here</span></div>
            </div>
            <div class="item">
                <a href="">
                    <img class="itemImg" src="../Images/Image3.png" alt="Picture of the product">
                </a>
                <div><span class="desc">Description about that specific item that is being showen to you above this text
                        right here</span></div>
            </div>
            <div class="item">
                <a href="">
                    <img class="itemImg" src="../Images/Image4.png" alt="Picture of the product">
                </a>
                <div><span class="desc">Description about that specific item that is being showen to you above this text
                        right here</span></div>
            </div>
        </div>
        <button class="right">
            <span class="Rmain"></span>
            <span class="Rside"></span>
        </button>
    </section>

CSS:

.title {
        text-align: center;
        position: absolute;
        margin: auto;
        border: 1px solid goldenrod;
        font-size: 40px;
    }

    .one {
        background-color: hotpink;
        position: relative;
    }

    .two {
        background-color: rgb(255, 0, 128);
    }

    /*items appearance*/
    .items {
        position: relative;
        margin: auto;
    }

    .item {
        border: 1px solid rgb(255, 170, 0);
        float: left;
        position: absolute;
        top: 0px;
        margin: 0px 8px;
        left: 12%;
        width: 350px;
        height: auto;
    }

    .itemImg {
        width: 100%;
        height: auto;
    }

    /*end of item appearance*/

    .right {
        right: 0px;
        top: 0px;
        position: absolute;
    }

    .left {
        left: 0px;
        top: 0px;
        position: absolute;
    }

Answer №1

To create a simple layout, use flexbox:

body {
  margin: 0;
  background: #1a1a1a;
  color: #fff;
  font-family: sans-serif;
}

h1#title {
  padding: 1rem;
  text-align: center;
}

main {
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#products {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  margin: 0;
}

.product {
  padding: 2rem 1rem;
  background: #fff;
  color: black;
  margin: .5rem;
}
<body>
  <h1 id="title">Items</h1>
  <main>
    <div class="arrow" id="arrow-left">&#8592;</div>
    <div id="products">
      <div class="product">
        Product
      </div>
      <div class="product">
        Product
      </div>
      <div class="product">
        Product
      </div>
      <div class="product">
        Product
      </div>
    </div>
    <div class="arrow" id="arrow-right">&#8594;</div>
  </main>
</body>

Answer №2

Below are some issues I have identified:

  • The .title element is set to position:absolute, causing it not to take up space and affecting the positioning of other elements. The solution is to remove position: absolute
  • The .title class belongs to a <span> element, which is an inline element that does not behave like block-level elements (<div>, <p>, <h1>, etc.). Therefore, the text-align: center property doesn't work as intended. To fix this, remove the span and assign the .title class to the parent <div>
  • The left and right buttons should be enclosed in a <div> with position:relative

For updated code, please refer to this jsfiddle link.

I haven't corrected the image positioning because it seems like they will be adjusted by JavaScript. However, relying on image dimensions for positioning is not a reliable method. I recommend using flexbox, which is widely supported by browsers. More information on flexbox can be found here.

You may also want to check out @yerme's example or refer to this guide on flexbox.

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

Utilizing jQuery to apply a class to an element and modify the CSS parameters with custom values simultaneously

Unsure if this idea is feasible, but I'll attempt to explain my requirements. I am interested in accomplishing something like the following: A CSS class with variables X, Y, and Z left undefined: .r {border-radius:Xpx;-webkit-border-radius:Ypx;-moz ...

Removing cookies after sending a beacon during the window unload event in Chrome

Here's the situation: I need to make sure that when the browser is closed or the tab is closed, the following steps are taken: Send a reliable post request to my server every time. After sending the request, delete the cookies using my synchronous fu ...

Tips on how to stop a webpage from scrolling and instead allow only the inner content to scroll

Check out my plunker for reference. The goal I am aiming for is as follows: The webpage should stay fixed and not scroll in any direction If the content overflows its container, a scroll bar should appear strictly within that container I have a main co ...

Responsive div that reacts to scrolling

I am looking for a div that can dynamically change its position as I scroll up or down the page. For example, it could start 30px from the top but then move to 100px as I scroll down further. This div will be located on the right-hand side of my page, clo ...

What is the best way to position three DIVs next to each other within another DIV while aligning the last DIV to the right?

I need help formatting a simple list item with three DIVs. The first DIV should be left justified, the second should be able to grow as needed, and the third should be right justified. I currently have them stacked side by side, but can't get the last ...

Strategies for implementing a minimum height for grid-template-column containers

Here's an issue I'm facing: when the first container has content, the other two containers without content end up being the same height as the first one: Check out this JSFiddle link for reference .grid { padding: 5 ...

Intrigued by the prospect of making HTML elements vanish or smoothly glide along with the content without obstructing it

I'm a beginner in coding and I want to make my website impressive. Right now, I have an HTML embedded element called "On this Page" that I would like to disappear when the user scrolls down the page. I've managed to position the element on the ...

Navigate to the following div, navigate back to the previous div

I am attempting to implement a div navigation system with next/previous buttons. Despite searching extensively on Google, I have not found the exact solution I am looking for. First and foremost, I want to maintain the integrity of my html structure. < ...

The use of htmltools::tags$iframe in Shiny to display an HTML file is not functioning properly when combined with renderUI()

Hello everyone, this is my first question on StackOverflow. I hope you can help me out even though my question may not be in the correct format. I have an HTML widget that was created using flowmapblue.R and saved as a file that I want to display in a Mar ...

Emphasize cells and headers when a cell's value deviates from its initial value

I implemented cell editing in my primeng table. Here is the code: <div class="card"> <p-table [value]="products" dataKey="id" [tableStyle]="{ 'min-width': '50rem' }"> <n ...

Launching a program through a web browser - a step-by-step guide

I am looking to create a specific sequence of events: A web browser opens, and a user logs in (the exact action is not crucial) The user is then redirected to a new page where another program should automatically open This process is similar to what happ ...

Unlimited Possibilities in Designing Shared React Components

Seeking the most effective strategies for empowering developers to customize elements within my React shared component. For example, I have a dropdown and want developers to choose from predefined themes that allow them to define highlight color, font siz ...

Updating the navigation with a dynamic CSS class using jQuery

Hey there, I'm on the lookout for a simple and discreet method to dynamically add an "active" class to a navigation menu based on the current directory. Unfortunately, I am unable to modify the body tag with a custom class, ruling out a pure CSS solut ...

HTML/CSS: Issue with image grid layout where images of different heights are not wrapping correctly

I have a Wordpress-based web project in progress at the following link: The current setup of the post thumbnails grid on the website was done with the assumption that all thumbnails will be the same height. However, I am looking to modify it so that the t ...

How to read a text file in JavaScript line by line

Coding Challenge: <script src="../scripts/jquery-3.1.0.min.js"></script> <script> $(document).ready(function(){ $('#test').click(function(){ var txtFile = '../data/mail.txt'; var file ...

Is it necessary to bring in CSS for the following page?

Should CSS be imported and used across multiple pages in a web application? Is the page structure outlined below considered acceptable, or is there a standard practice to follow? /pages /home index.js index.module.css /cart ...

The CSS is not displaying correctly on Safari and Chrome browsers in Mac OS

I'm having trouble with CSS not loading properly on Apple devices for a website I made. Despite maintaining all media query statements and style sheets separately, the display is not correct in MAC OS safari and chrome. However, everything looks fine ...

The color of the letters from the user textbox input changes every second

My task is to create a page where the user enters text into a textbox. When the user clicks the enter button, the text appears below the textbox and each letter changes color every second. I am struggling with referencing this jQuery function $(function() ...

How can I create two buttons on one line in a Struts2 form?

My form has two buttons - one for registering and the other for canceling the form. I created them using the following code: <s:submit name="cancel" key="project.button.cancel" /> <s:submit name="login" key="form.register.registerBtn" /> How ...

Pressing JavaScript buttons to trigger another button

I am looking to develop a script that generates a button which then creates another button. The subsequent button will be assigned an id attribute by incrementing from 1 to infinity. This feature should apply to all buttons (original button and the newly ...