Draw a verticle line between two vertically placed divs - CSS? - html

I am trying to achieve something like this
Circle with letters starts at starting of first div and second div with a line separater in between. Here the length of line should be calculated based on space in middle of two divs. I tried couple of ways but no luck. Please suggest me an approach to do this.
Update:
I tried #Vadim answer, but in my case separater length should be dynamic, not static, and div's are like this:
<div class="container">
<div class="first-div">
<div class="letter">Q</div>
<div>another div</div>
</div>
<div class="second-div">
<div class="letter">A</div>
<div>another div</div>
</div>
</div>
I would like to draw vertical line between letters. Height of line should be dynamic according to inner divs height.

You can use flexbox to achieve desired layout:
.container {
background-color: #ccc;
padding: 15px;
/* align items in one column and take space defined by content */
/* this is used for centering separator */
display: inline-flex;
flex-direction: column;
}
.letter {
width: 25px;
height: 25px;
background-color: #fff;
border-radius: 50%;
/* styles for text centering */
display: flex;
justify-content: center;
align-items: center;
}
.separator {
width: 5px;
height: 50px;
border-radius: 3px;
background-color: #fff;
margin-top: 5px;
margin-bottom: 5px;
/* center horizontally */
align-self: center;
}
<div class="container">
<div class="letter">Q</div>
<div class="separator"></div>
<div class="letter">A</div>
</div>

Related

Align div at bottom of div container [duplicate]

This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 1 year ago.
I know this question came up many times but none of the suggested solutions is working for me so I opened up a code sandbox and made an example.
What I want
I want to place an item at the bottom of a div that already has some children. In my code sandbox the desired div to place at the bottom has a className of note__actions.
What I tried
Setting the container to position: relative and the item to position: absolute
Using flexbox and setting the container to display: flex and the item to align-self: flex-end
Multiple other things like vertical-align, and making empty items to fill up the space in-between
Div setup
<div className="flexBoxContainer" />
<div className="item">
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
<div className="bottom__item">Actions>/div> // Only this div should go to the bottom of the "item" div
</div>
</div>
Sandbox
https://codesandbox.io/s/infallible-sound-wf166?file=/src/App.js
Give the container display: flex; justify-content: space-between; - that will pull the content from top to bottom. If you want only the last item on the bottom and the rest on the top, wrap the rest with a div, like
<div className="flexBoxContainer" />
<div className="item">
<div>
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
</div>
<div className="bottom__item">Actions>/div>
</div>
</div>
You are already using flexbox which is great. Now, use margin-top:auto on note__actions to push the div to the bottom.
.note__actions {
margin-top: auto;
}
You could add a flex: 1; to the flexible content, for instance
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
height: 400px;
width: 200px;
padding: 24px;
display: flex;
flex-direction: column;
}
.note-content {
flex: 1;
}
.note-action {
align-self: flex-end;
}
<div class="note">
<div>Peter Holbert - 09. Jul 21 23:28</div>
<div>Note Title</div>
<div class="note-content">Aldus PageMaker including versions of Lorem Ipsum.</div>
<div class="note-action">Action 1</div>
</div>
You can use a grid for that.
Change this in your Style on codesandbox.io:
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
width: 200px;
padding: 24px;
display: grid;
/*flex-direction: column;*/
grid-template-rows: 50px 80px 1fr 80px; /* height: 100%; */
/* align-items: flex-end; */
/* position: relative; */
}
.note__actions_bot {
background-color: red;
height: 100px;
display: grid;
justify-content: center;
align-items: center;
}
I recommend this awesome Video to get a nice overview and some hint's how to do, and when to use which one of flex, grid and other solutions:
https://www.youtube.com/watch?v=qm0IfG1GyZU&feature=youtu.be
Add margin-top : auto to the note__actions element

How to Set Flexbox child background to 100% height and center text vertically

I want to make the background color fill the area so it's always the same height for the longest amount of text, and center the text vertically, regardless of how many lines it is. I don't want to set a fixed height on the parent if possible as that may change.
.flexbox {
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin-left: -8px;
margin-right: -8px;
}
.flexbox .sm-col-3 {
width: calc(1/3 * 100% - 16px);
}
.entry {
position: relative;
}
.entry-image {
display: block;
margin: 0 auto;
}
.entry-title {
margin: 0
}
.entry-title-link {
position: absolute;
bottom: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
padding: 8px 9px 9px;
background: rgba(0, 87, 149, 1);
color: #fff;
width: 100%;
}
<div class="articles-container flexbox">
<div class="entry sm-col-3">
<div class="entry-header">
<a href="#" class="entry-image-link"><img
src="https://cdn.shopify.com/s/files/1/0533/2089/files/placeholder-images-product-6_large.png"></a>
</div>
<div class="entry-content">
<h2 class="entry-title">
Some Long Wrapping Two Line Text
</h2>
</div>
<div class="entry-footer"></div>
</div>
<div class="entry sm-col-3">
<div class="entry-header">
<a href="#" class="entry-image-link"><img
src="https://cdn.shopify.com/s/files/1/0533/2089/files/placeholder-images-product-6_large.png"></a>
</div>
<div class="entry-content">
<h2 class="entry-title">
One Line Text
</h2>
</div>
<div class="entry-footer"></div>
</div>
</div>
You should think of this problem in terms of 2 different layouts.
the first one is to put your entry divs next to each other, 3 per line, same height each. That's the flex row you are using on .flexbox
the second one is within each entry div, to put the image on top and the link on the bottom. That's what I'm proposing here with another flex layout, nested inside the first one.
So the way to do this is to apply flex and column on .sm-col-3 to create the nested flex layout. And then it's just up to you how you need to allocate the space. The most common way to do it in this type of case would be to allocate a fixed size (or at least fixed ratio) to the image on top, and then put a flex: 1 on the text at the bottom so that it will occupy everything that's left, thus occupying the same amount of space on each entry.
Now if you also need to vertically center the next in that bottom section .entry-content, that is a 3rd problem to be solved.
the box at the bottom should have its content vertically centered
Except that now that we've divided the problem into its sub-problems, that 3rd one is easy to solve (tons of tutorials online on how to vertically center something within its parent). But since we're doing flexboxes here, let's use that a third time. That's going to be display: flex and align-items on .entry-content.
.flexbox {
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin-left: -8px;
margin-right: -8px;
}
.flexbox .sm-col-3 {
width: calc(1/3 * 100% - 16px);
display: flex;
flex-direction: column;
}
.entry {
position: relative;
}
.entry-image {
display: block;
margin: 0 auto;
}
.entry-content {
flex: 1;
background: rgba(0, 87, 149, 1);
padding: 8px 9px 9px;
display: flex;
align-items: center;
}
.entry-title {
margin: 0;
}
.entry-title-link {
text-align: center;
color: #fff;
width: 100%;
}
<div class="articles-container flexbox">
<div class="entry sm-col-3">
<div class="entry-header">
<a href="#" class="entry-image-link"><img
src="https://cdn.shopify.com/s/files/1/0533/2089/files/placeholder-images-product-6_large.png"></a>
</div>
<div class="entry-content">
<h2 class="entry-title">
Some Long Wrapping Two Line Text
</h2>
</div>
<div class="entry-footer"></div>
</div>
<div class="entry sm-col-3">
<div class="entry-header">
<a href="#" class="entry-image-link"><img
src="https://cdn.shopify.com/s/files/1/0533/2089/files/placeholder-images-product-6_large.png"></a>
</div>
<div class="entry-content">
<h2 class="entry-title">
One Line Text
</h2>
</div>
<div class="entry-footer"></div>
</div>
</div>

CSS Flexbox float elements [duplicate]

This question already has answers here:
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Make a div span two rows in a grid
(2 answers)
Closed 5 years ago.
I'm trying to float two elements at the right of a "figure" element using flex but it end up floating just div1 at the right of figure and div2 is moved bellow, if I make div1 and div2 narrow enough, they are floated inline at the right of figure.
This is the CSS:
.container {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
}
Desired Result:
Actual Result:
How it works?
First, you make a flex-container (flexc in this case) and apply the display:flex property on it which aligns the elements by default in row alignment. If you want an element to preserve its dimensions set it to flex:0 0 auto; else you can make use of flex:1; which shrinks or grows as the browser is resized.
Then to align the contents in column (div1 and div2) you can just wrap then in a different container and since div isn't an inline container, and the flex property doesn't have any effect on any other than the direct children of the flex parent, they are aligned in seperate lines.
.flexc {
display: flex;
justify-content: flex-start;
}
#fig {
flex: 0 0 auto;
width: 200px;
height: 200px;
background: gray;
text-align: center;
color: white;
margin: 10px;
border: 2px solid black;
}
#d1,
#d2 {
width: 200px;
height: 50px;
background: purple;
text-align: center;
color: white;
margin: 10px;
border: 2px solid black;
}
<div class="flexc">
<div id="fig">Figure</div>
<div class="col">
<div id="d1">div1</div>
<div id="d2">div2</div>
</div>
</div>
Without altering the html:
.flexc {
display: flex;
flex-direction:column;
position:relative;
}
#fig {
flex: 0 0 auto;
width: 200px;
height: 200px;
background: gray;
text-align: center;
color: white;
margin: 10px;
border: 2px solid black;
}
#d1,
#d2 {
position:absolute;
left:250px;
width: 200px;
height: 50px;
background: purple;
text-align: center;
color: white;
margin: 10px;
border: 2px solid black;
}
#d2{
top:70px;
}
<div class="flexc">
<div id="fig">Figure</div>
<div id="d1">div1</div>
<div id="d2">div2</div>
</div>
Not sure what your HTML looks like, but display: flex is best used on the container wrapping all the elements you want aligned. Imagine it to be the largest box that you put smaller boxes inside.
Codepen example demonstrating this: https://codepen.io/corviday/pen/VyYdar
Following this hierarchy with .container as your largest box, since you want two columns, you can divide it further into two smaller boxes (.left in red and .right in blue in this case).
From there you would need to group div1/div2 together to float the way you'd like, and would be the items that fill the box .right.
You can use Bootstrap to resolve or put div1 and div2 in one div main to drop div main
Bootstrap exemple
<div class='container'>
<div class="col-md-12">
<div class="col-md-6">
1 text
</div>
<div class="col-md-6">
<div class="col-md-6">
2 text
</div>
<div class="col-md-6">
3 text
</div>
</div>
</div>
</div>
I think the best layout engine to use for your use case is hinted at in your description of the problem: Floats.
Here is a solution that doesn't require you to alter your html.
<div class="container">
<div class="medium-box">figure</div>
<div class="small-box">div 1</div>
<div class="small-box">div 2</div>
</div>
.container{
width: 500px;
}
.medium-box {
height: 200px;
width: 200px;
margin: 10px;
background: grey;
float:left
}
.small-box {
float:left;
height: 30px;
width: 200px;
background: blue;
margin: 10px;
}
https://codepen.io/stacyvlasits/pen/aVPZbY

Keep one element centered between two elements of different widths in flexbox

I am making a music playback controller, and the container has 3 sections: left, center, and right. However, since the left and right sides have different widths, the center section isn't in the true center of the div, but I need it to be. I am using flexbox's space-between option to layout the items.
#container {
display: flex;
justify-content: space-between;
background-color: lightgrey;
}
#container > div {
height: 100px;
border: 2px dashed red;
/*This is only for looks*/
text-align: center;
padding: 5px;
}
<div id="container">
<div>Left Side</div>
<div>I want this centered</div>
<div>Right Side (Extra text for extra length)</div>
</div>
You can use margins to approximate centering. But in order to get perfect centering with flexbox that's consistent across a variety of viewports, you'll have to slightly modify your HTML somewhat.
You need to turn the direct children of #container into flex containers themselves with a display:inline-flex declaration and give them a flex value of 1 and justify-content: center.
From there, you add your content into child divs. To get alignment on the left and right divs, use margin-right: auto and margin-left: auto, respectively.
#container {
display: flex;
background-color: lightgrey;
}
.flex {
flex: 1;
display: inline-flex;
justify-content: center;
}
.flex > div {
height: 100px;
border: 2px dashed red;
text-align: center;
padding: 5px;
}
.left div {
margin-right: auto;
}
.right div {
margin-left: auto;
}
<div id="container">
<div class="left flex">
<div>Left Side</div>
</div>
<div class="center flex">
<div>I want this centered</div>
</div>
<div class="right flex">
<div>Right Side (Extra text for extra length)</div>
</div>
</div>

How to reduce the indentation between first and second line inside circle?

This issue really was causing me troubles.
I have a circle. That circle is a square with border-radius: 50%. It's also flex.
The first line contains an icon, the second line has text.
My problem is that they have large indentation between, I'd like them to be closer to each other. I can't come up with an idea how to fix this.
JSFiddle
HTML
<ion-content has-header="false">
<div class="dashboard-grey-menu">
<div class="flex row no-padding">
<div class="col">
<div class="circle" ui-sref='clients'>
<div class="ion-ionic"></div>
<div>Second line</div>
</div>
</div>
</div>
</div>
</ion-content>
CSS
.dashboard-grey-menu {
height: 23vh;
background-color: #959595;
}
.circle {
border-radius: 50%;
width: 18vw;
height: 18vh;
background-color: #D0D0D0;
font-size: 1em;
font-weight: 900;
margin: auto;
display: flex;
align-items: center;
justify-content: space-around;
flex-flow: column;
}
How would you solve this? Thanks in advance!
Solution 1: add style="padding-bottom:8vh to your second div
Solution 2: delete justify-content: space-around; from .circle in css because main reason for this space is that one. U should do it with using padding or margin css commands with using vh to not lose responsivilty