Creating four divs in CSS/HTML where each div occupies one-quarter of the space while having borders that overlap by one pixel

Take a look at how my test page is currently set up:

I want the table (created using divs, not tables) to have 4 columns that are equally spaced with borders of 1 white pixel overlapping each other horizontally. This means there should be exactly 1 white pixel separating them. I understand that setting their borders to 1px will create a 2-pixel gap between them and they should take up the entire width of the containing div.

This is what my CSS looks like:

div.threadsrow:nth-of-type(2n+0)
{
    background-color: #ccc;  
}

div.threadsrow:nth-of-type(2n+1)
{
    background-color: #F9C624;    
}

div#threads
{
    width: 100%;  
    border: 5px solid #333;
    padding: 0px;
}

div.threadscol
{
    display: inline-block; 
    width: 24%;
    margin: 0px;
    border: 1px solid #fff;
    text-align: center;
}

And here's the HTML code I'm working with:

    <div id="threads">
        <div class="threadsrow">
            <div class="threadscol"><p><b>Title</b></p></div>
            <div class="threadscol"><p><b>Creator</b></p></div>
            <div class="threadscol"><p><b>Last Post Time/Date</b></p></div>
            <div class="threadscol"><p><b>Number of Posts</b></p></div>
        </div>
        <div class="threadsrow">
            <div class="threadscol"><p>Foo</p></div>
            <div class="threadscol"><p>Joe</p></div>
            <div class="threadscol"><p>11:49/4/16/2013</p></div>
            <div class="threadscol"><p>0</p></div>
        </div>
        <div class="threadsrow">
            <div class="threadscol"><p>Bar</p></div>
            <div class="threadscol"><p>Jack</p></div>
            <div class="threadscol"><p>11:34/4/16/2013</p></div>
            <div class="threadscol"><p>0</p></div>
        </div>          
    </div>

When adjusting the column width to 25%, the columns move down to the next line. Even changing the border to 0px doesn't fix this issue. Can someone help me understand why this is happening?

As a new web developer, I find myself getting stuck on these concepts easily even after two months of experience. If anyone could explain the logic behind achieving the formatting I want, it would be greatly appreciated!

Answer №1

Because your cells are inline-block elements, they are affected by white space, which may be creating some issues. To fix this problem, remove any additional white space and add the box-sizing:border-box rule to your div.threadscol divs. When you set box-sizing to border-box, it includes padding and border in the calculation of the 25% width.

Check out this jsFiddle example for further clarification.

Alternatively, instead of removing white space, you can set the font size on the parent element to zero and then adjust it for the cell data as needed.

Here is another jsFiddle example showcasing this method.

Answer №2

If you're struggling with white-space while using inline-block display, consider adding the CSS property white-space: nowrap; to div#threads. This should resolve the issue. Additionally, you can include margin: -5px (assuming your border is 5px) to the same element to ensure the border doesn't extend "off-the-page" on the right side.

For a visual demonstration, check out this demo link.

div#threads {
    width: 100%;
    border: 5px solid #333;
    margin: -5px;
    padding: 0px;
    white-space: nowrap;
}

Answer №3

If you are looking for a structured way to display your content, consider using a table approach. Organizing your posts in lists or tables can make the information more visually appealing and easier to navigate. It's important to think about how your content will appear on different devices, such as mobile phones.

Check out this jsFiddle

HTML Code:

<div class="table-w">

    <table class="your-table">
        <tr>
            <th>Title</th>
            <th>Creator</th>
            <th>Last Post Time/Date</th>
            <th>Number of Posts</th>
        </tr>
        <tr class="row">
            <td>Foo</td>
            <td>Joe</td>
            <td>11:49/4/16/2013</td>
            <td>0</td>
        </tr>
        <tr class="row">
            <td>Bar</td>
            <td>Jack</td>
            <td>11:34/4/16/2013</td>
            <td>0</td>
        </tr>          
    </table>

</div>


CSS Styles:

.table-w {
    border: 3px solid black
}

.your-table {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid white;
}

.your-table th {
    text-align: left;
    background-color: #F9C624;
}

.your-table th, .your-table td {
    width: 25%;
    padding: .5em;
    border: 1px solid white;
}

.row {
    background-color: lightgray;
}

.row:nth-of-type(odd) {
    background-color: #F9C624; 
}

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

CSS - Struggling to identify the source of unwanted horizontal scrolling?

Recently, I've been attempting to make adjustments to a responsive website that utilizes media queries. Despite my efforts, whenever the site is viewed on a mobile-sized screen, it consistently requires horizontal scrolling regardless of the screen&a ...

The designated origin in JavaScript is not displaying in the Django template

Sorry for the possibly silly question, but as I delve into Javascript and Django, I'm struggling with a specific issue. Despite spending hours on it, I can't seem to figure out why my image isn't displaying in my Django HTML template. Here ...

Adding text on top of an image

I am having trouble with the master slider plugin as slide1, 2, and 3 each have unique classes. I attempted to overlay text on the image but it is not showing up. I tried using the following CSS code but nothing appeared on the screen. .classnameslide1: ...

What is the best way to align text and images for list items on the same line?

HTML:- ul { list-style-image: url(https://i.sstatic.net/yGSp1.png); } <ul> <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit, consequatur?</li> <li>Lorem ipsum dolor sit amet, consectetur adipisici ...

Video background with alternate image for touchscreen devices

Check out this website! The background video is functional on desktops. On smaller screens, an image is shown instead using CSS display property. However, what should be done for touch screens like iPads, which are not exactly small? Here's the code ...

The WebRTC video feature is functioning on the local network but is encountering difficulties

Trying to grasp WebRTC has been quite the journey for me. In an attempt to troubleshoot my failed video transfer between computers, I uploaded what I have so far onto a temporary github repository: https://github.com/stevendesu/webrtc-failure My goal is ...

Please tap to dial: Access to navigation is restricted

Trying to add a click-to-call link with the following code: <a href="tel:+4912345678912">Tel: +4912345678912</a> Despite Google developers saying it should work, major mobile browsers are blocking the navigation when clicking on the link. It ...

The issue with disappearing HTML elements on input focus was resolved in iOS 8.3

I encountered a peculiar issue that bears resemblance to the fixed element problems on iOS highlighted on Stack Overflow, but with a unique twist. This anomaly only occurs when the parent document is scrollable and involves the use of an iframe. Below is a ...

Extracting HTML elements between tags in Node.js is a common task faced

Imagine a scenario where I have a website with the following structured HTML source code: <html> <head> .... <table id="xxx"> <tr> .. </table> I have managed to remove all the HTML tags using a library. Can you suggest w ...

How can I ensure that the HTML I retrieve with $http in Angular is displayed as actual HTML and not just plain text?

I've been struggling with this issue for quite some time. Essentially, I am using a $http.post method in Angular to send an email address and message to post.php. The post.php script then outputs text based on the result of the mail() function. Howev ...

How can we ensure that pointer events return the same coordinates as touch events when the viewport is zoomed in?

I attempted to utilize pointer events (such as pointerdown) instead of using a combination of touch events (e.g. touchstart) and mouse events (e.g. mousedown) to determine the input event coordinates. var bodyElement = document.body; bodyElement.addEvent ...

Displaying the second div once the first div has loaded, then concealing the first div

Current Approach: There are two divs occupying the same space, with div1 set to display:block and div2 set to display:none When a tab is clicked, jQuery hides one div over a period of 2000ms and reveals the other div. Challenge: The goal is for the ...

An alternative to Beautiful Soup for Python 3.2

Looking to create a web crawler for extracting information from various web pages, I discovered Beautiful Soup which seemed like an excellent tool. It allows me to parse entire documents, create dom objects, iterate through them, and extract attributes sim ...

Check to see if the ContentEditable div is currently focused

I'm struggling to determine if a contenteditable div has focus with my current code. Here is what I have so far: if ($("#journal-content:focus")) { alert("Has Focus"); } else { alert("Doesn't Have Focus"); } The issue I'm encounter ...

Text in a class button that is not skewed

I crafted a button for my website using a CSS class, aiming for a parallelogram shape by utilizing the skew function. The code worked like a charm, giving the button the desired shape. However, I encountered a dilemma where the text inside the button also ...

I encountered an error stating "angular not found" when attempting to dynamically load my Angular scripts

My Angular application is running smoothly. However, I found it tedious to include multiple script tags in my HTML documents every time. To simplify this process, I decided to create a small script that would automatically generate the necessary tags for m ...

Accessing HTML elements that are created dynamically in AngularJS

I am facing an issue where I cannot access a function within a newly created DOM element. Despite my best efforts, I can't seem to figure out what is causing this problem. $scope.createCustomHTMLContent = function(img, evtTime, cmname, evt, cust, ser ...

Tips for centering a bootstrap card on the screen using reactjs

I've been struggling to keep my card centered on the screen despite using align-items and justify-content. I've researched on various websites to find a solution. //Login.js import React, { Component } from 'react'; import './App ...

JavaScript code for Tree not functioning correctly on Internet Explorer

Seeking assistance to fix a bug in my JavaScript program. The code works perfectly on Google Chrome and Firefox, however it encounters an error in Internet Explorer 8 as indicated below. Any suggestions on how to resolve this issue would be greatly appreci ...

Tips for sending a secondary parameter through ajax to a php script

My current approach involves using ajax to retrieve data from a database and populate a drop-down list. Below is the code snippet for my ajax function: function searchq6(){ var searchstate = $("input[name='region']").val(); var searchTxt ...