Baseline of vertically stacked elements - html

I have a container of vertically stacked elements.
<div>
<div>line 1</div>
<div>line 2</div>
<div>line 3</div>
<div>line 4</div>
</div>
I want the baseline of the container to be identical to the baseline of a specific one of its elements, let's say the third one. It should look like this:
How can I do this with CSS?
As a related question, how is the baseline of such container of vertically stacked elements usually defined?
I want to give this container a property display: inline-block. This container appears next to other elements on a line, and I want them to be aligned according to the baseline.

This makes the baseline of the container coincide with the baseline of the third child div.
.container > div:nth-of-type(3) ~ div {
float: left;
clear: both;
}
Examples:
.container {
display: inline-block;
background: yellow;
padding: 0 0.5em;
width: 8em;
}
.b1 > div:nth-of-type(1) ~ div {
float: left;
clear: both;
}
.b2 > div:nth-of-type(2) ~ div {
float: left;
clear: both;
}
.b3 > div:nth-of-type(3) ~ div {
float: left;
clear: both;
}
.b4 > div:nth-of-type(4) ~ div {
float: left;
clear: both;
}
.container > div:nth-of-type(1) {
font-size: 14px;
}
.container > div:nth-of-type(2) {
font-size: 16px;
}
.container > div:nth-of-type(3) {
font-size: 24px;
}
.container > div:nth-of-type(4) {
font-size: 20px;
}
<div class="container b1">
<div>baseline</div>
<div>line 2</div>
<div>line 3</div>
<div>line 4</div>
</div>
<div class="container">
Lorem ipsum dolor sit amet
</div>
<div class="container">
consectetur adipiscing elit, sed do eiusmod tempor incididunt
</div>
<hr>
<div class="container b2">
<div>line 1</div>
<div>baseline</div>
<div>line 3</div>
<div>line 4</div>
</div>
<div class="container">
Lorem ipsum dolor sit amet
</div>
<div class="container">
consectetur adipiscing elit, sed do eiusmod tempor incididunt
</div>
<hr>
<div class="container b3">
<div>line 1</div>
<div>line 2</div>
<div>baseline</div>
<div>line 4</div>
</div>
<div class="container">
Lorem ipsum dolor sit amet
</div>
<div class="container">
consectetur adipiscing elit, sed do eiusmod tempor incididunt
</div>
<hr>
<div class="container b4">
<div>line 1</div>
<div>line 2</div>
<div>line 3</div>
<div>baseline</div>
</div>
<div class="container">
Lorem ipsum dolor sit amet
</div>
<div class="container">
consectetur adipiscing elit, sed do eiusmod tempor incididunt
</div>

If I understand your question correctly, something like this could work:
body {
line-height: 18px; /* set a line height and use it to calculate the offsets later */
}
div {
position: relative;
display: inline-block;
vertical-align: baseline;
background: black;
color: white;
}
div > div {
display: block;
}
div.align-1 > div:nth-child(2) {
position: absolute;
bottom: -18px; /* 1 x line-height of parent */
}
div.align-1 > div:nth-child(3) {
position: absolute;
bottom: -36px; /* 2 x line-height of parent */
}
div.align-1 > div:nth-child(4) {
position: absolute;
bottom: -54px; /* 3 x line-height of parent */
}
div.align-2 > div:nth-child(3) {
position: absolute;
bottom: -18px; /* 1 x line-height of parent */
}
div.align-2 > div:nth-child(4) {
position: absolute;
bottom: -36px; /* 2 x line-height of parent */
}
div.align-3 > div:nth-child(4) {
position: absolute;
bottom: -18px; /* 1 x line-height of parent */
}
Text
<div class="align-1">
<div>line 1</div>
<div>line 2</div>
<div>line 3</div>
<div>line 4</div>
</div>
more text
<br>
<br>
<br>
<br>
<hr />
<br> Text
<div class="align-2">
<div>line 1</div>
<div>line 2</div>
<div>line 3</div>
<div>line 4</div>
</div>
more text
<br>
<br>
<br>
<hr />
<br> Text
<div class="align-3">
<div>line 1</div>
<div>line 2</div>
<div>line 3</div>
<div>line 4</div>
</div>
more text
<br>
<br>
<hr />
<br> Text
<div class="align-4">
<div>line 1</div>
<div>line 2</div>
<div>line 3</div>
<div>line 4</div>
</div>
more text
<br>
<hr />
If you are using sass or less you could make it in a dynamic mixin that would work on variable element count.

Related

How to keep a <div> at the bottom of another <div> with scrolling elements behind it

I have a <div id = 'body-box> and and at the bottom I have an input for text .
I want to stay at the bottom of the while the chat messages scroll behind it. The stays in line and gets pushed down as more chat elements are added in.
I would like the input form to stay at the bottom while still within . What am I doing wrong in my css currently because this is not being achieved. Thanks!
</head>
<body style="background-image: url(./images/background.svg)">
<div id = 'bodybox'>
<div id = 'chat-message-list'>
<div class = "message-row you-message">
<div class = "message-content">
<div class = "message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class = "message-time">Apr 16</div>
</div>
</div>
<div class = "message-row you-message">
<div class = "message-content">
<div class = "message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class = "message-time">Apr 16</div>
</div>
</div>
<div id = 'chat-form'>
<input id = 'input' type = 'text' placeholder="What would you like to know"/>
<button id = 'sendbutton'>SEND</button>
</div>
</div>
</body>
</html>
#chat-form {
position: absolute;
bottom: 5px;
border-radius: 10px;
padding-left: 42px;
padding-right: 22px;
align-items: center;
align-content: center;
height: 70px;
display: inline-flex;
}
body {
font: 15px arial, sans-serif;
background-color: #FFFFFF;
padding-top: 15px;
padding-bottom: 15px;
/*display: grid;*/
}
#bodybox {
margin: auto;
max-width: 1000px;
font: 15px arial, sans-serif;
background-color: #F3F6FB;
padding-top: 20px;
padding-bottom: 25px;
padding-right: 25px;
padding-left: 25px;
border-radius: 15px;
max-height: 600px;
position: relative;
overflow: scroll;
}
You can apply sticky positioning to the form (instead of absolute, which you started with), remove the hard-coded height of the form, and achieve what you're after.
From MDN
The element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor), including table-related elements, based on the values of top, right, bottom, and left. The offset does not affect the position of any other elements.
In your case the relatively-positioned parent bodybox is acting as the nearest block-level ancestor, allowing us to pin the form to the bottom of the element.
Demo
#chat-form {
position: sticky;
bottom: 5px;
border-radius: 10px;
align-items: center;
align-content: center;
display: inline-flex;
bottom: -25px;
left: calc(50% - 80px);
}
body {
font: 15px arial, sans-serif;
background-color: #FFFFFF;
padding-top: 15px;
padding-bottom: 15px;
/*display: grid;*/
}
#bodybox {
margin: auto;
max-width: 1000px;
font: 15px arial, sans-serif;
background-color: #F3F6FB;
padding-top: 20px;
padding-bottom: 25px;
padding-right: 25px;
padding-left: 25px;
border-radius: 15px;
max-height: 600px;
position: relative;
overflow: scroll;
max-height: 8em;
}
<div id='bodybox'>
<div id='chat-message-list'>
<div class="message-row you-message">
<div class="message-content">
<div class="message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class="message-time">Apr 16</div>
</div>
</div>
<div class="message-row you-message">
<div class="message-content">
<div class="message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class="message-time">Apr 16</div>
</div>
</div>
<div class="message-row you-message">
<div class="message-content">
<div class="message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class="message-time">Apr 16</div>
</div>
</div>
<div class="message-row you-message">
<div class="message-content">
<div class="message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class="message-time">Apr 16</div>
</div>
</div>
<div class="message-row you-message">
<div class="message-content">
<div class="message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class="message-time">Apr 16</div>
</div>
</div>
<div class="message-row you-message">
<div class="message-content">
<div class="message-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div class="message-time">Apr 16</div>
</div>
</div>
<div id='chat-form'>
<input id='input' type='text' placeholder="What would you like to know" />
<button id='sendbutton'>SEND</button>
</div>
</div>
Link
jsFiddle

Make div Background heigth at 100%

How can i make my div background height at 100% depending on the height of it parent :
that's what i have for now :
and i want to fill all the space with the red color with the background color:
this is my HTML code :
<div class="row" style="margin-bottom: 20px">
<div class="col-6"><img style="width: 100%" src="https://cdn.pixabay.com/photo/2016/09/01/10/23/image-1635747_960_720.jpg"></div>
<div class="col-6 class1">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod</p>
</div>
</div>
<div class="row">
<div class="col-12">
<img src="https://cdn.pixabay.com/photo/2016/09/01/10/23/image-1635747_960_720.jpg" style="width:100%">
</div>
</div>
and that's my css :
.class1{
background-color: yellow;
height: 100%;
}
and thanks for help :-)
Please refer to below link:
https://jsfiddle.net/ulric_469/7mh4vs6p/6/
add height 100% to the image
.row {
display: flex;
align-items: stretch;
}
.row > div {
flex: 1
}

Problem using flex-wrap to break to the next row after the 5th element.

I've been working on an image gallery using flexbox with a flex-basis transition to grow and shrink the elements on hover. I am having a problem adapting the gallery to use flex-wrap to break to the next row after the 5th element.
The way I'm hoping for it to function is that each row has 5 elements that grow and shrink on the same row. After the a sixth element is placed in the container I want to break to the next row and have the elements continue functioning as expected (transitions included).
Elements stretch to fill available space (width of 20% for each of the 5 elements)
-> On hover, the other elements shrink 5% for a total of 20%
Hovered element grows to 40%
Sixth element that moves to next row inherits the size of the available space
Seventh element takes up 50% of row, eighth 33.3%...ect.
This is what I currently have: https://codepen.io/TommyBoyLab/pen/YdzGjB
(adapted from: https://codepen.io/joliveras/pen/GpLVKv)
HTML of element:
<div class="container">
<div class="item" style="background:url() center/cover">
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-flow: row wrap;
width: 100%;
}
.container .item {
display: grid;
position: relative;
flex: 1;
transition: 500ms;
min-width: 15%;
max-width: 20%;
height: 50vh;
}
.container .item:hover {
transition: 500ms;
max-width: 40%;
flex-grow: 1;
}
.container .content {
margin: auto;
font-size: 1.5em;
}
It seems you only need to increase the value of flex-grow on hover:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-flow: row wrap;
width: 100%;
}
.container .item {
display: grid;
position: relative;
flex: 1;
transition: 500ms;
min-width: 15%;
max-width: 20%;
height: 50vh;
}
.container .item:hover {
transition: 500ms;
max-width: 40%;
flex-grow: 5;
}
.container .content {
margin: auto;
font-size: 1.5em;
}
<div class="container">
<div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
</div>
Addition to CSS:
.item:first-child:nth-last-child(n + 5),
.item:first-child:nth-last-child(n + 5) ~ *{
min-width: 17%;
}

Need to gain space between two adjacent rows

On bootstrap 3,I can't make the layout how I want to and I can't see why.
My HTML :
<div class="container">
<div id="row1" class="row">
<div class="col-lg-4 col-lg-push-8 col-md-4 col-md-push-8 col-sm-push-8 col-sm-4">
<div class="round"></div>
</div>
<div class="col-lg-8 col-lg-pull-4 col-md-8 col-md-pull-4 col-sm-8 col-sm-pull-4 intro-text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>
</div>
</div>
<div id="row2" class="row">
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="round"></div>
</div>
<div class="col-lg-8 col-md-8 col-sm-8 intro-text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</p>
</div>
</div>
</div>
My CSS :
#row1 {
opacity:0.3;
background-color: red;
}
#row2 {
opacity:0.3;
background-color: yellow;
}
.intro-text {
text-align:center;
}
.round {
background:#bfd70e;
border-radius:50%;
width:160px;
height:160px;
border:2px solid #679403;
margin:0 auto;
}
#media(min-width:767px) {
.intro-text {
margin-top:60px;
}
#row2 {
margin-top:-15px;
}
}
I want to keep the same structure as the first JS fiddle but gain some space by making the row 2 over the first one. So I tried a margin-top but it break all the structure, I don't know why.
This is JS fiddle without :
#row2 {
margin-top:-15px;
}
JS fiddle
This is JS fiddle with
#row2 {
margin-top:-15px;
}
JS fiddle
How do I fix that ?
Change your:
margin-top: -15px;
to:
margin-top: 15px;
Otherwise you're pulling it down.
https://jsfiddle.net/584wcaa5/1/
It's because the elements are positioned statically, therefore colliding with the elements above it.
If you give your second row position:absolute it should stop it colliding with the elements as the position is set regardless of what other elements are doing.
https://jsfiddle.net/584wcaa5/2/

Flexbox flex-wrap: is there a way to force wrap over growth?

I have a set of flex-items vertically stacked. The flex container is set to wrap. I would like it to wrap as soon as possible so it will fill the page horizontally instead of growing vertically. Plus the items are filled with dynamic text so I don't want to set a fixed height nor see them cropping.
HTML
<div class="fc v menu">
<div class="fa">
<h1>Title</h1>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
<li>Vestibulum auctor dapibus neque.</li>
</ul>
</div>
<div class="fa">ITEM 2</div>
<div class="fa">ITEM 3</div>
<div class="fa">ITEM 4</div>
<div class="fa">ITEM 5</div>
<div class="fa">ITEM 6</div>
<div class="fa">ITEM 7</div>
<div class="fa">ITEM 8</div>
</div>
CSS
.fc {
display: flex;
}
.fc.menu {
flex-wrap: wrap;
height: 15em;
}
.menu > div {
padding: 1em;
}
.fa {
flex: 1 0 auto;
width: 10em;
height: 5em;
}
.fc.v {
flex-direction: column;
background: pink;
border: 4px fuchsia solid;
}
You can see in the codepen that even if it is possible to force a sort of wrap the content will not fit the container anymore.