简体   繁体   English

如何在html和CSS中的同一行中对齐两个元素?

[英]How to align two elements in the same line in html and css?

I have a webpage in which at the top there are two elements, one is homepage logo and the other is upload button. 我有一个网页,其中的顶部有两个元素,一个是首页徽标,另一个是上传按钮。 The upload button (upload button should be little bit more on the top) at this moment is not aligned in a straight line with the homepage logo. 此时的上传按钮(顶部的上传按钮应该稍微多一点)与首页徽标未成一直线。

Here is the fiddle . 这是小提琴

The HTML and CSS codes for the homepage logo and the button are: 主页徽标和按钮的HTML和CSS代码为:

HTML: HTML:

<div id="upload-button">
    <ul id="horizontal-list">
        <li class="homepage-logo">
            <a href="https://www.homesail.ca/prelaunch" target="_blank">
                <img src="assets/img/Uploads/logofinalhomesail.png">
            </a>
        </li>
        <li class="upload-button">Upload</li>
    </ul>
</div>

CSS: CSS:

/* homepage logo + Upload-button start */

#upload-button {
    display: table;
}

#horizontal-list {
    margin-top: 90px;
}

ul#horizontal-list li {
    display: inline;
}

ul#horizontal-list .homepage-logo {
    margin-left: 540px;
}


/* homepage logo + Upload-button finish */

I am wondering, how I can put the homepage logo and upload button properly aligned (with upload button little bit on the top). 我想知道如何将首页徽标和上传按钮正确对齐(顶部有一点上传按钮)。 I did make a separate class in html for upload button but unfortunately still in CSS, I am not able to align both of them together. 我确实在html中为上载按钮创建了一个单独的类,但是不幸的是,在CSS中,我无法将它们对齐。

For Top: 对于顶部:

ul li:last-child {
  vertical-align: top;
}

 #upload-button { display: table; } #horizontal-list { margin-top: 90px; } ul#horizontal-list li { display: inline; } ul#horizontal-list .homepage-logo { margin-left: 0px; } ul li:last-child { vertical-align: top; } img { width: 50px; } 
 <div id="upload-button"> <ul id="horizontal-list"> <li class="homepage-logo"> <a href="https://www.homesail.ca/prelaunch" target="_blank"> <img src="http://justcuteanimals.com/wp-content/uploads/2016/10/baby-bear-pictures-cute-animal-pics.jpg"> </a> </li> <li class="upload-button">Upload</li> </ul> </div> 

For Middle : 对于中:

ul li a img{
  vertical-align: middle;
}

 #upload-button { display: table; } #horizontal-list { margin-top: 90px; } ul#horizontal-list li { display: inline; } ul#horizontal-list .homepage-logo { margin-left: 0px; } ul li a img{ vertical-align: middle; width:50px; } 
 <div id="upload-button"> <ul id="horizontal-list"> <li class="homepage-logo"> <a href="https://www.homesail.ca/prelaunch" target="_blank"> <img src="http://justcuteanimals.com/wp-content/uploads/2016/10/baby-bear-pictures-cute-animal-pics.jpg"> </a> </li> <li class="upload-button">Upload</li> </ul> </div> 

Just go with flex container modern way 只需使用Flex容器现代方式

#horizontal-list{
    display: inline-flex;
    align-items: center;
}

DEMO DEMO

Please change 请更换

 ul#horizontal-list li {
    display: inline;

}

to following 跟随

ul#horizontal-list li {
    display: inline-block;
    vertical-align: middle;
}

I centered the "Upload" with the logo on its left and centered the logo&upload horizontally. 我将“上传”和徽标放在左侧居中,并将徽标和上传水平居中。

 * { box-sizing: border-box; } body { background-color: #4676f2; } /* homepage logo + Upload-button start */ #upload-button { display: table; margin: auto; } #horizontal-list { margin-top: 90px; } ul#horizontal-list li { display: table-cell; vertical-align: middle; } ul#horizontal-list .homepage-logo { margin-left: 540px; } /* homepage logo + Upload-button finish */ /* search box start */ #myInput { background-image: url(../assets/img/Uploads/searchicon.png); background-position: 99% 50%; background-repeat: no-repeat; font-size: 16px; margin-top: 100px; width: 670px; margin-right: auto; display: block; margin-left: auto; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; } /* search box finish */ /* search box dropdown start */ #myUL { list-style-type: none; padding: 0; width: 50%; margin-bottom: 200px; margin-left: auto; margin-right: auto; } #myUL li a { border: 1px solid #ddd; margin-top: -1px; /* Prevent double borders */ background-color: #f6f6f6; padding: 12px; text-decoration: none; font-size: 18px; color: black; display: block } #myUL li a.header { background-color: #e2e2e2; cursor: default; } #myUL li a:hover:not(.header) { background-color: #eee; } /* search box dropdown finish */ 
 <!DOCTYPE html> <html> <head> <title>Search Index</title> <link rel="stylesheet" href="css/style.css"> <link rel="shortcut icon" href="assets/img/Uploads/favicon.png"> </head> <body> <div id="upload-button"> <ul id="horizontal-list"> <li class="homepage-logo"> <a href="https://www.homesail.ca/prelaunch" target="_blank"> <img src="https://s9.postimg.org/4xgk9tban/logofinalhomesail.png"> </a> </li> <li class="upload-button">Upload</li> </ul> </div> <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search.." title="Type in a name"> <ul id="myUL"> <li><a href="#" class="header">A</a></li> <li><a href="#">B</a></li> <li><a href="#">C</a></li> <li><a href="#" class="header">D</a></li> <li><a href="#">E</a></li> <li><a href="#">F</a></li> <li><a href="#" class="header">G</a></li> <li><a href="#">H</a></li> <li><a href="#">I</a></li> <li><a href="#">J</a></li> </ul> <script> function myFunction() { var input, filter, ul, li, a, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); ul = document.getElementById("myUL"); li = ul.getElementsByTagName("li"); for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName("a")[0]; if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = ""; } else { li[i].style.display = "none"; } } } </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM