skip to Main Content

I have a header and a “p” element that I want on one horizontal line. I’ve managed to obtain the desired result manually using CSS “position” function, but I would like to learn the proper way to do this so that both elements are mathematically aligned for future reference. I want the middle of each element to be on the same horizontal line, as one element has more height than the other. I tried using “display: inline-block” and that didn’t work, perhaps because I have different margins set for each element but I’m not sure.Screenshot of issue

HTML:

    <div class="head-one">
        <h2 id="#about-me">About Me</h2>
        <p id="cal">My name is Cal Cook. I'm 26 years old and live in Boston, Massachusetts. I'm from NYC originally. I'm passionate about cryptocurrency, web design and SEO. I built this site to feature my work.</p>
    </div>

CSS:

h2 {
    padding: 75px;
    margin-left: 30px;
    font-family: 'Nunito Sans', sans-serif;
}

#cal {
    font-family: 'Roboto', sans-serif;
    border: solid;
    border-radius: 25px;
    padding: 10px;
    margin-left: 350px;
    margin-right: 250px;
    position: relative;
    bottom: 140px;
}

2

Answers


  1. I would suggest removing all of your positing code. Use display: flex on the container and flex: 1 on the #cal element.

    .head-one {
      display: flex;
      align-items: center;
    }
    
    h2 {
        font-family: 'Nunito Sans', sans-serif;
        white-space: no-wrap;
        padding: 75px;
    }
    
    #cal {
        font-family: 'Roboto', sans-serif;
        border: solid;
        border-radius: 25px;
        padding: 10px;
        flex: 1;
    }
    <div class="head-one">
      <h2 id="#about-me">About Me</h2>
      <p id="cal">My name is Cal Cook. I'm 26 years old and live in Boston, Massachusetts. I'm from NYC originally. I'm passionate about cryptocurrency, web design and SEO. I built this site to feature my work.</p>
    </div>
    Login or Signup to reply.
  2. It’s a perfect situation (like almost every other) for CSS_layout/Flexbox

    .head-one {
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
    
    #cal {
      max-width: 60vw;
      border: solid;
      border-radius: 25px;
      padding: 10px;
    }
    <div class="head-one">
      <h2 id="#about-me">About Me</h2>
      <p id="cal">My name is Cal Cook. I'm 26 years old and live in Boston, Massachusetts. I'm from NYC originally. I'm passionate about cryptocurrency, web design and SEO. I built this site to feature my work.</p>
    </div>

    CSS_layout/Flexbox

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search