css vertical align text middle with overflow hidden - html

i'm rendering divs within a loop. each div has a text (could be long- which means overflow: hidden is needed.)
As you can see the text on the left side is not vertical aligned middle.
I searched for an answer and every solution i found which made the left one vertical align middle, caused trouble to the first one, like in this case where the text of the right div is not display from the beggining ("***This is my desc..."):
The html code is:
<div class="js-recent-item recent-item" data-view-url="{{url}}">
{{#if isReport}}
<label class="report-card-title" title="{{title}}">{{title}}</label>
<div class="no-sides-padding report-container">
<div class="report-card-text Aligner" title="{{description}}">{{description}}</div>
</div>
{{/if}}
</div>
The css:
.report-card-text {
font-size: 12px;
padding: 10px;
text-align: center;
line-height: 18px;
height: 100px;
overflow: hidden;
}
.Aligner {
display: flex;
align-items: center;
justify-content: center;
}
.Aligner-item {
max-width: 50%;
}
.Aligner-item--top {
align-self: flex-start;
}
.Aligner-item--bottom {
align-self: flex-end;
}
I've tried to solve this with flexbox and with table-cell and didn't succeeded.
I will be happy to get your advises.
Thanks,
Ella.
************** UPDATE: The solution: *********************
{{#if isReport}}
<label class="report-card-title" title="{{title}}">{{title}}</label>
<div class="report-container">
<div class="report-card-text" title="{{description}}">{{description}}</div>
</div>
{{/if}}
The CSS:
.report-container{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 122px;
margin-top: -6px;
overflow: hidden;
}
.report-card-text{
.card-text;
padding : 5px 10px;
text-align: center;
line-height: 23px;
max-height: 100%;
}

Add a span inside the label and give it a max-height and overflow: hidden, and for the description you set a max-height/overflow: hidden on the report-card-text
body {
display: flex;
}
.js-recent-item {
display: flex;
flex-direction: column;
width: 200px;
}
.report-card-title {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 50px;
font-size: 26px;
border: 1px solid black;
}
.report-card-title span {
max-height: 50px;
overflow: hidden;
}
.report-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 150px;
font-size: 26px;
border: 1px solid black;
}
.report-card-text {
max-height: 100%;
overflow: hidden; /* use "auto" if to be able to scroll */
}
<div class="js-recent-item recent-item" data-view-url="{{url}}">
<label class="report-card-title" title="{{title}}">
<span>title 1 </span>
</label>
<div class="no-sides-padding report-container">
<div class="report-card-text" title="{{description}}">
description 1
</div>
</div>
</div>
<div class="js-recent-item recent-item" data-view-url="{{url}}">
<label class="report-card-title" title="{{title}}">
<span>first title title title title title title title title title last </span>
</label>
<div class="no-sides-padding report-container">
<div class="report-card-text Aligner" title="{{description}}">
first description description description description description description description description description description description description description description description last
</div>
</div>
</div>

Related

how to center div in the middle of the page?

After googling for hours, I am still stuck at this problem. Since I used flex-direction:column, to align the child divs hence, I used justify-content:center but the child divs look aligned to the left.
<div class=container>
<div class= "wrapper">
<div class = "image-float">
<img class = "profile-picture" src = "{% static 'images/image.jpg'%}">
</div>
<div>
<p>Welcome to Sparison...</p>
</div>
<div class="intro">
<h1>Copy code below and share with friends</h1>
</div>
<div class="url-container input-group">
<input type="text" id="random" class="url input-border form-control" value="">
<div class="input-group-append">
<span class="input-border input-group-append input-group-text">
<i class="far fa-copy url-copy-icon"></i></span>
</div>
</div>
</div>
</div>
The relevant CSS is below:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
font-family: Montserrat;
font-size: 50px;
font-weight: bold;
background-color: var(--very-pale-blue);
width:100%;
height:100vh;
}
.container{
max-width: 100%;
height: 90vh;
margin-top: 0;
}
.wrapper{
display: flex;
flex-wrap: wrap;
max-width: 100%;
height: 90vh;
justify-content: center;
flex-direction: column;
}
.url-container .input-group {
border-radius: 10px;
width: 50%;
}
.input-group {
position: relative;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-ms-flex-align: stretch;
align-items: stretch;
width: 50%;
/* background-color: #1DB954; */
border-radius: 2px;
}
Above is how the page renders. every element under the wrapper div should be in the middle of the page centered vertically.
You can find more information about flex here:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
.wrapper{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: 100%;
height: 90vh;
}
Link to jsfiddle:
https://jsfiddle.net/y4qra692/
Change the container width
.container {
width: 100%;
}
Then for the wrapper as you have changed the flex-direction to column you now need align-items rather than justify-content as (confusingly) when you change the direction these properties swap around:
.wrapper {
width: 100%;
display: flex;
align-items: center;
}
To make div centered you can add this code to .wrapper
.wrapper{
width: 50%;
margin:auto;
}
To make a div centered:
<section class="middle">
<div class="div">
<p>Hi</p>
</div>
</section>
Style.css
.middle {
height: 100vh; /* or height you want */
width: 100%;
position: relative;
}
.div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

Place image next to div with flexbox

I have this image and this box I'm trying to put on the same line. The box is just going to be holding a header and some text, but I can't seem to get them on the same line. I'm using flexbox and I did some research into this, but can't quite work it out. Here's the code:
#container {
min-height: 95vh;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.box {
height: 400px;
width: 600px;
background-color: #f5f2ed;
text-align: justify;
padding: 20px;
}
img {
width: auto;
height: 400px;
border-radius: 16px;
}
<div id="container">
<div class="main">
<img src="/">
<div class="box">
<h2>hello</h2>
<p>paragraph here...</p>
</div>
</div>
</div>
I made two divs inside the container because the image is going to be outside the box with the text.
Maybe display: flex; in .main{} can fix the problem.
You should add display:flex property to main element and flex :1 property to both child elements of main element
#container {
min-height: 95vh;
}
.main {
display : flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.box {
height: 400px;
width: 50%;
flex : 1;
background-color: #f5f2ed;
text-align: justify;
padding: 20px;
}
img {
width: auto;
height: 400px;
flex : 1;
border-radius: 16px;
}
<div id="container">
<div class="main">
<div class="pic-div">
<img src="/">
</div>
<div class="box">
<h2>hello</h2>
<p>paragraph here...</p>
</div>
</div>
</div>

Vertical scrolling for multiple div tags inside a div tag

I am trying to create a scrollable file tree. Minimal code can be found below.
https://jsfiddle.net/3kLmchot/1/
Problem is when I try to add overflow-y: scroll;, to the filebrowse-outer div, the first few items are not visible at all. This problem gets worse when I add more divs.
It seems like items are added about the center of the filebrowse-outer div, so the items that are added at the bottom can be found via scrolling, but the items that are added at the top are invisible. (see the visual aid below)
item 1
item 2
center of the div
item 3
item 4
Scroll bar forms at around item 2, which makes item 1 invisible. Does anyone know why this is happening, and potential solutions?
I got it to work with the following styles, specifically removing justify-content: center; from your .flex-container that is being used by multiple divs and only applying it to the one that is for .flex-container.filebrowse-inner
.side-bar {
position: fixed;
width: 20%;
height: 200px;
display: flex;
flex-direction: column;
}
.flex-container {
display: flex;
/* justify-content: center; <-- THIS LINE IS CAUSING THE ISSUE */
align-items: center;
}
.flex-container.filebrowse-outer {
flex-direction: column;
border: 1px solid #f00;
overflow-y: scroll;
align-items: stretch;
}
.flex-container.filebrowse-inner {
flex-direction: row;
justify-content: center;
width: 100%;
padding-bottom: 15px;
text-align: left;
position: relative;
}
/* .display-container {
width: 70%;
overflow: auto;
} */
I think that you meant to have scroll bar in the external div with class 'side-bar', if yes:
.side-bar {
position: fixed;
width: 20%;
height: 200px;
display: flex;
flex-direction: column;
overflow-y:scroll;
}
.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
.flex-container.filebrowse-outer {
flex-direction: column;
border: solid;
min-height: max-content; /* For safari*/
/* overflow-y: scroll; */
align-items: stretch;
}
.flex-container.filebrowse-inner {
flex-direction: row;
width: 100%;
min-height: max-content; /* For safari*/
padding-bottom: 15px;
text-align: left;
position: relative;
}
.display-container {
width: 70%;
overflow: auto;
}
<div class="side-bar">
<div class="flex-container filebrowse-outer">
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 1!</p>
</div>
</div>
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 2!</p>
</div>
</div>
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 3!</p>
</div>
</div>
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 4!</p>
</div>
</div>
</div>
</div>
You can then move the border to 'side-bar' class.

Background img width controlled by text inside. Height stays static

I'm trying to make a div with the background img of a bubble that have the count of comments on a post inside it. I want the width to be controlled by the text inside while the height of the div should stay static so I can keep the text inside centered verticaly.
What I have now is the code under here. It looks ok when the value inside is a 3 digit number, but if I change it to a 2 digit number or anything other then a 3 digit number the bubble moves verticaly and horizontaly so the text is not centered anymore.
#wrapper {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
min-width: 100%;
align-items: baseline;
}
#comment_text {
display: flex;
font-weight: bold;
}
#comment_img {
display: flex;
background: url('https://rajohan.no/img/icons/picons_basic_1/SVG/basic1-021_chat_comment_bubble.svg');
background-repeat: no-repeat;
background-size: 100%;
padding: 21px;
margin-left: 10px;
}
#comment_count {
display: flex;
font-weight: bold;
}
<div id="wrapper">
<div id="comment_text">
COMMENTS
</div>
<div id="comment_img">
<div id="comment_count">
160
</div>
</div>
</div>
Try adding background-position:center - this centers the background image horizontally as the element changes width. I think that's what you're trying to achieve.
#wrapper {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
min-width: 100%;
align-items: baseline;
}
#comment_text {
display: flex;
font-weight: bold;
}
.comment_img {
display: flex;
background: url('https://rajohan.no/img/icons/picons_basic_1/SVG/basic1-021_chat_comment_bubble.svg');
background-repeat: no-repeat;
background-position: center;
padding: 21px;
margin-left: 10px;
}
.comment_count {
display: flex;
font-weight: bold;
}
<div id="wrapper">
<div id="comment_text">
COMMENTS
</div>
<div class="comment_img">
<div class="comment_count">
16
</div>
</div>
<div class="comment_img">
<div class="comment_count">
160
</div>
</div>
</div>
I have added some code in your #comment_count.
#wrapper {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
min-width: 100%;
align-items: baseline;
}
#comment_text {
display: flex;
font-weight: bold;
}
#comment_img {
display: flex;
background: url('https://rajohan.no/img/icons/picons_basic_1/SVG/basic1-021_chat_comment_bubble.svg');
background-repeat: no-repeat;
background-size: 100%;
padding: 21px;
margin-left: 10px;
}
#comment_count {
display: flex;
font-weight: bold;
min-height: 20px;
min-width: 25px;
justify-content: center;
}
<div id="wrapper">
<div id="comment_text">
COMMENTS
</div>
<div id="comment_img">
<div id="comment_count">
160
</div>
</div>
</div>
<div id="wrapper">
<div id="comment_text">
COMMENTS
</div>
<div id="comment_img">
<div id="comment_count">
22
</div>
</div>
</div>

flexbox vertical center with 100% height

Is it possible to use flexbox to center vertically and horizontally for background color purposes?
.wrapper {
display: flex;
width: 100vw;
height: 100vh;
text-align: center;
}
.item {
flex: 1;
background-color: green;
align-self: center;
justify-content: center;
}
<div class='wrapper'>
<div class='item'>
sdafsdfs
</div>
</div>
If I add a height: 100% the text moves back to the top.
Add display: flex for your items. Then only it can align items inside it as per your requirements. Please have a look at the updated code.
.wrapper {
display: flex;
width: 100vw;
height: 100vh;
text-align: center;
}
.item {
display: flex;
background-color: green;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
}
<div class='wrapper'>
<div class='item'>
sdafsdfs
</div>
</div>
Hope this help