flexbox elements too far apart with different font sizes - css

I need all of my elements inside my flexbox container to be close to each other. Unfortunately, since they have different font sizes, this is proving hard to accomplish. I could fix it using margins, but it seems like a hacky way to do it.
#price {
font-weight: bold;
border: none;
font-size: 1.5em;
}
#currency {
color: #000000;
font-size: 1.5em;
font-weight: bold;
}
#price:focus {
outline: none;
}
#price-info {
display: flex;
align-items: flex-start;
}
<div id="price-info">
<p id="currency">U$S</p><input id="price" type="text" value={this.state.price} onChange={this.handleChange} />
<p>$/m2{sizePrice}</p>
</div>
Updated problem, still spacing problems

#price{
font-weight: bold;
border:none;
font-size: 1.5em;
}
#currency{
color:#000000;
font-size: 1.5em;
font-weight: bold;
}
#price:focus {
outline:none;
}
#price-info{
display: flex;
justify-content: center;
align-items: baseline
}
<div id="price-info">
<p id="currency">U$S</p><input id="price" type="text" value={this.state.price} onChange={this.handleChange} />
<p>$/m2{sizePrice}</p>
</div>
Here is a possible solution you are looking for.
You can play around with justify-content to place your elements horizontally.
I believe what you wanted can be done by align-items property.

Try this. but is not much easy you have to set margins manually
#price {
font-weight: bold;
border: none;
font-size: 1.5em;
}
#currency {
color: #000000;
font-size: 1.5em;
font-weight: bold;
margin-right: .3rem;
}
#price:focus {
outline: none;
}
#price-info {
display: flex;
align-items: center;
}
#size-price{
margin-left: -5rem;
}
.itens{
display: inline-block;
width: fit-content;
}
<div id="price-info">
<p class="itens" id="currency">U$S</p>
<input class="itens" id="price" type="text" value={this.state.price} onChange={this.handleChange} />
<p class="itens" id="size-price">$/m2{sizePrice}</p>
</div>

Related

grid-template-columns not respected in Safari 13

I'm having an issue where the grid columns in my React component are showing up on separate rows in Safari 13. Numerous Google searches have come up with nothing and I'm really hoping somebody here can help. Here is the code of my React component (I've left out a bunch of state stuff and functions that aren't relevant):
import React, { useState, useRef } from 'react'
import classNames from 'classnames'
import SlideToggle from 'react-slide-toggle'
import styles from './shoppingListItem.module.css'
const ShoppingListItem = ({
itemId,
listTitle,
canEdit,
description,
quantity,
notes
}) => {
const [currentQuantity, setCurrentQuantity] = useState(quantity)
return(
<div className={styles.root}>
<button className={styles.button} onClick={toggleDetails}>
<span ref={headerRef} className={classNames(styles.header, { [styles.headerEditable]: canEdit })}>
{canEdit &&
<span className={styles.editIcons} ref={iconsRef}>
<div className={styles.icon} ref={deleteRef} onClick={destroyItem}><FontAwesomeIcon className={classNames(styles.fa, styles.destroyIcon
)} icon={faTimes} /></div>
<div className={styles.icon} ref={editRef} onClick={showEditForm}><FontAwesomeIcon className={styles.fa} icon={faEdit} /></div>
</span>}
<h4 className={styles.description}>{description}</h4>
</span>
<span className={styles.quantity}>
{canEdit && <div className={styles.icon} ref={incRef} onClick={incrementQuantity}>
<FontAwesomeIcon className={styles.fa} icon={faAngleUp} />
</div>}
<div className={styles.quantityContent}>
{currentQuantity}
</div>
{canEdit && <div className={styles.icon} ref={decRef} onClick={decrementQuantity}>
<FontAwesomeIcon className={styles.fa} icon={faAngleDown} />
</div>}
</span>
</button>
<SlideToggle toggleEvent={toggleEvent} collapsed>
{({ setCollapsibleElement }) => (
<div className={styles.collapsible} ref={setCollapsibleElement}>
<p className={styles.notes}>{notes || 'No details available'}</p>
</div>
)}
</SlideToggle>
</div>
)
}
The relevant CSS is:
.root {
font-family: 'Quattrocento Sans', Arial, Helvetica, sans-serif;
background-color: var(--main-color);
border-bottom: 1px solid var(--border-color);
}
.root:hover {
background-color: var(--hover-color);
}
.button {
width: 100%;
background: none;
border: none;
border-bottom: 1px solid var(--border-color);
text-align: left;
cursor: pointer;
padding: 0;
display: grid;
grid-template-columns: 2fr 1fr;
}
.fa {
color: var(--title-text-color);
}
.destroyIcon {
margin-right: 8px;
}
.header {
display: inline-block;
padding: 32px 16px;
border-right: 1px solid var(--border-color);
}
.headerEditable {
display: flex;
align-items: flex-start;
}
.editIcons {
display: flex;
flex-direction: row;
margin: auto 12px auto 0;
}
.quantity {
font-family: 'Quattrocento Sans', Arial, Helvetica, sans-serif;
font-size: 1rem;
color: var(--title-text-color);
display: inline-block;
margin: auto 0;
padding: 32px 8px;
display: flex;
justify-content: space-between;
}
.quantityContent {
margin: auto;
}
.icon {
display: inline-block;
padding: 4px;
cursor: pointer;
}
.description {
display: inline-block;
font-family: 'Quattrocento Sans', Arial, Helvetica, sans-serif;
font-size: 1.025rem;
color: var(--title-text-color);
margin: auto 0;
line-height: 1.25;
}
This is what the list item .button element/its children look like in Chrome (i.e., the way it's supposed to look):
This is the way the element looks in Safari (i.e., how it's not supposed to look):
The issue here turned out to be that I was using the button element incorrectly as the slide-toggle trigger and Chrome didn't mind but Safari didn't like it. I had made it a button because of accessibility reasons but I guess that was the wrong way to go. Changing the slide-toggle trigger into a div made it look the same in both Chrome and Safari.

CSS: paragraph display inline when it shouldn't

I'm trying to design a Card but my <p> text is somehow inline. I don't want that. I tried display: block;and other options but that doesn`t work.
I work with Bootstrap and CSS but as far as I know, this is a plain CSS question.
Here is my Html for one card:
<div class="">
<div class="card">
<img class="card-img-top" src="../../../assets/images/offer_1.jpg">
<div class="card-body text-center">
<h1>{{offer.name}}</h1>
<p>{{offer.description}}</p>
<h2>{{offer.price}}€</h2>
</div>
And here my SCSS:
$font-serif: 'Playfair Display', serif;
$font-sans-serif: 'Raleway', sans-serif;
$color_price: #F96D00;
$color_description: #b8b8b8;
.card {
border-radius: 0;
.card-body {
padding: 3rem;
h1 {
font-family: $font-serif;
font-weight: bold;
line-height: 1.45;
font-size: 26px;
}
h2 {
font-family: $font-serif;
font-weight: bold;
line-height: 1.45;
font-size: 22px;
color: $color_price;
}
p {
font-family: $font-serif;
line-height: 1.45;
font-size: 18px;
color: $color_description;
height: 100px;
}
}
}
It looks like this:
Edit: The Problem is the paragraph has a flixible width but the text inside is somehow always inline and wider then the paragraph itself.
You don't need to use overflow:hidden or !important on text-align: center.
You need to set a maximum width on the card element that is the parent of the P.
Look at your styles rendered in a codepen:
https://codepen.io/NeilWkz/pen/vaaMoO
The name and price are left-aligned, and the description is center aligned.
This is because you've used a helper class 'text-center' I assume this comes from some framework that you have not mentioned in your question. The helper class will not cascade down if you have alignment set on the h1 or h2 element, in that case you need to apply it to the actual element:
HTML:
<div class="">
<div class="card">
<img class="card-img-top" src="../../../assets/images/offer_1.jpg">
<div class="card-body">
<h1 class="text-center">{{offer.name}}</h1>
<p class="text-center">{{offer.description}}</p>
<h2 class="text-center">{{offer.price}}€</h2>
</div>
</div>
SCSS:
$font-serif: 'Playfair Display', serif;
$font-sans-serif: 'Raleway', sans-serif;
$color_price: #F96D00;
$color_description: #b8b8b8;
.card {
border-radius: 0;
//Added width & background color for demo
width: 320px;
background: azure;
.text-center {
text-align: center;
}
.card-body {
padding: 3rem;
display: block;
h1 {
font-family: $font-serif;
font-weight: bold;
line-height: 1.45;
font-size: 26px;
display: center;
}
h2 {
font-family: $font-serif;
font-weight: bold;
line-height: 1.45;
font-size: 22px;
color: $color_price;
}
p {
font-family: $font-serif;
line-height: 1.45;
font-size: 18px;
color: $color_description;
height: 100px;
}
}
}
Here's a working pen with the styles above:
https://codepen.io/NeilWkz/pen/gjjJpw

strech two lines to same length?

I would like to reproduce the following image with CSS:
Especially important is to me that both lines have equal length:
I tried to recreate it with this code (jFiddle):
.box {
font-family: "Open Sans";
line-height: 28px;
font-weight: 700;
background-color: #2c343c;
margin: 20px;
padding: 20px;
width: 150px;
text-align: justify;
}
.box .name {
color: #fff;
font-size: 24px;
}
.box .sub {
color: #f29400;
font-size: 15px;
letter-spacing: 1px;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<div class="box">
<span class="name">Dr. Nielsen</span><br>
<span class="sub">WEBDEVELOPER
</div>
But its not quite perfect:
Is there a good way how to achieve this with CSS so that both lines get the same lengths on any device. Or is it recommended to rather use a picture for this?
You can give a try to text-align-last:justify;
Beside, to avoid setting a width, you may turn the box into a block that shrinks on its content via display:table; . You can also avoid the <br> setting spans into blocks
.box {
font-family: "Open Sans";
line-height: 28px;
font-weight: 700;
background-color: #2c343c;
margin: 20px;
padding: 20px;
display: table;
text-align: justify;
}
span {
display: block;
text-align-last: justify;
}
.box .name {
color: #fff;
font-size: 24px;
}
.box .sub {
color: #f29400;
font-size: 15px;
letter-spacing: 1px;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<div class="box">
<span class="name">Dr. Nielsen</span>
<span class="sub">WEBDEVELOPER</span>
</div>
<div class="box">
<span class="name">Dr. Nielsen</span>
<span class="sub">WEB-DEVELOPPER</span>
</div>
<div class="box">
<span class="name">Watch Out when top too long</span>
<span class="sub">single-short-breaks!</span>
</div>
You should remove the text-align: justify; on the container (.box) and give .name some extra letter-spacing so the 2 lines line up.
Be aware that this would be completely dependent on the font settings. Another font-family, size, etc. would change the size of both lines and make it different again. If people visiting your website changed their browser font size, then they won't see exactly what you see. If you want to avoid this (as much as possible) then look into font-size resets.
.box {
font-family: "Open Sans";
line-height: 28px;
font-weight: 700;
background-color: #2c343c;
margin: 20px;
padding: 20px;
width: 150px;
}
.box .name {
color: #fff;
font-size: 24px;
letter-spacing: .3px;
/* added */
}
.box .sub {
color: #f29400;
font-size: 15px;
letter-spacing: 1px;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<div class="box">
<span class="name">Dr. Nielsen</span>
<span class="sub">WEBDEVELOPER</span>
</div>

Is there a way to center a text vertically regardless of font-family or font-size?

I am looking for a font-agnostic way of centering text in a div. I have a button style fixed height div. And i want to center the text in it (one line only) vertically. The problem is that when i change the font-family, some fonts do not center vertically. Is there a way to compute the desired lineheight based on font metrics?
Try this solution, hope it works for you.
.content{
align-items: center;
display: flex;
justify-content: center;
height: 150px;
}
<div class="content">
<span>Hello world</span>
</div>
You can set line-height equal to the height of the button.
div {
height: 50px;
line-height: 50px;
border: 1px solid;
}
<div>Button</div>
Try this, im not using line height:
button {
height: 40px;
display: table;
}
button>span {
display: table-cell;
text-align: center;
vertical-align: middle;
}
button.one {
font-family: "Helvetica Neue";
font-size: 30px;
}
button.two {
font-family: sans-serif;
font-size: 18px;
}
button.three {
font-family: Helvetica;
font-size: 18px;
}
button.four {
font-family: Arial;
font-size: 20px;
}
button.five {
font-family: Times New Roman;
font-size: 30px;
}
<button class="one"><span>text</span></button>
<br>
<br>
<button class="two"><span>text</span></button>
<br>
<br>
<button class="three"><span>text</span></button>
<br>
<br>
<button class="four"><span>text</span></button>
<br>
<br>
<button class="five"><span>text</span></button>
If the text size extends the height of the button then you wil get a failed result which should be obvios, since the button has a fixed height.

Scroll issue with line height & Titillum Web font family

If I use Titillium web font family I can scroll on <span> and <input> with line-height value not normal and <1.6. But not when I use San-serif. I expect when I use line-height:1.4 there is no scroll.
I don't know, is it because of inline-block behavior?
.box {
padding: 0;
margin: 0;
background-color: white;
}
input {
display: inline-block;
line-height: 1.4;
font-size: 16px;
font-family: Titillium web;
padding: 0;
}
input.test {
line-height: 1.2;
}
input.test-2 {
line-height: normal;
}
span {
display: inline-block;
line-height: 1.4;
font-size: 16px;
font-family: Titillium web;
overflow: auto;
padding: 0;
background-color: aqua;
}
p {
font-size: 16px;
font-family: Titillium web;
line-height: 1;
padding: 0;
margin: 0;
}
<div class="box">
<span>####</span><input type="text" value='####' /><input class="test" type="text" value='####' /><input class="test-2" type="text" value='####' />
</div>
<p>#############</p>
Here are my question:
Is each font family have different optimal line-height?
If so, when I should use line-height: normal? Because many articles suggest using specific line-height
Try to use the same value in line-height and font-size. Instead of 1.4 put 16px in line-height also.

Resources