Aligning the text to be at the center of a textfield in Vuetify
Asked Answered
R

5

18

I have a read only textfield that shows a string. The string starts from the left side of textfield as it should. I was wondering if there is a way in Vuetify to align the string to the center of textfield?

UPDATE This is my code:

<v-text-field
  value="Select the configuration:"
  color="grey lighten-43"
  class="text--darken-3 mt-3 text-xs-center"
  outline
  readonly
  single-line
></v-text-field>
Ritornello answered 10/10, 2018 at 14:7 Comment(2)
please share your tried workFamine
Updated the initial question, thanks.Ritornello
W
32

In case you are using scoped styles, a deep selector (i.e. >>>) for the input field has to be used:

<v-text-field 
  class="centered-input text--darken-3 mt-3" 
  value="Select the configuration:" 
  color="grey lighten-43" 
  outline readonly single-line />
<style scoped>
    .centered-input >>> input {
      text-align: center
    }
</style>
Weide answered 21/11, 2019 at 6:23 Comment(3)
great finding: deep selector (i.e. >>>) , tks @WeideFeminism
you save me a lot of time. Thank you for this!Spar
Even w3schools did not mention that. Never saw that either. Nice finding!Kukri
R
11

The cause of the text being aligned to the left is the base class for input that sets text-align: start. To solve this add a rule that for that input, for example:

template:

<v-text-field 
  class="centered-input text--darken-3 mt-3" 
  value="Select the configuration:" 
  color="grey lighten-43" 
  outline readonly single-line />

css:

.centered-input input {
  text-align: center
}

new Vue({ el: '#app' })
.centered-input input {
  text-align: center
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js"></script>

<div id="app">
  <v-app id="stackoverflow">
    <v-text-field 
      value="Select the configuration:" 
      color="grey lighten-43" 
      class="centered-input text--darken-3 mt-3" 
      outline readonly single-line />
  </v-app>
</div>
Rotterdam answered 1/11, 2018 at 14:41 Comment(2)
this is not working, while another answer a using >>> isChromogen
Strange. Why in some cases .centered-input input works (this is working pretty nice for me), but in some cases .centered-input >>> input works?Exorcist
P
2

in my case

not working:

.centered-input input {
  text-align: center
}

working:

/deep/ .centered-input input {
  text-align: center
}
Pradeep answered 4/8, 2020 at 8:44 Comment(0)
H
0

Working

style="left: auto; right: 0px; position: absolute;"
Hecker answered 4/11, 2022 at 6:58 Comment(0)
E
0

In Vue 3, you can use the following syntax:

.center-text :deep(input) {
  text-align: center
}
Extenuatory answered 2/12, 2023 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.