简体   繁体   English

使用 flex 对齐 SVG

[英]Aligning SVGs using flex

I'm using flex to layout a menu for a website and I'm having a heck of a time trying to align an SVG to the right.我正在使用 flex 为网站布局菜单,并且我有一段时间试图将 SVG 向右对齐。 I'm not sure what I'm doing wrong because I'm using SVGs inside of flexboxes for my social icons and they're lining up proplerly.我不确定我做错了什么,因为我在 flexbox 内使用 SVG 作为我的社交图标,并且它们正确排列。

Here is my code for the css and the structure:这是我的 css 和结构代码:

 .seventh { display: flex; width: 100%; height: calc(100% / 7); overflow: hidden; margin: auto; } div.link, div.blank { display: flex; justify-content: flex-end; width: 46.25%; height: 100%; position: relative; flex-flow: column nowrap; overflow: hidden; margin-right: 0; } .svgLink.left { display: block; margin-right: 0; height: 100%; }
 <section id="blue" class="seventh"> <div class="link"> <svg class="svgLink left" viewBox="0 0 431.1 73.1"> <text transform="matrix(1 0 0 1 -0.2852 63.7813)" font-family="'Panton_xbk'" font-size="74.5982px">MARKETING</text> </svg> </div> <div class="pixel"> <svg class="pixel" viewBox="0 0 512 512"> <circle cx="256" cy="256" r="250" class="vf6"></circle> </svg> </div> <div class="blank"></div> </section>

This is what I am trying to get it to look like:这就是我试图让它看起来像的: 在此处输入图片说明

There are a few things you should watch out when using flexbox.使用 flexbox 时应该注意一些事项。

If you are trying to align the SVG's to the right the most important thing would be to remove the the last div with class="blank".如果您试图将 SVG 与右侧对齐,最重要的事情是删除最后一个带有 class="blank" 的 div。 In general it is actually abad practice to use empty div's for styling as you can do it with css (especially if you are using flexbox).一般来说,使用空 div进行样式设置实际上是一种不好的做法,因为您可以使用 css(尤其是在使用 flexbox 时)。
If you want to style specific child elements use the appropriate css properties.如果要设置特定子元素的样式,请使用适当的 css 属性。

There is a really great guide on how to use flexbox layout.关于如何使用 flexbox布局有一个非常好的指南


But let's continue with your code example :但是让我们继续您的代码示例

I have stripped out unnecessary code from your snippet and modified the css to use the flexbox layout.我从您的代码段中删除了不必要的代码并修改了 css 以使用 flexbox 布局。 If you want to align the SVG's to the left use justify-content: flex-start;如果要将 SVG 与左侧对齐,请使用justify-content: flex-start; and if they should be aligned to the right use justify-content: flex-end;如果它们应该对齐到正确的使用justify-content: flex-end; in the class selector .seventh as provided in the example below.在类选择器.seventh中,如下例所示。

 .seventh { display: flex; flex-flow: row nowrap; justify-content: flex-end; align-items: center; width: 100%; overflow: hidden; margin: auto; } div.link { width: 46.25%; } div.pixel { width: 7.5%; } div.pixel>svg { height: 100%; }
 <section id="blue" class="seventh"> <div class="link"> <svg class="svgLink left" viewBox="0 0 451.1 73.1"> <text transform="matrix(1 0 0 1 -0.2852 63.7813)" font-family="'Panton_xbk'" font-size="74.5982px">MARKETING</text> </svg> </div> <div class="pixel"> <svg viewBox="0 0 512 512"> <circle cx="256" cy="256" r="250" class="vf6"></circle> </svg> </div> </section>

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

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