Creating a grid pattern of boxes within a rectangle using CSS

I'm attempting to design a rectangle filled with colorful boxes

Here is the code I'm using:

<div class="col-md-3" >
  <div id="myViewport" style="height: 700px; width: 50px; padding: 10px; border: 5px solid gray; margin: 0;">
    <div style="height:10px;background-color:rgb(0,0,0);" id='pass0'/>
    <div style="height:10px;background-color:rgb(0,0,0);" id='pass1'/>
    <div style="height:10px;background-color:rgb(0,0,0);" id='pass2'/>           
    <div style="height:10px;background-color:rgb(0,0,0);" id='pass3'/>
    <div style="height:10px;background-color:rgb(0,0,0);" id='pass4'/>
    <div style="height:10px;background-color:#FFA500;" id='pass5'/>
    <div style="height:10px;background-color:#FFA500;" id='pass6'/>
  </div>
</div>

I'm aiming for a design similar to this:

https://i.sstatic.net/vKe5U.png

However, I can't seem to get the boxes to appear. Can anyone point out what I might be doing incorrectly?

Answer №1

Always remember to properly close your div tags with </div> instead of just />.

If you want each box to fill the entire div, assign a height in percentage to each child div. For example, in this scenario, it would be 14.28%(Number of elements / 100).

<div id="myViewport" style="height: 700px; width: 50px; padding: 10px; border: 5px solid gray; margin: 0;">
  <div style="height:14.28%;background-color:rgb(0,0,0);" id='pass0'></div>
  <div style="height:14.28%;background-color:rgb(0,0,0);" id='pass1'></div>
  <div style="height:14.28%;background-color:rgb(0,0,0);" id='pass2'></div>           
  <div style="height:14.28%;background-color:rgb(0,0,0);" id='pass3'></div>
  <div style="height:14.28%;background-color:rgb(0,0,0);" id='pass4'></div>
  <div style="height:14.28%; background-color:#FFA500;" id='pass5'></div>
  <div style="height:14.28%; background-color:#FFA500;" id='pass6'></div>
</div>

Answer №2

Update the color attribute to represent background-color instead. The "color" property should be used for font color specifically.

Answer №3

Remember, div tags are not self-closing. Instead of using />, you should close a div with </div>. Following this proper structure will ensure everything works as intended.

#myViewport { 
      height: 700px; 
      width: 50px; 
      padding: 10px; 
      border: 5px solid #aaa; 
      margin: 0;}

/* To calculate the height, divide the available space by the number of elements - creating boxes that fill the space available after accounting for padding. */

#myViewport div { margin: 1px; height: calc((100% - 10px) / 7); }

#pass0, #pass1, #pass2, #pass3, #pass4 { background: #000;}

#pass5, #pass6 { background: #ffa500; }
<div class="col-md-3" >
  <div id="myViewport">
    <div id='pass0'></div>
    <div id='pass1'></div>
    <div id='pass2'></div>          
    <div id='pass3'></div>
    <div id='pass4'></div>
    <div id='pass5'></div>
    <div id='pass6'></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

How can I replicate the Firefox style in IE8 using Bootstrap and CSS?

My webpage is styled with Bootstrap v2 and looks great in Firefox. However, I'm struggling to apply the same CSS color style in IE8: .navbar-inverse .navbar-inner { background-color: #1b1b1b; background-image: -moz-linear-gradient(top, #fab ...

error in css styling detected

Why was the CSS style "background-position: center;" missed? It was because the background will override the background-position property. <html> <head> <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

Display and conceal information upon tweeting

Can anyone help me troubleshoot an issue with hiding the Twitter button after displaying new content? I tried adding a script to make it work, but it's still not functioning properly. Any insights on what I might be doing wrong would be greatly apprec ...

Utilize a while loop in JavaScript to trigger a message when a variable dips below zero

Forgive me if I seem clueless. I am a beginner in the world of Javascript and am currently experimenting with loops. At the moment, I am toying around with this particular piece of code: <!DOCTYPE html> <html> <body> <button oncl ...

What are the methods for adjusting the height of one div in relation to another div?

How can I make the height of the first div equal to the dynamic height of the second div, when the second div contains a lot of dynamic data with an unpredictable height? <div style="width: 100%;"> <div class=& ...

Attempting to implement a functionality that will allow users to easily delete records from the database

I've taken on a school project where I am tasked with creating a login/registration form. Additionally, I need to develop functionality that allows users to register, login, view records, and delete records from the database using a dropdown menu and ...

Using JavaScript to empty input field when switching focus between input fields

I am experiencing an issue with clearing a input number field. Here is the code snippet in question: <input class="quantity_container" v-model="length.quantity" type="number" pattern="[0-9]*" inputmode="numeric" onfocus="if (this.value == &ap ...

Run a function continuously while a key is being held down

I am currently working on a Javascript program that will move a div up and down based on key inputs. The idea is to continuously update the 'top' style value of the div while a specific key is pressed. While the basic function works, I am struggl ...

Retrieve the updated text content from a textarea using JavaScript/jQuery

For a beginner in javascript and jquery like me, I encountered an issue with a textarea element that has preset content. When I change the value on the site to "new" and click the "showme" button, the alert box displays the preset value instead of the new ...

Obtaining the URL from the anchor tag

I am currently working on a project that involves scraping data from the cinch.co.uk website. My tools of choice are Python along with the BeautifulSoup4 and Request libraries. My goal is to extract car data from each advertisement, starting by navigating ...

Embed a photo into a pop-up window

Is there a way to dynamically insert an image into a Modal by utilizing the value of the variable data-image? This variable will change based on the selected image. <script type="text/javascript"> $('#myModal').on('show.bs.m ...

Enhancing Your WordPress Menu with Customized Spans

I have a WordPress menu structured like this: <div id="my_custom_class"> <ul class="my_custom_class"> <li class="page_item"><a href="#">page_item</a> <ul class='children'> <li class="page_item chil ...

Title vanishes when gradient is added to a div

I have encountered an issue with a recent update on my webpage. Previously, I had multiple divs with the title attribute that displayed information when users hovered over them. However, after adding a gradient background, the titles stopped appearing. Th ...

Float a div within text to be positioned vertically

Is there a way to use only CSS to create an article that includes a featured quote section starting around 50px from the top? The section should be half the width of the page with text wrapping around it at the top and bottom. Currently, I am using the fl ...

Stop the stream coming from getUserMedia

After successfully channeling the stream from getUserMedia to a <video> element on the HTML page, I am able to view the video in that element. However, I have encountered an issue where if I pause the video using the controls on the video element a ...

Executing functionality based on changes in Angular bindings

I am working on securing user passwords in my HTML form. Currently, the password field is stored in localstorage like this: In JS: $scope.localStorage = localStorage; And then in HTML: <input id="pass" type="password" ng-model="localStorage.pass" re ...

What steps should I take to correct the alignment of this grid using CSS?

I'm currently working on creating a grid layout for my website and I've almost achieved the desired result. However, I'm facing an issue with the size of the "Projects" title within the grid. I want it to stand out and be larger compared to ...

Modify the bootstrap dropdown background to display only an image

I am looking to make the image in a dropdown/dropup menu fill the entire background of the dropdown. Currently, there is still white space visible around the image. How can I ensure that the dropdown shows only the image and includes a scroll wheel? Addit ...

Getting the result from HTML5 FileReader: How does it work?

Dealing with the asynchronous nature of JS FileReader can be challenging. I need to retrieve the result after reading a file and work with that data, but I don't know when the result will be ready. How can I handle this situation effectively? $(docum ...

Navbar links in Bootstrap unexpectedly expanding in width

My website has a navigation menu that looks like this: https://i.stack.imgur.com/1R8mv.png However, when I click on a menu item, the link container inherits the width of the dropdown menu. Is there a way to prevent this using purely CSS? I am currently ...