Why use percentage value for div's width in CSS?

前端 未结 8 1857
无人及你
无人及你 2021-02-13 11:09

I am reading articles about css. I found out that many of writers suggest to use % value for div\'s width or height.

I am using pixels all the time.

Why should I

8条回答
  •  情深已故
    2021-02-13 11:44

    Although certainly not the only reason, use percentage to take up a certain amount of space of something you don't initially know the container parents width of.

    For example, let's say I want my div element to always occupy at least 50% of the viewers browser window, I could write that as:

    This will take up 50% of the width

    Doing this the fixed way by using "px" will require you know the resolution of the browser if you want at least 50% width taken up. You could do this using JavaScript, but CSS is much more easier and faster.

    Some other Examples using tables, divs, and animations: http://jsfiddle.net/BLQp7/1/

提交回复
热议问题