简体   繁体   English

CSS:元素的复杂宽度规则

[英]CSS: Complex width rules for an element

I'll save you the explanation of exactly what it is, but is it possible to have an element that has the following:我将为您解释它到底是什么,但是否有可能拥有一个具有以下内容的元素:

If the screen is less that say 600px then the element will be width: 100%;如果屏幕小于 600 像素,则元素将为 width: 100%; If the screen is greater than 640px then the element has a variable width but with margins of 20px on the left and right.如果屏幕大于 640 像素,则元素具有可变宽度,但左右边距为 20 像素。 Between 600 and 640 px the margin goes from 20px to 0px.在 600 到 640 像素之间,边距从 20 像素变为 0 像素。 Ideally stepped so when the screen has a width of 620 the margin is down to 10px etc. In addition, when the screen is >1000px then the width remains fixed at that, and the element remains centred.理想情况下,当屏幕宽度为 620 时,边距降至 10 像素等。此外,当屏幕 > 1000 像素时,宽度保持固定,元素保持居中。

My css knowledge is basic.我的 css 知识是基本的。 On their own I can implement any of those easily, but together?我自己可以很容易地实现其中的任何一个,但是在一起呢? I'm looking at javascript solutions, but as a last ditch attempt at a nice solution I'm asking here.我正在查看 javascript 解决方案,但作为最后一次尝试一个不错的解决方案,我在这里问。

Thanks谢谢

Mat

see: http://css-tricks.com/resolution-specific-stylesheets/见: http://css-tricks.com/resolution-specific-stylesheets/

This is available for most modern browsers.这适用于大多数现代浏览器。

You have TWO options here.您在这里有两个选择。

Option 1 : Stick to a fixed width of 600px and let it automatically get centered.选项 1 :坚持 600px 的固定宽度并让它自动居中。

To do this simply add the following line into your CSS for the outermost DIV tag:为此,只需将以下行添加到您的 CSS 中以获得最外层的 DIV 标签:

margin: 0 auto;

Option 2 : This is to use JavaScript.选项 2 :这是使用 JavaScript。 You can first set the min-width property of the outermost DIV using CSS.您可以首先使用 CSS 设置最外层 DIV 的 min-width 属性。 Then let JavaScript do the conditional calculations.然后让 JavaScript 做条件计算。

Personally, I'd go for option 1.就个人而言,我会选择 go 作为选项 1。

Below is the CSS and HTML of a layout for a site I made a for my son's Baseball Team.下面是我为儿子的棒球队制作的网站布局的 CSS 和 HTML。 The layout is similar to what you want.布局与您想要的相似。

CSS CSS

/* page element and contents */
#page
{
    overflow:auto;
    height: 100%;
}
#centerContent
{
    position: relative;
    overflow: hidden;
    height: 100%;
    width: 66%;
    min-height: 430px;
    min-width: 778px;
    margin: 0px auto;
    border-left-style: ridge;
    border-right-style: ridge;
    border-left-width: 5px;
    border-right-width: 5px;
    border-right-color: #f6e662;
    border-left-color: #f6e662;
}

#leftMargin
{
    position: absolute;
    overflow: hidden;
    left: 0px;
    top: 0px;
    height: 100%;
    width: 17%;
    border-right-width: 5px;
    text-align: center;
}
#rightMargin
{
    position:absolute;
    overflow:hidden;
    right:0px;
    top:0px;
    height:100%;
    width:17%;
    border-left-width: 5px;
}

/* header element and contents */
#header
{
    overflow:hidden;
    background-color: #FFFFFF;
    border-bottom: solid 1px #053c01;
    height:75px;
    top: 0px;
    left: 0px;
}
/* main element and contents. */
#main 
{
    position:absolute;
    overflow:hidden;
    background-color: #FFFFFF;
    top: 76px;
    bottom: 25px;
    left: 0px;
    right: 0px;

}
/* footer element and contents */
#footer
{
    position:absolute;
    overflow: hidden;
    background-color: #FFFFFF;
    border-top: solid 1px #053c01;

    height: 25px;
    bottom: 0px;
    left: 0px;
    right: 0px;
}

HTML HTML

<div id="page">
  <div id="leftMargin">
  </div>
  <div id="centerContent">
    <div id="header">
    </div>
    <div id="main">
    </div>
    <div id="footer">
    </div>
  </div>
  <div id="rightMargin">
  </div>
</div>

You can achieve that with media queries in modern browsers, just not in IE8 or earlier.您可以在现代浏览器中使用媒体查询来实现这一点,但在 IE8 或更早版本中则不行。 So for example you'd have:因此,例如,您将拥有:

@media screen and (max-width:600px) {
    #divID {width:100%;}
}

Which says if the screen is under 600px wide, then the div with the id called divID has a width of 100%.这表示如果屏幕宽度低于 600 像素,则 id 名为 divID 的 div 的宽度为 100%。

You can set conditions for an in between size like this:您可以像这样设置中间尺寸的条件:

media screen and (min-width: 601px) and (max-width:640px) {
    #divID {width:auto;}
}

And you can have a setting for widths over a certain size:您可以设置超过一定尺寸的宽度:

@media screen and (min-width: 601px) {
        #divID {width: 500px;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM