Building a Hero Section

Jun's Coding Journey·2022년 10월 31일
0

[Learn] HTML / CSS

목록 보기
25/30

A hero section is the area that is immediately shown when the user visits a website. This section of the website should be able to introduce the overall aspects of the website.

Key Factors:

  • What does the website offer?
  • Why should users trust this website?
  • What are the benefits?
  • What action should users take next?

Today, I attempted to create an example hero section that was provided in my udemy course. As always, I began by creating all the content elements that will be displayed on the web page using html. I divided all the contents into either header or section in order to differentiate the texts that are used in different components.

Inside the header, I created a nav component where the logo and the navigation tool for the website will be created. Additionally, I created another text component that briefly explains and advertises what the website is for. I also added a button element for some user interaction.

Inside the section, I created another text component that will futher explain features of the website in greater detail. For now, I only added some random text which I will change later after giving some structure to the overall layout.

    // html
    <header>
      <nav>
        <div>LOGO</div>
        <div>NAVIGATION</div>
      </nav>
      <div>
        <h1>A healthy meal delivered to your door, every single day</h1>
        <p>
          The smart 365-days-per-year food subscription that will make you eat
          healthy again. Tailored to your personal tastes and nutritional needs
        </p>
        <a href="#" class="btn">Start eating well</a>
      </div>
    </header>
    <section>
      <div>
        <h2>Some random heading</h2>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia vel
          nam totam, provident eligendi facilis saepe eum veniam laborum id
          magnam nobis dicta rerum ullam quibusdam officiis autem nesciunt
          dolore?
        </p>
      </div>
    </section>
    // css
    html {
      font-family: "Rubik", sans-serif;
      color: #444;
    }

Although there are some contents displayed on the website, it looks very boring without any colors or layout. Now would be a very good time to add some css effects to the website. For css, I always set all my margin and padding size to 0. I also set box-sizing to border-box. Setting these as default will clear any restrictions when I want to freely size elements.

For the entire header section, I gave a random background-color of orange-red to see how much space is occupied. Later, this will be replaced with an image.

I started with the nav element and went all the way down in order. For the navigation bar, I set the display property to flex and gave space between the logo and navigation by setting the justify-content property to space-between. For now, I gave a random background-color of green to the bar to see how much space is occupied.

For the text elements, I quickly adjusted their font-size and line-height while placing them in the appropriate location with margin and padding.

There was no need to give any background-color for the section component.

      // css
      nav {
        font-size: 20px;
        font-weight: 700;
        display: flex;
        justify-content: space-between;
        background-color: green;
      }
      h1 {
        font-size: 52px;
        margin-bottom: 32px;
      }
      p {
        font-size: 20px;
        line-height: 1.6;
        margin-bottom: 48px;
      }
      .btn:link,
      .btn:visited {
        font-size: 20px;
        font-weight: 600;
        background-color: #e67e22;
        color: #fff;
        text-decoration: none;
        display: inline-block;
        padding: 10px 32px;
        border-radius: 9px;
      }
      h2 {
        font-size: 44px;
        margin-bottom: 48px;
      }
      section {
        padding: 96px 0;
        background: #f7f7f7;
      }

Before moving on, I added an extra div element within the header container and in order to adjust the width of the header text to lean towards the left.

Using absolute positioning, I moved the header text on the center without affecting any other components.

Now its time to get add an image background to the header section and make some final adjustments for sizes. By using a property known as background-image, I was able to instantly apply an image file onto the website. Because the text needed to be readable, I made the image to look darker by adjusting the gradient. Additionally, I used the background-size property and set it to cover. This automatically sets the image to fit the size according to the screen.

With this, the hero section was finally complete! 😀!

      // css
      header {
        height: 100vh;
        position: relative;
        background-image: linear-gradient(
            rgba(34, 34, 34, 0.6),
            rgba(34, 34, 34, 0.6)
          ),
          url(hero.jpg);
        background-size: cover;
        color: #fff;
      }
      .header-container {
        width: 1200px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
      .header-container-inner {
        width: 50%;
      }

Reference Code

<!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" />
    <title>Omnifood Hero Section</title>
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Rubik:wght@400;500;700&display=swap"
      rel="stylesheet"
    />
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      html {
        font-family: "Rubik", sans-serif;
        color: #444;
      }
      .container {
        margin: 0 auto;
        width: 1200px;
      }
      header {
        height: 100vh;
        position: relative;
        background-image: linear-gradient(
            rgba(34, 34, 34, 0.6),
            rgba(34, 34, 34, 0.6)
          ),
          url(hero.jpg);
        background-size: cover;
        color: #fff;
      }
      nav {
        font-size: 20px;
        font-weight: 700;
        display: flex;
        justify-content: space-between;
        padding: 32px;
      }
      .header-container {
        width: 1200px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
      .header-container-inner {
        width: 50%;
      }
      h1 {
        font-size: 52px;
        margin-bottom: 32px;
        line-height: 1.05;
      }
      p {
        font-size: 20px;
        line-height: 1.6;
        margin-bottom: 48px;
      }
      .btn:link,
      .btn:visited {
        font-size: 20px;
        font-weight: 600;
        background-color: #e67e22;
        color: #fff;
        text-decoration: none;
        display: inline-block;
        padding: 10px 32px;
        border-radius: 9px;
      }
      h2 {
        font-size: 44px;
        margin-bottom: 48px;
      }
      section {
        padding: 96px 0;
        background: #f7f7f7;
      }
    </style>
</head>
  <body>
    <header>
      <nav class="container">
        <div>LOGO</div>
        <div>NAVIGATION</div>
      </nav>
      <div class="header-container">
        <div class="header-container-inner">
          <h1>A healthy meal delivered to your door, every single day</h1>
          <p>
            The smart 365-days-per-year food subscription that will make you eat
            healthy again. Tailored to your personal tastes and nutritional
            needs
          </p>
          <a href="#" class="btn">Start eating well</a>
        </div>
      </div>
    </header>
    <section>
      <div class="container">
        <h2>Some random heading</h2>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia vel
          nam totam, provident eligendi facilis saepe eum veniam laborum id
          magnam nobis dicta rerum ullam quibusdam officiis autem nesciunt
          dolore?
        </p>
      </div>
    </section>
  </body>
</html>
profile
Greatness From Small Beginnings

25개의 댓글

comment-user-thumbnail
2025년 1월 1일

her in fact awesome blog page. her realy content rich and then a this fantastic profession. i prefer this unique. i love my boyfriend

답글 달기
comment-user-thumbnail
2025년 1월 2일

Thanks a ton to get writing this sort of superb posting! I uncovered your web blog ideal for this demands. Contained in the grapefruit excellent plus handy discussions. Keep up to date favorable deliver the results! biurko młodzieżowe

답글 달기
comment-user-thumbnail
2025년 1월 2일

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. fototapety 3d

답글 달기
comment-user-thumbnail
2025년 1월 2일

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. افضل موقع زيادة متابعين

답글 달기
comment-user-thumbnail
2025년 1월 12일

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. hagia sophia visiting hours

답글 달기
comment-user-thumbnail
2025년 1월 16일

This is definitely the knowledge We are acquiring all over the place. Cheers for ones web site, I merely join your blog. This is the wonderful web site. . Comment fidéliser ses clients B2B ?

답글 달기
comment-user-thumbnail
2025년 1월 16일

I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it.. https://www.ini188pp.com/

답글 달기
comment-user-thumbnail
2025년 1월 16일

Great survey, I'm sure you're getting a great response. แทงบอลออนไลน์

답글 달기
comment-user-thumbnail
2025년 1월 16일

I did experience checking articles or reviews shared here. They are simply exceptional there are a large amount of advantageous knowledge. https://eventsguys.co.za/

답글 달기
comment-user-thumbnail
2025년 1월 18일

Enter 1X200BOX while registering on 1xBet to claim a 130% welcome bonus. Get up to €130 in extra funds and enjoy top-tier sports betting! code promo 1xbet anniversaire

답글 달기
comment-user-thumbnail
2025년 1월 20일

Continue the nice function, We study couple of articles about this web site as well as I believe that the internet weblog is actually actual fascinating and it has obtained arenas associated with wonderful info. https://thebeerstore.co.za/

답글 달기
comment-user-thumbnail
2025년 1월 20일

I did experience checking articles or reviews shared here. They are simply exceptional there are a large amount of advantageous knowledge. https://waxedbranding.co.za/

답글 달기
comment-user-thumbnail
2025년 1월 21일

Compete the great get the job done, As i browse small amount of content articles in this particular websites and even It looks like that your choice of word wide web web log is without a doubt realistic important and allows gotten forums in fabulous tips. https://www.corporateeventplanning.co.za/

답글 달기
comment-user-thumbnail
2025년 1월 21일

I did experience checking articles or reviews shared here. They are simply exceptional there are a large amount of advantageous knowledge.https://www.videoproductioncompany.co.za/

답글 달기
comment-user-thumbnail
2025년 1월 25일

The audio will be awesome. You might have several extremely skilled performers. My partner and i want an individual the most effective regarding accomplishment. łóżeczka dla niemowląt

답글 달기
comment-user-thumbnail
2025년 2월 8일

I’m prompted while using the surpassing in addition to preachy checklist you give in such very little timing. 잠실 룸싸롱

답글 달기
comment-user-thumbnail
2025년 2월 15일

You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. 급전대출

답글 달기
comment-user-thumbnail
2025년 2월 15일

I just couldn't leave your website before telling you that I truly enjoyed the top quality info you present to your visitors? Will be back again frequently to check up on new posts. agen toto

답글 달기
comment-user-thumbnail
2025년 2월 15일

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. best prop firm passing service

답글 달기
comment-user-thumbnail
2025년 2월 18일

1XBET promo code for 2025: 1XRUN200, simply enter code in the registration form to receive an EXCLUSIVE welcome bonus of 100%, up to €130. To maximize bonuses on 1xbet, use promo code goldenjulius when registering your account. After your initial deposit, you'll receive a 100% bonus. 1xbet promo code for registration

답글 달기
comment-user-thumbnail
2025년 2월 19일

Superior Place, My organization is a great believer during ad opinions regarding online websites that will let the webpage novelists recognize that they’ve put in an item worthwhile that will the online market place! 代々木上原 英会話

답글 달기
comment-user-thumbnail
2025년 2월 22일

So i am in fact content with see our site not to mention could have fun with perusing effective content pieces circulated in this case. Typically the creative ideas of this contributor was basically fantastic, thanks a lot for ones show.Personal training Orlando FL

답글 달기
comment-user-thumbnail
2025년 2월 22일

阿拉斯加邮轮是一种独特的旅行体验,让游客欣赏壮丽的冰川、雪山和丰富的野生动物。乘坐邮轮可以轻松探索朱诺、凯奇坎、史凯威等港口,感受原始自然风光和独特文化。此外,还能观赏鲸鱼、冰川崩裂等震撼景象,非常值得一试

답글 달기
comment-user-thumbnail
2025년 2월 23일

Nice knowledge gaining article. This post is really the best on this valuable topic. slot

답글 달기
comment-user-thumbnail
2025년 3월 2일

Decent put up, her the most fascinating blog page which are in this case, cultivate monetary management give good results, could be spine. 시흥 직장인 대출

답글 달기

관련 채용 정보