Continuously adjust element size as screen size ch

2019-01-29 04:17发布

Is there a way to use media queries to change the value of a CSS property gradually as the screen changes size (continuously/smoothly). Instead of setting a bunch of break points at which there is a change (step-ly)?

Like on this site: http://www.bluegoji.com/? The margins of the <a> tags in the <ul> at the top of the page (navbar) decrease continuously as you narrow the browser window.

That is the behavior I am trying to achieve.

Or is the solution just to use so many media queries that it appears to change smoothly??

3条回答
Explosion°爆炸
2楼-- · 2019-01-29 04:31

The example you gave uses flexbox like this:

ul {
  margin: 0;
  padding:0;
}
li {
  list-style:none;
  margin:0;
  padding:0;
  text-align:center;
  outline: 1px solid silver;
}

@media screen and (min-width: 500px){
  ul {
    display: flex;
    flex-direction: row;
  }
  li {
    flex-grow: 1;
  }
}
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

查看更多
放荡不羁爱自由
3楼-- · 2019-01-29 04:35

What you are looking for is not media queries. You can set widths, margins and padding in percentages. That is the normal way of doing this.

Here is an example:

HTML

<div>
    <a>Link</a>
    <a>Link</a>
    <a>Link</a>
    <a>Link</a>
</div>

CSS

a {
    width:25%;
    display:block;
    float:left;
}
查看更多
再贱就再见
4楼-- · 2019-01-29 04:40

Using lots of media queries is one method. But it may not be the best method.

Consider using viewport percentage units.

From the spec:

5.1.2. Viewport-percentage lengths: the vw, vh, vmin, vmax units

The viewport-percentage lengths are relative to the size of the initial containing block. When the height or width of the initial containing block is changed, they are scaled accordingly.

  • vw unit - Equal to 1% of the width of the initial containing block.
  • vh unit - Equal to 1% of the height of the initial containing block.
  • vmin unit - Equal to the smaller of vw or vh.
  • vmax unit - Equal to the larger of vw or vh.

DEMO

查看更多
登录 后发表回答