/    Sign up×
Community /Pin to ProfileBookmark

“position: fixed;’ appears to remove Grid child from the Grid. How to fix?

I am a CSS Grid Layout newbie. I have created a top navigation menu bar using CSS Grid Layout. I have placed the menu bar in a 3 column by 1 row grid in the middle grid cell so as to center it.

Next I added “position: fixed;” so as to fix the menu at the top of the browser window. The result is, is it shifts all the way to the left and is fixed to the top. (I want it in the middle.) By what I can tell in the Firefox Developer tools is the menu is being removed from the grid as the grid height becomes zero.

How can I get my menu centered and fixed.

CSS —————————————————————————————————

“`
.nav_bar {
display: grid;
grid-template-columns: 1fr max-content 1fr;
grid-template-rows: max-content;
}

/* Top menu bar */
.nav_bar nav {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: max-content;
/* Placing this grid in the parent grid */
grid-column-start: 2;
grid-column-end: 3;
position: fixed;
}

.nav_bar div{
height: 100%;
text-align: center;
font-size: 18px;
padding-left: 10px;
padding-right: 10px;
padding-top: 15px;
}

.nav_bar a {
text-decoration: none;
color: var(–text-color1);
}

.nav_bar .menu_cell {
background-color: var(–primary-color);
color: var(–text-color1);
}

.nav_bar .menu_cell:hover {
transition: all 750ms ease;
background-color: var(–secondary-color);
}
“`

HTML ————————————————————————————

“`
<div class=”primary_grid_con”>
<!– Navigation menu–>
<div class=”nav_bar”>
<nav>
<div class=”menu_cell” id=”menu1″><a href=”#”>Item 0</a></div>
<div class=”menu_cell” id=”menu2″><a href=”#”>Item 1</a></div>
<div class=”menu_cell” id=”menu3″><a href=”#”>Item 2</a></div>
<div class=”menu_cell” id=”menu4″><a href=”#”>Item 3</a></div>
</nav>
</div>
</div>
“`

to post a comment
CSS

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 09.2022 — @Programming_Padawan#1641265
  • 1. Please use code tags when posting code: `your code here</C> or triple backticks. I edited your posting accordingly.</LI>
    <LI>2. It seems to me that your HTML is incomplete: I read this in your CSS:
    <CODE>
    `<i>
    </i> .nav_bar {
    display: grid;
    grid-template-columns: 1fr max-content 1fr;
    grid-template-rows: max-content;
    }<i>
    </i>
    `</CODE>
    which requires that <C>
    .nav_bar</C> hast three items.</LI></LIST>

    Edit: I inserted the missing containers and was able to reproduce your issue:
    <QUOTE><i>&gt;</i>Next I added "position: fixed;" so as to fix the menu at the top of the browser window. The result is, is it shifts all the way to the left and is fixed to the top. (I want it in the middle.) By what I can tell in the Firefox Developer tools is the menu is being removed from the grid as the grid height becomes zero.</QUOTE>

    This is a general property of <C>
    position: fixed;`
    : It takes the item out of the regular flow of elements. Unfortunately I do not understand exactly what your intention is: Do you want to place the nav in the center being fixed while the content at the left and at the right is scrolling?
  • Copy linkTweet thisAlerts:
    @sibertJan 09.2022 — > @Programming_Padawan#1641265 How can I get my menu centered and fixed.

    Place the grid in a flexbox?

    https://jsfiddle.net/npzr2L8e/2/
    Copy linkTweet thisAlerts:
    @Programming_PadawanauthorJan 10.2022 — @Sempervivum#1641279 I should have mentioned that. I am seeking to have the menu bar fixed at the top and present at all times while the remainder of the page content can be scrolled.
    Copy linkTweet thisAlerts:
    @SempervivumJan 10.2022 — Was your grid layout just an attempt to get the items centered? As sibert showed you this can be done in a more simple way.

    Instead of `position: fixed;</C> I recommend using <C>position: sticky;` as the nav will not cover the main content then.
    Copy linkTweet thisAlerts:
    @Programming_PadawanauthorJan 10.2022 — After "playing" with it for a bit I have succeeded. Placing my grid top bar menu in a "flex" container was the missing element. (get it? element?).

    **I have learning a lot** about CSS Grid Layout through this menu. I'm sure to learn a lot more before I am finished with this single page web app. I can see myself using Grid more often in the future. The menu is to be able to smooth scroll to different sections of the page. Follow the link in the upper right corner to see the end result. (Yep, I finally broke down and signed up for JSFiddle)

    Many grateful Thanks for the help.

    https://jsfiddle.net/WaltWilliams/somb2a0w/15/
    ×

    Success!

    Help @Programming_Padawan spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.5,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...