I am having trouble getting my flexbox items to align properly

I have designed a webpage layout (as a JPG file) and am struggling to align the subheadings and body text according to specific positions. I created the design in Photoshop and am currently using pixel measurements within a flex container in my CSS styles. However, I also want to incorporate a breakpoint to adjust the styling at certain screen sizes. I'm uncertain if using pixel measurements is the best approach for this.

/*Universal selector-reset default*/
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: Barlow, sans-serif;
}

/*nav flex box content*/
.navbar-container {
    display: flex;
    justify-content: space-between;
    height: 64px;
    width: 1365px;
    background-color: #f9eaec;
    border: 2px solid red;
}

.navbar {
    height: 100%;
    width: 100%;
}

#websiteName {
    color: black;
    font-style: Extrabold;
    font-size: 18px;
    align-items: center;
    align-content: center;
    height: 100%;
}

#logo {
    left: 62.3px;
    top: 6px;
}

.dropdown {
    top: 26px;
    align-items: flex-end;
}

/*body flex box content*/
.body-container {
    display: flex;
    height: 410px;
    width: 693px;
    margin: auto;
    transform: translateY(115.6px);
    border: 2px solid red;
}

#name {
    font-style: Extrabold;
    font-size: 59px;
    left: 339.8px;
    top: 179px;
    border: 2px solid blue;
    width: 276.2px;
}

#subname {
    font-style: Extrabold;
    font-size: 59px;
    border: 2px solid blue;
    width: 276.2px;
}

.subheading {
    font-style: Extrabold;
    font-size: 18px;
}

#subheadingOne {
    margin-left: 317px;
    border: 2px solid blue;
    width: 156px;
}

#subheadingTwo {
    margin-left: 561px;
    border: 2px solid blue;
}

#chemistryLab {
    border: 2px solid blue;
    top: 149px;
    left: 0px;
    height: 235px;
    width: 287.2px;
}

#down {
    padding-top: 1000px;
}

#translations {
    border: 2px solid blue;
    font-style: Thin;
    font-size: 14px;
    margin-top: 61px;
    margin-left: 317px;
}

#definition {
    font-style: Thin;
    font-size: 14px;
    border: 2px solid blue;
    margin-top: 158px;
    margin-left: 317px;
}

.dropdown-content {
    display: none;
}

#dropdown-btn:checked~.dropdown-content {
    display: flex;
    flex-direction: column;
    top: 23px;
    left: 1329px;
    align-content: space-between;
    gap: 20px;
    margin: 20px 0;
}

.search-box {
    position: absolute;
    top: 23px;
    left: 1197.8px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
  <link rel="stylesheet" type="text/css" href="periodictable.css">
  <title>EVERYTHING CHEMISTRY</title>
</head>

<body>
  <!--nav bar flex box-->
  <div class="navbar-container">
    <!--navbar general styling-->
    <div class="navbar">
      <!--navbar building blocks-->
      <h3 id='websiteName'>EVERYTHING CHEMISTRY</h3>
      <img id='logo' src="icon.png" alt="Hydrogen atom">
      <div class="dropdown">
        <input type="checkbox" id="dropdown-btn">
        <label for="dropdown-btn" class="dropbtn">
          <i class="fa-solid fa-bars"></i>
        </label>
        <div class="dropdown-content">
          <a href="#">Home</a>
          <a href="#">About</a>
          <a href="#">Contact</a>
        </div>
        <!--search box-->
        <form class="search-box" action="action_page.php">
          <input type="text" placeholder="Search.." name="search">
          <button type="submit"><i class="fa fa-search"></i></button>
        </form>
      </div>
    </div>
  </div>

  <!--body flex box-->
  <div class="body-container">
    <!--body general styling-->
    <div class="body">
      <div class="page-title">
        <h1 id='name'>Periodic</h1>
        <h1 id='subname'>Table</h1>
      </div>
      <img id='chemistryLab' src="Chemistry.JPG" alt="Chemist Lab">
      <div class="subheading">
        <h2 id='subheadingOne'>118 Elements</h2>
        <h2 id='subheadingTwo'>7 Periods</h2>
      </div>
      <div class="body-text">
        <p id='translations'>{Eng: Periodic Table; Arabic: الجدول الدوري; Afrikaans: Periodieke tabel; Mandarin: 周期表
          (zhōuqī biǎo); Urdu: پیرودیک ٹیبل.}</p>
        <p id='definition'>The periodic table is a tabular arrangement of the chemical elements, organized on the basis
          of their atomic number, electron configurations, and chemical properties. Elements are presented in rows
          (called periods) and columns (called groups or families) according to their increasing atomic number. The
          elements in a group have similar chemical and physical properties, and those in a period show a progression
          of properties as the atomic number increases. The table provides a useful summary of the properties of the
          elements and their chemical reactivity. It is widely used in chemistry and other sciences to predict the
          properties and behavior of elements and their compounds.</p>
      </div>
    </div>
  </div>

  <aside class="move-down"><a href="#down"><i class="fa-solid fa-angle-down"></i></a>
  </aside>
</body>

</html>

Answer №1

To properly align items using flex, the parent or container itself must be set to display:flex. In this case, it seems that the subheading class is not being utilized as a flex container in your code. Additionally, the margin properties in subheadingOne and subheadingTwo should not be used when aligning items with flex. Here's how the updated codes should look:

.subheading {
    display: flex;
    justify-content: space-between;
    font-weight: bold;
    font-size: 18px;
}

#subheadingOne {
     border: 2px solid blue;
     width: 156px;
 }

#subheadingTwo {
    border: 2px solid blue;
}

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

Reveal or conceal the footer section as you reach the end of the webpage

My webpage layout consists of a fixed nav-bar at the top, a drop down sidebar, a <div> element with an id of content, some content inside the <div>, and a bottom <div> with a fixed position. I am trying to hide the bottom <div> whe ...

What is the easiest way to include a copyright symbol in a React component?

I am trying to include the copyright symbol in a React component, but it doesn't seem to be working for me. function Footer() { return ( <footer> <p>&copy</p> </footer> ); } <p>&copy</p> ...

Dynamically load directives through the use of ng-repeat

Creating a custom Dropdown menu by wrapping the HTML "select" element in my directive. This directive will display a list of options in the Dropdown menu. The select element is tagged with my custom "arBootstrapSelect" directive/attribute. This directive ...

Press on a rectangle inside an HTML5 SVG program

I'm currently developing a web application that utilizes the svg and Raphael library: workflowEditor.show(); var myList = document.getElementsByTagName("rect"); for (var a = 0; a < myList.length; a++) { myList[a].addEventListener("OnClick", f ...

RMarkdown Question: Is there a straightforward method to insert additional space following each tabset?

When creating reports using R Markdown, I implement tabsets (.{tabset}) to display multiple results without stacking them one after the other. For example: # First session {.tabset} ## A First result ## B Second result # Next session Howev ...

What could be causing the special characters to become scrambled after my HTML form is sent to a PHP script?

So, I've created a form for restaurant owners to fill out that includes a field for the business name. The problem I'm running into is that when someone enters something like "Foobar Café", it ends up being stored in my MySQL database as "Fooba ...

Guide to updating div card content when hovering in Vuejs

new Vue({ el: '#mouse', data: { message: 'Hover Me!' }, methods: { mouseover: function(){ this.message = 'Good!' }, mouseleave: function(){ this.message = 'Hover Me!' } ...

Connecting to particular sections on other pages

My website setup includes a page titled "news.html" that contains an iframe with fixed size. The iframe is linked to "innernews.html", which is the actual content to be displayed. I structured it this way for consistent page sizing, as the iframe prevents ...

How to use jQuery to retrieve the value of a select box based on the text it

Below is an example of a select box: <select id="year"> <option value="1">1 Year</option> <option value="2">2 Years</option> <option value="3">3 Years</option> <option va ...

Struggling to effectively customize a bootstrap theme

After incorporating a custom .css file from an internet theme that overrides some bootstrap properties, I noticed that it works in certain scenarios. However, when attempting to modify the primary color in the :root{} section of the styles.css file, the ch ...

Trigger AJAX request upon selecting a value from the dropdown menu

I am in the process of creating a basic dropdown menu that is only visible to users on mobile devices when they visit our website. The main objective is to allow users to select a value and then have that value sent via AJAX. For desktop devices, I have s ...

Generate spacing between rows of content in HTML or CSS without affecting the spacing between lines of text

In my R Markdown document in R, I want to replace the header labeled "Preface" with grey lines for visual indication. Here's the code snippet I've tried: :::{#preface} <p> Text </p> ::: However, there seems to be no margin at the beg ...

What is the best way to achieve CSS rounded corners while keeping the border lines sharp?

Currently, I am utilizing the border-radius property to make the corners of a div rounded. In addition, there is a border line at the bottom of the div. Here is an example: <div style="border-radius: .5em; border-bottom: 1px solid black">Content< ...

Understanding how to make images with adaptable dimensions is critical for responsive design

I'm curious about the concept of making images responsive. Some people say it's not correct to set width and height for images, but I'd like a clear explanation on when and why we should do this. Can anyone elaborate on this topic? ...

Navigating the style properties using jQuery loops

I have an event listener for a click on a specific div. $('div').on('click', function() { var styles = $(this).attr('style').split(';'); $.each(styles, function(i) { var style = this.split(':&ap ...

Navigating between pages with JQuery Mobile using interactive bubble buttons

Is there a way to create a swipe page with 3 pages and incorporate a "bubble" button that indicates the current page being swiped? I would appreciate any tips or resources where I can find an example of this. ...

Modifications to contenteditable elements result in a change to their IDs

Having some trouble with the behavior of a contenteditable div. The code structure is like this: <div contenteditable="true"> <p id="element-id-1">element-id-1</p> <p id="element-id-2">element-id-2</p> </div> E ...

What is the process for overriding global Bootstrap Stylesheet?

I have been attempting to adjust the width of a drop-down in my application by modifying the width attribute of the select tag. However, the width I set doesn't seem to take effect because it is already predefined in the global style sheet for the sel ...

Customize screen dimensions based on the user's web browser

Is it possible to dynamically adjust a user's screen size after they have visited my website? I am facing an issue where zooming out of my webpage causes containers and boxes to become misaligned. I am curious if there is a way to implement a CSS or ...

Using PHPMailer to send attachments uploaded through a form

I have a HTML form where users can select an attachment file (such as a PDF) and I want to send that file via email to a specified destination. While I am able to successfully send all other inputs through email, I'm having trouble with the attachmen ...