Johnny Robertson
Johnny Robertson

Reputation: 147

Responsive media query div element not aligning elements to the left

The image below illustrates my problem, my elements in my div with the class "icon-links" are not being left aligned to match the edge of the div container when I resize my window to below 768px. You can see the contact us link/button is still centered in the div.

enter image description here

Here's my code:

body{
  background: #C7E2F8;
}

img{
  width: 40%;
}

.icon-links{
    margin-top: 5rem;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: left;
    width: 50%;
}

.contact-link{
    color: black;
    font-size: 1.8rem;
    padding: 18px 27px;
    background-color: gray;
    margin-right: 5rem;
    transition: 0.3s;
    cursor: pointer;

}

.contact-link:hover{
    background-color: var(--blue);
    color: var(--darkGray);
    transition: 0.3s;
}

.icon-link{
    margin-right: 2.2rem;
    transition: all 0.3s;
}


.icon-wrappers{
    display: flex;
    flex-direction: row;
}


@media screen and (max-width: 768px){

    .icon-links{
        flex-direction: column;
        justify-content: flex-start;
        align-items: left;
    }

    .icon-wrappers{
        justify-content: flex-start;
        align-items: left;
        margin-top: 2rem;
    }
  
  .contact-link{
    margin: 0;
  }

}
                      <div class="icon-links">
                        <a class="contact-link">
                            Contact Us
                        </a>
                    
                    <div class="icon-wrappers">
                        <a class="icon-link" href="#"><img src="https://d.ibtimes.co.uk/en/full/1487456/twitter-logo.png" alt=""></a>
                        <a class="icon-link" href="#"><img src="https://d.ibtimes.co.uk/en/full/1487456/twitter-logo.png" alt=""></a>
                        <a class="icon-link" href="#"><img src="https://d.ibtimes.co.uk/en/full/1487456/twitter-logo.png" alt=""></a>
                    </div>

            </div>

Here's a codepen with the same exact code: https://codepen.io/thatrandomthinglol/pen/yLMdyJv

Here's a diagram I made to be more specific with what will happen with the div when my screen size gets smaller: enter image description here

Upvotes: 1

Views: 536

Answers (1)

Harsh Kairamkonda
Harsh Kairamkonda

Reputation: 397

  1. make display:block in .icon-links
  2. remove margin-top: 5rem; from .icon-links
  3. Add margin-top :2rem in .icon-link

Refer this example its work :-)

body{
  background: #C7E2F8;
}

img{
  width: 40%;
}

.icon-links{
    display: block;
    flex-direction: row;
    justify-content: flex-start;
    align-items: left;
    width: 50%;
}

.contact-link{
    color: black;
    font-size: 1.8rem;
    padding: 18px 27px;
    background-color: gray;
    margin-right: 5rem;
    transition: 0.3s;
    cursor: pointer;

}

.contact-link:hover{
    background-color: var(--blue);
    color: var(--darkGray);
    transition: 0.3s;
}

.icon-link{
    margin-right: 2.2rem;
    transition: all 0.3s;
    margin-top: 2.0rem;
}


.icon-wrappers{
    display: flex;
    flex-direction: row;
}


@media screen and (max-width: 768px){

    .icon-links{
        flex-direction: column;
        justify-content: flex-start;
        align-items: left;
    }

    .icon-wrappers{
        justify-content: flex-start;
        align-items: left;
        margin-top: 2rem;
    }
  
  .contact-link{
    margin: 0;
  }

}
                      <div class="icon-links">
                        <a class="contact-link">
                            Contact Us
                        </a>
                    
                    <div class="icon-wrappers">
                        <a class="icon-link" href="#"><img src="https://d.ibtimes.co.uk/en/full/1487456/twitter-logo.png" alt=""></a>
                        <a class="icon-link" href="#"><img src="https://d.ibtimes.co.uk/en/full/1487456/twitter-logo.png" alt=""></a>
                        <a class="icon-link" href="#"><img src="https://d.ibtimes.co.uk/en/full/1487456/twitter-logo.png" alt=""></a>
                    </div>

            </div>

Upvotes: 2

Related Questions