Struggling to figure out the best way to position logo and navigation next to each other using CSS grid

I'm currently working on aligning a logo and navigation bar in a single row at the top of a website using CSS grid.

After writing the code, I'm having trouble figuring out why it's not functioning as intended: https://codepen.io/chloewb/pen/wRRewQ

     .logo{
    grid-area: logo;
    background:white;}
    .navi{
     grid-area: navi;
    background:Yellow;}
    .section1{
     grid-area: features;
    background:LightSalmon;}
    .section2{
     grid-area: technology;
    background:PaleTurquoise;}
    .section3{
    grid-area: pricing;
    background:LightPink;}
    .section4{
    grid-area: email;
    background:PaleGreen;}
  
    .container {
    display: grid;
    grid-template-rows: repeat (5, auto);
    grid-template-columns: 1fr 1fr 1fr;
    font-size: 40px;
    width: 100%;
    background: grey;
    grid-template-areas:
      "logo navi navi"
      "features features features"
      "technology technology technology"
      "pricing pricing pricing"
      "email email email";
    }

Answer №1

One key observation is that by using display: grid on a container element, its direct children automatically become grid-items. The grid layout you create will then apply to these items.

For example:

<div class="container">
    <div class="child-1">
        <div class="child-2"></div>
        <div class="child-2"></div>
    </div>
    <div class="child-1"></div>
    <div class="child-1"></div>
    <div class="child-1"></div>
</div>

And the corresponding CSS:

.container{
        display: grid;
    }

In this scenario, only the child-1 elements will behave as grid items and can receive properties like

grid-area</code. Other elements inside <code>.child-1
, such as .child-2, will act as usual unless explicitly defined as grid items with display: grid.

If your header element is a direct child of .container, it becomes a grid item and can be positioned within the grid. However, elements like logo and navi are nested inside header and do not inherit the grid layout. You would need to either move them out of header for the grid rules to take effect or establish another grid within the header to utilize the entire first row. Consider the following example to understand how element nesting affects their behavior.

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: minmax(50px, auto);
  grid-template-areas: "logo navi navi";
  margin-bottom: 20px;
  border: 1px solid black;
}

.logo {
  border: 1px solid red;
  grid-area: logo;
}

.navi {
  border: 1px solid blue;
  grid-area: navi;
}
<div class="container">
  <div class="logo">Logo</div>
  <div class="navi">Nav</div>
</div>

<div class="container">
  <header>
    <div class="logo">Logo</div>
    <div class="navi">Nav</div>
  </header>
</div>

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

The evolving impact of the HTML5 <video> element

I've been attempting to find information on this topic, but unfortunately my search has not been very successful. I vaguely recall coming across a tutorial that covered something like this. Does anyone happen to know how to alter the perspective of a ...

Issues with the presentation of HTML DIV elements

I am puzzled by a situation where my HTML layout to present financial data does not seem to update properly in certain browsers. Despite using jQuery and inspecting it with Firebug, the visual display of the parent divs remains unaltered. This issue become ...

What could be causing my DIV to be smaller than its contents?

Currently, I am attempting to make adjustments to the Simple jQuery Slideshow created by Jonathan Raasch. This slideshow seamlessly transitions between images with a fading effect. If we take an example of a 350px square image within a 350px height DIV c ...

The css() function appears to be failing to apply the background property to the body element

I have tried using these different methods in my code to solve the issue, but none of them seem to work. Please suggest an alternative approach for resolving this problem. // Approach 1 if ("${actionBean.backgroundImage}" != null){ $(document.body). ...

Getting rid of the arrow icons in a Material UI TextField

I am facing a challenge with removing the up and down arrow icons from a Material UI TextField that I adjusted using the Material UI documentation (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section. After trying various sol ...

Tips for aligning an image vertically within a list item

I am having trouble aligning my image vertically in the li tag. This is what I currently have: <nav id='tabs-wrapper'> <ul id='tabs'> <li><a href='index.html'><img id='btn' src=&apo ...

When you adjust the size of your web browser, the text may become distorted in HTML and CSS

My website has a problem with text resizing when the browser window is made smaller. The text gets squashed in the middle and creates more lines than needed. Here is my code: Entire Document HTML: http://pastebin.com/kj1jvTjv CSS: http://pastebin.com/A1C ...

Creating a list element after clicking in ReactJS is achieved by using event handlers in

I have buttons and inputs that I want to use to generate a list item in the ul section at the end of my code. However, despite adding functions to handle this feature, it still doesn't work as intended. import { useState } from 'react'; impo ...

What is the method for displaying Facebook comments alongside the WordPress comments count?

I am currently using a template theme that initially had WordPress comments enabled on the website. By implementing the Facebook Comments plugin, I have successfully replaced Wordpress comments with Facebook Comments. **However, there seems to be an issue ...

Shape with a dark border div

Having some trouble with creating this particular shape using CSS border classes. If anyone can assist in making this black box shape using Jquery, it would be greatly appreciated. Check out the image here. ...

Tips for creating a dynamic menu item that stands out with a highlighted style

I would like to add a colored square to highlight the active menu item. .main-menu ul { padding: 0; margin: 0; text-align: center; } .main-menu li { list-style-type: none; display: inline-block; padding: 40px 0; } .main-menu a { font-fam ...

Element within Bootstrap is not in a hidden state

For Small Devices(sm) size, I want a hidden search box with a search button instead. This is how I want my search to look like in small devices: view image here This is the code I have: <div class="container"> <div class="header"> ...

Responsive design issues with Bootstrap Popover

Whenever I open the popover for the first time, the image is not displayed correctly. How can I ensure that it always shows correctly? Here is a link to the issue: https://jsfiddle.net/n2Lfro30/1/ Below is the button code causing the problem: <button ...

Adjust the text within the treeview node for proper alignment

My treeview has nodes that display text, but when the length of the text increases, it moves to the next line and starts one place before the upper text, causing alignment issues. Is there a way to use CSS or JavaScript to properly align the text? Regards ...

Using jQueryUi Tabs for Organizing Div Tables

Recently, I implemented a jQuery tab on my website using the following code snippet: <div id="tabs"> <ul> <li><a href="#tabs-1">Basic Info</a></li> </ul> <div id="tabs-1"> ...

Box with plus symbol

One of my current projects involves creating a pill button with the following styling: <button class="button">Pill Button</button> .button { border: 1px solid red; color: black; padding: 10px 20px; text-align: center; text-d ...

Conditional CSS comments for browsers and Internet Explorer version 9

Something peculiar is happening to me. I'm currently using Internet Explorer 9 and experimenting with changing the browser mode to IE 7 or IE 8, among others ... The issue arises when I include this code: <!--[if lt IE 7]> <link type=&bsol ...

Tips for incorporating tabs into a Rails 3 application with the help of Sass/Haml or CoffeeScript

Currently, I am looking to implement tabbed views in a project I am developing using Rails 3. To ensure compatibility with the latest features of Rails, I have decided to utilize Sass and CoffeeScript as my primary tools. If anyone has any resources or tu ...

Error: The specified type '{ color: string; loading: true; css: string; size: number; }' cannot be assigned to the type 'IntrinsicAttributes & LoaderSizeMarginProps'

When attempting to build the project, an error occurred: $ yarn run build yarn run v1.22.19 $ next build info - Loaded env from D:\folder_1\my_Project\client\.env.local info - Linting and checking validity of types .Failed to compile. ...

Using jQuery or JavaScript to adjust the bottom margin

Looking to Add Bottom Margin for Selection Box I have attempted the following CSS: #Selection { margin-bottom:23px; } Unfortunately, this method did not work for me. Is there a way to achieve this using JavaScript or jQuery? View Example on JsFiddle S ...