Youssef
Youssef

Reputation: 96

Input and button have different heights and I'm unable to match them

I saw many people who have the same problem here but none of the solutions on their questions worked for me. I have a div with a button and an input text inside and their heights don't match, even when I set fixed px values to them. The button is always a bit "taller."

Here's my HTML & CSS https://jsfiddle.net/7yd0p5jk/

This is where I'm facing problems:

#search-div {
    width: auto;
    height: auto;
    display: flex;
    gap: 0.4rem;
}

.show-btn {
    padding: 0.6rem;
    background: var(--light-color);
    border-radius: 5px;
    border: none;
    transition: ease-in 300ms;
    font-size: 1.2rem;
    cursor: pointer;
    height: 100%;
}

.show-btn:hover {
    background: var(--lighter-color);
    transition: ease-in 300ms;
}

#search {
    background: var(--lighter-color);
    color: var(--darker-color);
    height: 100%;
    width: 100%;
    font-size: 1.2rem;
    padding: 0.4rem 1rem;
    border: black 2px solid;
    border-radius: 2px;
}

Upvotes: 1

Views: 102

Answers (2)

s.kuznetsov
s.kuznetsov

Reputation: 15213

To urigolate the height of the components, you need to remove the height: 100% and add border: none in #search, and add an align-items: stretch to #search-div.

/* GENERAL */
:root {
    --light-color: #ccc;
    --lighter-color: #f4f4f4;
    --dark-color: #333;
    --darker-color: #222;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: var(--dark-color);
    color: var(--light-color);
    font-family: "Trebuchet MS";
}

/* HEADER */
#header {
    background: var(--darker-color);
    display: flex;
    flex-direction: row;
    align-items: center;
    text-align: center;
    justify-content: space-between;
    padding: 1.4rem 10rem;
    width: 100%;
}

#logo {
    font-size: 2.4rem;
    font-weight: 200;
}

#search-div {
    width: auto;
    height: auto;
    display: flex;
    align-items: stretch; /*add*/
    gap: 0.4rem;
}

.show-btn {
    padding: 0.6rem;
    background: var(--light-color);
    border-radius: 5px;
    border: none;
    transition: ease-in 300ms;
    font-size: 1.2rem;
    cursor: pointer;
    height: 100%;
}

.show-btn:hover {
    background: var(--lighter-color);
    transition: ease-in 300ms;
}

#search {
    background: var(--lighter-color);
    color: var(--darker-color);
    /*height: 100%;*/ /*remove*/
    width: 100%;
    font-size: 1.2rem;
    padding: 0.4rem 1rem;
    border: black 2px solid;
    border-radius: 2px;
    border: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contacts List</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="https://kit.fontawesome.com/3ad7573e76.js" crossorigin="anonymous"></script>
</head>

<body>
    <header id="header">
        <div id="logo-div">
            <h1 id="logo">
                Contact List
            </h1>
        </div>
        <form id="search-div">
            <button class="show-btn"><i class="fas fa-search"></i></button>
            <input id="search" type="text" placeholder="Search contacts...">
        </form>
    </header>

    <script src="js/main.js"></script>
</body>

</html>

Upvotes: 0

xKobalt
xKobalt

Reputation: 1508

You have padding: 0.6rem; set on .show-btn, while you also have padding: 0.6rem 1rem; property on #search element

Fixing this, you will get the following result:

/* GENERAL */
:root {
    --light-color: #ccc;
    --lighter-color: #f4f4f4;
    --dark-color: #333;
    --darker-color: #222;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: var(--dark-color);
    color: var(--light-color);
    font-family: "Trebuchet MS";
}

/* HEADER */
#header {
    background: var(--darker-color);
    display: flex;
    flex-direction: row;
    align-items: center;
    text-align: center;
    justify-content: space-between;
    padding: 1.4rem 10rem;
    width: 100%;
}

#logo {
    font-size: 2.4rem;
    font-weight: 200;
}

#search-div {
    width: auto;
    height: auto;
    display: flex;
    gap: 0.4rem;
}

.show-btn {
    padding: 0.6rem;
    background: var(--light-color);
    border-radius: 5px;
    border: none;
    transition: ease-in 300ms;
    font-size: 1.2rem;
    cursor: pointer;
    height: 100%;
}

.show-btn:hover {
    background: var(--lighter-color);
    transition: ease-in 300ms;
}

#search {
    background: var(--lighter-color);
    color: var(--darker-color);
    height: 100%;
    width: 100%;
    font-size: 1.2rem;
    padding: 0.6rem 1rem;
    border: black 2px solid;
    border-radius: 2px;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contacts List</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="https://kit.fontawesome.com/3ad7573e76.js" crossorigin="anonymous"></script>
</head>

<body>
    <header id="header">
        <div id="logo-div">
            <h1 id="logo">
                Contact List
            </h1>
        </div>
        <form id="search-div">
            <button class="show-btn"><i class="fas fa-search"></i></button>
            <input id="search" type="text" placeholder="Search contacts...">
        </form>
    </header>

    <script src="js/main.js"></script>
</body>

</html>

Then, you maybe want to fix the height corrispondency of both the items, which can be done implementing margin-top: 2px property on .show-btn element, here's the result:

/* GENERAL */
:root {
    --light-color: #ccc;
    --lighter-color: #f4f4f4;
    --dark-color: #333;
    --darker-color: #222;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: var(--dark-color);
    color: var(--light-color);
    font-family: "Trebuchet MS";
}

/* HEADER */
#header {
    background: var(--darker-color);
    display: flex;
    flex-direction: row;
    align-items: center;
    text-align: center;
    justify-content: space-between;
    padding: 1.4rem 10rem;
    width: 100%;
}

#logo {
    font-size: 2.4rem;
    font-weight: 200;
}

#search-div {
    width: auto;
    height: auto;
    display: flex;
    gap: 0.4rem;
}

.show-btn {
    padding: 0.6rem;
    background: var(--light-color);
    border-radius: 5px;
    border: none;
    transition: ease-in 300ms;
    font-size: 1.2rem;
    cursor: pointer;
    height: 100%;
    margin-top: 2px;
}

.show-btn:hover {
    background: var(--lighter-color);
    transition: ease-in 300ms;
}

#search {
    background: var(--lighter-color);
    color: var(--darker-color);
    height: 100%;
    width: 100%;
    font-size: 1.2rem;
    padding: 0.6rem 1rem;
    border: black 2px solid;
    border-radius: 2px;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contacts List</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="https://kit.fontawesome.com/3ad7573e76.js" crossorigin="anonymous"></script>
</head>

<body>
    <header id="header">
        <div id="logo-div">
            <h1 id="logo">
                Contact List
            </h1>
        </div>
        <form id="search-div">
            <button class="show-btn"><i class="fas fa-search"></i></button>
            <input id="search" type="text" placeholder="Search contacts...">
        </form>
    </header>

    <script src="js/main.js"></script>
</body>

</html>

Upvotes: 3

Related Questions