Eliminate the mystery overflow in your CSS styling

My website is experiencing overflow on the x axis, even though all vw/width properties are set to 100. I suspect that the .logout element may be causing this issue, but I'm not sure what exactly needs to be modified to resolve it. Any assistance would be greatly appreciated!

/* GLOBAL */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: monospace;
}

header {
    position: relative;
}

/* MAIN */
.logo {
    position: absolute;
    top: 25px;
    left: 40px;
    font-weight: 800;
    font-size: 2.5rem;
}

.container {
    background-color: black;
    width: 100vw;
    height: 100vh;
}

/* NAVBAR */
.nav {
    width: 100vw;
    height: 80px;
    background-color: #5f7866;
}
.navbar {
    position: absolute;
    top: 30px;
    left: 40%;
}

.items {
    display: flex;
    list-style: none;
}

.item {
    font-size: 2rem;
    margin-left: 20px;
    color: black;
    font-weight: lighter;
    transition: 0.2s;
}

.item:hover {
    transition: all 0.1s ease;
    transform: scale(0.98);
    border-bottom: 1px solid white;
}

.links {
    display: inline-block;
    text-decoration: none;
    color: black;
}

.logout {
    position: absolute;
    font-size: 2rem;
    border: 2px solid #49e31b;
    background-color: transparent;
    border-radius: 20px;
    padding: 10px 10px 10px 10px;
    top: -10px;
    left: 600px;
    transition: 0.3s;
    color: black;
    display: inline-block;
    text-decoration: none;
    overflow: hidden;
}

.logout:hover {
    background-color: #49e31b;
    cursor: pointer;
    transform: scale(0.98);
}
<!DOCTYPE html>
<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">
    <link rel="stylesheet" href="{% static 'css/index.css' %}">
    <title>{% block title %}  {% endblock title %}</title>
</head>
<body>
    <header class="nav">
        <h1 class="logo">Art Showcase</h1>
        <div class="navbar">
            <ul class="items">
                <li class="item">
                    <a class="links" href="{% url 'home' %}">Home</a>
                </li>
                <li class="item">
                    <a class="links" href="{% url 'about' %}">About</a>
                </li>

                <li class="item">
                    <a class="links" href="{% url 'register' %}">Register</a>
                </li>
                <li class="item">
                    <a class="links" href="{% url 'login' %}">Login</a>
                </li>
            </ul>
            <div>
                <a class="logout "href="{% url 'logout' %}">Logout</a>
            </div>
        </div>
    </header>

    <div class="container">
    {% block content %} 

    {% endblock content %}
    </div>

</body>
</html>

Answer №1

The problem lies in your approach to positioning and the faulty HTML structure (specifically, you neglected to close the navbar div after the ul element and opened a new div for the logout button). Refer to the corrected code below:

body {
  margin:0;
  padding:0;
  font-family:monospace;
}
.nav {
  display: grid;
  grid-template-columns:auto auto auto;
  grid-gap : 30px;
  background: #5f7866;
  padding:10px;
}

.navbar .items li {
  display:inline-block;
  list-style:none;
  font-size:2em;
  margin-right:10px;
}

.navbar .items li a {
  color:#000;
  text-decoration:none;
}

.item:hover {
    transition: all 0.1s ease;
    transform: scale(0.98);
    border-bottom: 1px solid white;
}

.logoutDiv {
  text-align:right;
}

.logout {
    font-size: 2rem;
    border: 2px solid #49e31b;
    background-color: transparent;
    border-radius: 20px;
    padding: 10px 10px 10px 10px;
    transition: 0.3s;
    color: black;
    display: inline-block;
    text-decoration: none;
}

.logout:hover {
    background-color: #49e31b;
    cursor: pointer;
    transform: scale(0.98);
}
<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">
    <link rel="stylesheet" href="{% static 'css/index.css' %}">
    <title>{% block title %}  {% endblock title %}</title>
</head>
<body>
    <header class="nav">
        <div>
          <h1 class="logo">Art Showcase</h1>
        </div>
        <div class="navbar">
            <ul class="items">
                <li class="item">
                    <a class="links" href="{% url 'home' %}">Home</a>
                </li>
                <li class="item">
                    <a class="links" href="{% url 'about' %}">About</a>
                </li>

                <li class="item">
                    <a class="links" href="{% url 'register' %}">Register</a>
                </li>
                <li class="item">
                    <a class="links" href="{% url 'login' %}">Login</a>
                </li>
            </ul>
           </div>
            <div class="logoutDiv">
                <a class="logout "href="{% url 'logout' %}">Logout</a>
            </div>
        </div>
    </header>

    <div class="container">
    {% block content %} 

    {% endblock content %}
    </div>

</body>
</html>

This solution should address the issue at hand.

Answer №2

Consider utilizing 100% instead of 100vw for a more effective approach...

The issue stems from the .logout class in your code

A quick resolution is to adjust your overflow settings

html , 
body{
    overflow-x: hidden;
 }

To fully address the CSS concerns, follow these guidelines:

/*GLOBAL */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: monospace;
}

header {
  position: relative;
}

/* MAIN */


.container {
  background-color: black;
  width: 100vw;
  height: 100vh;
}

/* NAVBAR */

.items {
  display: flex;
  list-style: none;
}

.item {
  font-size: 2rem;
  margin-left: 20px;
  color: black;
  font-weight: lighter;
  transition: 0.2s;
}

.item:hover {
  transition: all 0.1s ease;
  transform: scale(0.98);
  border-bottom: 1px solid white;
}

.links {
  display: inline-block;
  text-decoration: none;
  color: black;
}

.logout {
  font-size: 2rem;
  border: 2px solid #49e31b;
  background-color: transparent;
  border-radius: 20px;
  padding: 10px 10px 10px 10px;
  transition: 0.3s;
  color: black;
  display: inline-block;
  text-decoration: none;
  overflow: hidden;
  float: right;
}

.logout:hover {
  background-color: #49e31b;
  cursor: pointer;
  transform: scale(0.98);
}

.nav {
  width: 100vw;
  height: 80px;
  background-color: #5f7866;
  padding-inline: 5%;
}

.navbar  , .nav {
  display: flex;
  align-items: center;
}
.navbar > * {
  flex-basis: 100%;
}
.nav > * {
  flex-basis: 100%;
}

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

JavaScript function to toggle the visibility of navigation with a click of a button

I'm attempting to create a functionality in my code where the Open navigation button hides when the side navigation opens upon clicking. The desired behavior is for the openNav button to be hidden when it's clicked and then shown again when close ...

Collect the text shown in an alert message created with JavaScript

I'm currently unsure if this is achievable or not. In one of my scenarios, I aim to extract text that appears in alert boxes on external websites by including my JavaScript file. Is there a method to obtain the text content shown in an alert message ...

The footer should always be anchored at the bottom of the webpage, maintaining a consistent position regardless of any changes to the browser's

I've successfully implemented a footer with several buttons that remains positioned at the bottom of the page, 60px above the very bottom, regardless of the content or window size. The CSS I'm using is as follows: #container { min-height: 10 ...

HTML - PHP, navigating the history of the page with the browser's back button in real-time

My current project involves the development of a website using HTML and PHP. On one of my HTML pages, I have a form with certain fields marked as "REQUIRED". To ensure that all required fields are filled before submission, I have implemented a feature wher ...

What are some ways to customize the player for Youtube videos?

I'm looking to recreate the design of Youtube videos on my website when I embed them. The image below is just an example, but it reflects the simplicity I am aiming for. Any guidance or examples would be greatly appreciated, as I am unsure where to be ...

Updating the content with HTML and JavaScript

Hello everyone, I am currently working on a project to change the content of a div using JavaScript for educational purposes. Here is what I have done so far - <div id="navbar"> ... <ul> <li> <text onclick="getWordProcessing() ...

Navigation website with a twist

My design features a main navigation that is rotated 90 degrees, while the sub-menu remains horizontally aligned. <div id="nav"> <ul> <li><a href="#">Woonaccessoires</a></li> ...

Iterating over a range of values with _.each()

Can someone help me figure out why my syntax is incorrect for applying two values from different iteratees (day.classes and event.part) on line 5? <div class="days"> <div class="headers"> <% _.each(daysOfTheWeek, function(day) { %&g ...

Does setting white-space:nowrap enlarge the div?

My CSS code for div .name sets the width to 75% to truncate text if it doesn't fit. However, resizing the window causes the text to remain the same size and my entire website structure falls apart. This is the HTML code snippet: <tr> <t ...

Finding an element's id within a click event inside an iframe

<iframe id="myiframe" src="doc.html"> <button id="btn1"></button><!-- how do I retrieve this id? --> </iframe> $('#myiframe').on('click', function () { alert(this.id); }); I am trying to make it ...

Can an advertisement's appearance be altered to include custom content using only CSS?

Dealing with SAP for our ticket system has been quite a challenge due to the design that is causing some frustration. In an attempt to make it more tolerable, I used CSS to make some adjustments. However, my problem now lies in the fact that they keep maki ...

Building HTML tables with JQuery for handling large datasets of over 4,000 rows and beyond

My goal is to create a table with 4000+ rows and 18 columns displaying parsed pages from my site. I need it all on one page for the following reasons: I require the use of filters in the table I need to be able to sort by more than 5 columns Every cell ...

Bootstrap: Problem arises when columns do not total up to 12 and are pushed onto separate rows

So, I've been working with Bootstrap to structure columns and rows. It seems like I have the container, rows, and columns set up correctly for each post. However, I am puzzled as to why a new row is created for every post I make, causing them to stack ...

Tips on how to lock the ag-grid to the highlighted row's position and force it to scroll

We have integrated ag-grid into our system, and I am facing an issue with displaying the scrollbar in a child window based on the selected row position in ag-grid. Please refer to the Plunkr link below and click on source_id which will lead you to an edit ...

"Optimizing Image Display in Web Browsers

Currently, I am using JavaScript to dynamically size a transparent .gif on my website. The original image size is approximately 200x200 pixels, but it's usually resized to be between 600x600 and 800x800. When viewing the resized image in IE8 and FF3, ...

What are your thoughts on combining a JSON object with HTML?

When using ASP.NET MVC, it is possible to return a JSONResult. return JSON(new { View = RenderViewAsString("MyView", model), wasSuccessful = true}) This approach combines HTML and data in a single JSON object. The goal is to utilize strongly typed HtmlHe ...

Displaying divs depending on dropdown selection - Troubleshooting script issue

When I try to display certain divs based on dropdown selection using the script below, it works fine on a simple page. However, when I implement it on my current development page, it completely messes up the layout, turning everything black and adding stra ...

Struggling to add a border to an HTML div element

I'm completely baffled as to why I can't seem to add a border around my div. Here is the link to my jsfiddle: http://jsfiddle.net/4HnKs/1/ It's possible that I've been staring at my computer screen for too long, but no matter how hard ...

Creating a scrollable nested div on a mobile website

Is there a way to achieve a scrollable nested div on a jQuery mobile site? I am aiming for a fixed header and footer with the middle section being scrollable. Despite my attempts to set overflow:scroll (along with specifying the width and height of the div ...

Determine if a div contains an svg element with JavaScript

My question involves a div containing a question mark and some SVG elements. Here is the initial setup: <div id="mydiv">?</div> When validating a form submission in my code, I check if the div text contains a question mark like this: const f ...