What is the reason that `font-size` does not match the `height`? For example, if the `<span>` on the left is 2rem, on the right I could fit 2 smaller `<span>`s, each with a size of 1rem

How can I achieve a layout like this?

https://i.sstatic.net/rrISu.png

Essentially, I have 2 containers in flexbox:

  • The first one with text "33"
  • The second one is a grid container:
    • The first element in the grid is "EUR"
    • The second element in the grid is ".49"

Based on this description, I attempted to create an HTML structure using the following code:

<nav>
  <div class="item">
    <span class="date">Dom 07 Aug</span>

    <div class="price-container">
      <div class="first-price-container">
        <span class="price-big"> 371 </span>
      </div>

      <div class="second-price-container">
        <span class="currency">EUR</span>
        <span class="price-small"> 74 </span>
      </div>
    </div>
  </div>
</nav>

I tried applying CSS to achieve this layout, but the outcome is not accurate. I want the "33" text to be of the same height as the grid layout:

https://i.sstatic.net/Bgt1H.png

Here is the CSS code used:

@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
* {
  box-sizing: border-box;
}

nav {
  --small-font: 1rem;
  font-family: poppins;
  display: flex;
  gap: 1rem;
}

.item {
  display: grid;
}

.price-container {
  display: flex;
}

.second-price-container {
  display: grid;
  font-size: var(--small-font);
}

.price-big {
  font-size: calc(var(--small-font) * 2);
  font-weight: bold;
}

.price-small::before {
  content: ".";
}

.item .date {
  color: blue;
}
<nav>
  <div class="item">
    <span class="date">Dom 07 Aug</span>

    <div class="price-container">
      <div class="first-price-container">
        <span class="price-big"> 37 </span>
      </div>

      <div class="second-price-container">
        <span class="currency">EUR</span>
        <span class="price-small"> 49 </span>
      </div>
    </div>
  </div>

  ... <!-- additional repeated item blocks will follow -->
</nav>

In the code, I assumed that if two elements have a font size of 1rem each, then combining them should result in the same height as the grid layout.

However, it appears that there is an incorrect gap between elements in the grid layout.


Another attempt was to increase the font size significantly, but the issue persisted. It seems that when the larger font size increases, so does the adjacent element on the right.


Please assist me, as this aspect is unclear to me. The problem arises only with font size and not height property.

Question:

Is there a formula for ensuring that font size equals height? This would allow for achieving pixel-perfect designs without excessive coding.

Now I will provide another example code illustrating how I want it to appear

It is much simpler to manage height using grid and flex properties.

The goal is to replicate the image shown, but with fonts instead.

https://i.sstatic.net/SWKKT.png

As seen, the height matches the other two elements on the right perfectly.

Sample code (for demonstration purposes):

nav {
  display: flex;
}

.item {
  /* square */
  height: 10rem;
  width: 10rem;
  display: flex;
}

.item>* {
  flex: 1;
}

.another {
  display: grid;
}
<nav>
  <div class="item">
    <div class="big" style="background-color: red;"></div>
    <div class="another">
      <div class="small" style="background-color: yellow;"></div>
      <div class="small" style="background-color: green;"></div>
    </div>
  </div>
</nav>

Answer №1

Font size does not necessarily determine the height of text.

font-size * line-height == height

To adjust the height, I included line-height: 1 in your .price-container

@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900
1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
* {
  box-sizing: border-box;
}

nav {
  --small-font: 1rem;
  font-family: poppins;
  display: flex;
  gap: 1rem;
  /* space between copies of the container .item */
}

.item {
  display: grid;
}

.price-container {
  display: flex;
  line-height: 1;
}

.second-price-container {
  display: grid;
  font-size: var(--small-font);
}

.price-big {
  font-size: calc(var(--small-font) * 2);
  /* I think that since the grid layout has two text with 1rem height, it will be 2rem (but not seems to work) */
  font-weight: bold;
}

.price-small::before {
  content: ".";
}

.item .date {
  color: blue;
}
<nav>
  <div class="item">
    <span class="date">Dom 07 Ago</span>

    <div class="price-container">
      <div class="first-price-container">
        <span class="price-big"> 37 </span>
      </div>

      <div class="second-price-container">
        <span class="currency">EUR</span>
        <span class="price-small"> 49 </span>
      </div>
    </div>
  </div>

  <div class="item">
    <span class="date">Dom 07 Ago</span>

    <div class="price-container">
      <div class="first-price-container">
        <span class="price-big"> 37 </span>
      </div>

      <div class="second-price-container">
        <span class="currency">EUR</span>
        <span class="price-small"> 49 </span>
      </div>
    </div>
  </div>

  <div class="item">
    <span class="date">Dom 07 Ago</span>

    <div class="price-container">
      <div class="first-price-container">
        <span class="price-big"> 37 </span>
      </div>

      <div class="second-price-container">
        <span class="currency">EUR</span>
        <span class="price-small"> 49 </span>
      </div>
    </div>
  </div>
  <!-- and soo on... repeatedly, however after I will find the correct structure, I will create a function in javascript that generate it -->
</nav>

Answer №2

You can achieve the desired layout shown in the screenshot by using flex properties to position the elements. It's a good idea to group the "EUR" and ".49" components of the price into their own div with a flex-column style.

@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
* {
  box-sizing: border-box;
}

nav {
  --small-font: 1rem;
  font-family: poppins;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  row-gap: 1em;
}

.date {
  margin-bottom: 1em;
}

.item {
  display: grid;
}

.right-side {
  display: flex;
  flex-flow: column;
}

.price-container {
  display: flex;
}

.second-price-container {
  display: grid;
  font-size: var(--small-font);
}

.currency {
  padding-left: 7px;
  font-weight: 300;
}

.currency,
.price-small {
  font-size: 1.6em;
}

.price-small {  
  font-weight: 500;
}

.price-big {
  font-size: calc(var(--small-font) * 5);
  font-weight: 600;
  line-height: 1;
}

.price-small::before {
  content: ".";
}

.item .date {
  color: blue;
}
<nav>
  <div class="item">
    <span class="date">Dom 07 Ago</span>
    <div class="price-container">
      <span class="price-big">33</span>
      <div class="right-side">
        <span class="currency">EUR</span>
        <span class="price-small">49</span>
      </div>
    </div>
  </div>
  <div class="item">
    <span class="date">Dom 07 Ago</span>
    <div class="price-container">
      <span class="price-big">33</span>
      <div class="right-side">
        <span class="currency">EUR</span>
        <span class="price-small">49</span>
      </div>
    </div>
  </div>
  <div class="item">
    <span class="date">Dom 07 Ago</span>
    <div class="price-container">
      <span class="price-big">33</span>
      <div class="right-side">
        <span class="currency">EUR</span>
        <span class="price-small">49</span>
      </div>
    </div>
  </div>
</nav>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating choices within a div in real-time by changing the select box dynamically

I need to swap out the select box that is nested inside this div <div class="models"> <select disabled="disable"> <option>Model Name</option> </select> </div> I am attempting to target the div and manipul ...

Is there a way to enable scrolling on the page upon loading, without moving the embedded PDF object itself?

After embedding a PDF object on my webpage, I noticed that when loading the page and scrolling using the mouse wheel, it actually scrolls up and down inside the PDF instead of moving through the entire page. To fix this issue, I have to first click in a bl ...

How can I create an input field that only reveals something when a specific code is entered?

I am currently developing a website using HTML, and I would like to create an admin section that requires a password input in order to access. After consulting resources on w3schools, I attempted the following code: <button onclick="checkPassword()" i ...

The background image within Bootstrap theme layouts appears to be missing from both the styles and divs

Encountering a peculiar dilemma here. Attempted to apply a background image to a custom style in order to override/extend a bootstrap column style. Additionally, tried embedding a style directly in the div attribute. Strangely, neither method seems to be e ...

What are the steps to shift columns to the left within a table?

I need to adjust the alignment of the columns and also create a fixed size space between them. Currently, it appears like this: https://i.sstatic.net/F7Rqk.png My desired outcome is something similar to: https://i.sstatic.net/ALKa9.png CSS Code: .tabl ...

Creating interactive and adaptable maps

Currently, I am working on making a Joomla website responsive. However, I have encountered an issue with the SobiPro Map. The problem arises when displaying a map (background img) and points (a link + img) over it with an absolute position. As a result, wh ...

How can I apply specific styling to certain cells within a table?

Is there a way to apply styles specifically to certain table headers in an HTML table without altering the HTML or introducing JavaScript? I need to target only 4 out of the total 8 table headers. To see the code and example for this question, visit: http ...

Using PHP and Bootstrap to input data inside a modal

I am working on a page that has a form located here: https://jsfiddle.net/5tzg8kzm/9/ <body> <h1>&nbsp;</h1> <!-- Page Content --> <div class="container"> <!-- Trigger the modal with a button --> ...

Center the content within the div

Is there a way to center the content of the second column within this div? I've tried various methods without success so far. I'm currently using Bootstrap 4. Can anyone provide guidance on how to achieve this? <div class="card"> < ...

Padding problem arises when you wrap an image with an anchor tag

I'm having an issue with a div containing an anchor-wrapped image, as there seems to be extra padding at the bottom of the div. How can I remove this? HTML: <div id="lineup"> <div class="line-up-click"> ...

The absence of the `breakInside` rule in GrooCSS

Currently, I am utilizing GrooCSS 1.0-GA for my CSS coding needs. Regrettably, it appears that GrooCSS does not provide support for breakInside as illustrated in the code snippet below: GrooCSS.process(new Config(prettyPrint: true)) { body { breakIns ...

The HTML content I created is too large for the screen and is extending beyond its borders

The navigation bar and footer on my HTML page adjust themselves according to the screen width, but the main content is not adjusting properly. I tried using a media query for mobile resolution (517px), but when I tested it on a PC with a smaller screen wi ...

Guide to Repairing Uncaught TypeError: players.setAttribute is not a recognized method?

I'm a beginner and encountered an error saying Uncaught TypeError: players.setAttribute is not a function when submitting an action. How can I solve this issue? Please share your insights. ''' //selecting all necessary elements const s ...

Why can't JQuery/javascript's dynamic gallery's images be used as buttons instead of links?

I've been struggling with my online portfolio for several nights as I build my website. Despite exhaustive internet searches, I couldn't quite articulate my problem in words and tried multiple workarounds. To see the issue, please visit the beta ...

Exploring the functionality of the onblur HTML attribute in conjunction with JavaScript's ability to trigger a

How do the HTML attribute onblur and jQuery event .trigger("blur") interact? Will both events be executed, with JavaScript this.trigger("blur") triggering first before the HTML attribute onblur, or will only one event fire? I am using ...

Retrieving data from an HTML input tag and storing it in a variable

I'm currently working on a project that involves reading the contents of an uploaded file through an input tag and storing it in a variable. My goal is to then use an algorithm to decrypt the .txt file: <input type="button" value="decrypt" id="dec ...

Placing one element beside another element

I am facing an issue with my layout where I have a list taking up 100% of the height and next to it, I am attempting to place an image box. Here is the fiddle link for reference: https://jsfiddle.net/wrdvkgyj/ Even after setting my image container to inl ...

react native ScrollView items with uniform padding at the top and bottom

I'm currently working on a scroll component that resembles a gallery of images. My goal is to center the images based on the device height with equal padding on both the top and bottom. However, I'm facing an issue where the top padding does not ...

What is the functionality behind a free hosting website?

Is anyone familiar with websites like Hostinghood, where users can create a subdomain and upload HTML, CSS, etc.? I'm curious about how they operate and how I can create a similar site. This is my first question here, so please respond instead of disl ...

Exploring the power of internal linking with GatsbyJS

I am currently developing a website using gatsbyjs. I have concerns about the crawlability of "onClick" for SEO purposes and I would appreciate some assistance. This is my current code: render={data => ( <div className='feed'> ...