Invisible boundary line within the column

My task involves creating a table using div elements. The layout of the table includes two columns structured as follows:

<div>
<div class = "columnborder"><span>Some text for column 1</span></div>
<div><span>Some text for column 2</span></div>
</div>

To separate the columns with a vertical border, I applied a class 'columnborder' to the div representing a table cell as shown in the CSS code below:

.columnborder{
  border-right: 1px black;
}

Initially, everything was working fine and I achieved the desired outcome of having a table with two columns separated by a vertical border. However, I encountered an issue when the text in the first column is empty, resulting in the absence of the span element. Here is the structure when the first column text is empty:

<div>
 <div class="columnborder"></div>
 <div><span>Some text for column 2</span></div>
</div>

Surprisingly, in this scenario, the border does not appear. This confuses me because I styled the div, not the span. How can I prevent this situation from occurring? What might be causing this unexpected behavior?

Answer №1

Update Your CSS Style:

According to SW4, your current statement is incorrect. The correct syntax should be as follows:

.columnborder{border-right:1px solid;}

Remember to properly close the div tags:

<div class="table">
    <div class="columnborder"></div> /* closing tag for div */
    <div><span>Additional content for column 2</span></div> /* closing tag for div */
</div>

Answer №2

One solution is using flexbox. This will ensure that the border always spans the table height, regardless of cell height. The reason for this is that flexbox items default to align-items: stretch (which stretches to fill the container).

.table {
  display: -webkit-flex;
  display: flex;
}
.table > div {
  -webkit-flex: 1;
  flex: 1;
  padding: 1em;
}
.table > div:first-of-type {
  border-right: thin solid red;
}
<div class="table">
  <div>
    <span>Some text for column 1</span>
  </div>
  <div>
    <span>Some text for column 2</span>
  </div>
</div>

Answer №3

Consider declaring the following:

.columnborder { display:table-cell; border-right:1px solid black; }

An empty div will have a height of 0, causing the border not to be displayed. To ensure visibility, you can also utilize the min-height: 1.5em; property to establish a minimum height for the empty div.

Answer №4

To simplify the problem, consider the following scenario:

<style>
div {
  width: 5em;
}
.columnborder{
  border-right: solid 5px red;
}
</style>
<div class = "columnborder">something</div>
<div class = "columnborder"></div>

In this case, the second div does not have a right border.

I've specified a fixed width and used a thick red border for better understanding. These changes are not crucial to the issue at hand. However, setting border-right :1px black; won't work as expected because it defaults to none, unless overridden elsewhere in your CSS code.

By inspecting the elements with a browser's Developer Tools, you'll see that the height of the second div is zero, resulting in an invisible border due to lack of content.

There are various solutions available. You can utilize CSS table formatting to give the div a non-zero height or explicitly set the height. Alternatively, inserting dummy content like a no-break space can also create the necessary line box. Here are some examples:

<style>
div {
  width: 5em;
}
.columnborder{
  border-right: solid 5px red;
}
</style>
<div class = "columnborder">something</div>
<div class = "columnborder" style="height: 1em"></div>
<div class = "columnborder">&nbsp;</div>

Answer №5

Make sure to close your div tags properly and check the overall structure of your HTML code.

See demo here

<div>
    <div class="column-border"><span>Content for column 1</span></div>
    <div><span>Content for column 2</span></div>
</div>

CSS

.column-border {
  border-right: 1px solid black;
  width: 160px;
}

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

Using the justify-content:space-between property does not seem to be effective in a Nextjs

In my Next.js app, I am facing an issue with the justify-content: space-between; property not working as expected for two divs inside a container. However, justify-content: center; works fine. Interestingly, when I use the same code in an index.html file, ...

What are the drawbacks of utilizing CSS word-wrap across all elements on a website?

Is there a downside to utilizing the CSS property word-wrap:break-word in this manner? body { word-wrap: break-word; } The definitions for the values of this property are as follows: normal: Breaks words only at allowed breakpoints break-word: Perm ...

Having trouble getting the header div to span the entire screen

I'm currently working on a project where I have set the header div to 100% width and the main container to also be 100% width. However, I am facing an issue where the right and left sides of the header are not filling the entire space. I managed to fi ...

Is it possible to apply CSS styling to all placeholders in a Sencha Touch application?

Having trouble figuring out how to style all the placeholders in my application with CSS. I've attempted a few different methods: .customField input::-webkit-input-placeholder { color: #2e4bc5; } .x-text-field input::webkit-input-placeholder{ c ...

Issue with locating JavaScript controller in Angular HTML file [Node.js/Angular]

Here are all the files and folders in my project located within the same directory. If you want to check out the project on Github, you can find it here: https://github.com/JohnsCurry/learnAngular. Just to confirm, they are all in the same folder. Unfortu ...

Implementing the sticky positioning property to keep a div container fixed at the bottom of the page

As Bootstrap 4 no longer supports .affix, I had to look for an alternative solution to keep a box fixed. I needed a div container to remain fixed as you scroll to it. My current workaround is using: .fixedcard{ position: sticky; top:75%; } However, th ...

Having trouble getting the toggle menu to work using Jquery?

I am trying to create a toggle menu using jQuery on this particular page: . The menu is located in the top right corner and I want it to appear when someone clicks on the "Menu ☰" button, and then disappear when clicked again (similar to this website: ). ...

Empower your presentations with slick cloning technology

I am facing an issue with my slider that I cannot seem to resolve. Currently, I am using slick to display 3 slides, but only the center one shows all the content. To achieve a continuous infinite slider effect where all slides appear in the center, I need ...

Showcasing a dynamically created JSON document on a basic web page

Looking for a solution to display the contents of a result.json file generated by my Java tool on a simple website. I am aware that accessing local files directly with JavaScript is not possible, so seeking alternative methods that do not involve using a w ...

Guide on utilizing Thymeleaf for dynamic updates in a table

My code includes a segment like this: <div class="ui-table"> <div class="items"> <div class="justify-content-end d-flex"> <span class="text- ...

Alignment of text fields in a vertical manner

How can I vertically align text input fields using CSS? <form action='<?= $_SERVER['PHP_SELF']; ?>' method='post'> <p><label for='username' class=''>Username:</label& ...

Searching for data in a bootstrap table that is not case-sensitive

Searching for and highlighting text within a bootstrap table has proven to be a challenge. Check out the Fiddle for an example: https://jsfiddle.net/99x50s2s/74/ HTML <table class='table table-bordered'> <thead> <tr& ...

Unusual spacing problem detected on iPad device

When I visit my demo site on my iPad, I'm having a strange issue where the player seems to be shifted to the top. This is how it looks on my iPad Mini running iOS 6.1 and tested on both Chrome and Safari: Here is the link to the demo site (change the ...

Can this layout be achieved using DIV elements?

Struggling to create a unique HTML/CSS layout that extends beyond the center? Imagine a standard horizontally centered page, but with one div expanding all the way to the right edge of the browser window. This design should seamlessly adjust to window res ...

[php][html] stuck on trying to solve this error

Any help in figuring out the error on line 37 would be greatly appreciated. I've tried moving the ""; after echo and putting the entire PHP code in an img class="profilePic" src="http://i.imgur.com/ZICYW5z.png"/> and then using echo for the name (I ...

How far apart are two surfaces when they are rotated along the Y-axis

Access Code: Click here to access the code I am trying to make sure that the red surface lies completely flat on top of the gray surface. However, there seems to be a gap (marked ??) between the two surfaces when I rotate 'modifier1'. How can I ...

Is it time to shake up the location of your Toastr

Recently, I've been working on implementing Toastr from this GitHub repository. While I am able to receive alerts and toasts as intended, I'm running into issues when trying to apply custom options. It seems to only stick to the default settings ...

Styling Descendants Conditionally in Angular 2

.parent { color: blue; } .parent * { color: red; } .child { color: black; } .grandchild { color: green; } <div class="parent"> Parent <div>Child <div>GrandChild</div> </div> </div> Imagine a scenari ...

Is there a way to horizontally align text in a div using center alignment?

Excuse me for what might seem like a silly question, but I am having trouble figuring this out. Edit: All I want is to horizontally center the text without affecting the image's position. The image should stay aligned with the baseline of the text. ...

When you click anywhere on the page, JavaScript's method __dopostback is triggered

I'm encountering an issue in my asp.net application where I have an html table. Specifically, when a user clicks on a td element within the table, I use JavaScript to store the id of that td element in a hidden field. Afterwards, I trigger __dopostbac ...