The Flex-Direction property set to column is not applying correctly within a @media query

I'm still in the process of learning how to code, so please bear with me. I am attempting to create a navigation bar that, on desktop, will be displayed horizontally on the right side of the screen. However, when viewing on a device smaller than 800px, I want it to switch to a vertical column layout instead of a horizontal row. Unfortunately, whenever I try to implement this, it continues to display as a row even below 800px. Any assistance or guidance would be greatly appreciated!

body {
    background: #135e46;
}

div {
    background: #73a788;
    display: flex;
    justify-content: flex-end;
    padding: 20px;
    font-size: 1em;
    flex-direction: row;
}

a {
    color: #e9d0bd;
    text-decoration: none;
    font-size: 1.2em;
    margin: 10px;
}

@media(max-width:800px){

body {
    background: #135e46;

}

div {
    background: #73a788;
    flex-direction: column;
    display: flex;
    font-size: 1em;
    align-items: center;
    margin-top: -10px;
    max-width: 100%;
    overflow-x: hidden;
    padding: 20px;
}

a {
    color: #e9d0bd;
    text-decoration: none;
    font-size: 1.2em;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>
        Title
    </title>
    <link rel="stylesheet" href="items/styles.css">
</head>
<body>
    <div>
        <nav>
            <a href="#">Home</a>
            <a href="#">Portfolio</a>
            <a href="#">Education</a>
            <a href="#">Resume</a>
            <a href="#">Contact</a>
        </nav>
    </div>
</body>
</html>

Answer №1

To display the a elements in a column within your navigation menu, you must add the following CSS properties to your nav element: display:flex and flex-direction:column.

If your nav element has only one child, it may appear unchanged visually unless you consider the main axis. More information on flex-direction can be found at this link.

For further help with using flexbox, visit this resource.

In relation to media queries, you can adjust the positioning of your navbar by utilizing justify-content (for horizontal alignment) or align-items (for vertical alignment). Ensure that your container is large enough if you intend to move your navbar vertically.

It's unnecessary to duplicate properties already defined outside of your media queries. Media queries serve as an overlay system that alters styles based on the specified conditions, often screen size.

body {
    background: #135e46;
}

div {
    background: #73a788;
    display: flex;
    justify-content: flex-end;
    padding: 20px;
    font-size: 1em;
    flex-direction: row;
}

a {
    color: #e9d0bd;
    text-decoration: none;
    font-size: 1.2em;
    margin: 10px;
}

@media(max-width:800px){

body {
    background: #135e46;

}

div {
    justify-content:center;
    margin-top: -10px;
    max-width: 100%;
    overflow-x: hidden;
}

nav{
  display:flex;
  flex-direction:column;
}


}
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>
        Title
    </title>
    <link rel="stylesheet" href="items/styles.css">
</head>
<body>
    <div>
        <nav>
            <a href="#">Home</a>
            <a href="#">Portfolio</a>
            <a href="#">Education</a>
            <a href="#">Resume</a>
            <a href="#">Contact</a>
        </nav>
    </div>
</body>
</html>

Answer №2

you should modify the a tag to be styled as a block element instead of its default inline display

body {
    background: #135e46;
}

div {
    background: #73a788;
    display: flex;
    justify-content: flex-end;
    padding: 20px;
    font-size: 1em;
    flex-direction: row;
}

a {
    color: #e9d0bd;
    text-decoration: none;
    font-size: 1.2em;
    margin: 10px;
}

@media(max-width:800px){

body {
    background: #135e46;

}

div {
    background: #73a788;
    flex-direction: column;
    display: flex;
    font-size: 1em;
    align-items: center;
    margin-top: -10px;
    max-width: 100%;
    overflow-x: hidden;
    padding: 20px;
}

a {
    color: #e9d0bd;
    text-decoration: none;
    font-size: 1.2em;
    display:block /* added this rule */
}
  

}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8>
    <title>
        Title
    </title>
    <link rel="stylesheet" href="items/styles.css">
</head>
<body>
    <div>
        <nav>
            <a href="#">Home</a>
            <a href="#">Portfolio</a>
            <a href="#">Education</a>
            <a href="#">Resume</a>
            <a href="#">Contact</a>
        </nav>
    </div>
</body>
</html>

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 URL parameters in Node.js applications

I am encountering an issue with my nodejs setup, and I am hoping someone can assist me. My challenge lies in trying to pass a parameter with a specific value to a page.html file, like this: page.html?s=1 However, I am encountering a problem where the pa ...

Leveraging basscss through NPM/Webpack installation

As a newcomer to the webpack/react app environment, I am attempting to incorporate the Basscss CSS framework into my project. After successfully running 'npm i basscss' and adding require('basscss/css/basscss.css'); to my app entry poin ...

Aligning hyperlinks with background images

Trying to align the link with the background image centered but it keeps ending up at the top, even though I specified the sizes and each background image has different dimensions. https://i.sstatic.net/HMdMx.png HTML <section> <h3> ...

I am getting text content before the input element when I log the parent node. What is causing this issue with the childNodes

Does anyone know why 'text' is showing up as one of the childNodes when I console.log the parent's childNodes? Any tips on how to fix this issue? <div id="inputDiv"> <input type="text" id="name" placeholder="Enter the nam ...

Looking to rearrange the layout of jqgrid

I need to reposition the jqgrid away from its default top left position. I have attempted some adjustments but they are not working as expected. Below is the code snippet for your review and suggestions: Here is the HTML code snippet: <div> &l ...

String object causing jQuery selector to malfunction

const newHTML = '<html><head><title>Hello</title><link rel="apple-icon-touch-precomposed" href="image.jpg" /></head><body></body></html>'; alert($(newHTML).find('link[rel="apple-icon-touc ...

Optimizing CSS for Landscape Letter Print Alignment

When attempting to print in letter size and landscape orientation, the two divs fail to align properly. However, when using "@size a4 landscape" for printing, the alignment issue is resolved. What could be missing from my print CSS that would allow them to ...

Maintaining Styles After Focus is Removed in CSS: A Guide

The CSS that styles our buttons is as follows: .btn-outline-primary { color: blue; border: 1px solid blue; background: transparent; transition: all 0.3s ease 0s; } .btn-outline-primary:hover, .btn-outline-primary:focus { background: ...

Issue with opening image modal when clicking on images, JavaScript seems to be malfunctioning

I created a gallery of modal images that I want to open when clicked, but for some reason it's not working as expected. What I'm trying to achieve is passing the image ID of each image to JavaScript when they are clicked so that the script can th ...

I am attempting to restrict a text box so that it can only accommodate 4 digits

I have successfully implemented a restriction on a text box to allow only a maximum of 4 digits, along with an alert message if the length is less than 4. The code I used is as follows: <tr> <td id="ExamNumber">ExamNumber </td> ...

Extracting information from console output and displaying it in a table with angular2

https://i.stack.imgur.com/BMt6J.pngI am facing an issue with retrieving data from the console output and populating it into an HTML table. Can someone please assist me with this? Here is the HTML Code: <table class="table"> <tr> <t ...

Ways to correct the issue of multiple <div>s overlapping each other after being rotated by 90 degrees

Can someone assist me with rotating multiple divs without them overlapping? Below is a simplified version of my code: <div> <div class="rect"></div> <div class="rect"></div> <div class="rect"></div> & ...

Is there a CSS Grid that supports IE6+ with percentage-based layouts?

Seeking a sleek CSS grid system that is percentage-based with 12 columns and works well on IE6+ as well as all modern browsers. Support for nested columns would be a bonus but not mandatory. Many grids out there lack compatibility with IE6 or are based sol ...

Is there a way to shift my navigation to the right while keeping the (display: inline;) property intact?

Is there a way to align my navbar to the right side while keeping the display: inline; property intact? When I remove the display: inline; property, the li elements automatically align to the right side of the page, but in a list format. HTML <div id= ...

Achieve a seamless transition for the AppBar to extend beyond the bounds of its container in Material

I am finalizing my app structure by enclosing it within an MUI Container with maxWidth set to false. The issue I'm facing is that the AppBar inside the container doesn't span the full width of the screen due to paddingX property enforced by the c ...

Recreated using CSS and JavaScript: iOS "Background Open" Animation

I am currently seeking a method to replicate the iOS Safari animation that occurs when opening a link in the background (the item jumps up and quickly animates to where it can be accessed) - This same animation is also seen on OSX Safari's "save to re ...

Tips for efficiently removing a user as an administrator

Whenever a user logs in or registers, I save their session and cookie information and also add certain details to the database. Login/Register Module (login/register.php) $_SESSION['userid'] = $userid; $selector = base64_encode(random_bytes(9)) ...

Creating a task management application using AXIOS and VUE for easy data manipulation

I am currently working on a small app to enhance my skills in VUE and Axios. However, I am facing a roadblock when it comes to the update functionality. I am struggling to pass the ID to the form for updating and despite watching numerous tutorial videos ...

Retrieve data from the database and automatically populate all text fields when the dropdown value is modified

I need assistance with populating all textbox values based on the dropdown selection. The dropdown values are fetched using an SQL query. Here is the HTML Code: <select name="name" ID="name" class="form-control"> <opt ...

Adjusting padding evenly across an unspecified number of columns

I'm currently facing a challenge with a multi-column layout. In a basic setup of three columns, I aim to adjust the padding so that each gap between the columns is consistent (2rem). However, the tricky part lies in crafting rules that can be applied ...