score:6
Text alignment in SVG does not work the way we are used to from HTML and CSS where everything is box with some dimensions and we can apply e.g. text-align: center
.
In <svg:text>
the starting coordinates define point from which will text line expand.
text-anchor
attribute controls which direction this expansion will occur: center
value means it will expand both ways so the initial anchor point will be in the middle of bounding box width (for horizontal writing systems). See excellent answer illustrating this text-anchor
as the best mean for centering text in SVG: https://stackoverflow.com/a/23272367/540955. Also, there is no CSS position properties (left/top) inside SVG, only x/y coordinates, nor margins and rest of box-model as you know it in HTML.
So in your code adding text-anchor="middle"
and moving the x
coordinates further right would produce centered text. I'd advise to use bare <text>
elements as opposed to <tspan>
s, because shifting them with dx
/dy
is relative to the last preceding character and this character could be some white space from parent <text>
(depending on code formatting) what would produce unbalanced centering. Also for easier calculations dominant-baseline
="central"
(or just middle
for horizontal writing systems) is useful, because it moves the anchor point from the base line to "center line".
So using dy
attribute (as you already do) to move the first line "one half" of line-height up and the other down should do the trick:
<svg viewBox="0 0 800 200" text-anchor="middle" dominant-baseline="central" font-size="100">
<!-- Outline and diagonals with center point for illustration: -->
<path d="M0,0h800v200h-800zl800,200m0 -200L0,200" fill="#FC9" stroke-width="1" stroke="rgba(0,0,0,0.3)"></path>
<circle cx="50%" cy="50%" r="10" fill="red"></circle>
<!-- Centered text: -->
<text x="50%" y="50%" fill="rgba(0,0,0,0.3)">So</text>
<!-- Shifted up and down: -->
<text x="50%" y="50%" dy="-0.5em">So</text>
<text x="50%" y="50%" dy="+0.5em">Food</text>
</svg>
(Not entirely related: the clipping could be done in CSS only with background-clip: text
; here is rough variation of your design as it appears in Chrome browser, with animated text background, but without shadows. Unfortunately adding shadows would require more elements or attributes, I think. This should work in any browser supporting background-clip.)
div {
display: flex;
flex-direction: column;
align-items: center;
font-size: 30vh;
line-height: 30vh;
font-weight: bold;
font-family: Impact;
}
span {
color: #fff;
background-color: #000;
width: 100%;
text-align: center;
}
@supports (-webkit-text-fill-color: transparent) and (-webkit-background-clip: text) {
span {
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
animation: 2s wohoo infinite alternate cubic-bezier(1,0,1,1);
background-position: 0 0;
background-size: 100% 100%;
background-color: transparent;
background-image: linear-gradient(
to right,
#f2385a 0,
#f2385a 20%,
#f5a503 0,
#f5a503 40%,
#e9f1df 0,
#e9f1df 60%,
#56d9cd 0,
#56d9cd 80%,
#3aa1bf 0,
#3aa1bf 100%
);
background-repeat: no-repeat;
transform-origin: center center;
}
}
@keyframes wohoo {
from {
background-size: 0 100%;
background-position: -5vh 0;
transform: scale(0.7);
}
50% {
transform: scale(0.7);
}
90% {
transform: scale(0.9);
}
to {
background-size: 500% 100%;
background-position: 0vh 0;
transform: scale(0.9)
}
}
html,body{margin:0;overflow:hidden;}
body {
background-color: #1d1f20;
color: snow;
display: flex;
flex-direction: row;
justify-content: center;
width: 100%;
}
<div>
<span>Baz</span>
<span>Gazonk</span>
<span>Qux</span>
</div>
Source: stackoverflow.com
Related Query
- How can I center my clip-path's text relatively to the viewport and display all the clip-path's text?
- ReactJS: How can I center the placeholder and text for Material-UI's <DatePicker/>?
- How can I center the text in the headers for an AG-grid control?
- (codesandbox) How can I get all the label `checkbox` and `first option` to be checked and selected by default
- How can I take the previous 5 values I am passing through search and then using it in API. I want to display pervious five values
- How can I limit a draggable element to the body, disable text selection and enable form inputs?
- how to evenly center image and text on the page
- How can i use useParams() with an onClick button to navigate to different pages and display the data in react.js?
- How can arrange the text and the logo properly?
- How can I create a counter as components to use multiple times and create one button to reset all the counter in React JS?
- How can I store all the dynamic values and attribute labels in a state in react js
- How can I use the context provider api and typescript without correcting me all the time
- how i can display some UI e.g circle and when data arrives i can change the color of specific circle?
- How can I combine onBlur and onFocus in my React Component without reselecting the text on every letter I type in?
- React: same component reappearing multiple times in the app, how can I make sure that all states are captured accurately and not just the last?
- How can I get all the object inside an object which is present inside an array and add the result into another array
- how to validate date based on input type text and display the day according to input date in react
- How can i move a text right of the screen and back to left of screen
- How to i write a react code to display all the locality in a pincode when we type pincode in text box?
- How can I center Logo and text in material-ui Appbar?
- How can I align my text and put the button on the extrem right of my gray bar?
- How do I define the security rules where only logged in user can read and write all of the collections and subcollection?
- How to display a ".json" file as text in component so it can be copied and pasted?
- How can we get a text and make it so it is display only a in limited amount of characters?
- In React, how can I make a single api call, set the result to a state variable, and display it?
- How can I display the percentage inside the graph and on hover?
- How to display a loading screen until all the useEffect, and fetches are done?
- How to retrieve text from JSON and display the text in point form (ReactJS Hook)
- How can I iterate through a dictionary in React to get the values separately and display using html?
- How can I center my header buttons and push items to the far right of my toolbar?
More Query from same tag
- how do I access data in a nested json data
- How to fire onSelectStart in React/JSX?
- Sort react-table at mount component
- How do I get the base url of the site for twitter:image meta tag?
- How to handle async errors in Flux?
- Dynamically loading React components
- React-google-maps + redux - InfoWindow on marker click
- Is there a way to use GreenSock (GSAP) to animate an SVG component in React?
- Order of setState
- React - input checkbox onchange won't fire first time
- React: Pass through props on wrapper/convenience components
- React set state not updating object data
- How to store sensitive data in react js frontend?
- Redux Nested State... with dynamic key
- Accessing an object returned by a Mongo collection within render() method in .jsx file(React)
- Can I use Luxon to combine a DateTime with a time string?
- How to use the window.location.assign with ReactJS?
- Why am I getting the error message "Property 'then' does not exist on type 'AsyncThunkAction'"?
- Radio button with label align vertical
- React setState hook not updating OUTSIDE a loop
- TypeError: this.props.persistor.subscribe is not a function Redux Persist
- TypeError: Cannot read property 'events' of undefined
- use filter only if an attribute exists with map function
- How Can I extend a React functional Component Prop Type?
- Unable to set the grids accurately in material ui react
- React-router-dom redirect issue
- How to get to work mobx + react + ui components?
- Elasticsearch query to return those documents also which are missing an indexed value
- 404 Not Found when requesting after deploying to Heroku, works locally
- TypeError: this.state.champions.map is not a function