skip to Main Content

I’m new to front end development, so please don’t insult me if it’s a very stupid question.
I have a set a 2 color background with the color red taking 2/3 of the screen, and the yellow taking the remaining 1/3.
Now i need to place an image, that is inside a div, beetween the two colors of the background, half image in red and half image in yellow, how do i do it?Like this

HTML:

<body>
    <header>
        <div class="logo-name">
            <img class="logo" src="img/burger.svg">
            <h1 class="name">Burger</h1>
        </div>
        <nav class="navbar">
            <ul class="nav-links">
                <li><a href="#">Home</a></li>
                <li><a href="#">Menu</a></li>
                <li><a href="#">About Us</a></li>
                <li><a href="#">Contacts</a></li>
            </ul>
        </nav>
        <button class="btn"><a href="#">Book A Table</a></button>
    </header>

    <main>
        <div class="left">
            <h1 class="left-title">Savor <span style="color: #ffac0c;">Every</span> Bite</h1>
            <p class="left-description">Welcome to our burger haven, where every burger is more than a meal, they're a culinary adventure waiting to be savored.</p>
        </div>

        <div class="right">
            <img class="burgerimg" src="img/burgerimg.webp">
        </div>
    </main>
</body>

CSS :

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Montserrat;
    text-decoration: none;
    color: inherit;
}

body {
    background: linear-gradient(
        to right,
        #801404 0%,
        #801404 70%,
        #ffac0c 30%,
        #ffac0c 100%
      );
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 50px 10%;
    height: 80px;
}

.nav-links li {
    color: white;
    text-transform: uppercase;
    font-weight:700;
    display: inline-block;
    list-style: none;
    padding: 0 20px;
    font-size: 2rem;
}

.logo-name {
    display: inline-flex;
}

.logo {
    width: 40px;
    height: auto;
    margin: 0 20px;
    filter: invert(99%) sepia(8%) saturate(808%) hue-rotate(172deg) brightness(122%) contrast(100%);
}

.name {
    font-size: 3rem;
    font-weight: 700;
    color: white;
    line-height: 80px;
}

.btn {
    padding: 15px 20px;
    background-color: #801404;
    color: white;
    text-transform: uppercase;
    border: none;
    border-radius: 50px;
    font-weight: 700;
}


main {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.left {
    position: relative;
    left: 10%;
    margin: 12.5% 0;
}

.right {
    margin: 0 18%;
}

.left-title {
    font-size: 8rem;
    color: white;
}

.left-description{
    font-size: 3rem;
    color: white;
}

.burgerimg {
    width: 400px;
}

I tried using margin and padding with %, but the image (burger) changes position when resizing the window.

2

Answers


  1. hello im also a new developer so please don’t insult me if it’s a very stupid answer but did you try:

    .right{
        position:relative;
        right: 150px; <!--try to find the right number of pixels or %-->
    <div class="right">
                    <img class="burgerimg" src="img/burgerimg.webp">
                </div>
    Login or Signup to reply.
  2. I would recommend, that you use absolute positioning for you image. Get rid of the two column approach and just keep a wrapper around your text to set a width so it does not run into the yellow color. For the image itself, use:

    1. position: absolute; – removes the element from the normal flow.
    2. left: 70%; – this will align the left edge of the image along the border of the two background colors
    3. transform: translate(-50%, 0); – realigns the image by half of its width to the left so the border of the background colors are exactly in the center if the image. You can also use a negative margin-left instead, if your image has a fixed width.

    I have simplified your example and removed the header, so the actual solutions becomes clearer.

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: Montserrat;
    }
    
    body {
        background: linear-gradient(
            to right,
            #801404 0%,
            #801404 70%,
            #ffac0c 30%,
            #ffac0c 100%
          );
    }
    
    main {
        position: relative;
        padding: 100px;
    }
    
    .text-wrapper {
        width: 50%;
    }
    
    h1 {
        font-size: 8rem;
        color: white;
    }
    
    p {
        font-size: 3rem;
        color: white;
    }
    
    #burger-image {
        position: absolute;
        top: 200px;
        left: 70%;
        transform: translate(-50%, 0); /* instead of transform you could also use margin-left: -200px; */
        width: 400px;
        
        /* some properties so you can see the image shape */
        height: 300px;
        background: blue;
    }
    <body>
        <main>
            <div class="text-wrapper">
                <h1>
                  Savor <span style="color: #ffac0c;">Every</span> Bite
                </h1>
                <p>
                  Welcome to our burger haven, where every burger is more than a meal, they're a culinary adventure waiting to be savored.
                </p>
            </div>
    
           <img id="burger-image" src="burger.png">
        </main>
    </body>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search