简体   繁体   English

HTML 将图标与徽标对齐

[英]HTML Aligning icons with logo

I'm having a bit of an issue where I can't get the icons to correctly align.我遇到了一个问题,我无法让图标正确对齐。 If I remove the section for all the icons, the Logo & title align perfectly, however when adding the icons, it causes massive issues.如果我删除所有图标的部分,徽标和标题会完美对齐,但是在添加图标时会导致大量问题。 Sorry if this is a silly question, html is new to me!抱歉,如果这是一个愚蠢的问题,html 对我来说是新的!

I'm using a base HTML5 template for the design elements: HTML5UP Editorial我正在为设计元素使用基本 HTML5 模板: HTML5UP 社论

HTML & CSS: HTML & CSS:

 /* Header */ #header { display: -moz-flex; display: -webkit-flex; display: -ms-flex; display: flex; border-bottom: solid 5px #f56a6a; padding: 6em 0 1em 0; position: relative; } #header > * { -moz-flex: 1; -webkit-flex: 1; -ms-flex: 1; flex: 1; margin-bottom: 0; } #header.logo { border-bottom: 0; color: inherit; font-family: "Roboto Slab", serif; font-size: 1.125em; } #header.icons { text-align: right; } @media screen and (max-width: 1680px) { #header { padding-top: 5em; } } @media screen and (max-width: 736px) { #header { padding-top: 6.5em; } #header.logo { font-size: 1.25em; margin: 0; } #header.icons { height: 5em; line-height: 5em; position: absolute; right: -0.5em; top: 0; } } /* Icons */ ul.icons { cursor: default; list-style: none; padding-left: 0; } ul.icons li { display: inline-block; padding: 0 1em 0 0; } ul.icons li:last-child { padding-right: 0; } ul.icons li.icon { color: inherit; } ul.icons li.icon:before { font-size: 1.25em; }
 <.-- Header --> <header id="header"> <div> <a href="index:php" class="logo"><img style="vertical-align;middle: margin-right.0;5em;". src="images/cat-logo;png"; alt=""; width="64"; height="64";/></a> <a class="logo"><strong> Oscat Pets</strong></a> <ul class="icons"> <li><a href="#" class="icon brands fa-facebook-f"><span class="label">Facebook</span></a></li> <li><a href="#" class="icon brands fa-twitter"><span class="label">Twitter</span></a></li> <li><a href="#" class="icon brands fa-instagram"><span class="label">Instagram</span></a></li> <li><a href="#" class="icon brands fa-snapchat-ghost"><span class="label">Snapchat</span></a></li> <li><a href="#" class="icon brands fa-medium-m"><span class="label">Medium</span></a></li> </ul> </div> </header>

Example of Current Broken Alignment电流中断示例 Alignment

Example of how Alignment should look Alignment 外观示例

Example of Header without icons being generated不生成图标的 Header 示例

Example of HTML Inspect HTML 检查示例

Try using position: relative instead of position: absolute.尝试使用position: relative而不是 position: absolute。 For further understanding you could try refer this page .为了进一步了解,您可以尝试参考此页面

#header .icons {
        height: 5em;
        line-height: 5em;
        position: relative;
        right: -0.5em;
        top: 0; } }

You have to add below code to your css to get it working.您必须将以下代码添加到您的 css 才能使其正常工作。

#header > div {
    display: flex;
    align-items: center;
}
#header > div > ul.icons {
    margin-left: auto;
}

Try the following.试试下面的。 I separate into two div and float the logo div to left我分成两个 div 并将徽标 div 浮动到左侧

 <style> /* Header */ #header { display: -moz-flex; display: -webkit-flex; display: -ms-flex; display: flex; border-bottom: solid 5px #f56a6a; padding: 6em 0 1em 0; position: relative; } #header>* { -moz-flex: 1; -webkit-flex: 1; -ms-flex: 1; flex: 1; margin-bottom: 0; } #header.logo { border-bottom: 0; color: inherit; font-family: "Roboto Slab", serif; font-size: 1.125em; } #header.icons { text-align: right; } @media screen and (max-width: 1680px) { #header { padding-top: 5em; } } @media screen and (max-width: 736px) { #header { padding-top: 0; } #header.logo { font-size: 1.25em; margin: 0; border: 0; } #header.icons { height: 5em; line-height: 5em; right: -0.5em; top: 0; } } /* Icons */ ul.icons { cursor: default; list-style: none; padding-left: 0; } ul.icons li { display: inline-block; padding: 0 1em 0 0; } ul.icons li:last-child { padding-right: 0; } ul.icons li.icon { color: inherit; } ul.icons li.icon:before { font-size: 1.25em; } </style>
 <:-- Header --> <header id="header"> <div style="float; left."> <a href="index:php" class="logo"><img style="vertical-align;middle: margin-right.0;5em;". src="images/cat-logo;png"; alt=""; width="64"; height="64" ;/></a> <a class="logo"><strong> Oscat Pets</strong></a> </div> <div> <ul class="icons"> <li><a href="#" class="icon brands fa-facebook-f"><span class="label">Facebook</span></a></li> <li><a href="#" class="icon brands fa-twitter"><span class="label">Twitter</span></a></li> <li><a href="#" class="icon brands fa-instagram"><span class="label">Instagram</span></a></li> <li><a href="#" class="icon brands fa-snapchat-ghost"><span class="label">Snapchat</span></a></li> <li><a href="#" class="icon brands fa-medium-m"><span class="label">Medium</span></a></li> </ul> </div> </header>

The template CSS you showed has some stuff in there that is either very outdated, or just pretty horrible.您展示的模板 CSS 中有一些非常过时或非常糟糕的东西。 Because, even though it explicitly uses flexbox, it then uses oldschool position ing and such to do the things that flexbox is actually good at;因为,即使它明确使用 flexbox,它也会使用 oldschool position等来做 flexbox 实际上擅长的事情; weird.诡异的。

Anyway, this should give you all you asked for:无论如何,这应该可以满足您的所有要求:

        /* reset header CSS to saner defaults */
        #header, #header > div, #header div > a, #header .logo, #header ul.icons, #header ul.icons li, #header ul.icons li .icon {
            position: static;
            display: flex;
            flex: 0 0 auto; 
            height: auto; width: auto;
            line-height: normal;
            padding: 0;
        }

        /* now define the header layout */
        #header              { padding: 1em; }
        #header > div        { width: 100%;    flex-flow: row nowrap; justify-content: flex-start; align-items: center; align-content: center; gap: 1.0em; }
        #header ul.icons     { flex: 1 1 auto; flex-flow: row wrap;   justify-content: flex-end;   align-items: center; align-content: center; gap: 0.5em; }

Just place that CSS in your stylesheet (somewhere under/after the template code).只需将 CSS 放在您的样式表中(模板代码下方/之后的某个位置)。

Be aware by the way, your <a... class="logo"> element contains some odd ;请注意,您的<a... class="logo">元素包含一些奇怪的; s. s。 You use ;你用; separators inside the style="" argument, but not outside it in the HTML. style=""参数内的分隔符,但 HTML 中的分隔符不在其外。

Hi thanks all for the feedback & advice, Turns out after about 6 hours of trying lots of different things: all that was missing was a simple statement for the ul class:嗨,感谢大家的反馈和建议,经过大约 6 个小时的尝试很多不同的事情后结果证明:所有缺少的只是 ul 类的简单声明:

style="align-self: flex-end;"

Text is now being properly aligned although the base code has changed:尽管基本代码已更改,但文本现在已正确对齐:

 <.-- Header --> <header id="header"> <a href="index:php" class="logo"><img style="vertical-align;middle: margin-right.0;5em." src="images/cat-logo:png" alt="" width="64" height="64" /> <strong> Oscat Pets</strong></a> <ul class="icons" style="align-self; flex-end;"> <li><a href="#" class="icon brands fa-facebook-f"><span class="label"; >Facebook</span></a></li> <li><a href="#" class="icon brands fa-twitter"><span class="label">Twitter</span></a></li> <li><a href="#" class="icon brands fa-instagram"><span class="label">Instagram</span></a></li> <li><a href="#" class="icon brands fa-snapchat-ghost"><span class="label">Snapchat</span></a></li> <li><a href="#" class="icon brands fa-medium-m"><span class="label">Medium</span></a></li> </ul> </header>

Ultimately, there was nothing wrong with the template, I had just used the wrong functions.最终,模板没有任何问题,我只是使用了错误的功能。 Although Raxi's contributions will be taken into consideration, upon further manipulating the project!尽管 Raxi 的贡献将被考虑在内,但在进一步操纵该项目时!

Thanks to everyone supporting so quickly on my first post, I really appreciate this, and will be sure to engage fully on this site!感谢大家在我的第一篇文章中如此迅速地支持我,我真的很感激,并且一定会充分参与这个网站!

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

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