Scene: In a fixed-height div, there is a floating element that needs to be centered vertically on the floating element.
The original code is as follows:
The
code is as follows:
<! DOCTYPE html>
<html>
<head>
<title></title>
<style type= "Text/css" >
. wrapper{
width:400px;
height:300px;
Background-color: #1f8dd6;
}
button{
Float:right;
Display:inline-block;
height:50px;
width:100px;
line-height:50px;
}
</style>
</head>
<body>
<div class= "wrapper" >
<button>float right.</button>
</div>
</body>
</html>
Now it's just simple to set this button to float, and the effect of the implementation looks like this:
Now you need to center the button vertically across the div. My approach is to add a span to the button's outer layer and float the span element instead of the previous button. It is also necessary to set the height and row height of this span to be the same as the outer Div.
代码如下:
span{
float: right;
height: 300px;
line-height: 300px;
}
This should be the case now:
Complete code:
The
code is as follows:
<! DOCTYPE html>
<html>
<head>
<title></title>
<style type= "Text/css" >
. wrapper{
width:400px;
height:300px;
Background-color: #1f8dd6;
}
span{
Float:right;
height:300px;
line-height:300px;
}
button{
Float:right;
Display:inline-block;
height:50px;
width:100px;
line-height:50px;
}
</style>
</head>
<body>
<div class= "wrapper" >
<span>
<button>float right.</button>
</span>
</div>
</body>
</html>