我正在设计一个flash消息弹出窗口,类似于StackOverflow本身使用的样式 -
他们通过将消息浮动到左侧并将关闭按钮浮动到右侧来实现它。他们还通过给出一个固定的margin-top
来垂直“居中”关闭按钮,如图所示。
我的Flash消息略有不同,因为消息居中。
在高水平,我接近它如下 -
<div>
s,显示为inline-block
margin-left
10%。 float: right
,因为我知道there's some difficulty in vertically centering floated elements。 如上所示,我正在最困难的时间垂直居中关闭按钮。它只是拥抱底部。
我的HTML和CSS在下面 - 是否有任何直接的方法来实现这一目标?
谢谢!
<div id="flash" class="center active error">
<div class="flash-message">
Please enter an email. Please enter an email. Please enter an email. Please enter an email. Please enter an email. Please enter an email.
</div>
<div class="flash-close">x</div>
</div>
// Flash messages
#flash {
// Display it as an offset from the parent <body> tag
position: absolute;
display: block;
top: 4em;
left: 0;
right: 0;
// Center it on the page
width: 50%;
text-align: center;
z-index: 101;
line-height: 2.5;
overflow: hidden;
// Cool shadows
-webkit-box-shadow: 0 0 5px $black;
-moz-box-shadow: 0 0 5px $black;
box-shadow: 0 0 5px $black;
border-radius: 3px;
// Set color based on message type
&.error { background: $mustard; }
&.notice { background: $light-green; }
.flash-message {
display: inline-block;
width: 80%;
padding-left: (100% - 80%)/2;
// Give it 3px on the top and bottom for some space
margin: 3px 0;
}
.flash-close {
display: inline-block;
// This doesn't seem to do anything.... -_-
// I've also tried auto-setting margins, and making it `position: relative`
// so I can set top/bottom as 50%
vertical-align: middle;
// Give it 3px on the top and bottom for some space
// Also 11px from the right edge
margin: 3px 11px 3px 0;
// Styling the "X" and the box around it
padding: 2px 6px;
font-family: Arial, sans-serif;
font-size: 1em;
font-weight: normal;
color: $white;
line-height: 1;
border: 1px solid rgba(255,255,255,0.2);
}
}
答案 0 :(得分:1)
您可以尝试使用flexbox进行居中。
#flash {
display: flex;
align-items: center;
justify-content: center;
}
其他信息:Flexbox