Background img width controlled by text inside. Height stays static - html

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>

Related

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>

Flex boxes center of page after max height

I set a max width and height for my flex boxes for responsive pages. After it surpasses the max height and width the boxes no longer become positioned in the center of the page even though I have justify-content: center; on my .flex-container. What am I doing wrong here? Anything helps, thanks!
CodePen
.flex-container {
display:flex; justify-content: center;}
.flex-post {
background-color: #f1f1f1;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
flex: 1 0 auto;
height:auto;
max-height:270px;
max-width:270px;}
.flex-post:before {
content:'';
float:left;
padding-top:100%;}
.flex-post:hover {
background-color: rgba(1,1,1,0.5);}
<div>
</div>
<div class="flex-container">
<div class="flex-post">1</div>
<div class="flex-post">2</div>
<div class="flex-post">3</div>
</div>
<div class="flex-container">
<div class="flex-post">4</div>
<div class="flex-post">5</div>
<div class="flex-post">6</div>
</div>
<div class="flex-container">
<div class="flex-post">7</div>
<div class="flex-post">8</div>
<div class="flex-post">9</div>
</div>
You need to center the containers.
Add this to your code:
body {
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
height: 100vh;
margin: 0;
}
revised codepen
Note that block elements consume the full width of their parent, by default. This behavior, however, does not extend to height (more details).
.flex-post {
background-color: #f1f1f1;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
flex: 1 0 auto;
height: auto;
max-height: 270px;
max-width: 270px;
display: flex;
align-items: center;
justify-content: center;
}

CSS Flexbox annoying slight offset of row elements [duplicate]

This question already has answers here:
White space under image [duplicate]
(5 answers)
Remove white space from image
(3 answers)
Closed 5 years ago.
So I have two divs in a full width container that I want to give variable sizing with flexbox, but no matter what I do, there is an annoying offset at the bottom. Using margins I can come close to fixing the problem, but it's never perfect.
If you run the code snippet below and scroll to the bottom you can see it, the image and the black content container are not aligned at the bottom.
What's going on?
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
margin-bottom:7px;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg"/>
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</container>
There is some space below the image since the image is an inline-element and as such there is some space reserved below the (invisble) baseline that the image is aligned to vertically. To avoid that, there are two possible solutions:
1.) Apply display: block; to the image (see first snippet)
or
2.) Apply font-size: 0 to the image container (see second snippet)
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
img {
display: block;
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg" />
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</div>
SECOND SOLUTION:
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
font-size: 0;
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg" />
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</div>
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
margin-bottom:4px;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg"/>
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</container>
Looks like the margin is just a bit off

css vertical align text middle with overflow hidden

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>

vertical centered columns with headings flexbox

i managed to have vertically aligned column content with flexbox.
However, I need to have headers for the columns as well.
How can i have headers, in which the text is centered to the width of the column and which are at the top of the columns (the content must remain centered vertically)
see my codepen try:
http://codepen.io/anon/pen/QNmrNx
.tothetop{
position: absolute;
top:0;
text-align:center;
background: yellow;
}
put the headers on top, but the width does not match the column width so I can't center the text
If I understand you correctly, you want header text to be center horizontally in respect to columns. This may help you -
header {
display: flex;
justify-content: center;
width: 500px;
margin: auto;
}
.myView {
margin: auto;
display: flex;
flex-direction: row;
justify-content: space-around;
height: 500px;
width: 500px;
}
.wpType {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
}
.wpType:nth-child(even){
background: blue;
}
.wpType:nth-child(odd){
background: red;
}
.wp{
flex: 0 1 auto;
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
background: white;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
<header>header</header>
<div class="myView">
<div class="wpType">
<div class="wp"></div>
</div>
<div class="wpType">
<div class="wp"></div>
<div class="wp"></div>
<div class="wp"></div>
</div>
<div class="wpType">
<div class="wp"></div>
<div class="wp"></div>
<div class="wp"></div>
</div>
<div class="wpType"><div class="wp"></div>
<div class="wp"></div></div>
<div class="wpType"><div class="wp"></div></div>
</div>