Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to set width and height of elements? [closed]

Tags:

html

css

web

Every website has elements and their width and height must be set. here is my question how do we can set the width and height such that the website works correctly on different monitors with different width/height? is px preferred to %? when we should px and when we should use %? what are advantages and disadvantages of each one?

like image 639
M a m a D Avatar asked Feb 18 '14 16:02

M a m a D


People also ask

How can I set the height and width of an image?

The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.

Is used to set the width size of an element?

The CSS height and width properties are used to set the height and width of an element. The CSS max-width property is used to set the maximum width of an element.

When you set the width and height properties of an element with CSS you just set the width and height of the?

Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add padding, borders and margins.

Which element height and width can not be set?

Inline element properties: The height of an inline element is the height of the content. The width of an inline element is the width of the content. The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS.


3 Answers

It all depends upon the type of your website and how you want to response to various screen resolutions.

Just using % and px is not enough, there are other unit for text size for example em and pt etc.

Also we have media queries

http://kyleschaeffer.com/development/css-font-size-em-vs-px-vs-pt-vs/

http://msdn.microsoft.com/en-us/magazine/hh653584.aspx

http://www.htmlgoodies.com/html5/client/common-techniques-in-responsive-web-design.html#fbid=YIeEpYZXBt-

Relevant SO Question Responsive Layout - PX, EM, or %?

like image 167
Vinay Pratap Singh Bhadauria Avatar answered Nov 15 '22 00:11

Vinay Pratap Singh Bhadauria


Use px when you want to specify a fixed/static height in pixels. % refers to the percentage total of the elements parent. Using percent means the web site will adapt when the browser is resized wheras using pixels it will not.

Which one you choose really depends on the look and feel that you are trying to achieve.

like image 41
B-Lat Avatar answered Nov 15 '22 01:11

B-Lat


it all depends of what kind of web you plannning to do.

Many web pages use a container with a fixed width on pixels (like 960px). then it's extremely easy if you have a well-made predesign on photoshop, illustrator, indesign, etc to work with pixels. These webs though doesn't work well outside PC screens as if you have a window width smaller than the container the content won't be show unless scrolling horizontally.

Now, if you want your content to adjust to any horizontal width you should use % but imo it is much difficult and you need a clear idea what you want to show on each possible width before starting working.

If you decide to go for a "responsive" design... the work will be harder. I recomend you to use something like:

@media screen and  (min-width:600px) {}
@media screen and  (min-width:800px) {}
@media screen and (min-width:1024px) {}
@media screen and (min-width:1524px) {} 

in your CSS to tweek your classes wahtever necesary for each width (or platform). (there's also many other usefull @medias

like image 26
Alvaro Menéndez Avatar answered Nov 15 '22 01:11

Alvaro Menéndez