我冒昧地将HTML中的一些CSS移动到CSS区域。在您的网页上,您应该将其放在<style>
标记中,而不是硬编码到每个元素中以使其可读。
我改变了你的HTML结构,因为你的非常......有趣......我把它格式化成它应该出现的样子:一个容器包含所有内容 - >左右两个区域。从你看来,这似乎是一次性修复,你不会需要非常灵活,所以我想出了this hard coded solution。它涉及到正确的内容并绝对定位左侧内容以使其居中。如果您拥有的不仅仅是“文本”更大的内容,你将需要为类调整top
值\
这里是我的编辑HTML
<div class='container'>
<b class='text'>Text</b>
<div class='appointment'>
<input type="text" value="2013-01-01 12:45 PM" name="triggerOn[]" id="triggerOn_'+ v.call_code_id +'" readonly="readonly" style="width: 175px;">
</br>
<input type="checkbox" id="isAppointment" name="isAppointment" value="1" />
<div id='spacing'></div>Make an appointment.</div>
</div>
和我的编辑CSS(单独的,而不是在标签)
.container {
display: block;
width: 500px;
height:70px;
border: 1px dashed #C0C0C0;
margin: 0 auto;
padding: 5px;
}
.appointment {
display: inline-block;
float:right;
}
.text {
position:absolute;
top:35px;
margin-left:15px;
}
#spacing {
width:10px;
height:2px;
display:inline-block;
}
编辑:
既然你想要的左侧区域完全垂直居中,我改变了HTML的结构,并改变了CSS以适应它,但如果没有一些JavaScript的帮助,它是真的不可能的。我使用jQuery将每个元素完美对齐。另一个解决方案是使用Flexbox,但我选择了jQuery路由。 Here is the new dynamic solution
这里是我的更新HTML
<div class="container">
<div class="bubble-left">
<b class='text'>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</b>
</div>
<div class='bubble-right'>
<div class='appointment'>
<input type="text" value="2013-01-01 12:45 PM" name="triggerOn[]" id="triggerOn_'+ v.call_code_id +'" readonly="readonly" style="width: 175px;"></br>
<input type="checkbox" id="isAppointment" name="isAppointment" value="1" /><div id='spacing'></div>Make an appointment.</div>
</div>
</div>
CSS
.container {
width: 500px;
height: auto;
position: relative;
border: 1px dashed #C0C0C0;
margin: 0 auto;
padding: 5px;
}
.bubble-left {
position: absolute;
left: 5px;
top: 0;
width: 300px;
height: 100%;
display: table;
}
.bubble-left b {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.bubble-right {
position: absolute;
padding-top:5px;
right: 50px;
top: 40%;
width: 135px;
height: 30px;
display: table;
}
#spacing {
width:10px;
height:2px;
display:inline-block;
}
,并添加JavaScript的
var biggestHeight = "0";
// Loop through elements children to find & set the biggest height
$(".container *").each(function(){
// If this elements height is bigger than the biggestHeight
if ($(this).height() > biggestHeight) {
// Set the biggestHeight to this Height
biggestHeight = $(this).height();
}
});
// Set the container height
$(".container").height(biggestHeight);
//position right content verically
$('.bubble-right').css('top', $('.bubble-right').parent().height()/2 - $('.bubble-right').height()/2);
谢谢。这个代码只有在你知道文本的长度时才有效,所以你可以硬编码绝对值。但在我的情况下,我不知道文本的长度。因此,如果我有一段长文本,我希望文本在中间对齐,并且复选框/日期在中间对齐。 – Jaylen
你应该在下一次的问题中这么说。查看我的更新以获取更新的解决方案 –
@Mike是否确实符合您的需求? –