Having difficulty positioning the image on the left side

Learning CSS can be a challenge, but I recently made progress by using display flex to create a navigation bar. However, I encountered an issue where the background image for the navbar doesn't align with the left side of the screen and results in unwanted scroll bars. Here is the code snippet:

.container {
  width: 100%;
  background-color: rgba(79, 79, 79, 0.13);
  box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 50px;
}

nav ul li {
  display: inline-block;
  margin: 0 15px;
}

.logo {
  width: 150px;
  height: auto;
  cursor: pointer;
}

.bg-img {
  margin: 0;
  opacity: 0.9;
  position: absolute;
  z-index: -1;
  justify-content: center;
}
<div class="container">
  <img src="files/images/logo.png" alt="LOGO" class="logo">
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/">Second</a></li>
      <li><a href="/">Third</a></li>
    </ul>
  </nav>

  <img src="files/images/top-bg.jpg" alt="bg" class="bg-img">

</div>

Answer №1

https://i.sstatic.net/cRrPs.pngWhen it comes to the css code, this is what will make it work:

body {
    margin: 0;
    padding: 0;
}

.container {
    width: 100%;
    height: 75px;
    background-color: rgba(79, 79, 79, 0.13);
    box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

nav ul li {
    display: inline-block;
    margin: 0px 20px;
}

nav ul {
    margin-right: 50px;
}

.logo {
    width: 150px;
    height: auto;
    cursor: pointer;
    margin-left: 50px;
}

.bg-img {
    margin: 0;
    top: 0;
    left: 0;
    height: 75px;
    width: 100vw;
    opacity: 0.9;
    position: absolute;
    z-index: -1;
    justify-content: center;
}

The issue arises from the fact that a fixed padding cannot be set for the navbar due to the presence of the background image. The background image also needs to have a fixed height in order to align correctly. It is recommended to set the navbar height to a fixed value of 75px for optimal results.

Answer №2

.container {
  width: 100%;
  background-color: rgba(79, 79, 79, 0.13);
  box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 50px;
}

nav ul li {
  display: inline-block;
  margin: 0 15px;
  
}
.lp {
  float: right;
  
}

.logo {
  width: 150px;
  height: auto;
  cursor: pointer;
}

.header {
  background-image: url("files/images/top-bg.jpg");
  display: flex;
}
/* additional styling for the header background */
<div class="container">
<div class="header">
  <img src="files/images/logo.png" alt="LOGO" class="logo">
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/">Second</a></li>
      <li><a href="/">Third</a></li>
    </ul>
  </nav>
</div>
</div>

Answer №3

*{box-sizing:border-box;}

This rule applies to every element on the page, ensuring that their height and width are accurately calculated without any overflow.

Answer №4

For the initial image adjustment, I made some changes to its positioning to ensure it aligns perfectly with the left edge of the navbar. By increasing the padding of the background image to 18.5px, I ensured that it doesn't overlap with the logo. As for the scrollbar, I included both overflow: hidden and box-sizing: border-box properties to hide the scrollbar and maintain all elements within a unified space.

It appears that the use of display: flex was interfering with the float property and overall positioning - just something to consider.

.container {
  width: 100%;
  background-color: rgba(79, 79, 79, 0.13);
  box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 50px;
  box-sizing: border-box;
  overflow: hidden;
}

nav ul li {
  display: inline-block;
  margin: 0 15px;
}

.logo {
  width: 150px;
  height: auto;
  cursor: pointer;
  position: relative;
  left: -40px;
}


.bg-img {
  padding: 18.5px;
  opacity: 0.9;
  position: sticky;
  right: 1080px;
  top: -10px;
  float: left;
  z-index: -1;
  justify-content: center;
}
<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <div class="container">
  <img src="files/images/logo.png" alt="LOGO" class="logo">
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/">Second</a></li>
      <li><a href="/">Third</a></li>
    </ul>
  </nav>

  <img src="files/images/top-bg.jpg" alt="bg" class="bg-img">

</div>
    </body>
</html>

Hopefully, this advice proves helpful. (:

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

choose courses using jQuery

<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <p class="myChild">one</p> <p class="myChild">two</p> <p class="myChild">th ...

Tips for integrating CSS with Material UI TableContainer

I have utilized Material UI Grid to display data in a chart. However, the appearance of the chart does not match my expectations. Instead of resembling the desired "dense table," it looks different: Actual Look of the Chart Below is the code snippet I ha ...

Discovering the required rotation angle for an object to directly face another using JS HTML5 CANVAS

Hey there, I'm currently working on a game project in Javascript where I have a player and a cursor. I already know the positions of both objects, but what I need help with is determining the angle in degrees or radians that the player needs to rotate ...

Guide to designing a dropdown navigation menu in GWT

Hey, I'm currently working on setting up a drop down menu using GWT and here's the layout I have in mind: | Categories | Pictures | Other | When the categories dropdown opens, my goal is to display the categories grouped in pairs. For example: ...

The process of combining a CssStyleCollection with a System.Web.UI.WebControls.Style

I am working with a list item and trying to apply styles using System.Web.UI.WebControls.Style. However, I noticed that there isn't a MergeStyle option similar to what's available for controls. Instead, there is an Attributes.CssStyle property of ...

What is the significance of using HttpUtility's HtmlDecode function in my code?

One of my ASP.NET MVC controllers returns a view where I generate some HTML code. For example: @Html.ActionLink(HttpUtility.HtmlDecode("&iquest;Olvid&oacute; su contrase&ntilde;a&#63;"), "ForgotYourPassword", "Account", null, new { style = ...

Tooltip Bootstrap timing

I am currently working on creating a navigation bar with icon-only buttons that display tooltips when touched or tapped. Here is the code I have implemented: $('a[rel="tooltip"]').tooltip({ animated: 'fade', placement: ' ...

Setting up button alignment in Bootstrap

In my current template, I am utilizing Bootstrap and need to position three buttons accordingly. The first button should be all the way to the left, the second in the middle, and the third (final) one on the right within a container element. While attempt ...

What is the process for implementing hover transitions on link elements?

I am trying to incorporate a transition effect for my links. Essentially, I want the text-decoration (which is currently set to underlink) to gradually appear. Unfortunately, my previous attempt at achieving this has been unsuccessful: #slink{ transit ...

Achieving True Nested Divs: Making Children Truly Inside

I want to create nested divs for positioning children with top and left properties, allowing them to overlap each other: https://jsfiddle.net/e0cpuarv/ .boo { position: absolute; left: 10px; top: 10px; width: 100px ...

Conceal the div element when located beneath an ordered list with a designated

I need help hiding the display of comment information if it is a child comment. Below is my attempt to hide the div with the id "info" if the "ol" element has a class of "children". If you have another method for hiding the div when the comment is a chil ...

What is the process for utilizing Angular's emulated encapsulation attributes on HTML elements that are dynamically added to an Angular component?

Within my custom Angular component, a third-party library is dynamically adding an HTML element. I am seeking a solution that can apply Angular's encapsulation attributes to these elements regardless of the specific third-party library being used. If ...

What is the best way to conceal part of a chart or a div in Angular?

I have a large chart that I need to showcase on my website, but due to its size it would take up too much vertical space and potentially overwhelm the user upon their first visit. My goal is to provide a preview of the chart, similar to an excerpt of text ...

Issue with form submission using getElementById

I am struggling to submit a form that is generated by PHP echo. The getElementById function doesn't seem to be functioning properly. <?php include 'connect.php' ; $sql=mysql_query("SELECT mess_id,receiver,subject ...

Issue with Element's Response to JQuery Click Event

After pulling elements from the database in PHP, I utilized Jquery to add Elements to the page. Each button has two classes; one for controlling the GUI and another for handling the click event of that specific button. Here is the code snippet: echo " ...

Which is the better option for opening a link in a button: using onclick or href?

What is the most effective way to open a link using a button? <button type="button" onclick="location='permalink.php'">Permalink</button> <button type="button" href="index.php">Permalink</button> ...

Navigating to User's Specific Info Page using Node.js

Would love some input on my current issue. I have a table featuring a list of users, and my goal is to display user information in detail whenever a user (which corresponds to a row in the table) is clicked. The process involves identifying which user was ...

Choose an option from the drop-down menu and transfer the selected value to the following page using a hidden

Struggling to capture the selected value from a combo box and set it to a hidden field in order to retrieve it on the next page. Need some assistance with this task. Here is my form tag: <form class="well" name="ddm" id="ddm" style="margin-left: 30px; ...

Looking to retrieve the value of an input element within an ng-select in Angular 6?

Currently, I am working on a project where I aim to develop a customized feature in ng-select. This feature will enable the text entered in ng-select to be appended to the binding item and included as part of the multiselect function. If you want to see a ...

Particular arrangement of a table

I am looking to create a responsive table that has a vertical left header row on mobile devices and a normal horizontal header row on desktop, like the following: Header 1 data data data Header 2 data data data Header 3 data data data Does anyone have ...