html - 如何在页面上定位 CSS 元素?没有位置属性?

标签 html css

我用 CSS 制作了 4 个进度条,并将它们插入到我的容器中,但是我无法让它们从左侧移动而不会弄乱,我希望它们这样当人们调整浏览器窗口大小时它也会移动与它。

图像 1:这是我添加“位置:___”属性时发生的情况:

snapshot

如果没有 position 属性,它似乎会正确停留和移动,这很好,但我现在想在页面上随心所欲地移动条形图。

HTML

<div class="meter">
  <span style="width: 90%"></span>
</div>
<div class="meter">
  <span style="width: 70%"></span>
</div>
<div class="meter">
  <span style="width: 50%"></span>
</div>
<div class="meter">
  <span style="width: 90%"></span>
</div>

CSS

.meter { 
  height: 15px;  /* Can be anything */
        margin-bottom: 10px;
  top: 80px;
    left: 600px;
    background: #555;
    -moz-border-radius: 25px;
    -webkit-border-radius: 25px;
    border-radius: 25px;
        width: 210px;
    padding: 0px;
    -webkit-box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3);
    -moz-box-shadow   : inset 0 -1px 1px rgba(255,255,255,0.3);
    box-shadow        : inset 0 -1px 1px rgba(255,255,255,0.3);
}
.meter > span {
  display: block;
    height: 100%;
       -webkit-border-top-right-radius: 8px;
    -webkit-border-bottom-right-radius: 8px;
           -moz-border-radius-topright: 8px;
        -moz-border-radius-bottomright: 8px;
               border-top-right-radius: 8px;
            border-bottom-right-radius: 8px;
        -webkit-border-top-left-radius: 20px;
     -webkit-border-bottom-left-radius: 20px;
            -moz-border-radius-topleft: 20px;
         -moz-border-radius-bottomleft: 20px;
                border-top-left-radius: 20px;
             border-bottom-left-radius: 20px;
    background-color: rgb(43,194,83);
    background-image: -webkit-gradient(
      linear,
      left bottom,
      left top,
      color-stop(0, rgb(43,194,83)),
      color-stop(1, rgb(84,240,84))
     );
    background-image: -webkit-linear-gradient(
      center bottom,
      rgb(43,194,83) 67%,
      rgb(84,240,84) 69%
     );
    background-image: -moz-linear-gradient(
      center bottom,
      rgb(43,194,83) 37%,
      rgb(84,240,84) 69%
     );
    background-image: -ms-linear-gradient(
      center bottom,
      rgb(43,194,83) 37%,
      rgb(84,240,84) 69%
     );
    background-image: -o-linear-gradient(
      center bottom,
      rgb(43,194,83) 37%,
      rgb(84,240,84) 69%
     );
    -webkit-box-shadow: 
      inset 0 2px 9px  rgba(255,255,255,0.3),
      inset 0 -2px 6px rgba(0,0,0,0.4);
    -moz-box-shadow: 
      inset 0 2px 9px  rgba(255,255,255,0.3),
      inset 0 -2px 6px rgba(0,0,0,0.4);
    position: relative;
    overflow: hidden;
}

DEMO

(如果答案是某种类型的 float ,请注意当您在我上面提供的 CSS Desk 链接中添加“float: right;”时进度条会发生什么情况,请尝试向代码中添加 float 。进度条变为垂直对齐)

最佳答案

只需将这些 CSS 规则添加到 .meter 类:

float: right;
clear: right;

这是 Fiddle .

狮子座!

编辑:如果您想“在您的控制下”移动一个元素(我没有想到更好的定义)并在容器内(我的意思是,与浏览器的窗口无关)只是添加这些 CSS 规则:

到父元素:

position: relative;

到子元素:

position: absolute;
top: value;
right: value;
left: value;
bottom: value;

其中 toprightleftbottom 属性是将元素 移动到“下面你的控制权”

我希望你能理解我。


这是给 child 添加position: absolute而不给父添加position:relative的区别:

标记:

<div>
  <img src="images/img.png" />
  <p>Lorem ipsum dolor sit amet....</p>
</div>

CSS:

div img {
  position: absolute;
  top:  50px; 
  left: 50px;
}

enter image description here

如您所见,使位置成为浏览器窗口的引用。

但是,如果您将 position: relative; 添加到父...

div {
  border: 2px solid #CCC;
  padding: 1em;
  margin: 1em 0 1em 4em;
  width: 300px;
  position: relative;
}

div img {
  position: absolute;
  top: 50px; 
  left: 50px;
}

enter image description here

引用是父元素。


图片取自http://www.librosweb.com (学习网络技术的好网站 - 西类牙语)。

关于html - 如何在页面上定位 CSS 元素?没有位置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17540560/

相关文章:

html - 如何使容器中的表单居中?使用 flex

javascript - 更多页面中的 Websocket

javascript - 使用 Knockout 更改单选按钮时设置复选框

css - 将 Font Awesome 图标添加到 Bootstrap 4 选择

css - 在 2 行和 2 列响应中打破 4 个 div

html - 右浮动框的宽度到最大宽度

javascript - javascript 变量内的 html 和 php

javascript - 结合 javascript 和 jquery 来填充多个隐藏字段 - 坏主意?

css - Google 自定义搜索样式问题

javascript - Highstock显示不一致