What is the best way to insert a vertical line between two 960.gs containers?

Incorporating the 960.gs grid system into my design, I am looking for the optimal method to include a slim separating vertical line between two boxes that allows for customization of width and color.

One approach I have considered involves creating div classes with absolute positions and background colors for each potential position, then using JQuery to ensure they align in height with adjacent boxes. However, this process feels somewhat convoluted. Are there alternative solutions available?

Answer №1

To create a border using the pseudo-selector :after and absolute positioning, follow this code:

.line:after {
    border-right: 1px solid #000000;
    content: "";
    display: block;
    margin: 1px;
    position: absolute;
    right: -11px;
    top: 0;
    bottom: 0;
}

.grid_1, .grid_2, .grid_3, .grid_4, .grid_5, .grid_6, .grid_7, .grid_8, .grid_9, .grid_10, .grid_11, .grid_12, .grid_13, .grid_14, .grid_15, .grid_16 {
    position:relative;
}

Check out the demo here

Make changes to the code here

Answer №2

To prevent the separating line from shifting the next row of DIVs, I recommend using absolute positioning. One approach is to utilize an absolutely-positioned :after selector to place something in relation to the bottom of the box without impacting the layout. This method has been successful for me in adding a line between boxes while maintaining the layout integrity. Simply adjust the values of the last four properties as necessary:

#topbox:after {
    content: "";
    display: block;
    position: absolute;
    margin-top: 25px;
    height: 5px;
    width: 400px;
    background-color: #999;
}

Answer №3

It is completely feasible to achieve this without relying on jQuery. The key challenge lies in accommodating the varying heights of the elements.

You can find a helpful reference here: http://jsfiddle.net/uqZgt/1/

Here is the HTML structure:

<div class="container">
    <div class="box-1">
        This box contains a lot of text. This box contains a lot of text. This box contains a lot of text.
    </div>

    <div class="box-2">
        This box has less content compared to box-1. This box has less content compared to box-1. This box has less content compared to box-1. This box contains more text than box-1. This box contains more text than box-1. This box contains more text than box-1.
    </div>
</div>

The corresponding CSS styling:

.container {
   width: 242px;   
}

.container div {
   width: 100px;
   background: #ff0000;
   float: left;
   padding: 10px;
   border-right: 2px solid #000
}

.box-1 + .box-2 {
   border-right: none;
   border-left: 2px solid #000
}

.box-1 ~ .box-2 {
    margin-left: -2px
}

In this example, all divs within the .container have a 2px solid black border on their right side. However, if an element with class box-2 immediately follows an element with box-1, it will have a 2px solid black border-left and no right border. This setup creates a 3px border gap between the two elements.

The selector .box-1 ~ .box-2 targets any instance of box-1 that directly precedes a box-2 and adjusts its margin-left to -2px. This action shifts its neighboring element by two pixels to the left, essentially causing their borders to overlap.

The total width of the .container div equals the combined width of both elements (200px), along with padding (10px on both sides = 20px) and the width of one border (2px). This sums up to 242px, ensuring a perfect fit for the two elements.

Regardless of which div contains more text, the border will visually extend to match the height of the taller div.

Answer №4

It seems like there might be some confusion about the issue you're facing. One solution could be to add a border on either the right or left side of one of the columns, and then adjust the padding as needed to ensure that it is centered between the two.

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

Experience choppy scrolling in Internet Explorer

Check out my click and drag scrolling Image Viewer here. While it functions perfectly in Firefox and Chrome, Internet Explorer is giving me some trouble. The movement seems jerky, especially when scrolling diagonally. It's like the scroll is sluggish ...

Responsive carousel with multiple items

I am in the process of developing a website that requires the implementation of a carousel feature. Since the website is based on AngularJS, I initially considered using Angular's Bootstrap Carousel. However, I encountered an issue where this carousel ...

Tips on preventing the fixed position from scrolling at the footer

Despite extensive research, I am still unable to understand why this particular issue is not resolving for me. The problem lies within two specific div elements that are causing trouble. One of the divs is utilizing position: fixed in order to remain visi ...

Creating a unique custom shape for Bootstrap navigation bar

I'm looking to create a unique custom navigation bar shape inspired by the image attached below. https://i.sstatic.net/G7iIV.png To achieve this shape, I've utilized the following CSS: .navbar-nav.nav-bar-custom { transform: skew(-21deg); ...

Exploring the power of nested SCSS pseudo selectors

I am working with a structure of pseudo elements, which looks like this: &::before { content: ''; position: absolute; height: 100%; width: 1px; background: ${(props) => props.theme.colors.link}; right: 6px; } &:first-child: ...

What is the best way to ensure that content fills the entire screen?

My goal is to position the footer at the bottom of the page. This is the initial code I tried: <html> <head> <title>Freds Cars, Trucks & Jeeps</title> <style> #container{ height: auto; ...

Creating a background overlay on top of an image background using styled-components in React: A step-by-step guide

I need help figuring out how to construct a unique component in react styled component that combines a background image with a black semi-transparent overlay. For visual reference, here is an example: https://i.sstatic.net/R6IBY.jpg ...

"Learn the best way to redirect the parent theme directory URL to the child theme directory in a dynamic and efficient

Looking to leverage my skills on the WordPress platform, I recently ventured into creating a child theme and require some help with PHP coding and functions.php related tasks. In an effort to maintain folder structure, I copied content from the parent the ...

Adjusting iframes for responsive design using only CSS

It's common for websites to have embedded Youtube videos, and with Youtube now compatible on phones too. With the rise of responsive design, it only makes sense for iframes containing Youtube videos to also be responsive, right? After spending days s ...

`The header navigation is not responding to window resizing functionality`

I am currently developing a header navigation that consists of a logo on the left side, a profile icon on the right side, and some navigation links in the middle. A left slide menu has been implemented to trigger when the window width is less than 700px. ...

What types of HTML are compatible with SweetAlert?

I am exploring the HTML elements that can be used with the SweetAlert alert function (). It seems that headings like h1, h2, etc. and strong tags are allowed, but when I tried to add inline styles like style="color:blue;", the alert stopped working. The v ...

Troubleshooting Margins in Bootstrap Container

Can someone help me figure out how to set consistent margins on a container using Bootstrap spacing properties without messing up the alignment?https://i.sstatic.net/unp8d.jpg * { box-sizing: border-box; } .container { max-width: 960px; } <div ...

Creating a banner using four grid elements, with each element containing four child elements, is a simple process that can

Can you assist me in recreating my idea from an image? I am having trouble understanding how to select and place other elements, and then return them to their original positions after deselecting... I attempted to use a grid but it did not work properly. ...

Select elements in jQuery using both a specific selector and a negative selector

I am currently working with a JQuery function that highlights a specific word and scrolls to it: $('article.node--article p, .video-title').highlightWordAndScroll({ words : search_word, tag : '<span class="found_key ...

Populating check boxes in a jQuery multiselect dropdown based on option text

I want to implement the jQuery multiselect plugin, but the checkboxes are appearing after the option text. Is there a way to configure them to be on the left side? https://i.sstatic.net/2oB2y.png ...

Clickable links and compliant W3C coding

Recently, I have encountered errors in the W3C validator due to the presence of "name" attributes in some <a> tags in my document. The validator flagged these attributes as obsolete. I am curious to know when and why this happened? Additionally, I n ...

Display the images adjacent to the usernames, followed by their respective levels underneath

<div class="players"> {{#each users}} {{#each usersInLobby}} <img src="/img/characters/{{skin}}.png" style="height: 85px; display:inline;"> <p>{{username}}{{level}}</p> {{/each}} {{/each}} ...

Superimpose one element on top of another

My current page layout includes the following code: <div class="card"> <div class="card-block"> <h4 class="card-title text-muted">UltimateWarrior15</h4> <h6 class="card-subtitle text-muted"> Ad ...

Pressing the Enter key will open the file dialog for selecting specific files

Having an issue with my project Seeking to enhance accessibility by opening the input file on pressing "Enter" key. Added tabindex to label so that when tabbing and hitting "enter", a function is triggered to open the input file picker. . ...

Adjustable height for Material UI tab components

I encountered an issue that I initially thought was related to the general configuration of my app and the height I assigned to my page wrapping classes. However, after creating a simple out-of-the-box material UI tab example, it became clear that this beh ...