调整页面大小时div会移动

时间:2014-08-06 15:59:49

标签: html css css-position

我正在尝试重新创建的网站上有一个时间轴,当我将窗口调整为移动设备时,div会分开。我离网站有点远,对于我需要做些什么才能做到这一点非常谨慎。时间线有图标(有自己的div)&一个图标背景(也有自己的div),但我想我需要将它们包裹在一个更大的div中以使这个工作,但不知道如何。

在调整大小之前和调整大小之后,请在此处查看图像:

enter image description here

了解图标如何与背景分离?

enter image description here

时间轴中每个项目的Html代码:

                    <div class="timeline_item">
                     <div class="timeline_time"><p><em>1 hr ago</em></p></div>
                     <div class="icon_background"></div>
                     <div class="timeline_icon "><i class="ss-icon">doc</i></div>
                     <div class="timeline_text"><p>You read the article </p> </div>                  
                    </div>

CSS:

.timeline_item {
 width: 100%;
 padding-bottom: 17%;
}

.timeline_item .timeline_time {
 float: left;
 width: 20%;
}

.timeline_item .timeline_icon {
 float: left;
 width: 1%;
 }

.timeline_item .timeline_text {
 float: left;
 width: 65%;
 padding-left: 3%;
}

.timeline_icon {
 margin-left: -4%;
 margin-top: 2.5%;
}

2 个答案:

答案 0 :(得分:1)

无法访问正确的Jsfiddle演示,这里有一个使用伪元素的HTML结构减少的建议。

您可以使用图标字体,精灵或任何我使用过单个字母的地方。

Codepen Demo

<强> HTML

<div class="timeline">

  <article class="timeline_item">
    <div class="timeline_time">
      <p>1 hr ago<p>
    </div>
    <div class="timeline_text" data-event-type="doc"><p>You read the article </p> </div>                  
  </article>

  <article class="timeline_item">
    <div class="timeline_time">
      <p>1 hr ago<p>
    </div>
    <div class="timeline_text" data-event-type="alert"><p>You have a 'Do Not Miss' Meeting scheduled for tomorrow 9a.m.</p> </div>                  
  </article>

  <article class="timeline_item">
    <div class="timeline_time"><p>1 hr ago<p>
    </div>
    <div class="timeline_text" data-event-type="video"><p>You watched a video</p> </div>                  
  </article>

</div>

<强> CSS

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.timeline {
  width:50%;
  margin: 1rem auto;
  border:1px solid lightgrey;
}

.timeline_item {
  display: table;
  width:100%;
}
.timeline_time,
.timeline_text {
  display: table-cell;
  padding:1rem 2rem;
}

.timeline_time {
  width:25%;
  text-align: right;
  border-right:1px solid lightgrey;
}

.timeline_text {
  position: relative;
}
[data-event-type]:before {
  position: absolute;
  content:"";
  width:32px;
  height:32px;
  line-height: 32px;
  text-align: center;
  color:white;
  left:0;
  top:0;
  border-radius: 50%;
  transform:translate(-50%,50%);
}

[data-event-type="doc"]:before {
  content:"D";
  background: #00f;
}

[data-event-type="alert"]:before {
    content:"A";
  background: #f00;
}
[data-event-type="video"]:before {
    content:"V";
  background: #0f0;
}

此处显示的技巧可能会帮助您了解当前的问题。

当然,这种布局方法有替代方案,包括浮点数和实际表格。其中一些将需要修复&#39;达到“平等高度”这是CSS表格的原生。

答案 1 :(得分:0)

由于没有完整的html或css,这是一种移动的第一种方法,需要相当不错的css技能才能遵循,但它以合乎逻辑的方式开始,适用于小视口并且流畅地调整。最小宽度是较大视图端口的布局开始的位置。之前的样式是全局的(由所有视口大小共享)。

DEMO:http://jsbin.com/zerijo/1/

http://jsbin.com/zerijo/1/edit

CSS:

/*demo only body */
body {
    font-family: sans-serif;
    background:#fff;
    padding:5%;
}

/* ---------- timeline styles ------------ */

.timeline_wrapper:after, .timeline_item:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
}

.timeline_wrapper, .timeline_item {
    display: inline-block
}

* html .timeline_wrapper, * html .timeline_item {
    height: 1%
}

.timeline_wrapper, .timeline_item {
    display: block
}

.timeline_wrapper,
.timeline_wrapper div,
.timeline_wrapper *:before,
.timeline_wrapper *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

.timeline_wrapper {
    position: relative;
    padding-top: 10px;
    line-height: 1.5;
}

.timeline_wrapper:before {
    position: absolute;
    top: -5px;
    bottom: 10px;
    left: 15px;
    content: "";
    height: 100%;
    display: block;
    border-right: 1px solid #777;
    width: 1px;
    z-index: -1;
}

.timeline_wrapper p {
    margin: 0
}

.timeline_icon i {
    background: red;
    display: block;
    border-radius: 50%;
    width: 30px;
    height: 30px;
    line-height: 32px;
    text-align: center;
    font-size: 15px;
    color: #fff;
    float: left;
}

.timeline_text {
    float: left;
    width: 100%;
    padding: 0 0 0 40px;
}

.timeline_time {
    padding-left: 40px
}

.timeline_item {
    width: 100%;
    position: relative;
}
.timeline_item:not(:last-child) {
    padding-bottom: 30px;
}



@media (min-width:480px) { 
    .timeline_wrapper {
        position: relative;
        padding-top: 10px;
    }

    .timeline_wrapper:before {
        left: 27%
    }

    .timeline_item > div {
        float: left;
        position: relative;
    }

    .timeline_time {
        width: 22%;
        text-align: right;
        padding: 0;
        left: -10%;
    }

    .timeline_icon {
        width: 10%;
        min-height: 50px;
        left: 22%;
        top:-5px;
    }

    .timeline_icon i {
        margin: 0 auto;
        float: none;
    }

    .timeline_text {
        float: left;
        width: 100%;
        margin-left: -37%;
        padding: 0 0 0 37%;
    }
}

HTML - 移动设备的结构不同,然后在最小宽度调整位置:

<div class="timeline_wrapper">
 <div class="timeline_item">
  <div class="timeline_icon">
   <i class="your-icon"></i>
  </div>
  <div class="timeline_time">
   <p><em>1 hr ago</em></p>
  </div>
  <div class="timeline_text">
   <p>Lack of peppering one's sentences with gerunds.Lack of peppering one's sentences with gerunds. Lack of peppering one's sentences with gerunds. You read the article </p>
  </div>
 </div>
 <div class="timeline_item">
  <div class="timeline_icon">
   <i class="your-icon"></i>
  </div>
  <div class="timeline_time">
   <p><em>1 hr ago</em></p>
  </div>
  <div class="timeline_text">
   <p>You read the article Lack of peppering one's sentences with gerunds. Lack of peppering one's sentences with gerunds.Lack of peppering one's sentences with gerunds.Lack of peppering one's sentences with gerunds.</p>
  </div>
 </div>
 <div class="timeline_item">
  <div class="timeline_icon">
   <i class="your-icon"></i>
  </div>
  <div class="timeline_time">
   <p><em>1 hr ago</em></p>
  </div>
  <div class="timeline_text">
   <p>You read the article.</p>
  </div>
 </div>
相关问题