Creating a table-like structure in HTML using `<div>` elementsWould you like to

I am looking to design a basic HTML page with a menu bar on the left side occupying 17% of the width, and content on the right side occupying 83% of the width.

Below is the HTML code I have constructed:

.row {
  display: block;
}

.cell {
  padding: 0;
  margin: 0;
  display: inline-block;
  border: solid;
}

div#leftCol {
  width: 17%;
}

div#rightCol {
  width: 83%;
  float: right;
}
<div class="row">
  <div class="cell" id="leftCol">
    Menu Bar goes here
  </div>
  <div class="cell" id="rightCol">
    Content goes here
  </div>
</div>

However, I am facing an issue where the divs are overlapping each other.

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

I attempted to adjust the right div size to 82%, which resolved the overlapping issue but caused a gap between the divs.

How can I ensure that the divs align on the same line with 17% and 83% width distribution without incorporating <table> elements?

Desired outcome:

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

Answer №1

The issue at hand is the addition of a border, which also requires space due to its width. To address this, you can utilize CSS box-sizing, which incorporates the widths of the border and padding into the element's overall width.

* {
  box-sizing: border-box;
}

.row {
    display: block;
}

.cell {
    padding: 0;
    margin: 0;
    display: inline-block;
    border: solid;
}

div#leftCol {
    width: 17%;
}

div#rightCol {

    width: 83%;
    float: right;
}
    <div class="row">
        <div class="cell" id="leftCol">
            Menu Bar here
        </div>
        <div class="cell" id="rightCol">
            Content here
        </div>
    </div>

In any case, in the current year of 2019, it would be beneficial to explore or implement CSS Grid Layout or flexbox

Answer №2

To achieve the desired layout, change the display types of the row and cell to table and table-cell respectively. Ensure the row is set to width: 100% and remove the float: right style.

<html>

<head>
    <style>
        .row {
          display: table;
          width: 100%;
        }

        .cell {
            padding: 0;
            margin: 0;
            display: table-cell;
            border: solid;
        }

        div#leftCol {
            width: 17%;
        }

        div#rightCol {
            width: 83%;
        }
    </style>
</head>

<body>
    <div class="row">
        <div class="cell" id="leftCol">
            Menu Bar here
        </div>
        <div class="cell" id="rightCol">
            Content here
        </div>
    </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

Populate a database with information collected from a dynamic form submission

I recently created a dynamic form where users can add multiple fields as needed. However, I'm facing a challenge when it comes to saving this data into the database. You can view a similar code snippet for my form here. <form id="addFields" me ...

Tips for reducing the height of the footer without compromising the alignment of the text within

My attempt to reduce the height of my footer in CSS using padding, margin, and other methods was unsuccessful. All of these adjustments either pushed the footer completely out of the bottom or left the text uncentered. footer { background: #fce138; wid ...

Preserving content following the use of jQuery's fadeIn/fadeOut methods

I have a jQuery function that fades in and out a sprite (sprite without text) from one background to another, and it's working as intended. However, this function is also fading out the text within an element. HTML: <div id="menu"> <ul c ...

Issue with manipulating background color with JQuery in CSS

Currently, I have a simple navigation menu consisting of five links. The navigation bar is styled with a background image and changes to a darker shade when hovered over. My goal is to implement a jQuery script that dynamically changes the color of the act ...

Can a rotation animation be incorporated into an image in next.js when it is clicked?

Looking to enhance a next.js image with an animation? How about making it rotate 360 degrees upon each click? Despite my attempts at toggling classes, I can't seem to achieve the desired outcome. Any guidance would be greatly appreciated. Thank you in ...

Node.js and Express.js fails to transmit files to clients

Attempting to send a gif to the client for future use in CSS, but encountering a 404 error in the console log. The gif is located within the public directory. Server: var app = require('express')(); var http = require('http').Server(a ...

Ensure that grid rows occupy the least amount of space possible

I'm relatively new to grid layout and I've encountered a challenge that has me stuck. Here's what I have so far: codepen And this is the relevant part of the grid: grid-template: 'img date' 'img head' 'img s ...

What is the best way to retrieve a particular value from a database using PHP?

$sql="Select chanceNo From gm_gacha_token1 where token_code='$mytoken'"; $result=mysql_query($sql); $count=mysql_num_rows($result); Language: PHP I am attempting to retrieve a single value of `chanceNo. Is there a way to get this value and stor ...

The controller remains unresponsive when attempting to activate the button on the webpage

I'm a beginner in frontend development and I'm currently working on a website that uses Thymeleaf. On this website, there is a div block with a button. Please take a look at the HTML code below, specifically the button at the last three rows. &l ...

By checking the active box and completing the fields, the button will be activated

I'm currently working on a form that requires all fields to be filled out and the acceptance checkbox to be checked before the Submit button becomes active. If any entry or the checkbox is subsequently removed, the button should become inactive again. ...

Bootstrap - Utilizing a boxed grid within a div located in the .fluid-container

Can you please review these two images? I'm trying to place ".myDivInTheGrid" inside a boxed bootstrap div. Any ideas on how to do this? This is what I currently have... <div class="fluid-container"> <div class="col-md-6"></d ...

Equal-height rows and columns using Flexbox

I've been working on a row layout with two columns, each set to 50% width. The left column contains an image, while the right column displays text. Here is my HTML: .row{ display:flex; ...

Display a progress bar in an application to visualize the loading of database records

Within my application, I am accepting a numerical input that is used to generate results on a separate page. The process involves running multiple sequential JDBC queries against Oracle database tables, resulting in a significant delay (1-2 minutes) for th ...

Is it necessary for two sibling elements to be of the same type in order for their margins to collapse together?

I am currently working on enhancing the visual appearance of a Bootstrap 4 card and encountered an issue: https://i.stack.imgur.com/p8Qzhm.png The spacing between the About the gig heading and the top Date item appears to be uneven. I would like to make ...

Scale first, then drag and drop to fix a faulty registration

I'm currently working on a project that involves dragging and dropping the correct items onto a technical drawing, such as a motherboard. The user is presented with pictures of components like CPUs or DDR memory and must drag these items to their corr ...

What is the process for enabling HTMLField in Django Admin and ensuring it is accurately displayed on the template?

One of the models in my Django app is for dorms and it looks like this: class Dorm(models.Model): dorm_name = models.CharField(max_length=50, help_text="Enter dorm name") dorm_description = models.TextField(max_length=1000, help_text="Enter dorm d ...

JavaFX Animation for Charting

My FXML file contains a JavaFX AreaChart <AreaChart id="myAreaChart" onMouseClicked="#handleAreaChartMouseClicked" GridPane.columnIndex="0" GridPane.rowIndex="0" fx:id="myAreaChart" > <xAxis> <Nu ...

Tips for centering links in the navigation bar on Bootstrap 5 when viewing in a small size

I had previously inquired about customizing the Bootstrap 5 navbar in this post: Adjust navbar height to be smaller and I received some helpful answers. Thank you to everyone who responded. However, I now have a follow-up question: I am interested in ch ...

I am having trouble displaying the background on my website using css/html

I'm currently working on enhancing my website BJBGaming1.com, and I've encountered an issue with setting a background. Here's the code snippet I have: <!doctype html> <html class="no-js" lang="en"> <head> <meta charset= ...

Resizing the width to fit truncated content

When displaying text within a span element inside a fixed-width structure, I want to use ellipsis if the text overflows. To show the full text, I have initialized a Bootstrap tooltip in these elements. However, the tooltip is visually misplaced due to the ...