SVG embedded in CSS is not being displayed properly - css

I'm implementing a layout for a web-app running on Rails 5.2.
The layout has a header with a Css class (named section-divider) whose background is a SVG and it should occupy all the page width.
The SVG works fine in the HTML file I received from the designer: if I open the html file, it works correctly (check the first image).
The SVG doesn’t behave properly when I run the same code from the rails app (check the second image).
The only difference between the HTML version and the Rails one, is the reference to the SVG in the SCSS file.
I’ve tried multiple options and ways to reference the file without luck.
The SVG is being always displayed but it doesn’t stretch as it should.
It’s driving me crazy because the output HTML and Css are the same (from Rails and from the HTML provided by the designer)
SVG file
<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="638" viewBox="0 0 1920 638">
<path id="Rectangle_69" data-name="Rectangle 69" d="M0,0H1920a0,0,0,0,1,0,0V392a246,246,0,0,1-246,246H246A246,246,0,0,1,0,392V0A0,0,0,0,1,0,0Z" fill="#aaa"/>
</svg>
HTML CODE: the section-divider class is the one whose background is the SVG (just at the beginning, in the header)
<header class='search-bar text-center section-divider'>
<div class='container pt-4'>
<div class='mx-auto input-group col col-md-6 col-xxxl-4 ' >
<input type='text' class='form-control shadow-sm search-input' placeholder='' aria-label='Qué buscas' aria-describedby='button-addon2'>
<div class='input-group-append'>
<button class='btn btn-outline-primary' type='button' id='button-addon2'>Buscar</button>
</div>
</div>
</div>
</header>
CSS in Rails
.section-divider (landing_page.scss){
background: image-url("front/divider/header.svg");
background-position: center;
background-repeat: no-repeat;
background-size: 100% 100%;
padding-bottom: 2rem;
}
CSS in designer files (landing_page.scss)
.section-divider{
background: url('../front/divider/header.svg');
background-position: center;
background-repeat: no-repeat;
background-size: 100% 100%;
padding-bottom: 2rem;
}
In this image, the grey area is the SVG and in this image and it is displayed correctly
In this other image, the grey area is the SVG and it is NOT displayed correctly (because it doesn't stretch)

I suppose this is what you want to achieve.
.section-divider{
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' preserveAspectRatio ='none' viewBox='0 0 1920 638'%3E%3Cpath d='M0,0H1920V392a246,246,0,0,1-246,246H246A246,246,0,0,1,0,392V0Z' fill='%23aaa'/%3E%3C/svg%3E");
background-position: center;
background-repeat: no-repeat;
background-size: 100% 100%;
padding-bottom: 2rem;
border:1px solid;
}
<header class='search-bar text-center section-divider'>
<div class='container pt-4'>
<div class='mx-auto input-group col col-md-6 col-xxxl-4 ' >
<input type='text' class='form-control shadow-sm search-input' placeholder='' aria-label='Qué buscas' aria-describedby='button-addon2'>
<div class='input-group-append'>
<button class='btn btn-outline-primary' type='button' id='button-addon2'>Buscar</button>
</div>
</div>
</div>
</header>
In this case you need to use the following svg instead:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 638" preserveAspectRatio ="none">
<path id="Rectangle_69" data-name="Rectangle 69" d="M0,0H1920V392a246,246,0,0,1-246,246H246A246,246,0,0,1,0,392V0Z" fill="#aaa"/>
</svg>
Please observe that the svg has no width and height. Also I've added preserveAspectRatio ="none".
Please read about the preserveAspectRatio
attribute. If preserveAspectRatio="none" the image will be scaled non-uniformly.
Also your path has 2 arcs with radius 0. I've removed those arcs.

Related

CSS: How to create a background-image masked by a mask-image

I have a div that I want to give a regular background-image modified by a mask-image, so that the page background, which uses background-attachment: fixed; can "float" as if seen through a window, behind the div.
For the mask image, I want to be able to use either PNG or SVG.
Even if I use an SVG, I would rather not use the mask:url(mask.svg#element); method because the SVG artwork may not be simple clipping masks, but also include shades of gray in the form of blurs, gradients, etc.
I would like the mask to use the standard white = opaque / black = transparent schema. I believe mask-mode: alpha; is correct for this configuration?
Here's what I've managed to get working so far. The trouble is that it relies on an SVG id (instead of a whole image), and also the size is way smaller than the div, for some reason.
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #174;
background-color: #def;
font-family: sans-serif;
}
.masked-bg {
width: 600px;
height: 600px;
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAL0lEQVQIW22KQQ4AMAjC4P+PZlXnzZoYwVpypFjACsHmCtPlZwxAaCqf5XzWWPsB9TsMBL7veUEAAAAASUVORK5CYII=);
background-repeat: repeat;
background-attachment: fixed;
background-size: 10px;
image-rendering: pixelated;
mask-image: url(#svgmask);
mask-repeat: no-repeat;
mask-size: contain;
mask-position: center;
mask-origin: content-box;
}
</style>
</head>
<body>
<h1>mask-image</h1>
<p>with a fixed background</p>
<div class="masked-bg"></div>
<svg width="0" height="0"> <!-- sized to 0 because this SVG data is only to be used in the div background -->
<mask id="svgmask">
<polygon fill="#ffffff" points="100,10 40,198 190,78 10,78 160,198"></polygon>
<text x="0" y="36" fill="white" transform="rotate(30 20,40)" font-family="sans-serif">V e c t o r</text>
</mask>
</svg>
</body>
</html>

Scale images of different size using Bootstrap/CSS

I have a web application layout like this, styled with Bootstrap:
------------------------
| Header |
------------------------
| Display Area |
------------------------
The Header is a collection of control elements (mostly buttons) and therefore almost of same height.
The Display Area contains the following:
<div class="row">
<div class="col">
<img class="img-fluid" src="http://localhost/api/currentImage" />
</div>
</div>
http://localhost/api/currentImage returns an image. The image's size always differs: sometimes the width is bigger than height, sometimes vice versa.
Now I'd like to scale the image in that way that it uses as much as possible of the available Display without "overflowing". By overflowing, I mean that there is never a need to show a horizontal or vertical scroll bar because the image is too wide or too high. Right now, <img class="img-fluid" ... only scales the width correctly.
How can I achieve this using Bootstrap/CSS?
Bootstrap will let you resize images with its img-fluid class, but if you need to make the image cover the entire space you would have to write your own CSS, you could make use of the object-fit property to set the image to fill the container, while maintaining its aspect ratio and clipping off if necessary;
As you can see in the example below, the image is narrow, but it will fill the entire container even if it has to expand to do so.
EDIT: Included two more examples with fill and contain so you can see how their behavior changes.
header {
border: 1px solid red;
padding: 10px;
}
section {
border: 1px solid blue;
}
img {
height: calc(100vh - 20px);
width: 100%;
/* This is just to remove a blank space at the bottom of the image */
display: block;
}
img.cover {
object-fit: cover;
}
img.contain {
object-fit: contain;
}
img.fill {
object-fit: fill;
}
<header>
This is a header
</header>
<section>
<img class="cover" src="https://via.placeholder.com/200x700" />
</section>
<section>
<img class="contain" src="https://via.placeholder.com/200x700" />
</section>
<section>
<img class="fill" src="https://via.placeholder.com/200x700" />
</section>
Bootstrap has a predefined classes for responsive image. Check the following class,
.img-responsive Makes an image responsive (will scale nicely to the parent element)
<div class="row">
<div class="col">
<img class="img-fluid img-responsive" src="http://localhost/api/currentImage" />
</div>
</div>
`

Center text below an SVG

I would need something like this
and i am using this svg
<svg viewBox="0 0 26 26" id="terms" className="terms-svg">
<path id="Path_349" data-name="Path 349" className="info-icon" d="M13,0A13,13,0,1,0,26,13,13,13,0,0,0,13,0Zm2.024,21.812H10.962V8.756h4.062ZM12.972,7.154A1.986,1.986,0,0,1,10.864,5.13,1.978,1.978,0,0,1,13,3.078a2.042,2.042,0,1,1-.028,4.076Z"/>
</svg>
The problem i am having is how to make the terms & info text under it center around the svg i have tried just putting it in a <p> tag under the svg but then the text doesn't care about the center of the SVG. i have tried solutions like these
Align text center below a SVG circle?
How to place and center text in an SVG rectangle
but those make it so only part of the text shows. The part that fits the SVG width and nothing more. Is there any easy way to make th text align itself after the SVG center?
Try using display: flex;. Hope this code helps
.d-flex {
display: flex;
}
.flex-column {
flex-direction: column;
}
.align-items-center {
align-items: center;
}
<div class="d-flex flex-column align-items-center">
<svg viewBox="0 0 26 26" id="terms" className="terms-svg">
<path id="Path_349" data-name="Path 349" className="info-icon" d="M13,0A13,13,0,1,0,26,13,13,13,0,0,0,13,0Zm2.024,21.812H10.962V8.756h4.062ZM12.972,7.154A1.986,1.986,0,0,1,10.864,5.13,1.978,1.978,0,0,1,13,3.078a2.042,2.042,0,1,1-.028,4.076Z"/>
</svg>
<p>
Terms & Info
</p>
</div>

Layout for welcome modal with prev/next buttons

I'm trying to design an informational and instructional welcome modal box with 3 "pages" using custombox.js
The modal appears in the middle of the screen, I then need to display content in the middle of the modal, with a previous button to the left, next to the right and a footer showing progress.
I did this successfully using CSS grid layout, but many of the users do not have browsers that support this. I can't figure out how to make this using plain CSS...
Here's my html
<div id="modal">
<div id="leftNnav">
<i id="leftNnavImage" class="fa fa-chevron-left" aria-hidden="true"></i>
</div>
<div id="modalContent1" class="modalContent activeModalContent">
<img id="modalContentMedia" src="https://cdn.pixabay.com/photo/2013/04/06/11/50/image-editing-101040_960_720.jpg"/>
content text goes here, lots and lots and lots and lots and lots and lots more
</div>
<div id="modalContent2" class="modalContent">
Content2 = YT Video
</div>
<div id="modalContent3" class="modalContent">
Content3 = text
</div>
<div id="rightNnav">
<i id="rightNnavImage" class="fa fa-chevron-right" aria-hidden="true"></i>
</div>
<div id="modalFooter">
<svg height="40" width="40">
<circle id="circle1" class="circle activeCircle" cx="20" cy="20" r="10"></circle>
</svg>
<svg height="40" width="40">
<circle id="circle2" class="circle" cx="20" cy="20" r="10"></circle>
</svg>
<svg height="40" width="40">
<circle id="circle3" class="circle" cx="20" cy="20" r="10"></circle>
</svg>
</div>
</div>
https://jsfiddle.net/oppt6v9j/15/
But this way just ends up really messy, the text overflows and it just doesn't feel like the correct way to do things, I feel like I'm missing something really obvious. I know I can resize the image using media queries, so that's fine, but I'm not sure how to manage the text.
Any help with how I can design this modal in CSS is very much appreciated!
If your problem with aligning the components, I would suggest the following
.right { float: right; }
.left { float: left; }
.modal { display: flex; }
.modalContent { margin: auto; }
I believe using ID's as CSS selectors is bad practice.
Hope it helps.
Right, this is a tough one since I don't know the exact dimensions of the box (and you use alot of position: absolutes).
I'd imagine it varies with screen size but your image has a height/width set (300px by 150px) so I focused on that size instead.
Note This is not an "end all be all" answer, it won't solve every usecase under the sun, rather it answers the original question:
But this way just ends up really messy, the text overflows and it just
doesn't feel like the correct way to do things, I feel like I'm
missing something really obvious.
It does this by providing a way of thought rather than a clear answer.
The solution
You're using a lot of absolute positions, this means you have to work with alot of magic numbers. Try converting them to relative positions instead.
The text is really easy, you should wrap it in a <p> tag like so:
<p>
Content text goes here, lots and lots and lots and lots and lots and lots more
</p>
And style it appropriately (box padding / corners etc):
p {
color: #001818;
padding-right: 40px;
}
The same holds true for the image:
#modalContentMedia{
width: 300px;
height: 150px;
margin-left: 15px;
}
And I'm not sure where you'd want the arrows positioned (they are in the center of the box right now) but I can imagine you'd want them in the center of the image. To do so use:
#leftNnav{
position: absolute;
cursor: pointer;
z-index: 20;
padding-left: 10px;
left: 5px;
top: calc(50% - 40px);
}
#rightNnav{
position: absolute;
cursor: pointer;
z-index: 20;
padding-right: 10px;
right: 5px;
top: calc(50% - 40px);
}
This makes the entire thing look much neater:
By illustrating how I would start solving this issue I've demonstrated fixing the 3 major things (text, image position not centered, arrows) and trust you'll be able to learn and fix the rest of the issues yourself.

Re-styling a checkbox in bootstrap 3

I'm having a real difficult time trying to style a checkbox in bootstrap, I currently have the default checkbox, and I need to style it to look like this
I'm aware this picture it's round but the designer made a mistake, so instead of being round it still needs to be square
I have looked at the following and also tried what is suggested.
Twitter Bootstrap radio/checkbox
I did on the other hand find a website which has a similar to style to what i'm trying to achieve which is on here located on the left hand side where you do the filtering
Example of the checkbox
I tried using firebug to get/check out the CSS but I was unable to obtain the CSS.
So if you don't need support for IE8 you can easily do this with a background image and the :checked selector in CSS only. I used an svg image, but you could use a font, sprite or just two images.
#import "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css";
body {
background-color:#f5e1c6;
}
.image-checkbox {
position: absolute;
left: 100%;
visibility: hidden;
}
.image-checkbox-label {
height: 50px;
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 96 84" enable-background="new 0 0 96 84" xml:space="preserve"><path fill="rgb(213,195,170)" d="M74.2,73.5H19.5c-2.8,0-5.1-2.3-5.1-5.1V15.6c0-2.8,2.3-5.1,5.1-5.1h54.7c2.8,0,5.1,2.3,5.1,5.1 v52.7C79.3,71.2,77,73.5,74.2,73.5z"/></svg>') no-repeat;
color: #7b7163;
}
.image-checkbox:checked + .image-checkbox-label {
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 96 84" enable-background="new 0 0 96 84" xml:space="preserve"><path fill="rgb(213,195,170)" d="M74.2,73.5H19.5c-2.8,0-5.1-2.3-5.1-5.1V15.6c0-2.8,2.3-5.1,5.1-5.1h54.7c2.8,0,5.1,2.3,5.1,5.1 v52.7C79.3,71.2,77,73.5,74.2,73.5z"/><polygon id="check" fill="rgb(251,253,223)" points="30.2,31.8 30.2,43 46.1,54 80.1,19.1 80.1,6.1 46.4,44.7 "/></svg>') no-repeat;
color: #c3b39c;
}
.checkbox label {
padding-left: 60px;
line-height: 50px;
font-weight: bold;
font-size: 2em;
}
<div class="container">
<div class="row">
<div class="col-xs-12">
<form>
<div class="form">
<div class="checkbox">
<input id="remember-me" type="checkbox" class="image-checkbox" />
<label for="remember-me" class="image-checkbox-label">
Remember Me
</label>
</div>
</div>
</form>
</div>
</div>
</div>
How it Works
Give the input an id so that you can use the for attribute on the label. Don't forget to make your ids unique. Give the input a class (I used .image-checkbox), and position the input so that it's hidden offscreen but still displayed.
Style your label using the background-image for the unchecked box style. Using the :checked pseudo selector and the sibling selector (+), you can target how the label should be styled when the input is selected. In this case, I changed the background image to the checked image and changed the font color.
To get the actual label text to align nicely, I'm also overriding some of the default Bootstrap styles for .checkbox label. You can adjust them to suit your needs.

Resources