Decrease div height relative to its' original - html

How can I decrease the height of the div in relation to the height it would have been rendered natively?
Seems like a rather simple question but setting height to a percentage isn't an option, neither is margin: 0 -10px 0 0; or padding: 0 -10px 0 0; (obviously) there aren't any elements within the div I can decrease the size of, if I apply the margin it seems to work except border-radius is hurt from this process.

using jquery:
$('div').animate({'height':'-=10px'}, 1000);
the "-=10px" will decrease the current height by 10px.

Related

How to set custom "margin" on CSS

I have setted the body's margin to 0 on CSS. But I couldn't setted the div's margin. So, I want to set another things' margin to 0; but the abc classed div's margin to 5.My code is:
body {
margin: 0;
}
div {
margin: 5;
}
How can I do this?
if I understand correctly, you want all div margins to be 0 except for the div with abc class. to do that just set div element margin to be 0 and div with class abc to be 5px
div {
margin: 0px;
}
div.abc {
margin: 5px;
}
But it's not possible, when you set:
body{ margin: 0px; }
In fact you have set that only for body element and that doesn't set for other child elements like DIV or H and other tags ...
I think your code should work properly
Setting the margin of the body to zero wouldn't affect affect other margin of the parent elements.
If you want to set margins for parent elements, ensure you do so with figures and unit, the unit is very important, and some of these units are px, rem, em and as well as % (percentage).
And you should also note that "margin" works for margin top, bottom, left, and right.
You can read more on unit here:https://www.w3schools.com/cssref/css_units.asp
Margin: https://www.w3schools.com/css/css_margin.asp
Try adding px to the value 5 in your code above. Values in CSS should always have a unit. It could be px, pt, %, em, rem, etc.
With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).
Add px to 5 and 0, just like the way you will add m or cm to a measurement.

Margin and Padding behaviour

I'm getting strange behaviour from margins. A vertical scrollbar appears even though I'm no where near the bottom. I assume this is the desired behaviour, considering that I tested this and got the same results in the latest versions of Chrome, IE11 and Firefox.
The following code results in a scrollbar
<html>
<head>
<style>
body {
margin: 0;
height: 100%;
padding: 1px;
}
div {
margin: 15px;
}
</style>
</head>
<body>
<div>Hmm</div>
</body>
</html>
Changing the body's padding to 0.1px results in no margin.
Changing the body's padding to 0px also results in a margin.
Also, adding box-sizing: border-box to the body removes the scrollbar as long as the padding is not zero.
I haven't added a Fiddle because I can't replicate it there. You need to test this in a simple html file.
Is this actually the expected behaviour? Is there a logical explanation why they implemented it like this?
Looks like the reason you're seeing the scrollbar is a combination of defining a height and setting a padding value. The height property dictates the height of an element's content, excluding the margin and padding added onto that value. The scrollbar appears after adding padding because you've set the content of the element's height to 100% of the page, plus the padding, causing the element's entire height to overflow.
Additionally, applying box-sizing to an element makes the height and width properties include padding in the value. Funny thing is, it doesn't include margin. So if you were to apply:
body {
box-sizing: border-box,
margin: 1px,
padding: 0
}
You'd still see the scrollbar. Once understanding that an element's height property, by default, only dictates the height of the content within the element, it makes a little more sense.
Hope this helps :)
Setting the height of the body to 100% makes it take all of the height of it's parent element which is the html element. The html element's width and height in turn are governed by the window it is in. Adding a margin or a border would increase the dimensions beyond the available space thus inducing the scroll.
However, the other issue is that adding the margin to the div is pushing the body down by 15px. This has to do with collapsing margins.
Check out https://stackoverflow.com/a/2680515/6184852 for further information.

Add padding to min-height instead of including it?

Is there a way to have the padding on an element be added to the min-height value instead of being included in it? For example you have 10px of padding on the top and bottom a element and a min-height of 150px the height should be 170px instead of the element's height being 130px + 20px of padding.
I'm currently building a site using Material Design Lite and it adds padding to almost everything so it's a bit of a pain to have to always find the padding on something when you want to set a min-height value.
I might be a little late, but setting box-sizing: content-box; on the element would solve the issue.

Background image - padding changes size of background (with display block)

When you change the padding(top, bottom), it also changes (add 10px) the height of the background. How can I fix this? Vertically, it seems to work well.
http://jsfiddle.net/VyYB7/3/
I have added padding to my text/content.
Try using margin instead (or in addition) of padding, like so:
margin: 10px 0;
EDIT
To use both padding and margin (so that the text will be padded):
margin: 10px 10px;
padding: 0 10px;
Check out the CSS background-origin and background-clip properties. Set them to whatever works to get your background anchored in the right place (probably content-box for both).

How to Control Width of Title (H2) with CSS?

I've tried the CSS below. All of the other specifications are working except the width. You can see an example of the being too wide here if you need to.
Thanks for your help - Tara
.title h2 {
margin:10px 0 0 0;
width:780px;
font-family: HarabaraHandItalic;
font-size: 30px;
padding: 0 0 0 15px;
color:#000;
}
The width you're using is too wide. Try to make the width 750px for example. You'll see that it works fine!
Keep in mind that the padding will be added to the width of the element.
In your case the h2 element is 780px (width) + 15px (padding-left) = 795px.
It is working fine here.
I suspect you are being confused about what width means. The property describes the content width. The padding (15px) and borders (0px) and margin (0) all appear around the width.
The container is only 670px wide anyway, so you probably don't want something that is 795px across (width plus padding) inside it.
You need to reduce both the "width" and the "font-size" to make it fit.
Or, if the font-size needs to stay the same, you need to reduce the amount of copy.