Adding a thin line at the bottom of the element(s) positioned above a horizontal row with a bottom border

I have a variable number of items that may or may not fit in one "row" inside a container. If they do not fit, additional "row(s)" should be created.

Each item will only contain a single word (or maximum 2), keeping the data to one line. I want each item to have a bottom border of 1px, and each row should also have a bottom border of 1px, resulting in a layout like this:

|-----------------------------------
|                                  |
|  Foo bar    Foo bar      Foo bar |
|--=======-------------------------|
|                                  |
|                                  |
|  Foo bar    Foo bar      Foo bar |
|----------------------------------|

(The mouse is hovering over the first item).

The bottom border of the items should overlap the line at the bottom of the row (concealing as much of the row border as the width of the item allows).

I found this codepen, but

  • I am unsure how to style the bottom border of each "row"
  • The items shift whenever I hover over them with the mouse

What steps can I take to achieve the desired outcome?

Note: I am open to using CSS3 if it simplifies the process.

.container {
  border: 1px solid grey;
  width: 300px;
}

.item {
  float: left;
  /*border: 1px solid green;*/
  border: 1px solid transparent;
  padding: 2px 7px 2px 7px;
}

.item:hover {
  border-bottom: 1px solid red;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
</div>

Answer №1

When dealing with single-line content, a clever trick is to set the height of each item to a fixed height (e.g., height: 2em) and then utilize a linear-gradient background for the parent container at the same height. This creates a row line effect, visible in the second block of code below. The lines appear as part of the parent's background, even without any actual content. It gives the impression that each item has a full-width bottom border.

This method doesn't require changes to your markup, but keep in mind that it works smoothly as long as the content fits within one line only.

.container {
  width: 300px;
  font-size: 14px;
  /* creating blue lines */
  background: linear-gradient(to bottom, transparent 1.95em, blue 1.95em);
  background-size: 100% 2em;
  background-origin: border-box;
  background-position: 0px 0px; /* y position depends on border-top-width */
}
.item {
  box-sizing: border-box; /* includes padding in element's dimensions */
  float: left;
  height: 2em; /* fixed height */
  padding: 2px 7px 2px 7px;
  border-bottom: 1px solid transparent; /* prevents jumping */
}
.item:hover {
  border-bottom: 1px solid red; /* red line appears on hover */
}

/* just for demonstration */

.demo{
  margin-top: 10px;
  border: 1px solid grey;
  background-position: 0px 1px;
  height: 8em;
}
.container:hover{
  font-size: 16px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="item">Foo bar long</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
  <div class="item">Foo bar</div>
</div>

<div class="container demo">

</div>

Output Screenshot:

https://i.sstatic.net/7L2We.png

Answer №2

By converting your HTML to grouped elements, you can achieve a cleaner structure:

<div class="container clearfix">
   <div class="row">
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
   </div>
   <div class="row">
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
   </div>
   <div class="row">
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
       <div class="item">Foo bar</div>
   </div>
</div>

You can add styling effects by using the following CSS:

.row:hover .item {
      border-bottom: 1px solid red;
}

Check out this fiddle for a live example:

https://jsfiddle.net/1LLzr8q7/

Answer №3

Make sure to include the following CSS code in your stylesheet.

.element {
  display: block;
  border-top: 1px solid #ccc;
  padding: 5px 10px;
}

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 implement a Mouse Over event listener for a FlexTable in GWT 1.7?

What is the best method to attach an event listener or handler to widgets in GWT 1.7? I have looked at similar questions on SO, but they appear to be outdated. How can I add a Hover listener to a FlexTable, disregarding the existing :hover in CSS? ...

Adding a background image to a box in MUI is a simple task that can enhance

I've been attempting to include a background image in the Box component of mui, but I can't seem to get it to work. Here is the code I've been using: const Main = () => { return ( <Box sx={{backgroundImage:'images/cove ...

Issues with Angular directive causing form field validation to malfunction

Currently, I am working with the most recent version of AngularJS, specifically 1.2rc3. Bootstrap is being utilized for styling purposes and I have a directive set up as follows: angular.module('form.field', []) .directive('efield', f ...

Can someone please help me figure out why the "setInterval" function in my script isn't functioning as expected?

I've been experimenting with controlling the refresh rate of drawn objects in a canvas using JavaScript. Even after going through the tutorials and examples on w3.school, I'm still unsure why the "setInterval" function is not executing the "gener ...

A guide on dynamically adjusting image size in ReactJS

Here is the structure I have: img { width: auto; height: 200px; } .cards { display: flex; justify-content: center; margin-top: 2em; width: 80%; flex-wrap: wrap; } .card { margin: 1em; box-shadow: 0 0 6px #000; ...

Executing a C# program that sends a web request without using JavaScript

My goal is to programmatically download the contents of a website, but it seems like this content is being loaded through ajax calls. Interestingly, when I disable javascript in my browser, only one request is made by the page and all the content is displa ...

Changing colors when hovering over different elements

I am struggling to create a hover color change effect that applies to all elements (icon, text, and button) from top to bottom. Currently, the hover effect only changes the color of either the text and icon or the button individually. Below is the code sn ...

Creating a FadeIn animation for a hidden div element by using the display:none property

Struggling to implement a fadeIn function for a div tag with a display:none property. How can I make the div tag visible and smoothly fade in while still keeping the display:none property? Attempted solution so far: <div class="graphs_line_based cle ...

Ways to show a div element within an input field?

I want to incorporate tags similar to stackoverflow on my website. Users will be able to create tags for filtering results, searching, showcasing expertise, and more. I have managed to create tags, but I am struggling to display them inside an input box l ...

Background color set for only half of the div

Is it possible to achieve a design similar to this using CSS, with a Half-background color for a div? ...

Prevent the modification of a session

My dilemma involves a piece of code that produces a random number ranging from 1 to 1000. This number is then saved as a session and sent to you via email where it needs to be entered into a form before submission. However, the issue arises when submitting ...

JavaScript/HTML5/jQuery Drag-And-Drop Upload - "Error: Unable to access the 'files' property"

Just a heads up, my JavaScript skills are pretty limited. Here is the current JavaScript code I have: $('#xhr-filebox').bind({ "dragover": HandleDragEvent, "dragleave": HandleDragEvent, "drop": HandleDropEvent }); function HandleDr ...

What could be the reason for the absence of a second alert appearing?

I have a small application that validates email addresses for the correct elements, but it's not displaying the confirmation message. $(document).ready(function(){ $("#sendButton").click(function(){ var name = $("#name").val(); ...

Tips on ensuring Material-UI Datatable adapts to varying window dimensions

I am facing an issue with the responsiveness of my MUIDatatables. The table is not adjusting properly to different window sizes. I specifically want each row in the table to be displayed on a single line and utilize horizontal scrolling. I attempted to pl ...

JQuery functions for click and hover are not functioning as expected on a div element

I am having trouble with a div that I have declared. The click event is not working as expected, and I also need to use the :hover event in the css, but it isn't functioning either. What could be causing this issue? <div id="info-button" class="in ...

Can you provide some examples of CSS code snippets?

Can someone share some CSS codes similar to :hover that can be used in the : part for different effects? I tried looking it up but couldn't find much, so any help would be greatly appreciated. ...

Incorporate a Selectable Class when selecting a cell in a table

I have a single HTML table. <table id="tbl_1"> <tr> <td>ABCD</td> <td>ABCD</td> </tr> <tr> <td>ABCD</td> <td>ABCD</td> </tr> < ...

The onchange() function appears to be undefined, could it be inaccessible from the index.html file?

I have encountered an issue while trying to assign an 'onchange' attribute to a <select> tag in my HTML. The function I defined as filterChanged() in my main.js file (which is bundled into bundle.js) cannot be accessed when the onchange eve ...

Using the power of jQuery to load Ajax content, unfortunately, the content remains

Hey there, I'm currently working with an HTML file that utilizes AJAX to load content from other HTML files on the same server. However, for some reason, it's not functioning properly. I'm new to AJAX and I'm not sure what could be caus ...

Ways to Get Rid of Line Beneath the Table

Can anyone assist me in removing the underline at the bottom of my table? Here is the code I am using: <table> <tbody> <tr> <td><iframe src="//player.vimeo.com/video/99496559" width="220" height="150" frameborder="0" allowfull ...