18910140161

HTML-我只需要将无序列表居中,并将图像放在空白页中间-堆栈溢出

顺晟科技

2022-10-19 11:34:06

127

我试图在一个页面的中心只居中两个元素。这是一个带有可点击图标的页面,用于您的社交链接。但我需要找到一种方法,让它们以一种无论设备大小都能居中的方式居中。请记住,这是我成长的第二天。非常感谢您的反馈!

以下是我的代码

<div class="fresh">
    <img width="160" src=logo.svg>
</div>

<div class="icons-inline">
    <ul class="icons">
        <a href="https://www.hypeunit.co" class="hype"><svg>insert super long svg</svg></a>
        <a href="https://twitter.com/Fresheys" class="twitter"><svg>insert super long svg</svg></a>
        <a href="https://www.youtube.com/channel/UCjD9wyRBMV9hqC2snDGqEUA" class="youtube"><svg>insert super long svg</svg></a>
        <a href="https://twitch.tv/fresheys" class="twitch"><svg>insert super long svg</svg></a>
        <a href="https://discord.com/invite/ez6YhCs" class="discord"><svg>insert super long svg</svg></a>
        <a href="https://www.tiktok.com/@fresheys?" class="tiktok"><svg>insert super long svg</svg></a>
        <a href="https://instagram.com/fresheys" class="instagram"><svg>insert super long svg</svg></a>
    </ul>
</div>

CSS:

<div class="fresh">
    <img width="160" src=logo.svg>
</div>

<div class="icons-inline">
    <ul class="icons">
        <a href="https://www.hypeunit.co" class="hype"><svg>insert super long svg</svg></a>
        <a href="https://twitter.com/Fresheys" class="twitter"><svg>insert super long svg</svg></a>
        <a href="https://www.youtube.com/channel/UCjD9wyRBMV9hqC2snDGqEUA" class="youtube"><svg>insert super long svg</svg></a>
        <a href="https://twitch.tv/fresheys" class="twitch"><svg>insert super long svg</svg></a>
        <a href="https://discord.com/invite/ez6YhCs" class="discord"><svg>insert super long svg</svg></a>
        <a href="https://www.tiktok.com/@fresheys?" class="tiktok"><svg>insert super long svg</svg></a>
        <a href="https://instagram.com/fresheys" class="instagram"><svg>insert super long svg</svg></a>
    </ul>
</div>

我目前只是使用文本对齐,然后将顶部文本的填充设置为20%,这样它就可以将两个文本都向下推,并且只在特定的屏幕上居中显示。但我希望它能在电话、其他显示器上工作。


顺晟科技:

我喜欢使用这种方法:

您可以直接从MDN:https://developer.mozilla.org/en-美国/docs/web/css/place-items

使用flex-box,尝试以下操作:

您可以跳过它,因为它将覆盖文档中的每个元素,并且您必须手动设置边距和填充

<div class="fresh">
    <img width="160" src=logo.svg>
</div>

<div class="icons-inline">
    <ul class="icons">
        <a href="https://www.hypeunit.co" class="hype"><svg>insert super long svg</svg></a>
        <a href="https://twitter.com/Fresheys" class="twitter"><svg>insert super long svg</svg></a>
        <a href="https://www.youtube.com/channel/UCjD9wyRBMV9hqC2snDGqEUA" class="youtube"><svg>insert super long svg</svg></a>
        <a href="https://twitch.tv/fresheys" class="twitch"><svg>insert super long svg</svg></a>
        <a href="https://discord.com/invite/ez6YhCs" class="discord"><svg>insert super long svg</svg></a>
        <a href="https://www.tiktok.com/@fresheys?" class="tiktok"><svg>insert super long svg</svg></a>
        <a href="https://instagram.com/fresheys" class="instagram"><svg>insert super long svg</svg></a>
    </ul>
</div>

HTML:

<div class="fresh">
    <img width="160" src=logo.svg>
</div>

<div class="icons-inline">
    <ul class="icons">
        <a href="https://www.hypeunit.co" class="hype"><svg>insert super long svg</svg></a>
        <a href="https://twitter.com/Fresheys" class="twitter"><svg>insert super long svg</svg></a>
        <a href="https://www.youtube.com/channel/UCjD9wyRBMV9hqC2snDGqEUA" class="youtube"><svg>insert super long svg</svg></a>
        <a href="https://twitch.tv/fresheys" class="twitch"><svg>insert super long svg</svg></a>
        <a href="https://discord.com/invite/ez6YhCs" class="discord"><svg>insert super long svg</svg></a>
        <a href="https://www.tiktok.com/@fresheys?" class="tiktok"><svg>insert super long svg</svg></a>
        <a href="https://instagram.com/fresheys" class="instagram"><svg>insert super long svg</svg></a>
    </ul>
</div>

如果您想要一个水平显示元素,只需添加类到它,它将水平显示,然后您可以添加这个规则来更好地处理这些类型的列表,例如。G:

<div class="fresh">
    <img width="160" src=logo.svg>
</div>

<div class="icons-inline">
    <ul class="icons">
        <a href="https://www.hypeunit.co" class="hype"><svg>insert super long svg</svg></a>
        <a href="https://twitter.com/Fresheys" class="twitter"><svg>insert super long svg</svg></a>
        <a href="https://www.youtube.com/channel/UCjD9wyRBMV9hqC2snDGqEUA" class="youtube"><svg>insert super long svg</svg></a>
        <a href="https://twitch.tv/fresheys" class="twitch"><svg>insert super long svg</svg></a>
        <a href="https://discord.com/invite/ez6YhCs" class="discord"><svg>insert super long svg</svg></a>
        <a href="https://www.tiktok.com/@fresheys?" class="tiktok"><svg>insert super long svg</svg></a>
        <a href="https://instagram.com/fresheys" class="instagram"><svg>insert super long svg</svg></a>
    </ul>
</div>

您的代码可以如下所示:

<div class="fresh">
    <img width="160" src=logo.svg>
</div>

<div class="icons-inline">
    <ul class="icons">
        <a href="https://www.hypeunit.co" class="hype"><svg>insert super long svg</svg></a>
        <a href="https://twitter.com/Fresheys" class="twitter"><svg>insert super long svg</svg></a>
        <a href="https://www.youtube.com/channel/UCjD9wyRBMV9hqC2snDGqEUA" class="youtube"><svg>insert super long svg</svg></a>
        <a href="https://twitch.tv/fresheys" class="twitch"><svg>insert super long svg</svg></a>
        <a href="https://discord.com/invite/ez6YhCs" class="discord"><svg>insert super long svg</svg></a>
        <a href="https://www.tiktok.com/@fresheys?" class="tiktok"><svg>insert super long svg</svg></a>
        <a href="https://instagram.com/fresheys" class="instagram"><svg>insert super long svg</svg></a>
    </ul>
</div>

您将拥有一个大小与fhe窗口相同的主容器,其元素居中,在其中您将拥有一个元素水平对齐的列表

创建一个至少与屏幕一样大的容器,并将内容与容器的中心对齐。

元素的居中可由flexbox处理。您只需要确保保存徽标和社交媒体图标列表的容器与用户可能拥有的任何屏幕一样大。您可以使用viewport-percent sizes,例如,表示“使此元素的高度至少为视口高度的100%”,其中视口是浏览器窗口的大小。

以下是一个可行的解决方案:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航