Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRalf Koller
4,506 PointsIssues positioning and sizing SVG placed as background-object in CSS
I am struggling completely understanding the behaviour of SVG files placed as background-images in a CSS file. Somehow i still haven't got to the point where the SVG is doing what I want and understanding why it behaves like it behaves.
I've assembled a quick demo on Codepen showing of the issues i have: http://codepen.io/rpkoller/pen/qgAxB the source of the embedded and base64 encoded svg is:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 73.406 50.834"><polygon fill-rule="evenodd" clip-rule="evenodd" fill="#20201E" points="59.2,0 46,0 36.7,7.3 27.4,0 14.2,0 0,14.7 36.7,50.8 73.4,14.7"/></svg>
My basic goal is to get a fluidly scaling SVG image fitting into the width of its parent container 100%. Three scenarios.
1) .heart1 (green background) by setting height and width of .heart1 to 100% it is filling up the container which owns 50% of the viewport. So that is more or less ok.
2) .heart2 (yellow background) but now the trouble starts. if i set the svg object to a width smaller than 100% the object is aligned to the left and top. Instead i would prefer to have the heart centered in its parent yellow container with a margin at the top. But nothing works. changing background: url() from no-repeat 0 0; to no-repeat center 0; doesn't change anything as well as using margin-top: 1 em or margin: 0 auto;
3) .heart3 (blue background) the toughest part is the menu. i tried to position a heart next to the menu. ideally would be some kind of vertical-align: middle , that the center of the menu links aligns with the center of the small heart next to it? is it the correct way to go with columns like i did or should i just float the heart or maybe use inline svg in html?
Honestly i am a bit confused how to deal with those issues to get in total control again so that the hearts and menu and stuff scale smooth and fluidly. Best regards Ralf
4 Answers
Guil Hernandez
Treehouse TeacherHey Ralf Koller,
Positioning and sizing SVGs as backgrounds is very similar to positioning regular background images with CSS. Hopefully this forum tip I recorded helps you out. :)
Andre' Jones
26,671 PointsHow will I be able to come back to these Forum Videos. Is there a list somewhere? (Other than subscribing or bookmarking it)
Patrick Castle
Courses Plus Student 14,591 PointsGood question. Some of these responses become great mini-tutorials in their own right. Would be good to accumulate them somehow.
Francesco Saviano
Courses Plus Student 24,491 PointsNice tut man! Thanks a lot to all of u!
Guil Hernandez
Treehouse TeacherYou're welcome, Francesco Saviano. Glad you liked it!
Federico Prieto
3,902 PointsFantastically exposed. Concise and straight to the point. Perfect.
Ralf Koller
4,506 PointsRalf Koller
4,506 PointsNice one Guil Hernandez! Thanks a lot! I will work through the video and try if all my uncertainties will be covered and i am able to solve my hassle. ;) But it looks more than promising. Have already taken a brief look.
And about the base64 encode. In this case i've used: http://www.mobilefish.com/services/base64/base64.php if i remember correctly. but there is also a quick way directly inside the osx / unix terminal ( via the two Kyle Foster videos dealing with SVGs), either by:
openssl base64 < thefile.png | tr -d '\n' | pbcopy
or
cat path/to/file.png | openssl base64 | tr -d '\n' | pbcopy
but normally i prefer the compass image-url function which does the encoding part for the user.
Ralf Koller
4,506 PointsRalf Koller
4,506 PointsOk Guil thanks again. Got through your video and especially the second demo opened my eyes. ;) Guess i get it now and i suppose i know what my trouble was. I've used the single line short form for background urls and mine was faulty and missing properties i suppose. ;) For the moment until i am totally comfortable with svgs i will stick to the longer form you've used. Seems saver for the moment.
And one little cosmetic issue in your second demo. Your heart-main example isn't centered when the windows is enlarged in width - it is missing the background-position: center property like you've used in previous examples.
Thanks again and best regards Ralf
Amila KASTHURIARACHCHI
1,088 PointsAmila KASTHURIARACHCHI
1,088 Points.You're = Awesome [Guil] { because of : your nice tutorials (that I've subscribed to) treehouse mainly; and to learn latest : CSS to change my ugly website; Guil : thanks for latest tutorials and the way of teaching; }
ps {
Guil : If you're interested I'm looking someone to give my website customization project, With your talents and knowledge I believe that you could achieve it well }
.contact { me : via my profile email; }
Thanks;