【问题标题】:How to make 2 elements floated side by side, to line up with the banner at the top?如何让 2 个元素并排浮动,与顶部的横幅对齐?
【发布时间】:2015-06-30 22:03:40
【问题描述】:

我正在构建一个简单的网站布局。我在顶部有一个横幅,下面有一个 div 框和一个图像,它们都浮动到左侧。我想获得与标题横幅右侧内联的女性图像。我尝试过使用边距,但它要么使它离开页面,要么没有完全对齐。

这是 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%;

这里是代码笔的链接: http://codepen.io/Pea92/pen/JdpoqN

【问题讨论】:

    标签: html css css-float


    【解决方案1】:

    在这里,我为你编辑了 CSS。

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

    您只需要将 .woman(图片)向右浮动,或许可以将 margin-left 降低到 3%。

    看起来左列和图像的宽度加起来应该是 99%,但由于左列和图像的边框为 3px,它可能已经超过 100%。

    【讨论】:

    • 谢谢,但还没有真正排好队!呸呸呸。我什至缩小了边界
    【解决方案2】:

    右栏向右浮动

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

    这里加上这个(去掉宽度)

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

    view my fiddle

    【讨论】:

    • 嘿,谢谢,但这不起作用。图片仍未与顶部黑色横幅的右侧对齐。
    • 好的我更新了,你之前没有图像,新的更改将修复它。我附上了小提琴
    • 太棒了,谢谢。我想知道为什么 100% 的宽度会让它这样做,奇怪。谢谢!
    • 我也一直试图让 .line div 与其上方的内容分开很长时间。由于某种原因,“margin-top”不起作用。你认为是因为浮动?我已经清除了上面的浮动:S codepen.io/Pea92/pen/JdpoqN
    • 不,您可以同时使用浮动和边距。您只是不能将垂直对齐与浮点数一起使用
    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多