how to center properly an icon in an input text? - html

I'm new to stackoverflow, been using it to solve problems very often but it's the first time I ask something !
So. What I'm trying to do seems simple, but I can't figure out a satisfying and valid way to do it.
My code is made with React. I can't change the way the icon is called, so here is not the question.
I want to vertically center this icon, which is called inside the label, so wrapped with my input, like this :
centered icon vertically
Here's my code :
<label class="input-label">
nom
<input type="text" class="input input-text input-with-icon" name="nom" placeholder="exemple" value=""/>
<span class="input-icon input-icon--right" tabindex="0">
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="ad76e940dfa45c8f1e8d7675f49d95e7"><path id="c2de19005ef6cc9c6fd13489277b23b8" fill-rule="evenodd" clip-rule="evenodd" d="[a very long code I won't put here]" fill="#4C4F5C"></path></g></svg>
</span>
</label>
I put flex on it so that the Label stack on the input, as I want. BUT ! If I do so, then my icon, which I center with absolute position on the parent (the label), get centered on the entire block which is two lines, instead of being centered only on input.
Also, W3C doesn't validate to have the label on one hand, then a div that wrap the two elements inside him. I tried it and, either I did it wrong or it's simply something that W3C is not ok with ! So I won't use it unless I'm forced to.
The only solution I see would be to calculate the input height, then do a bottom of the height divided by 2 (and then let the transform:translate(-50%) for the rest to be perfectly centered. But I don't have the specific height of the input. So I can also just tell : ok the input is 20px, I set a bottom of 10px, and 'finito pepito' (aka : it's over). But it's really not accurate, as this input is made for a design system. So it must be called in several cases, and always be responsive and adapted.
And to finish, I found that the input type search was answering to this but the icon can't be customized, so in any way I have to put this icon myself. Plus, I'm not sur it's a good idea to put a input search when you need an input text.
Do you see something more adapted ? Something acceptable and valid (I don't want to put some extra code or do something heavy if I can do it with less code.)
Thanks for your help !

You can simply use the for attribute of label tag in order to move input and icon into their own container. I've used flex and transform to position icon but there's many ways to achieve the same result :
.text-input-container {
display: flex;
align-items: center;
}
.text-input {
padding-right: 24px;
}
.text-input-icon {
transform: translateX(-20px);
}
<label for="text-input">Nom</label>
<div class="text-input-container">
<input id="text-input" type="text" class="text-input">
<svg height="12" width="12" class="text-input-icon">
<circle cx="6" cy="6" r="6" stroke="black" stroke-width="3" fill="red" />
</svg>
</div>

Related

Unable to align certain elements in a website layout I designed. Beginner at coding

I'm a beginner at coding and I was stuck at a silly issue. I'm unable to align content according to the UI Design. If anybody would be kind enough to help that would be awesome. Here are the image references Finished UI Protoype Unfinished Website Layout.
Please find my HTML code below.
Index.html
<a href="#" class="secondary-cta">
<span>Spring '23 Collection</span>
<svg width="31" height="23" viewBox="0 0 31 23" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M30.0221 12.5978C30.6284 12.0333 30.6623 11.0842 30.0978 10.4779L20.8987 0.597304C20.3342 -0.00901737 19.385 -0.0429153 18.7787 0.521591C18.1724 1.0861 18.1385 2.03524 18.703 2.64156L26.88 11.4243L18.0973 19.6013C17.491 20.1658 17.4571 21.115 18.0216 21.7213C18.5861 22.3276 19.5352 22.3615 20.1416 21.797L30.0221 12.5978ZM0.946463 11.999L28.9465 12.999L29.0535 10.001L1.05354 9.00096L0.946463 11.999Z" fill="black"/>
</svg >
</a>
you have not given the link to the html code thats the name of the code.
if you want to center text try
text-align: center;
if you want to center an image or input box try, putting an id and center by doing
align-items:center;
justify-content:center;

Star rating component - What are the correct accessibility and semantics to be used

I have a star rating component that currently has the following semantic. The SVG are just stars.
<div role='img' aria-label="6 out of 6 ratings">
<svg role="presentation"></svg>
<svg role="presentation"></svg>
<svg role="presentation"></svg>
<svg role="presentation"></svg>
<svg role="presentation"></svg>
<svg role="presentation"></svg>
</div>
I have used the role='img' aria role as I want to ideal treat this image as one image. I have then used the role='presentation' aria role on the SVG as I think the SVG's alone provide no extra information so want to remove the semantic meaning to the SVG and it's children. I read about the presentation role here.
I read a article on SVG accessibility and they go with slightly difference approach. The example they give is the following:
<body>
...
<svg xmlns=http://www.w3.org/2000/svg role="img" aria-labelledby="title desc">
<title id="title">Circle</title>
<desc id="desc">Large red circle with a black border</desc>
<circle role="presentation" cy="60" r="55" stroke="black" stroke-width="2"
fill="red" />
</svg>
…
</body>
They use role='img on the svg. I believe this does not apply to my situation and would be better for me to keep role='presentation' on my SVG?
They use title and desc. I believe that as a single star SVG it would not provide any extra benefit to the user? Also hovering over the SVG would reveal the title (and desc?) which I do not want.
Coming back to my example of how I currently have it:
Is there anything I can do to improve the accessibility/ semantic design to make it more user friendly?
Have I correctly used the right aria role for the container and the SVG?
Should I be able to tab through my rating image (as a whole) as for the aria-label to be read out?
Are there any other aria attributes I should be using?
Looking at the example on semantic-ui, they have no aria roles from what I can see.
The MDN Web docs for img role has a link to a star rating role="img" example
They use aria-hidden instead of role="presentation"
According to the specs, the img role should already make its children presentational
I would add that being accessible implies more than just being accessible for screenreaders. For instance "3 blacks stars, 2 white stars" in the MDN example is something people may not understand (eg. people naturally reading from right-to-left).
"Grandma: Honey, Is it better to have 5 white stars, or 5 black stars?"
I would say that adding "Note: 3/5" next to the ratings, might be a good help.

SVG Scaling Giant in Internet Explorer, works in Chome(AngularJS)

I've been having an issue I can't get my head around, I've tried many different fixes, mostly around viewBox 0 0 x x various width="" and height="" modifications (removing width and height, which I never had in the first place).
Basically, I use Angular ng-nepeat to construct a row of SVG of specific colour, this is deployed in a propriety visualisation website (PIVision) which deploys HTML pages as draggable resizable objects. I don't think thus has anything to do with the issue but the information may have been helpful.
In Chrome, my HTML symbol shows up nicely as so : Nice SVG
In IE however, the symbol shows up huge, and does not resize or scale as can be seen on the container resize box : IE's SVG
Below is my HTML, as I do not believe it to be a scripting issue, however if required I have attempted to create a JSFiddle (however doesn't work properly, I believe it is to do with my Angular JS set up in JSFiddle, as the implementation the company uses is prebuilt and slightly different, apologies).
HTML :
<div ng-style="{'height': config.Height, 'width':config.Width}" style="cursor:pointer" >
<table style="height:100%; width:100%">
<tr style="width:100%; height:100%" id="theTableRow">
<td ng-attr-title="{{getSortedValue($index)}}" ng-style="{'width':theCircleWidth + '%', 'padding':'0.3%'}" ng-repeat="n in config.makeCounterArray(config.columnMax) track by $index">
<svg ng-if="sortedListOfValues[$index].elementName.charAt(0) == 'P'" style="filter:url(#dropshadow);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="45" ng-attr-stroke="{{config.stateColours[getSortedValue($index)]}}" stroke-width="4" ng-attr-fill="{{config.stateColours[getSortedValue($index)]}}" />
</svg>
<svg ng-if="sortedListOfValues[$index].elementName.charAt(0) == 'H'" style="filter:url(#dropshadow);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 100 100">
<rect height="100" width="100" ng-attr-stroke="{{config.stateColours[getSortedValue($index)]}}" stroke-width="4" ng-attr-fill="{{config.stateColours[getSortedValue($index)]}}" />
</svg>
</td>
</tr>
</table>
</div>
Is it possible to fix this or will I have to resort to using coloured border-radius div's to accomplish what I want?
Thanks in advance for any help given, apologies for anything wrong with the question, this is my first!
EDIT : After changing the table to include table-layout:fixed it scales a bit better, however I have this strange space above the table, link in first comment.
SVGs are subject to CSS when directly embedded in the document, different browsers just have different default rendering.
Try this:
svg {
max-width: 100%;
max-height: 100%;
}
For a new default styling. Resize them as needed in specific cases.

SVG tag <use> doesn't work within polymer component in Firefox

Here is an example: http://miriti.ru/svgtest/
If you'll look at this example in any browser you will see two grey squares with green circles (labled "Symbol") inside. Except for Firefox (I am testing on Firefox 35.0.1 on windows and mac os x).
These SVGs are absolutely identical but the second one is placed inside the polymer component.
Code on GitHub: https://github.com/miriti/svgtest
Any suggestion about what can be the cause of this problem?
I seem to find a workaround for your problem. First of all, wrap your <svg> element with <div id="content">. This is necessary, because later we will reinitialize the inner content of this div.
<template>
<div id="content">
<svg width="400" viewBox="0 0 400 400">
...
</svg>
</div>
</template>
Then in the script section do the following woodoo-magic:
domReady: function() {
this.async(function() {
this.injectBoundHTML(this.$.content.innerHTML, this.$.content);
}, this);
}
Please don’t complain about the weirdness of this trick :)
You are done: FF is satisfied now.
Using SVG sprites I ran into issues that are similar to the one you describe but not the same. So what I propose is not exactly a solution to your problem, however you could avoid such issues altogether by using iron-iconset-svg (https://elements.polymer-project.org/elements/iron-icons?active=iron-iconset-svg), which in my opinion provides a cleaner/easier solution. Its simply a wrapper for your SVG sprite sheet, so you define your icons almost the same way and use them with iron-icon.
Defining a custom iconset (put it directly into the page or wrap it inside an element + set a name that describes the icons, here: 'your-iconset-name')
<iron-iconset-svg name="your-iconset-name" size="24">
<svg>
<defs>
<g id="your-icon-name">
<rect x="12" y="0" width="12" height="24" />
<circle cx="12" cy="12" r="12" />
</g>
</defs>
</svg>
</iron-iconset-svg>
If you wrap them, lets say in 'your-custom-iconset', you can include the iconset like this:
<your-custom-iconset></your-custom-iconset>
Using icons
When you need an icon you just include iron-icons (https://elements.polymer-project.org/elements/iron-icons) and place it like this:
<iron-icon icon="your-iconset-name:your-icon-name"></iron-icon>
You can then give it a class to apply styling and don't need to use 'fill' for its color, just use 'color'.
I fixed it in Polymer 1.0 like this:
attached: function () {
//Fix "svg-use" in Firefox!! -> Properly bad for Performance (Issue: https://github.com/Polymer/polymer/issues/1182)
this.async(function () {
this.$.content.innerHTML = this.$.content.innerHTML;
}, this);
}
but I don't use Bindings in my component (yet)

Including FontAwesome icons into SVG

I'd made all that Duopixes said in this question but it didn't help. The icon shows as text still.
Here my HTML:
<g class="info-container" transform="translate(240,283)">
<text class="svg-icon" x="-60" y="0">&#xf040</text>
</g>
and
.svg-icon{
font-family: FontAwesome;
}
The style is applied to the text element and FontAwesome.css is attached to the page. What I've done wrong?
UPDATE:
I'm creating svg-elements via D3.js library. And that was the point.
The answer is here. In a D3-chart we need to use \ufXXX format for an icon text.
infoGroup.append("text").attr("class", "svg-icon").text("\uf005");