Border vs. padding in CSS - css

I'm attempting to understand the exact relation between padding and border in CSS.
Would it be accurate to say that a solid border of transparent color is essentially the same thing as the same amount of padding, and can be expected to behave similarly- except insofar as they will coexist on the same element rather than overwriting each other?
In other words, is there any case in which:
#Square1{
padding: 50px;
}
, if replaced with:
#Square1{
border-style: solid;
border-width: 50px;
border-color: transparent;
}
would be expected to display different behavior in any meaningful way, except if there was something like this in the same CSS file:
.ClassThatSquare1HappensToHave{
border-style: solid;
border-width: 50px;
border-color: transparent;
}
, in which case you will effectively end up with twice the amount of border/padding/element-expanding space?

Maybe this is what you are looking for. Note how overflow behaves differently both in the content that is left displayed (which is clipped to the padding edge) and the position of the scrollbar, which sits outside padding, but inside border.
#Square1{
padding: 50px;
}
#Square2{
border-style: solid;
border-width: 50px;
border-color: transparent;
}
body {
width:400px;
}
div {
background-color:lightblue;
height:100px;
overflow:auto;
}
<div id="Square1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<hr>
<div id="Square2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

They both belong to CSS box model but they serve different purposes.
In the box model :
The padding area, bounded by the padding edge, extends the content area to include the element's padding. Its dimensions are the padding-box width and the padding-box height.
(...)
The border area, bounded by the border edge, extends the padding area
to include the element's borders. Its dimensions are the border-box
width and the border-box height(...)
If the box-sizing property is set to border-box, the border area's
size can be explicitly defined with the width, min-width, max-width,
height, min-height, and max-height properties.
With that said, you should use padding whenever you want to give inner space and use border to give exactly what it states - a border - a line that if has padding will show after the padding area.

Related

How to delete the content that are out of a div size

I have a parent div with a certain size and inside him I have other divs and if any of those child divs start showing their content out of the parent div I want to delete that div, and no I don't want to put it hidden with the overflow I really want to delete him. I tried to search the solution but I didn't found any. How can I manage this problem?
I have this JSFiddle to a better understading the problem.
Here is the code:
<div style="width: 300px; height: 55px; background-color: gray">
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div style="border: 2px solid red">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. <strong>I WANT DO DELETE THIS DIV!</strong>
</div>
</div>
I don't know if I can do this with Blazor but I'm here to learn.
Thank you for your attention.
Basically you can do this by looping over the children, and checking if their top corner is “higher” on the Y axis (“higher” value on that axis meaning, it is lower down on the screen), than the parent’s own height. You can use the offsetTop property here, if the parent element is also the offsetParent of the children - if positioning is involved anywhere, that might change the offsetParent, so in that case you might need to work with coordinates relative to the viewport.
var p = document.getElementById('parent'),
c = p.children;
for(var i=c.length-1; i>=0; --i) {
console.log(c[i], c[i].offsetTop, p.offsetHeight)
if(c[i].offsetTop >= p.offsetHeight || c[i].offsetHeight > p.offsetHeight) {
p.removeChild(c[i]);
}
}
#parent { position: relative; }
foo
<br>
<div id="parent" style="width: 300px; height: 55px; background-color: gray">
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div style="border: 2px solid red">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. <strong>I WANT DO DELETE THIS DIV!</strong>
</div>
</div>
I am looping over the children in reverse order here - that gives less hassle with removing elements, and the index of the remaining elements in the live HTMLCollection returned by children.
I introduced the foo<br> at the beginning here, so that the whole element is set off a bit from the viewport - then you’ll notice how setting the parent to position:relative changes the offsetTop values, remove that from the CSS and see how the values in the debug output change.
Now this is a trivial way to do this, based on your rather trivial example. In more complex layout situations, it might not work that easily.
Edit: Need to check not only the offsetTop, but the offsetHeight as well. Otherwise, the last element might not get removed, if it is higher than the parent itself.
you need to add a css attribute overflow : hidden
See more: https://developer.mozilla.org/fr/docs/Web/CSS/overflow
I think this will work.. Try this code..
css
div div:not(:first-child) {
display: none;
}

Truncate text on line number N CSS

What is the best way to truncate text(or line clamp) on a specific row?
Lets say I have a paragraph with 8 lines of text but I only want to show 3?
Is this possible via CSS or do I need something else?
Set line-height, and max-height as multiple for n rows you want to show. For example, if line-height is 30px, only show 2 lines:
HTML
<p class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</p>
CSS
.text {
line-height: 30px;
max-height: 60px;
overflow: hidden;
}
Example: https://jsfiddle.net/rk8y0rsd/
Yes, it is simple, look the example bellow.
html:
<div class="i-speak-too-much">
Hello I like speak, I live in the future I am the Universe
</div>
css:
.i-speak-too-much {
width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; //making dots
}
jsfiddle: https://jsfiddle.net/ot9excbr/

Center last line of justified text in Safari and Chrome ("text-align-last" not working)

Hi I am trying to format my web paragraphs so that the text is justified and the last line is centered. I found the CSS property "text-align-last" which allows me to specify the alignment for the last line.
The problem is that this property is not supported by Chrome and Safari (yet...?).
Anyone have an alternative or a trick to do that?
The W3C manual: http://www.w3schools.com/cssref/css3_pr_text-align-last.asp
Thanks
I don't think it's possible in CSS alone.
Here's a JavaScript solution, in which a clone lies behind the element. The element has text-align: justify and the clone has text-align: center.
The code then reduces the height of the original element so that only the clone's last line displays.
var p= document.getElementById('lorem'),
clone= document.createElement('p');
clone.textContent= p.textContent;
clone.className= 'clone';
p.parentNode.insertBefore(clone, p);
p.style.height= p.offsetHeight - 14 + 'px';
#lorem, .clone {
position: absolute;
line-height: 14px;
font: 14px arial;
width: 500px;
}
#lorem {
text-align: justify;
overflow: hidden;
background: white;
}
.clone {
text-align: center;
}
<p id="lorem">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

Align Text Top with Sibling Element Background

Ignore whether or not this actually looks good.
I'm looking for the best way to align the top of text with an adjacent block element with a background or image in it. With the test-case snippet below, what I'm trying to [elegantly] get rid of is the red gap:
.col {
width: 40%;
min-height: 300px;
float: left;
margin-right: 4%;
}
.bg {
background: #333;
}
p,h1 {
margin: 0;
}
<div class="col bg"></div>
<div class="col">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
This is essentially coming from the line-height. Setting line-height to 1 solves the problem but then borks any multi-line text which I need to support. Even applying the line-height change just to the :first-line results in bad spacing on multi-line text. Right now, the best I can think of is trying to come up with some magic-number negative em margin-top value to apply to headings, paragraphs, etc., but I'm wondering if there's a better way.
The h1 itself is aligned to the simbling element, but the text inside it is not.
To verticaly align the text into a h1 tag you need to set the line height.
.col {
width: 40%;
min-height: 300px;
float: left;
margin-right: 4%;
}
.bg {
background: #333;
}
p,h1 {
margin: 0;
}
.col h1 {
line-height: 21px;
}
.col h1 span {
vertical-align:super;
}
div{
padding:0;
}
<div class="col bg"></div>
<div class="col">
<h1><span>Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title Title </span></h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
EDIT:
to use it in a multi-line: put your text into a span with a vertical-align:super; The line-height property on h1 will control the space between lines.
The other answer provided right now does work but I really wanted to avoid the extra markup required to make it work. Unfortunately that led me to a magic number solution. I'm not sure there's a good solution right now that's pure-CSS. I played with the :first-line pseudo selector but to ill effect.
What I ended up with was just using a negative margin-top on the <h1> element. I think the value of of the margin more or less works out to:
-(({line-height} - 1 ) / 2)em
That assumes you're using unitless line-height values.

textarea's rows, and cols attribute in CSS

I'd like to set the textarea's rows and cols attributes via CSS.
How would I do this in CSS?
<textarea rows="4" cols="50"></textarea>
It is equivalent to:
textarea {
height: 4em;
width: 50em;
}
where 1em is equivalent to the current font size, thus make the text area 50 chars wide.
see here.
width and height are used when going the css route.
<!DOCTYPE html>
<html>
<head>
<title>Setting Width and Height on Textareas</title>
<style>
.comments { width: 300px; height: 75px }
</style>
</head>
<body>
<textarea class="comments"></textarea>
</body>
</html>
I don't think you can. I always go with height and width.
textarea{
width:400px;
height:100px;
}
the nice thing about doing it the CSS way is that you can completely style it up. Now you can add things like:
textarea{
width:400px;
height:100px;
border:1px solid #000000;
background-color:#CCCCCC;
}
As far as I know, you can't.
Besides, that isnt what CSS is for anyway. CSS is for styling and HTML is for markup.
I just wanted to post a demo using calc() for setting rows/height, since no one did.
body {
/* page default */
font-size: 15px;
line-height: 1.5;
}
textarea {
/* demo related */
width: 300px;
margin-bottom: 1em;
display: block;
/* rows related */
font-size: inherit;
line-height: inherit;
padding: 3px;
}
textarea.border-box {
box-sizing: border-box;
}
textarea.rows-5 {
/* height: calc(font-size * line-height * rows); */
height: calc(1em * 1.5 * 5);
}
textarea.border-box.rows-5 {
/* height: calc(font-size * line-height * rows + padding-top + padding-bottom + border-top-width + border-bottom-width); */
height: calc(1em * 1.5 * 5 + 3px + 3px + 1px + 1px);
}
<p>height is 2 rows by default</p>
<textarea>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
<p>height is 5 now</p>
<textarea class="rows-5">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
<p>border-box height is 5 now</p>
<textarea class="border-box rows-5">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
If you use large values for the paddings (e.g. greater than 0.5em), you'll start to see the text that overflows the content(-box) area, and that might lead you to think that the height is not exactly x rows (that you set), but it is. To understand what's going on, you might want to check out The box model and box-sizing pages.

Resources