how to float an underlying div to a svg - html
I am trying to let float an underlying div to a svg. problem is: when i reduce the screenwidt, the distance between the svg and the underlying div increases.
How can i achieve that the div under the svg always floats against the svg without whitespace?
This is my code:
<div class="svg">
<svg viewBox="0 0 1439 150" style="position:fixed; top:250px; left:0;" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Sell-on-FG-Flow" transform="translate(-1.000000, -2412.000000)" fill="#1e90ff">
<g id="Customer-Section" transform="translate(1.000000, 1026.000000)">
<g id="loading" transform="translate(0.000000, 1386.807849)">
-----pathes are here ---
</g>
</g>
</g>
</g>
</svg>
</div>
<div class="under">
let the red div float against the waves
</div>
And the css:
.svg {
height: 400px;
width: 100%;
}
.under {
height: 40px;
background: red;
}
Fiddle: https://jsfiddle.net/45b3n5q8/1/
Position fixed inline style in the SVG removes the element from the document flow and it will behave like an absolute position, remove the inline style from the svg and it should be ok, there's a small margin that can be removed with a negative margin top.
.under {
background: red;
margin-top: -5px;
}
see pen
Related
Extra pixels when element is inline
I am struggling to understand how the calculation of width/height in inline elements works. My question is very similar to this Extra pixels are being added to the span element yet slightly different. There is a div element of size 50x50. Within the div, there is a span with padding 15px. The span contains SVG circle of size 20x20. So there are three use cases: Only div is a block div - size 50x50 ✔️ span - size: 50x47 ❌ where are those three pixels? svg - size: 20x20 ✔️ div and span is a block div - size 50x50 ✔️ span - size: 50x54 ❌ where do these 4 pixels come from? svg - size: 20x20 ✔️ eveything is a block div - size 50x50 ✔️ span - size: 50x50 ✔️ svg - size: 20x20 ✔️ span { /* display: block; */ padding: 15px; } div { height: 50px; width: 50px; } svg { /* display: block; */ height: 20px; width: 20px; } <div> <span> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> </div> CodePen is available here. Note: I tried it in the latest Chrome but I think it will be the same everywhere. It's probably just some fundamental thing I am missing. :)
Your second case is covered here: Image inside div has extra space below the image. Due to the default alignment you will have extra space under your SVG. This can be fixed by adding display:block like you discovered or by adding vertical-align:top which is more logical as solution: span { display: block; padding: 15px; outline:1px solid green; } div { height: 50px; width: 50px; margin:30px; outline:1px solid blue; } svg { height: 20px; width: 20px; outline:1px solid red; } <div> <span> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> </div> <div> <span> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" style="vertical-align:top;" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> </div> Your first case is a bit tricky because it has nothing to do with the SVG or the width/height you are setting. It's all about font metrics. To simplify let's remove the div around and consider different SVG inside the same span and without padding: span { border: 1px solid green; margin:0 10px; } svg { outline: 1px solid red; } <span> <svg viewBox="0 0 24 24" height="20" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="30" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="50" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="200" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> Notice how the span has always the same height whataver the SVG inside due to the nature of inline element. Let's increase the font-size span { border: 1px solid green; margin:0 10px; } svg { outline: 1px solid red; } body { font-size:40px; } <span> <svg viewBox="0 0 24 24" height="20" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="30" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="50" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="200" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> The span are now bigger in height and the SVG are kept the same. You will also note the small gap at the bottom of the SVG due to the alignment I explained previously. Try to add font-size:0 and see the result. As you can see the height of your span has nothing to do with the SVG. To that height, you add the vertical padding to get the final height. In your case, the height was 17px and adding the padding you will have 47px which is close to 50px but there is no relation with. Note that you may get a different result than 47px if you test in different browsers/OS since the font will not for sure be the same and the initial height can vary. If you check the speficiation you can read: The 'height' property does not apply. The height of the content area should be based on the font ... The vertical padding, border and margin of an inline, non-replaced box start at the top and bottom of the content area, Making the span block element will change this behavior and you will have a more intuitive result as you noticed in your last example: 2*15px of padding + 20px of SVG height. Related question with more detail in order to understand how the height of element are calculated: How to determine height of content-box of a block and inline element Another related question: Can specific text character change the line height?
Is there a way to use pseudo element :before :after in svg path
I'm trying to add a new element after an svg path, that will have the same shape and path as his parent using :after or :before I created a g tag wrapping the path i want to target calling it by the class eyes <div id="svg-container"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 641.27 1107.33"> <defs> <style> .cls-30,.cls-32{stroke:#000;}.cls-30,.cls-32{stroke-miterlimit:10;}.cls-32{stroke-width:2px;}.cls-32{fill:#fff79a;} </style> </defs> <g class="eyes" id="GLOBE_GAUCHE" data-name="GLOBE GAUCHE"> <path class="cls-32" d="M298.37,210.19a39.52,39.52,0,0,0-18.46,7.23c-13.18,9.52-37,30.1-31.86,51.4,2.06,8.63,4.47,28,28.41,36.54a56.76,56.76,0,0,0,52.73-8.23,58.12,58.12,0,0,0,23.45-38.34,39.45,39.45,0,0,0-.5-15.94C348.79,229.24,337.62,205.53,298.37,210.19Z" transform="translate(-63.9 148.72)"> </path> </g> <g class="pupilles" id="PUPILLE_GAUCHE" data-name="PUPILLE GAUCHE"> <path class="cls-30" d="M281.69,266.67s-1.81-13.91,3.65-2.52c3.85-1.57,13.76-7.9,7.61.2,1.37,1.6,8.89,2.93-.84,4.93.48,2.24,6,10-3.41,4.87-1.9-.39-4,9.57-5.66-.92C282.63,271.22,272.29,270.37,281.69,266.67Z" transform="translate(-63.9 148.72)"> </path> </g> <g class="eyes" id="GLOBE_DROIT" data-name="GLOBE DROIT"> <path class="cls-32" d="M434.11,207.73A39.52,39.52,0,0,0,415.65,215c-13.18,9.52-37,30.1-31.86,51.4,2.06,8.63,4.47,28,28.41,36.54a56.76,56.76,0,0,0,52.73-8.23,58.12,58.12,0,0,0,23.45-38.34,39.63,39.63,0,0,0-.5-15.94C484.53,226.78,473.35,203.07,434.11,207.73Z" transform="translate(-63.9 148.72)"> </path> </g> <g class="pupilles" id="PUPILLE_DROIT" data-name="PUPILLE DROIT"> <path class="cls-30" d="M450.29,260.72s8.59-12.71-4.63-4.78c-1.06-1.35-5-7.78-5.28-.32-1,2.91-15.65,1.23-1.35,7.42.61,1.7-5.82,5.89,2.28,4.74.91,1.9,2.19,6.85,5.92.87C450.32,265.93,457.76,268.9,450.29,260.72Z" transform="translate(-63.9 148.72)"> </path> </g> </svg> </div> Then in the css i'm tryin to insert a new element :after the path of the eyes class html, body { height: 100%; margin: 0; } #svg-container { background-color: #f1f1f1; height: 100%; } svg { display: block; margin: auto; height: 100%; } .eyes path::after { content: ''; position: absolute; top: 0; left: 0; height: 100%; width: 100%; background: #d9a12a; } I can see in the console that it create the :after element at the right position, however it doesn't seem to show and the number of pixels are not showing (not even 0) Is there a way to implement this ?
Alternatively clicking on two overlapping svgs
I'm trying to create a buttons panel for a web page. I made the buttons with .svg files. Using margins an other css attributes i got them to the position shown in the code. My problem: I can't make both buttons fully clickable, specifically, I can use the orange part just fine, but the bottom half of the white circle, which is overlapping the orange's bounding-box, I cannot click on ,the top area is good. Of course i wanna be able to successfully click anywhere in the white or the orange, without the overlapping interfering. I've been reading on pointer events and figure they are the solution, but i can't figure out where (html or css) to use them or how. Also i'm not sure which property would be correct. How the buttons are working <!--my html code--> <div id="brand"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 106 106"> <defs> <style>.cls-1{fill:#ffffff;}</style> </defs> <a xlink:href="index.html"> <path class="cls-1" d="M67.78,52.28a5.58,5.58,0,0,0-6,0,6,6,0,0,0-2.11,2.45,9.11,9.11,0,0,0,0,7.42,6,6,0,0,0,2.11,2.45,5.58,5.58,0,0,0,6,0,6.05,6.05,0,0,0,2.11-2.45,9.11,9.11,0,0,0,0-7.42A6.05,6.05,0,0,0,67.78,52.28Z"/><path class="cls-1" d="M53,0a53,53,0,1,0,53,53A53.06,53.06,0,0,0,53,0ZM77.67,78.67a10,10,0,0,1-4.3.91A13.53,13.53,0,0,1,69.14,79a12.7,12.7,0,0,1-3.74-2.09,34.09,34.09,0,0,1-4.26-4.15A13.74,13.74,0,0,1,55.34,70a14.09,14.09,0,0,1-3.91-5A15,15,0,0,1,50,58.44L50,45.66,43.86,55.92H39.69L33.61,46.1V58.44H25v-28h7.77L41.9,45.34,50.79,30.4h7.77L58.6,45a15.72,15.72,0,0,1,6.2-1.21,15.38,15.38,0,0,1,7.6,1.88,13.68,13.68,0,0,1,5.27,5.24,15,15,0,0,1,1.91,7.56A14.78,14.78,0,0,1,77.3,66.6a13.64,13.64,0,0,1-6.17,5.24,3.52,3.52,0,0,0,1.14.9,3.13,3.13,0,0,0,1.29.26,4.94,4.94,0,0,0,3.63-1.81L81,76A9,9,0,0,1,77.67,78.67Z"/> </a> </svg> </div> <div id="reserve"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 944.38 115.07"> <defs> <style>.cls-2{fill:#ff7800;}</style> </defs> <a xlink:href="reserve.html"> <path class="cls-2" d="M494.41,80.8h-3.78v7h3.78a4.7,4.7,0,0,0,3.15-.91,3.23,3.23,0,0,0,1.07-2.59,3.27,3.27,0,0,0-1.07-2.61A4.65,4.65,0,0,0,494.41,80.8Z"/><path class="cls-2" d="M400.62,80.8h-3.77v7h3.77a4.73,4.73,0,0,0,3.16-.91,3.23,3.23,0,0,0,1.07-2.59,3.27,3.27,0,0,0-1.07-2.61A4.68,4.68,0,0,0,400.62,80.8Z"/><path class="cls-2" d="M940.67,10a55.59,55.59,0,0,0,3.71-10H535.66A63.67,63.67,0,0,1,474,59.85h-3.54A63.67,63.67,0,0,1,408.72,0H0A55.59,55.59,0,0,0,3.71,10H364.28V20H9.42a68.63,68.63,0,0,0,8.32,10H364.28V40H30.75C40.78,45.88,53.9,50,71,50H364.29V98.82a16.24,16.24,0,0,0,16.25,16.25h183.3A16.25,16.25,0,0,0,580.1,98.82V50H873.4c17.08,0,30.2-4.12,40.23-10H580.1V30H926.64A68.52,68.52,0,0,0,935,20H580.1V10ZM404.92,99.48l-4.36-6.39h-3.71v6.39h-6.8v-24h11a13.71,13.71,0,0,1,5.66,1.08,8.75,8.75,0,0,1,3.8,12.37,8.38,8.38,0,0,1-3.49,3l5.19,7.55Zm29.84,0h-19.3v-24h18.85V80.7H422.19v4h10.68v5.08H422.19v4.4h12.57Zm21.67-3.36a8.25,8.25,0,0,1-3.61,2.79,14.55,14.55,0,0,1-5.84,1,22.23,22.23,0,0,1-5.66-.72,14,14,0,0,1-4.47-1.92l2.23-5A14.38,14.38,0,0,0,442.83,94a14.06,14.06,0,0,0,4.19.65c2.61,0,3.91-.65,3.91-2a1.62,1.62,0,0,0-1.11-1.53,21.14,21.14,0,0,0-3.59-1,35.84,35.84,0,0,1-4.54-1.25,7.63,7.63,0,0,1-3.12-2.15,5.79,5.79,0,0,1-1.31-4,6.84,6.84,0,0,1,1.21-4,8,8,0,0,1,3.59-2.8,14.69,14.69,0,0,1,5.85-1,20.9,20.9,0,0,1,4.67.53,14.31,14.31,0,0,1,4.05,1.57l-2.09,5a14.06,14.06,0,0,0-6.66-1.85,5.63,5.63,0,0,0-3,.6,1.79,1.79,0,0,0-.92,1.56,1.55,1.55,0,0,0,1.09,1.45,20.1,20.1,0,0,0,3.54,1,34.24,34.24,0,0,1,4.55,1.25,8,8,0,0,1,3.13,2.13,5.72,5.72,0,0,1,1.32,4A6.8,6.8,0,0,1,456.43,96.12Zm23.62,3.36h-19.3v-24h18.86V80.7H467.48v4h10.68v5.08H467.48v4.4h12.57Zm18.65,0-4.36-6.39h-3.71v6.39h-6.8v-24h11a13.71,13.71,0,0,1,5.66,1.08,8.42,8.42,0,0,1,3.71,3.09,8.5,8.5,0,0,1,1.31,4.73,8.41,8.41,0,0,1-1.22,4.55,8.32,8.32,0,0,1-3.49,3L506,99.48Zm24.62,0h-6.7l-10.26-24h7.34l6.53,15.66,6.66-15.66h6.73Zm31,0H535v-24h18.85V80.7H541.76v4h10.68v5.08H541.76v4.4h12.57Z"/> </a> </svg> </div> <!--my css (wide interface being a mayor div that encases all the buttons)--> #brand { margin: auto; } #reserve { margin: auto; } #media screen and (min-width: 751px) { #brand { width: 11%; overflow: hidden; } #reserve { width: 95%; margin-top: -5.4%; overflow: hidden; } #wide-interface { overflow: hidden; white-space: nowrap; letter-spacing: 5px; text-align: center; } }
You need to apply a clipPath to the white circle to restrict that shape. By default, pointer-events must not be dispatched on the clipped (non-visible) regions of a shape. You will need to adjust the radius and coordinates but something like; <clipPath id="myClip"> <circle cx="53" cy="25" r="25" /> </clipPath>
I want to implement SVG clip-path for SVG element
I want to implement SVG clip-path for SVG element. I have a DIV element in which I want to put SVG element which will act as a clipping mask, and also I have the separate SVG element that has an image to which the clipping mask will be applied. The first problem I faced with is that clipping mask moves to the left top corner of the viewport but not located inside of the parent DIV element. The second problem is that I want to make an image on the full screen not depending on the screen size. Incorrect Mask Circle Correct Mask Circle (what I want to have) Do you have suggestions how to make it? Thanks in advance! html, body { margin:0; padding:0; overflow:hidden } svg { position:absolute; top:0; left:0;} .image-clip-src { width: 100%; height: 100%; } .svg-wrapper { width: 72px; height: 72px; padding: 2.5em; border: 1px solid #4D4F51; margin: 0 auto; border-radius: 50%; overflow: hidden; position: fixed; top: 55%; z-index: 9; left: 64%; transform: translateY(-50%); cursor: pointer; } .clipped-image image { clip-path: url(#clipping); } <svg class="clipped-image" width="100%" height="100%" viewBox="0 0 1440 960" preserveAspectRatio="xMinYMin meet"> <image class="image-clip-src" xlink:href="https://images.unsplash.com/photo-1526327227970-4bda49fa3489?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3c4bce33d96df6b18af53fb2dae3363e&auto=format&fit=crop&w=1650&q=80" width="100%" height="100%" overflow="visible"/> </svg> <div class="svg-wrapper"> <svg class="svg-defs"> <defs> <clipPath id="clipping"> <circle r="72" stroke="black" stroke-width="3"/> </clipPath> </defs> </svg> </div>
That's not the way SVG works. When you tell something to use a clip path, all it sees is the clip path definition itself. It doesn't know or care about where on the page you have positioned it's parent <svg>. If you want the clip circle to be at a certain position on the water image, you need to specify its position using cx and cy. html, body { margin:0; padding:0; overflow:hidden } svg { position:absolute; top:0; left:0;} .image-clip-src { width: 100%; height: 100%; } .clipped-image image { clip-path: url(#clipping); } <svg class="clipped-image" width="100%" height="100%" viewBox="0 0 1440 960" preserveAspectRatio="xMinYMin meet"> <defs> <clipPath id="clipping"> <circle cx="64%" cy="55%" r="72" stroke="black" stroke-width="3"/> </clipPath> </defs> <image class="image-clip-src" xlink:href="https://images.unsplash.com/photo-1526327227970-4bda49fa3489?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3c4bce33d96df6b18af53fb2dae3363e&auto=format&fit=crop&w=1650&q=80" width="100%" height="100%" overflow="visible"/> <circle cx="64%" cy="55%" r="72" fill="none" stroke="#4D4F51" stroke-width="1"/> </svg>
Shaped svg background
I have to code design like on the picture below: and it's responsive version looks like that: Im Have no idea how to code background with this shape and also with shaped line before background. Should I just create two very big svg's for bg and line or do it other way ? I would be grateful for any help
How I would approach this is by stacking three SVGs on top of one another. Using position: absolute SVGs inside a position: relative container element. The three layers are: A background graph shape with a grey fill A mid-ground SVG with the three boxes A foreground graph shape that has a blue stroke, but no fill, so you can see layers #1 and #2 behind it. We can make the two graph shapes preserveAspectRatio="none" so that they stretch to the width of the screen, We can also make it so that they share the same path definition to save space. The middle layer we make preserveAspectRatio="xMaxYMid meet" so it hugs the right hand side of the screen. That's just one way, of several we could have used, to achieve that effect. body { margin: 0; padding: 0; background-color: #111166; } #svg-container { position: relative; } #svg-container > svg { position: absolute; top: 0; width: 100%; height: 400px; } #background-graph { fill: #f8f8f8; overflow: visible; } #foreground-graph { fill: none; stroke: #111166; stroke-width: 2; } <svg width="0" height="0" display="none"> <!-- The graph shape. Used in two places below. --> <polygon id="jagged" points="0,360, 65,325, 100,340, 120,330, 130,335, 225,270, 255,295, 280,275, 290,290, 340,250, 360, 255, 400,235, 400,2000, 0,2000" style="vector-effect: non-scaling-stroke"/> </svg> <div id="svg-container"> <svg id="background-graph" viewBox="0 0 400 400" preserveAspectRatio="none"> <use xlink:href="#jagged"/> </svg> <svg id="boxes" viewBox="0 0 400 400" preserveAspectRatio="xMaxYMid meet"> <rect id="box1" x="30" y="10" width="225" height="175" fill="#fafafa"/> <rect id="box2" x="75" y="155" width="265" height="190" fill="#fff"/> <rect id="box3" x="260" y="80" width="125" height="70" fill="#fafafa"/> </svg> <svg id="foreground-graph" viewBox="0 0 400 400" preserveAspectRatio="none"> <use xlink:href="#jagged"/> </svg> </div>