简体   繁体   English

如何使2个元素并排漂浮,并与顶部的横幅对齐?

[英]How to make 2 elements floated side by side, to line up with the banner at the top?

I'm building a simple website layout. 我正在构建一个简单的网站布局。 I have a banner at the top, and a div boxe and an image underneath it, both floated to the left. 我在顶部有一个横幅,下面有一个div框和一个图像,都漂浮在左侧。 I want to get the image of the woman inline with the right side of the header banner. 我想让女人的形象与标题横幅的右侧保持一致。 I've tried using margin, but it either makes it go off the page, or it doesn't quite line up. 我已经尝试过使用margin,但是它要么使它脱离页面,要么就不太整齐。

Here's the CSS: 这是CSS:

.topbanner {
width:100%;
background-color:black;
padding:1%;
margin-top:1%;
}

#nav {
text-align:center;

}

.leftcolumn {
width:40%;
height:230px;
background-color: grey;
border: solid red 3px;
margin-top:2%;
float:left;

}

.leftcolumn p {
color:white;
text-align:center;
padding:13%;
font-style:italic;
font-size:20px;
}

.woman {
width:55%;
height:230px;
border: solid black 3px;
margin-top:2%;
float:left;
margin-left:4%;

Here's a link to the codepen: http://codepen.io/Pea92/pen/JdpoqN 这是指向代码笔的链接: http ://codepen.io/Pea92/pen/JdpoqN

Just float the right column right 只需将右列向右浮动

.rightcolumn {

width:55%;
height:230px;
border: solid black 3px;
margin-top:2%;
float:right;
margin-left:4%;
}

here add this (take the width out) 在这里添加(取出宽度)

.topbanner {

  background-color:black;
  padding:1%;
  margin-top:1%;
}

view my fiddle 查看我的小提琴

Here, I edited the CSS for you. 在这里,我为您编辑了CSS。

.woman {
  width: 55%;
  height: 230px;
  border: solid black 3px;
  margin-top: 2%;
  float: right;
  margin-left: 3%;
}

You just need to float .woman (image) right and perhaps decrease the margin-left to 3%. 您只需要向右浮动.woman(图片),然后将左边距降低到3%即可。

It seems like the width of the left column and the image should have added up to 99%, but it might have exceeded 100% due to the 3px borders given to the left column and image. 似乎左列和图像的宽度应该加起来为99%,但是由于给左列和图像赋予了3px边框,所以它可能已经超过100%。

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

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