如何在自己的div之外浮动/显示图片?

时间:2018-12-26 09:56:41

标签: html css

我希望我的徽标漂浮在overflow:hidden div之外

这是我的HTML代码:

    .login-card{
        width: 280px;
        background: rgb(255,250,250,0.6);
        margin-left: 45px;
        border-radius: 2px;
        box-shadow: 0 2px 2px rgba(0,0,0,.3);
        overflow: hidden;
    }

    .login-card img{
        width:70%;
        height:70%;
        margin-top: -25px;
        background-color: crimson; /*this is just to cover up the image*/
    }
   <div class="login-card">
        <img src="captiveportal-logo.png"/>
        <form name="login_form" method="">
            <input type="text">
            <input type="password">
            <input type="submit">
        </form>				
 </div>

我真的希望它漂浮在div之外

2 个答案:

答案 0 :(得分:0)

我认为您需要另一个额外的外部容器来解决此问题。看一下代码片段。在这种情况下,您应该注意图像的大小,因为它现在是从设置了position: relative;

的div外部继承的大小。

.outside {
	position:relative;
}
.login-card{
	width: 280px;
	background: rgb(255,250,250,0.6);
	margin-left: 45px;
	border-radius: 2px;
	box-shadow: 0 2px 2px rgba(0,0,0,.3);
	overflow: hidden;
}
.login-card img{
	width:100px;
	height:100px;
	position:absolute;
	left:0;
	top:-25px;
	background-color: crimson; /*this is just to cover up the image*/
}
<div class="outside">
<div class="login-card">
        <img src="https://via.placeholder.com/100.png/09f/fff"/>
        <form name="login_form" method="">
            <input type="text">
            <input type="password">
            <input type="submit">
        </form>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
 </div>
</div>

答案 1 :(得分:-1)

最简单的方法是将溢出:隐藏在父div上,并且不指定高度:

#parent { overflow: hidden }

另一种方法是也浮动父div:

#parent { float: left; width: 100% }

另一种使用清晰元素的方法:

<div class="parent">
   <img class="floated_child" src="..." />
   <span class="clear"></span>
</div>

CSS

span.clear { clear: left; display: block; }