Enabling Flexbox elements to come together as screen size decreases

Struggling with making my flexbox navbar responsive and in need of some help. I want the padding between links to decrease as the page width shrinks, but can't figure out how to prevent the "Contact Us" link from being hidden by the browser window when it gets small (see photo below).

I'm wondering if there's a way to achieve this without using media queries or if I'm just overcomplicating things for myself. Any experts out there who can lend a hand? I've already wasted too many hours on this!

Screenshot of phone-sized screen


<!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="/CSS/style.css" />
    <title>Home Page</title>
  </head>
  <body>
    <header>
      <img src="Images/logo.png" alt="logo" id="logo" />
      <!--Logo-->
      <nav>
        <ul class="nav_links">
          <li><a href="./index.html">Home</a></li>
          <li><a href="./merchandise.html">Merchandise</a></li>
          <li><a href="./about.html">About</a></li>
          <li><a href="./contactUs.html">Contact Us</a></li>
        </ul>
      </nav>
    </header>
  <div class="flex-container">
    <div class="column">
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui itaque
        repellat suscipit voluptas, ad rerum corrupti incidunt assumenda, fugit
        amet, recusandae officiis unde similique ullam. Quos magni cupiditate
        omnis dignissimos. Repudiandae architecto alias odio modi neque
        asperiores veritatis dicta ipsum! Enim cupiditate laborum voluptatem
        ipsa incidunt optio nulla facilis natus?
      </p>
    </div>
  </body>
  <div class="column">
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui itaque
      repellat suscipit voluptas, ad rerum corrupti incidunt assumenda, fugit
      amet, recusandae officiis unde similique ullam. Quos magni cupiditate
      omnis dignissimos. Repudiandae architecto alias odio modi neque asperiores
      veritatis dicta ipsum! Enim cupiditate laborum voluptatem ipsa incidunt
      optio nulla facilis natus?
    </p>
  </div>
  <div class="column">
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui itaque
      repellat suscipit voluptas, ad rerum corrupti incidunt assumenda, fugit
      amet, recusandae officiis unde similique ullam. Quos magni cupiditate
      omnis dignissimos. Repudiandae architecto alias odio modi neque asperiores
      veritatis dicta ipsum! Enim cupiditate laborum voluptatem ipsa incidunt
      optio nulla facilis natus?
    </p>
  </div>
</div>
</html>

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-image: linear-gradient(to top, #ddf2eb, #d3cdd7);
  background-repeat: no-repeat;
  height: 100vh;
}

header {
  display: flex;
  padding: 10px 0;
  background-color: #ddf2eb;
  align-items: center;
}

#logo {
  height: 70px;
  width: 70px;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: 2%;
}

.nav_links {
  list-style: none;
  margin-left: 10%;
  white-space: nowrap;
  min-width: 0;
  min-height: 0;
  max-width: 800px;
}

.nav_links li {
  display: inline-block;
  padding: 0px 20px;
}

.nav_links li a {
  transition: all 0.3s ease 0;
  font-family: "Montserrat", "sans-serif";
  font-weight: 500;
  font-size: 15px;
  color: #606d5d;
  text-decoration: none;
}

.nav_links li a:hover {
  color: honeydew;
}

.flex-container {
  display: flex;
}

Answer №1

If you're looking to create a responsive design without much hassle, consider using justify-content: space-between. This property is flexible and ensures that elements are evenly spaced across the container. Applying this to the header will create space between the logo and navigation, while adding it to the .nav-links class will distribute space between the navigation links as desired.

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  background-color: #ddf2eb;
}

.nav_links {
  display: flex;
  justify-content: space-between;
  align-items: center;
  list-style: none;
  max-width: 800px;
}

Answer №2

One possible solution is as follows:

@media (max-width:600px) {
  .nav_links li {
  padding: 0px 3px;
}
    .nav_links li a {
    font-size: 11px;
  }
}

Link: https://codepen.io/en0ndev/pen/WNoWwOo

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-image: linear-gradient(to top, #ddf2eb, #d3cdd7);
  background-repeat: no-repeat;
  height: 100vh;
}

header {
  display: flex;
  padding: 10px 0;
  background-color: #ddf2eb;
  align-items: center;
}

#logo {
  height: 70px;
  width: 70px;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: 2%;
}

.nav_links {
  list-style: none;
  margin-left: 10%;
  white-space: nowrap;
  min-width: 0;
  min-height: 0;
  max-width: 800px;
}

.nav_links li {
  display: inline-block;
  padding: 0px 20px;
}

.nav_links li a {
  transition: all 0.3s ease 0;
  font-family: "Montserrat", "sans-serif";
  font-weight: 500;
  font-size: 15px;
  color: #606d5d;
  text-decoration: none;
}

.nav_links li a:hover {
  color: honeydew;
}

.flex-container {
  display: flex;
}

@media (max-width:800px) {
  .nav_links li {
  padding: 0px 10px;
}
}

@media (max-width:700px) {
  .nav_links li {
  padding: 0px 5px;
}
    .nav_links li a {
    font-size: 13px;
  }
}

@media (max-width:600px) {
  .nav_links li {
  padding: 0px 3px;
}
    .nav_links li a {
    font-size: 11px;
  }
}
<!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="/CSS/style.css" />
    <title>Home Page</title>
  </head>
  <body>
    <header>
      <img src="Images/logo.png" alt="logo" id="logo" />
      <!--Logo-->
      <nav>
        <ul class="nav_links">
          <li><a href="./index.html">Home</a></li>
          <li><a href="./merchandise.html">Merchandise</a></li>
          <li><a href="./about.html">About</a></li>
          <li><a href="./contactUs.html">Contact Us</a></li>
        </ul>
      </nav>
    </header>
  <div class="flex-container">
     <div class="column">
       <p>
         Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui itaque
         repellat suscipit voluptas, ad rerum corrupti incidunt assumenda, fugit
         amet, recusandae officiis unde similique ullam. Quos magni cupiditate
         omnis dignissimos. Repudiandae architecto alias odio modi neque
         asperiores veritatis dicta ipsum! Enim cupiditate laborum voluptatem
         ipsa incidunt optio nulla facilis natus?
       </p>
    </div>
  </body>
  <div class="column">
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui itaque
      repellat suscipit voluptas, ad rerum corrupti incidunt assumenda, fugit
      amet, recusandae officiis unde similique ullam. Quos magni cupiditate
      omnis dignissimos. Repudiandae architecto alias odio modi neque asperiores
      veritatis dicta ipsum! Enim cupiditate laborum voluptatem ipsa incidunt
      optio nulla facilis natus?
    </p>
  </div>
  <div class="column">
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui itaque
      repellat suscipit voluptas, ad rerum corrupti incidunt assumenda, fugit
      amet, recusandae officiis unde similique ullam. Quos magni cupiditate
      omnis dignissimos. Repudiandae architecto alias odio modi neque asperiores
      veritatis dicta ipsum! Enim cupiditate laborum voluptatem ipsa incidunt
      optio nulla facilis natus?
    </p>
  </div>
</div>
</html>

Answer №3

.menu_items {
    display: flex;
    list-style: none;
    margin-left: 15%;

    min-width: 0;
    min-height: 0;
    max-width: 1000px;
}

.menu_items li {
    display: inline-block;
    padding: 0 1.2vw;
}

Make these changes instead. The issue was caused by the white space: nowrap property and the use of pixel-based padding which is not responsive, so I switched it to vw units.

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

Using Angular 4, you can dynamically insert values into an inline style through interpolation

I am currently teaching myself Angular 4 and have a question regarding the following code snippet: <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="0" aria- valuemin="0" aria-valuemax="100" style="width {{ it ...

Block Button styles not displaying in IE when set to Full Width

I can't figure out why, but it seems to be functioning properly on every other internet platform except this one. The first picture shows the button in IE, the second contains the code, and the third displays the button in Chrome. https://i.sstatic. ...

Ensure that div2 remains aligned with the left, bottom, and right edges of the screen, while also maintaining a consistent distance of 400px below div

Currently, I am faced with a challenge involving two divs: one positioned at the top (div1) and the other at the bottom (div2). The goal is to ensure that div2 maintains a consistent distance of 400px below div1 while also aligning its left, bottom, and r ...

What's causing my CSS layout to get disrupted by this PHP script?

This website utilizes the $_GET method to extract an asset ID and query a MySQL database for information retrieval. If the 'id' does not correspond to any record, no results are displayed but the page layout remains unaffected. However, if &apos ...

Organizing CSS elements for input into a database

SCENARIO: In my current project, I am working on a CMS website where each user's custom CSS is stored in a database table with specific fields: int id (primary key) int site_id (index) string selector string property strin ...

Converting a Multidimensional Array from Jquery to HTML

In an attempt to arrange pieces on the board in a specific order, I have shared the following code. The idea is to move the pieces by clicking the buttons below the board, with a total of eight moves. I plan to implement fast-forward and rewind buttons as ...

Creating a split button can be enhanced by incorporating a disabled button with a tooltip feature

I am attempting to create a split button using Material UI components such as ButtonGroup and Button. You can find more information about split buttons here. The issue I am facing is that the first button may need to be disabled and display a tooltip when ...

Concentrate on Input in the Middle of the Text

Is it feasible to target a specific character within an input using JavaScript/jQuery for focus? Consider this input field: Would it be achievable to focus on the element and position the cursor after the first sentence, as an illustration? ...

The pseudo class before is experiencing issues with background color in Safari and not functioning as

Issue with Pseudo class before not displaying background colour in Safari browser To view the code, please visit the following link here HTML <div id='test'> <p>Test </p> </div> CSS #test { position: relative; per ...

Learn how to send dynamic data to another HTML element using Ajax's success function

I received a successful ajax response from one HTML file, but I'm struggling to dynamically set the data from this response to another HTML file. Can anyone help me with this issue? Here is the code snippet from my ajax report: success: function( ...

The art of selecting elements and attaching event listeners in a React environment

Currently, I am working on my portfolio website using Gatsby. The layout includes a sidebar on the left with navigational links (Home, About, Work, etc.), and the main content is displayed as one long strip of sections on the right. When a user clicks on a ...

Using jQuery to generate a JSON object dynamically based on the values entered in each input field

I'm facing a situation where I need to extract data from a JSON format using PHP. However, I'm struggling with how to structure the Javascript object in order to dynamically create the JSON format. Here is my current scenario: <input title=" ...

Transform a Div element into an image, ensuring compatibility with Internet Explorer 7 and 8

Currently, I am utilizing the jqplot charting library to create charts in my ASP.NET MVC application. Within a div element, the chart is being rendered. The goal is to convert this div element into an image and export it to a PDF document. The jqplotToIm ...

Increase the nestling of bullet points with inner indentation <li>

Hey there, I'm currently facing an issue with the right-side indentation of the bullet points. I have included a code snippet and an image that should help clarify the situation: https://i.sstatic.net/vmOVc.png <h1 style="display: block;marg ...

"Enhance your website design with a navbar that seamlessly blends with the background color

Question 1: I have a requirement for my navbar to affix overlay on top of the background color and image of the div (top-banner). Currently, when I scroll down, the background color overlays my navbar affix instead. How can I ensure that my navbar sta ...

Ways to conceal text as a div moves over it during hover effects?

How can I hide the title when hovering over the first child in my preview div items? I want to change the opacity of the title to zero on hover. Is there a simple way to achieve this effect? Currently, the layout looks like this - <div className="col ...

Creating a WordPress Infographic: How to Design a "Wide" Graphic

I have a question for those who are experienced with infographics. I'm in the process of getting an infographic designed for my blog and I've noticed that some websites have a feature that allows the infographic to expand to full width when you c ...

I am having trouble getting the div tag to display its content when I try to access it through jQuery

Here is the HTML code: <div class="col-md-12"> <span>Please Choose a Payment Method</span> <select class="form-control" name="select1" id="select1" onchange="showPaymentTextbox()"> <option value="x"> ...

The resource could not be loaded: net::ERR_FILE_NOT_FOUND error encountered when trying to load <img> tag

I attempted to insert an image using the 'img html tag' on my page (index.html). The image is located in the folder (imgs) within my project directory (Book_Store). Despite my efforts, I encountered this error message: Failed to load resource: ...

Issue with click event for submit button in ASP.Net following a change in dropdown list index using JavaScript

Currently, I have an asp.net application where a confirmation popup alert is displayed when the selected index changes to a specific value ("Cancelled") in a dropdown list. <asp:DropDownList ID="ddlStatus" runat="server" CssClass="selectstyle" DataText ...