Apply borders to inline-block elements uniformly

Click here for the code snippet

Can anyone suggest a solution to adding borders to inline-block divs in a way that avoids repetition and ensures equal border width on each side of the box? I am trying to create a calendar layout.

#parent{
  width: 400px;
}
#parent > div{
  display: inline-block;
  width:50px;
  height:50px;
  line-height:50px;
  background:cyan;
  border:1px solid;
  text-align:center;
}
<div id="parent">
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div>
</div>

Answer №1

A more straightforward approach is to utilize the outline property as an alternative to using border

#parent > div{
    display: inline-block;
    width:50px;
    height:50px;
    line-height:50px;
    background:cyan;
    outline: 1px solid;
    text-align:center;
  }
<div id="parent"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div>
  </div>

Answer №2

To create the desired layout, apply a negative margin to the elements.

#parent {
  width: 400px;
}

#parent>div {
  margin: -1px 0 0 -1px;
  display: inline-block;
  width: 50px;
  height: 50px;
  line-height: 50px;
  background: cyan;
  border: 1px solid;
  text-align: center;
}
<div id="parent">
  <div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div>
</div>

Answer №3

To achieve the desired effect, consider applying a negative margin either to the top and left or right and bottom edges:

margin: -2px 0 0 -2px;

Answer №4

To ensure proper alignment, it is important that the last line in a display contains an equal or fewer number of elements compared to the previous lines. Additionally, knowing the exact number of elements in one line allows you to avoid using wrapping.

#parent > div{
  display: flex;      
}

#parent > div > div{
  display: flex;
  width:50px;
  height:50px;
  line-height:50px;
  background:cyan;
  justify-content:center;
  align-items: center;

  border-bottom:1px solid;
  border-right:1px solid;
}

#parent > div:first-child > div {
   border-top: 1px solid;
}

#parent > div > div:first-child {
   border-left: 1px solid;
}
<div id="parent">
  <div>
    <div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div></div>
  <div><div>8</div><div>9</div><div>10</div></div>
</div>

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

Can someone help me understand how to change the structure in order to identify which class has a body?

I have a small example to share with you: link HTML CODE: <div class="body"> <div class="test">TEST</div> </div> JS CODE: switch (className) { //instead of n, I need to write className case body: alert("my cla ...

What is the best way to shift a text element within the footer section?

I have arranged two columns within the container, but I am looking to position the .company and .copyright text at the bottom left of the footer column. The Follow Us heading should be on the right side with the .justify-text below it, followed by social m ...

Position a DIV element without specifying the width dimension

Possible Duplicate: How can I center something if I don't know ahead of time what the width is? I am seeking guidance on aligning a div element without prior knowledge of its width. My approach differs from previous inquiries as I am open to usin ...

Creating Tabbed Navigation with CSS - A Step-by-Step Guide

I am looking to create tab contents using CSS. I have managed to create a custom tab style, but I am struggling to style its content with CSS. Can anyone provide guidance on how I can achieve this? CSS: .tabs { list-style: none; margin: 0; pa ...

Discovering the Javascript Code Executing on a <span> element with the Help of Chrome Inspector or Firebug

I have encountered a situation where a website is dynamically generating information into <span></span> tags using jQuery. The span has a class of "Price" and an id corresponding to a Product Code, with the data being pulled from a JSON data sh ...

Troubleshooting problem with clicking on SVG within an HTML <div>

I currently have a parent container with a child object tag that houses my svg. I've been attempting to detect clicks on my image, but unfortunately, it's not functioning as expected. In the provided code snippet, the alert message only appears ...

What is the method for inserting form control values into a QueryString within HTML code?

Struggling with passing HTML form control values into a QueryString for page redirection. While I can easily input static values into a QueryString and retrieve them using PHP's GET method, I am encountering difficulties when it comes to dynamic valu ...

The designated <input type=“text” maxlength=“4”> field must not include commas or periods when determining the character limit

In the input field, there are numbers and special characters like commas and dots. When calculating the maxLength of the field, I want to ignore special characters. I do not want to restrict special characters. The expected output should be: 1,234 (Total ...

Hovering over one element in Jquery triggers actions on multiple elements simultaneously

I'm working on a project where I have multiple cards, and I've implemented a hover effect using jQuery to make the images inside the cards bigger. However, I'm facing an issue where hovering over one card triggers the effect on all the other ...

HTML list with clickable elements for querying the database and displaying the results in a <div> element

Patience please; I am a newcomer to StackOverflow and this is my inaugural question. Struggling with writing an effective algorithm, I wonder if my attempts to "push" it forward are causing me to complicate what should be a straightforward concept, or if i ...

Javascript addClass and removeClass functions are not functioning as expected

Can you help me figure out why the icon still goes into the "startSharing" section even when it is turned off? In my html file, I have the following code for the icon: <div class="startSharing"></div> And in my javascript file, I have the fo ...

Make your grid easily expandable with full width capabilities using the power of Bootstrap 4

I am currently working on creating a grid for a gallery that features an expanding preview to showcase more details. I found some helpful code in this informative article: https://tympanus.net/codrops/2013/03/19/thumbnail-grid-with-expanding-preview/ Every ...

Copying to the clipboard now includes the parent of the targeted element

Edit - More Information: Here is a simplified version of the sandbox: https://codesandbox.io/s/stupefied-leftpad-k6eek Check out the demo here: https://i.sstatic.net/2XHu1.jpg The issue does not seem to occur in Firefox, but it does in Chrome and other ...

HTML input field template

Is there a way to limit the HTML text box to only allow specific patterns such as: A1+A2*A3 It must consist of alphanumeric characters (A1 to A5) 2) It should be able to recognize and accept arithmetic operators in the specified format above. I am util ...

Arrange the image in a way that flows smoothly with the text

I want to achieve the effect of having a picture of a Pen positioned behind the text "Create" on the left side. It looks good at full size, but the positioning gets messed up when the screen size is adjusted. How can I make it responsive so that the image ...

Customizing material-ui-next: Adjusting image dimensions to perfectly fit within a specified container

Struggling with Material-ui-next, I'm facing difficulties in making images take up the entire size of a container. Here's an example of my code: const styles = theme => ({ Card: { width: 300, margin: 'auto' }, Media: { ...

Create a div on the right side that expands to occupy all remaining space

Is there a way to have two divs positioned next to each other, with the left one being a fixed width of 300px and the right one taking up the rest of the available space on the screen? I appreciate any guidance you can provide. Thanks! ...

Can you tell me the CSS equivalent of the SASS/SCSS expression "&$"?

Hey there! I've been diving into the world of Material-UI components and stumbled upon this interesting styling snippet: root: (0, _extends3.default)({}, theme.typography.subheading, { height: theme.spacing.unit * 3, boxSizing: 'content- ...

Elevate when the mouse hovers over the button

My current task involves increasing the bottom-padding of a span when the mouse hovers over a button. Here is the button code: <button class="btn btn-success btn-circle" id="myBtn" title="Go to top"> <span id="move" class="fa fa-chevron-up"&g ...

I am experiencing issues with React Bootstrap's responsive mode not functioning as desired

As a new developer for bootstrap, I created a product that collapses automatically from either the right or left side of Chrome in a responsive manner. However, when I manually choose an absolute width using the inspect tool, it does not display the same a ...