Creating a 3x3 CSS grid where only the center div has a specific height and width

For some time now, I have been facing challenges in creating a 3x3 grid layout where the center div has fixed dimensions while the others adjust to fit the window size. Despite my efforts, I have been unable to make the non-center divs behave as desired. Although I came across solutions for a two-column layout, I struggled to apply them to a three-column layout. Here is what I have managed to do so far: http://jsfiddle.net/WGaVH/

I am new to CSS, so any assistance would be greatly appreciated. Thank you!

Answer №1

After working on it, here's the solution I came up with: http://example.com/WGaVH/21/

I made some adjustments to the HTML code to simplify it. There are a few overlapping div elements that might pose challenges depending on your background requirements (but these could be addressed by adding another nested div).

Updated HTML Structure

<div id="wrapper">

    <div class="top left"><p><span id="topLeftContent">1</span></p></div>
    <div class="top mid"><p><span id="topCenterContent">2</span></p></div>
    <div class="top right"><p><span id="topRightContent">3</span></p></div>

    <div class="main left"><p><span id="mainLeftContent">4</span></p></div>
    <div class="main mid"><p><span id="mainCenterContent">
        <object width="84" height="60" align="middle"></object>5
    </span></p></div>
    <div class="main right"><p><span id="mainRightContent">6</span></p></div>


    <div class="bottom left"><p><span id="bottomLeftContent">7</span></p></div>
    <div class="bottom mid"><p><span id="bottomCenterContent">8</span></p></div>
    <div class="bottom right"><p><span id="bottomRightContent">9</span></p></div>

</div>

CSS Stylesheet Updates (color codes used for demonstration purposes)

html, body{width:100%;height:100%;}
html,body {margin:0;padding:0}

#wrapper{width:100%;height:100%;background:#bbffbb;overflow:hidden;}

.top, .main, .bottom {
   text-align:center;
   float: left;
   position: relative;
   background-color: #FFFFCC;
}

.top {
   height: 50%;
   margin-bottom: -30px;
}
.top p {
   margin-bottom: 30px;
}
.main {
   height: 60px;
   z-index: 2;
}
.bottom {
   height: 50%;
   margin-top: -30px;
}
.bottom p {
   margin-top: 30px;
}
.left {
   width: 50%;
   margin-right: -42px;        
}
.left p {
   margin-right: 42px;
}
.mid {
   width: 84px;
   z-index: 2;
}
.right {
   width: 50%;
   margin-left: -42px;
}
.right p {
   margin-left: 42px;
}
.main.mid {
   z-index: 3;
   background-color: #CCFFFF;
}
.mid {
   background-color: #FFFFFF;
}
.main {
   background-color: #FFCCFF;
}

Answer №2

Check out this link for some inspiration: http://jsfiddle.net/WgF7Z/1/.

All the different divs on the webpage are using percentage widths, except for the central one. Perhaps this can help spark some creativity in your own project.

To prevent any overlap like in this example, make sure to set a minimum height/width for the wrapper div that is three times the size of your fixed center div.

If CSS3 is an option for your project, definitely look into implementing The CSS 3 Flexible Box Model

Answer №3

Creating a grid layout using the display table property:

<style type="text/css">
    html, body {
        padding: 0;
        margin: 0;
    }
    .grid3x3 {
        display:table;
        height:100%;
        width:100%;
    }
    .grid3x3 > div {
        display:table-row;
        width:100%;
    }
    .grid3x3 > div:nth-child(2) {
        height: 100px;
    }
    .grid3x3 > div > div {
        display:table-cell;
    }
    .grid3x3 > div > div:nth-child(2) {
        width:100px;
    }

    div {
        outline: 1px solid orange;
    }


</style>
<div class="grid3x3">
    <div>
        <div>1</div>
        <div>2</div>
        <div>3</div>
    </div>
    <div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
    </div>
    <div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
    </div>
</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

Creating a custom implementation of Bootstrap for a specific section on your website

Trying to incorporate Bootstrap into a specific section of my website has been quite frustrating. Every time I add the code: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> It ends up causing iss ...

Display the name of the file on the screen

Is there a way to dynamically display the file name in a view instead of hardcoding it? I would appreciate any assistance. Thank you! Here is my code snippet: <li> @if (Model.Picture2 != null) { base2 = Convert.ToBase64String(Model.Pict ...

What is the best way to align a collection of search bars with HTML and CSS?

Can someone help me align my search bars and submit buttons? I'm new to HTML and struggling with this task. (By the way, this is just a small part of the browser layout, it doesn't usually look so messy!) .center-block{ margin-top: 220px; ...

Transforming a horizontal list into a vertical one

What might be causing the message list to display horizontally in the admin dashboard, but vertically on other pages? .user-list{ display:inline; } .users header, .users-list a{ display: flex; align-items: center; padding-bottom: 20px; border-bottom: 1px s ...

Is there a way to implement two distinct JavaScript functions - one for desktop and another for mobile - within the same onclick event attribute in an HTML document?

I'm currently working on a responsive HTML page that has versions for desktop and mobile. In order to properly execute specific functions based on the device's screen width, I have two separate functions - one for desktop and one for mobile - bot ...

Implementing a maximum width container within a grid item

Having a container with a max-width of 80 rem and margin-inline set to auto, when applied to the grid, it perfectly centers the grid within 80 rem. <div class="grid-container container"> <header class="primary-header flex & ...

Tips on adjusting the width of an HTML table to prioritize a larger size for the last column over the first two

Hello everyone, I am pretty new to Django, HTML, and CSS. This is actually my first time using stackoverflow so I could really use your assistance. My issue involves a table I have created: <div class="grade_book"><table class="tab ...

Utilizing PHP to retrieve and insert data from a database, then display the information within a div. Despite CSS being configured with different width and height settings, the div expands accordingly

I am currently utilizing PHP to input information into a database and then use that information in a simple comment system. Being new to PHP, I am unsure if there is an easier way to achieve what I am doing. However, I am facing a challenge where if the in ...

What is the best way to implement a CSS transition for styles that are dynamically created by React?

I have a situation where I am using a button component that is styled based on a theme provided by a context: The code in Button.js looks like: () => { const theme = useContext(themeContext); // { primaryColor: "blue" } return <button className ...

Vuetify - Reordering items to the bottom of the list

Hey there! I have a quick question about Vuetify. I'm looking to move the social media icons to the bottom of my navigation drawer, but I'm having some trouble figuring it out. Can anyone offer some guidance on how to achieve this using Vuetify c ...

Having trouble with CSS selectors functioning properly in Rhomobile Studio for Android

I am a beginner in the world of Rhodes and Rhomobile Studio. Coming from a Rails background, I am used to the .css.scss file extensions which allow powerful CSS3 codes to be executed. However, in Rhomobile Studio, these extensions are not recognized. I am ...

What causes the unexpected behavior of MatPaginator within an ngIf statement?

CHECK OUT MY STACKBLITZ DEMO Here's the material table setup I'm working with: <table mat-table [dataSource]="dataSource" > ... Rows ... <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row * ...

Is there a way to modify the color of a corner in an HTML box?

<iframe width="100%" height="500" src="https://minnit.chat/Real97ChatRoom?embed&amp;web" style="border: none;" allowtransparency="true"></iframe><br /><a href="https://minnit.chat/Real97ChatRoom" target="_blank" rel="noopener noref ...

Having trouble with integrating user input from HTML into a JavaScript file to execute a GET request

I am currently working on a project to create a website that integrates the google books API for users to search for books. To start, I have set up a server using express in index.js at the root of the project directory, and all my static files are stored ...

jQuery does not provide the reference of a basic canvas element

I'm having trouble with a simple initialization function that is supposed to create a canvas element in the body and save its reference in a variable. No matter what I try, jQuery doesn't seem to want to return the reference. I attempted refere ...

How can you incorporate a specific CSS file based on conditions in Angular?

What is the method for selectively applying CSS files in Angular? It is required to have several CSS files and apply them to an Angular component based on a condition. What are the various approaches available to achieve this? Any method can be considere ...

Checking to see if there are a minimum of two checkboxes selected before inputting the data into the database

I am currently using a combination of HTML, PHP, JavaScript, MySQL, and Wampserver. In my project, I have implemented 3 checkboxes where the user can choose a maximum of two options. Once selected, these choices are then inserted into the database. Initi ...

Adjust the column count based on the screen size, Susy suggested

When using the Compass/Sass plugin Susy, you typically set the number of columns in the _base.scss file. I prefer to have 12 columns for a desktop view, but this may be too many columns for a mobile display. Is there a method to alter the number of column ...

Dealing with the issue of achieving transparency in linear-gradient borders using

Can anyone help me understand why the bottom left and right edge borders are fading in my design? I want to work on this picture, but I can't figure out why the border-radius in my code is fading. Click here for image Click here for image <div cl ...

Insert the URL into either a div or an iframe

It seems like a common issue. I've come across multiple solutions for this problem. using jquery load using iframe I attempted both methods but encountered difficulties in loading content. Specifically, I tried to load google.com and it didn't ...