Issue
I want to align three blocks: one to the left bottom, one in the middle top, and one in the right bottom.
The width is fixed, but one or two of the blocks may be absent.
I couldn't find a way to use position:absolute
because the container must have the height of the higher block.
My solution so far is using three inline-block
whose widths add up to the container width, and mixed vertical-alignment
. But when i hide the middle block it breaks.
I set a fiddle for it here that shows my desired result, except when I hide.
Of course I can add a script to set the margins when I hide the block, but I'm looking for a CSS only solution. Also I would rather not use tables or display:table-cell
, for their side effects.
Solution
You may use display:flex;
flex:1
and margin:auto
will keep away from the direction it is applied, on 2 opposite direction it will be centered :
- when middle is hidden, left and right comes 50% of width. width of div is set via
flex:1
:
$("input").click(() => {
$("#middletop").toggle();
})
div {
border: 1px solid green;
margin: 0;
flex: 1;
}
#container {
min-height: 50vh;/* demo purpose */
display: flex;
}
#leftbottom {
margin-top: auto;
}
#middletop {
margin: auto 0;
text-align: middle;
}
#rightbottom {
width: 200px;
margin-bottom: auto;
text-align: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="leftbottom">
Lorem ipsum dolor sit amet, consectetur adipiscing 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
</div>
<div id="middletop">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididun.
</div>
<div id="rightbottom">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<input type="button" value="Hide middle" />
https://jsfiddle.net/9b9a7wqv/8/
- when middle is hidden, it leaves a gap. Here width of div is set via
width
.
$("input").click(() => {
$("#middletop").toggle();
})
div {
border: 1px solid green;
margin: 0;
}
#container div {
width:33%;
}
#container {
min-height: 50vh;
display: flex;
}
#leftbottom {
margin-top: auto;
margin-right:auto;
;
}
#middletop {
margin: auto 0;
text-align: middle;
}
#rightbottom {
margin-bottom: auto;
margin-left:auto;
text-align: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="leftbottom">
Lorem ipsum dolor sit amet, consectetur adipiscing 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
</div>
<div id="middletop">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididun.
</div>
<div id="rightbottom">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<input type="button" value="Hide middle" />
https://jsfiddle.net/9b9a7wqv/10/
Answered By - G-Cyrillus Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.