在页面内容旁边放置列的最佳方法是什么?

时间:2016-01-09 05:38:29

标签: html css css-float css-position

这是我到目前为止所拥有的, http://codepen.io/anon/pen/XXRYXY

将绿色框(它们代表广告)放置在页面内容右侧的最佳方法是什么?在下面的代码中,我尝试使用宽度:50%。

在编写任何代码之前,我是否应该计划内容的宽度和广告的宽度?我对人们如何计划创建他们的网页感到困惑,因为我的屏幕宽度是1920px,如果我使用该宽度作为模型,那么屏幕较小的用户将以不同的方式查看我的网页。在规划测量和元素定位时,我应该使用哪些尺寸?

<body>
<div id="wrap">
 <div id="head">
  <div id="header" style="float:left;">
    <p id="title">
      <a href="#">Blog Title</a>
    </p>
    <p id="description">Blog Description</p>
  </div>

  <div id="navbar" style="float:right;">
    <ul style="list-style-type:none;">
      <li><a href="#">Start Here</a></li>
      <li><a href="#">Section 1</a></li>
      <li><a href="#">Section 2</a></li>
      <li><a href="#">Section 3</a></li>
      <li><a href="#">About</a></li>
    </ul>
  </div>
</div>

<div id="welcome-wrap" style="clear:both;">
  <div id="first-time">
    <h2>First time here?</h2>
    <p style="margin:0;">Fusce ut finibus purus, quis convallis orci. Donec a est eget tellus euismod tincidunt non eu urna. Sed id gravida dui. Phasellus sed faucibus nisl. Proin blandit nisl eu lectus convallis.</p>
    <br>
    <br> If it's your first time here,
    <a href="#">read this</a>
  </div>

  <div id="topics">
    <h2>TOPICS</h2>
    <p><a href="#"><strong>Link Topic 1</strong></a>
      <p><a href="#"><strong>Link Topic 2</strong></a>
        <p><a href="#"><strong>Link Topic 3</strong></a>
  </div>
</div>

<div id="inner" style="clear:both;">
  <div id="content">
    <div class="post">
      <a href="#"><img width="300" height="200" src="contentimg.jpg"></a>
      <p>Mauris gravida diam id sem blandit, vitae vestibulum nunc interdum. Nulla congue felis eget tincidunt rhoncus. Nunc rutrum, massa a venenatis fermentum, nisi leo dictum sapien, ut venenatis quam neque sit amet quam. Nunc efficitur hendrerit
        hendrerit. Sed sit amet tortor ex. Pellentesque dapibus porttitor massa eu iaculis. Curabitur varius vulputate sagittis. Aenean ac mauris eu justo lobortis accumsan a vitae est. Praesent imperdiet quam vitae porta lacinia.</p>
      <a class="more-link" href="#" rel="nofollow">Continue Reading</a>
    </div>

    <div style="clear:both;"></div>

    <div class="post">
      <a href="#"><img width="300" height="200" src="contentimg.jpg"></a>
      <p>Nulla porttitor rhoncus justo in molestie. Aliquam sed vehicula mauris, quis egestas augue. In hac habitasse platea dictumst. Donec congue arcu mollis aliquet aliquam. Pellentesque purus dolor, commodo eget tincidunt nec, fringilla quis magna.
        Nam suscipit cursus purus, in sodales tortor egestas suscipit. Proin ut augue sit amet nulla laoreet maximus id eu nibh. Pellentesque dapibus finibus egestas.</p>
      <a class="more-link" href="#" rel="nofollow">Continue Reading</a>
    </div>

    <div style="clear:both;"></div>

    <div class="post">
      <a href="#"><img width="300" height="200" src="contentimg.jpg"></a>
      <p>Fusce ut finibus purus, quis convallis orci. Donec a est eget tellus euismod tincidunt non eu urna. Sed id gravida dui. Phasellus sed faucibus nisl. Proin blandit nisl eu lectus convallis, vel ultricies risus varius. Morbi id nibh enim. Suspendisse
        potenti. Nulla facilisi. Quisque congue lacinia dapibus.</p>
      <a class="more-link" href="#" rel="nofollow">Continue Reading</a>
    </div>
  </div>

  <div id="sidebar" style="float:right;">
    <div>
      <p>
        <a href="#"><img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS1uD8Fj5M9sS4Lo1OrKIycgx2gMDnEE7zHru3j--pDFlwqujXiKA"></a>
      </p>
      <p>
        <a href="#"><img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS1uD8Fj5M9sS4Lo1OrKIycgx2gMDnEE7zHru3j--pDFlwqujXiKA"></a>
      </p>
      <p>
        <a href="#"><img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS1uD8Fj5M9sS4Lo1OrKIycgx2gMDnEE7zHru3j--pDFlwqujXiKA"></a>
      </p>
      <p>
        <a href="#"><img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS1uD8Fj5M9sS4Lo1OrKIycgx2gMDnEE7zHru3j--pDFlwqujXiKAg"></a>
      </p>
    </div>
  </div>

CSS:

    body{
    padding:0;
    margin:0;
}

#wrap{
    margin-left:500px;
    margin-right:500px;
    max-height:5000px;
}

/*head*/

#head{
    min-height:135px;
    width:960px;
    margin-top:50px;
}

#title a{
    font-size:2em;
    text-decoration:none;
    color:black;
}

#description{
    font-size:0.9em;
}

#header p{
    margin:0 !important;
}

#navbar ul li{
    float:left;
    padding-left:1.5em;
    margin-left:0;
}

#navbar ul li a{
    color:black;
    text-decoration:none;
    font-size:1.2em;
    margin-left;0;
}

/*sub head*/
#welcome-wrap{
    width:100%;
    height:500px;
}

#first-time{
    float:left;
    width:450px;
    padding-right:150px;
}

#topics{
    float:left;
    text-align:center;
    background-color:green;
    width:300px;
    height:280px;
}
/*content*/

#inner{
    width:50%;
    float:left;
}

#sidebar{
    float:right;
    width:50%;
}

.post a{
    float:left;
}

1 个答案:

答案 0 :(得分:1)

好的,在将您的代码放入IDE(Notepad ++)之后,我已经找到了您的问题。

首先,你必须关闭你的主div #wrap。

第二个关闭属性值margin-right:赋予#wrap是完全没必要的,因为它没有提供任何视觉目的,也阻止你的(广告)浮动到页面的右侧

如果您想要向上或向下移动侧边栏,只需分别添加到边距顶部或边距底部。