- This topic is empty.
-
AuthorPosts
-
December 29, 2011 at 2:52 pm #35883
mikes02
ParticipantI read Ethan Marcotte’s book on Responsive Web Design but something that still isn’t clear to me is when to use px vs. % for margins and padding on elements. If any of you have the book, on page 37 there is an example where he uses:
.article {
padding: 40px 8.48056537%
}
When is it appropriate to use PX vs. %? I am confused about using the combination of the two on one element.
December 29, 2011 at 3:01 pm #93636davidlab.be
Participantthat is px for top and bottem and % for left and right. Meaning the width is adjustable and the height will be a fixed height. Common for responsive web design, but you can also have the height adjust as well by using %…good for keeping content above the fold for different size browsers.
December 29, 2011 at 3:41 pm #93640mikes02
ParticipantHow could it be a fixed height if it’s responsive though?
December 29, 2011 at 3:44 pm #93641davidlab.be
Participantwhy not? mobile browsers scroll also so height is not that much as an issue as width of a device.
December 29, 2011 at 3:47 pm #93642noahgelman
ParticipantIt can have a fixed width, but a responsive width. For example:
#element {
padding:20px 15%;
}The height will stay the same but the width will change as you resize the page
December 29, 2011 at 4:29 pm #93648mikes02
ParticipantFrom what I understand, I set the width via percentage, I don’t touch the height at all, so let’s say I have a 500px width div inside of a 900px container, if I understand correctly the percentage would be:
#sample-div
{
width: 55.55555555555556%; /* 500/900 = 0.5555555555555556 */
}
But then if I want to add 30px of padding to that div, according to the box model the div would be 440px so the new math would be:
#sample-div
{
width: 48.88888888888889% /* 440/900 = 0.4888888888888889 */
}
So if I wanted to have 30px of padding all the way around the div? would it be
#sample-div
{
width: 48.88888888888889% /* 440/900 = 0.4888888888888889 */
padding: 30px 6.81818181818182%; /* 30/440 = 0.0681818181818182 */
}
or would it be percentage all the way around?
#sample-div
{
width: 48.88888888888889% /* 440/900 = 0.4888888888888889 */
padding: 6.81818181818182%; /* 30/440 = 0.0681818181818182 */
}
December 29, 2011 at 5:06 pm #93655davidlab.be
Participant**
Comment removed because there wasn’t really any need and this is a place of love, bunnies peace and other assorted furry animals. Thanks! :D– Robskiwarrior
**December 29, 2011 at 6:37 pm #93669mikes02
ParticipantI suppose it doesn’t have to be completely fluid, I was just trying that route out, but in the interest of time on this project you may be right in just using media queries to deliver different styles at the break points.
December 29, 2011 at 9:16 pm #93684mikes02
ParticipantFor the record though, I am looking to find a better understanding of percetage based margins and paddings on fluid layouts, if possible, based on what I posted above.
October 11, 2012 at 12:24 pm #111669relish27
MemberI’m also a little bit confused about height. I’ve been doing some of the mixed height-with-px-width-with-percentage business… but I have a situation where I want something absolutely fixed a certain percentage below an image that is resizing. I will probably just use media queries… but was hoping there was a more dynamic way to do this.
October 11, 2012 at 1:04 pm #111677Paulie_D
MemberPercentage doesn’t work for HEIGHTS.
Any percentage is ALWAYS a percentage of the page WIDTH.
October 11, 2012 at 5:56 pm #111707joshuanhibbert
Member@Paulie_D Hmm, not sure how accurate that last statement is? That is only true if there is no parent element with a width set.
October 12, 2012 at 4:02 am #111724Anonymous
InactiveI don’t use heights very often, but will use padding/margin to adjust height. I use % and px for top/bottom margin/padding and things work just fine. % gives a more proportional look, whereas px gives a more predictable look.
October 12, 2012 at 4:58 am #111725Paulie_D
Member@joshuanhibbert You are right. I ‘over-spoke’.
October 12, 2012 at 9:09 am #111732Vermaas
ParticipantCheck this definition: https://github.com/csswizardry/CSS-Guidelines#layout
I think this guy is right. An element should never contain a height property. Only when it’s predefined (like an icon or a picture that needs a particular height).
Thereby I prefer to use em’s or % instead of pixels to make everything flexible. Although height isn’t always that easy to define in paddings or margins.
In using “height” in paddings or margins, you should use % instead of px, in case your website is responsive. Because the big deal about responsive is: it’s responsive. It should be responsive, so make it responsive. Pixels are not responsive.
-
AuthorPosts
- The forum ‘CSS’ is closed to new topics and replies.