Exploring the potential of width and calc in CSS styling

I've been playing around with CSS calc to create some external spacing, similar to an external margin.

For example, in a 3-column layout, the central column ends up being slightly different in width compared to the other two. However, I'm struggling to figure out how to make the text within each column the same width.

There are certain requirements that I need to meet:

  • I must only use padding, not margin.

  • In this case, I cannot add padding to container C3.

  • I want to solve this using the logic behind calc. I am unsure whether percentages are applied first or if I should define the paddings first for the computer to later calculate the percentages...

  • While I prefer to use the border-box model because it solves many issues in my code, I am willing to sacrifice this if needed.

body {
  margin: 0;
  padding 0;
}
* {
  box-sizing: border-box;
}
p {
  text-align: justify;
}
.C3 {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}
.C3>div {
  width: 33.33%;
  padding: 50px;
}
.C3>div:first-child {
  background-color: #DFD;
  width: calc(33.33% + 140px);
  padding-left: 140px;
}
.C3>div:last-child {
  background-color: #FEE;
  width: calc(33.33% + 140px);
  padding-right: 140px;
}
<section class="C3">
  <div>
    <p>1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
  </div>

  <div>
    <p>2. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.</p>
  </div>

  <div>
    <p>3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros. Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
  </div>
</section>

Any suggestions?

Answer №1

If you're utilizing the box-sizing: border-box property, keep in mind that the width calculation already takes into account the padding. In this case, adding the 140px value twice is unnecessary.

To achieve the desired layout, simply use width: 33.33%.

Answer №2

After reading 4castle's answer, I had a realization that I was indeed repeating something, but upon closer inspection, it turned out to be the 50px margin.

To rectify this, I subtracted 50px from the calculation in my margin, resulting in a total of 90px.

The difference may seem subtle, but now I have achieved the exact same text size for each column.

body {margin: 0; padding 0;}

*{box-sizing: border-box;}

p {
text-align: justify;}

.C3 {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;}

.C3>div {
width: 33.33%;
padding: 50px;}

.C3>div:first-child {
background-color: #DFD;
width: calc(33.33% + 90px);
padding-left: 140px;}

.C3>div:last-child {
background-color: #FEE; 
width: calc(33.33% + 90px);
padding-right: 140px;}
<section class="C3">
<div>
<p>1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
</div>

<div>
<p>2. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.</p>
</div>

<div>
<p>3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros. Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
</div>
</section>

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

Adjust the styling of CSS classes within an ExtJS application

I've been trying to customize the appearance of my calendar panel using extjs. In my EHR application, I have implemented the extjs calendarpanel and datepicker similar to the setup showcased in this link: When a date is selected from the datepicker a ...

Is there a way to create space at the bottom after setting margin to auto?

I'm currently mastering Bootstrap v5.0 and experimenting with creating a webpage using Bootstrap. I've encountered an issue that requires assistance. Specifically, I'm trying to add extra space between two columns on smaller screens while ma ...

Is it possible to establish borders in relation to the text?

Take a look at this image that illustrates my goal. What I want to achieve is having lines on both sides of a text element (usually a heading tag) that stretch out a specific distance (in this case, 65px). I'm looking for a solution that adjusts dyn ...

Can you explain the functionality of this CSS rule: ~"-moz-calc(..)"?

As I was reviewing some CSS code, I came across a rule that appeared like this: width: ~"-moz-calc(100% - 10px)"; While I am familiar with -moz-calc, I am puzzled by why this is enclosed in a string and what exactly is the purpose of the '~' sy ...

Place a gap between each image

I currently have three posts on my front page, each spanning the full width of the page. I want to add a 30px padding to the left of these posts, but I'm facing an issue where there is also a 30px padding at the beginning of each row. My goal is to on ...

Is there a reason for specifying a CSS parameter twice, with an asterisk on the second declaration?

Similar Question: CSS reset - purpose of asterik within a style While going through the CSS styles for HTML5BoilerPlate, I stumbled upon an unfamiliar code snippet: button, input, select, textarea { font-size: 100%; margin: 0; vertical-align: base ...

How come my Ajax response is showing up above my navigation menu?

Every time I scroll my mouse, the admin cards end up covering the navbar, which is really annoying. I can't figure out where I went wrong with this issue. Any assistance would be greatly appreciated. Thank you! #mainsec { height: 100vh; width ...

Is there a way to adjust the alignment of the placeholder text to the top of a sizable input field?

Here is how my code currently appears: https://i.sstatic.net/WwXd2.png I am trying to align the text at the top of the input text field. I have attempted using align-text-top, align-top, and creating a custom class with vertical-align: top; and vertical ...

Tips for adjusting the size of an HTML 2D canvas when it changes during animation

I am striving to create an HTML/CSS layout that utilizes flexbox to fill available space with various elements, while also maximizing the size of a 2D canvas within that space. Although CSS flexbox can help achieve this desired layout, there is a challeng ...

Dynamic CSS overlay based on database field content without requiring user interaction

Is there a way to automatically display overlays for specific options in a database field using Bootstrap code? The options are SOLD, CONTRACT, and NO. I need different overlays to appear for SOLD and CONTRACT, while no overlay is needed for the NO option ...

How to attach an event listener to an input element using Angular

I am looking to add a listener to an input element that will be triggered every time the user changes the input values. The goal is to display the current values chosen by the user. Example HTML template: <div id="idDoseLabel1" class="da ...

Adjust the text in Bootstrap to collapse on command

I'm currently utilizing Bootstrap collapse in my project. I am attempting to implement a basic jQuery script that changes an icon once a user clicks on one of the collapses. Since there are multiple collapses on the page, I believe I need to use the " ...

Tips for incorporating color into the bootstrap card's left side

I need assistance in adding color to the left side of a bootstrap card like shown below: enter image description here I am unsure how to do this, can anyone provide guidance? ...

How to center align an input vertically with Bootstrap 3

I am having trouble vertically aligning an input in my project. Interestingly, my code works for a span but not for the input! I'm puzzled - what could be the reason behind this? JSFiddle Here is the CSS code snippet: .float { display: table-cel ...

The drop-down links vanish into the depths of the webpage's body

Can someone assist with my drop-down menu issue for the link "services"? The link disappears behind the page content when I try to modify it. Any help would be appreciated, thank you! This is the CSS: .bodycontent { margin: 0 25% 0 25%; ...

Can an image be resized to fill either the full height or width based on its orientation?

Looking to display a dynamic list of images with varying sizes and orientations - some landscape, some portrait. The challenge is that the landscape images should fill 100% width of their parent container, while the portrait images should fill 100% height ...

Ensure the website is able to adjust its size when the top banner increases in size

On my website, there is a top banner containing contact information that expands when clicked. However, the expanded div overlaps the site content. Is there a way to make the site scale with the expanded div so that the top of the site starts below it? I ...

Deactivating Cluetip tool tips and adjusting the height limit in JQuery

How can I momentarily turn off the hints? I have seen references to being able to do so on the website and in this forum, but I am having trouble locating the command to disable them. I just need to temporarily deactivate them and then enable them again. ...

When hovering over the image, it enlarges and gradually becomes transparent, unveiling the hidden text beneath

I've been struggling to achieve the desired effect for quite some time now, putting in hours of effort without success. Essentially, I am aiming to create a div that showcases an image. When the mouse hovers over the image, it should zoom in and fade ...

Issue with the flexbox div not fully occupying the vertical space

I can't seem to get flexbox to fill the spaces as I want it to. I've been trying to figure out what I'm missing, but I just can't wrap my head around it. Here's a recreation of the issue with a link to a codepen. I want the div in ...