The issue observed with Qt CSS border and padding appears to be causing the text to be

I have multiple QPushButton elements positioned next to each other. Each button has a padding of 8px on both the left and right sides. When one of them is focused, I change the background color. Currently, it looks fine:

https://i.sstatic.net/7V92N.png

Now, in addition to changing the background color, I want to add a white 2px border. However, when I do this, the text appears to be cut off due to the size of the border. With a 2px border, it looks like this:

https://i.sstatic.net/ojsnw.png

If I increase the border size to, for example, 4px, the text completely disappears because the button does not resize correctly.

In my Qt CSS, I have the following styling:

QPushButton {
    background-color: transparent;
    border-style: none;
    font: bold 12px;
    color: white;
    padding-left: 8px;
    padding-right: 8px;
}

QPushButton:focus {
    background-color: blue;
    border-style: solid;
    border-width: 2px;
    border-color: white;
}

Edit, Solution from Rushi: The workaround involves setting a transparent border for the regular (non-focused) button. Upon receiving focus, the button's width seems to be calculated correctly. Although I'm not sure why Qt behaves this way, it resolves the issue for me :-)

CSS with the fix applied:

QPushButton {
    background-color: transparent;

    /* Define border with transparent color */
    border-style: solid;
    border-width: 2px;
    border-color: transparent;

    font: bold 12px;
    color: white;
    padding-left: 8px;
    padding-right: 8px;
}

QPushButton:focus {
    background-color: blue;
    border-color: white;
}

Answer №1

Hey there! Take a look at the HTML and CSS code snippet below for assistance with resolving this particular issue.

<html>
    <head>
        <title>Page Title</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            .box{ background: #000; width: 400px; height: 100px; margin: 50px; padding: 50px;}
            .QPushButton {
    background-color: transparent;
    font: bold 12px;
    color: white;
    padding-left: 8px;
    padding-right: 8px;
     border-style: solid;
    border-width: 2px;
    border-color: transparent;

}

.QPushButton:focus {
    background-color: blue;
    border-style: solid;
    border-width: 2px;
    border-color: white;
}
        </style>
    </head>
    <body>
        <div class="box">
            <a href="#" class="QPushButton">3</a>
            <a href="#" class="QPushButton">4</a>
        </div>
    </body>
</html>  

Answer №2

To optimize the design, consider reducing the padding size using the following solution:

QPushButton:focus {
    ....other styles.....
    border-width: 2px;
    padding-left: 6px;
    padding-right: 6px;
}

Answer №3

I find @Evgeny's solution more appealing because there are cases where having a border in the default state is necessary, and making it transparent wouldn't solve the problem.

Here is an example that demonstrates this concept:

/* Default state */
.btn {
    border: 1px solid black;
    padding: 1px;
}

.btn:hover {
    border: 2px solid black;
    padding: 0px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Applying FAB (Material UI) to span across 2 columns in a CSS Grid: A step-by-step guide

Is there a way to make the zero button larger than the other buttons in its row by spanning 2 columns? I would like it to be an oversized oval shape. Appreciate any assistance you can provide. import "./styles.css"; import Button from '@mui/materia ...

Tips for cropping an image using CSS with dimensions specified for width, height, and x and y coordinates

As I work on implementing a cropping feature using react-easy-crop, my goal is to store the user's image along with pixel coordinates that dictate their preferred cropping area. My objective is to utilize the parameters provided by react-easy-crop in ...

Trouble with toggling class "collapsed" in Bootstrap v3 Navbar-toggle when using multiple data-targets

When using the data-target attribute with multiple CSS selectors, the behavior of toggling the "collapsed" class in navbar-toggle does not work as expected. If only one selector is used, the class is toggled properly, but when multiple selectors are specif ...

Here is a step-by-step guide on how to use JavaScript to eliminate the page title, URL, date and

When printing a page using window.print, is there a way to exclude the page title, URL, page number, and date/time from appearing? ...

List of Unicode symbols to use in CSS for ::before content styling

I've been looking through different Unicode resources such as this one, but I haven't found a list that includes \0108. This particular character is an alternative dollar sign similar to the one used in font-awesome. Additionally, there are ...

Centering the overlay gallery box in MonoBook Skin of MediaWiki using CSS

While working on transforming the MonoBook Skin into a DarkMode version, I encountered an issue with the gallery overlay css. The Overlay displays a semi-translucent white box with text at the bottom by default, overlaying the image. I made adjustments to ...

Is there a way to remove the scrollbar from the menu created with react-burger-menu?

How can I remove the scroll bar from the menu created with react-burger-menu? Despite trying various CSS properties adjustments, I have not been able to achieve the desired effect. Here is an image of the open menu and the corresponding CSS: (https://i.st ...

Tips on verifying if user x has sent over two messages in the chat and concealing their identity

I have a JavaScript chat application that is causing me some trouble. I recently implemented a feature to display user names on top of messages, and while it works well for the first message, I am struggling to hide the user's name when they send subs ...

The CSS class is malfunctioning

I currently have the following code snippet within my HTML file: echo "<article class='room-full-width'>"; $room_query = mysql_query("SELECT * FROM roomdetails WHERE hotelname = '".$row['hotelname']."'"); while($ ...

Issue with continuous loader malfunction

I integrated a 3-second mini-loading animation on my website. It shows up every time I refresh the site or navigate to another page. The issue I'm facing is that once I add the loading section, it never stops (it should only last for 3 seconds). Usua ...

Experiencing difficulties with my image slider and struggling to determine the cause

Trying to create an image slider, but I'm encountering numerous challenges. It seems like everyone finds it easy to do, but for me, it's a struggle. I've been following along with this tutorial. Watch Tutorial Here However, the buttons don ...

Troubles with CSS in MUI Component Integration

How can I successfully implement a vertical timeline using Material UI in React? I've set it up, but the CSS isn't functioning as expected. Is there an error in my implementation? List of Dependencies: "@emotion/react": "^11.11.1& ...

I'm currently reviewing a CSS stylesheet for a React webpage, and I've noticed that several classes are utilizing content to dynamically create images. However, the display of content in VSCode appears as a bullet point

Exploring an existing ReactJS website, I've noticed that many images are rendered using the CSS content property. While examining the CSS file in VSCode, I've come across classes where the content is displayed as "". I'm uncertain whether ...

Utilizing identical ID values multiple times to link to the identical location

I am currently working on a horizontal grid project with 3 image squares. My goal is to create an action where clicking on any grid section will anchor the user down to a slideshow box displayed below the grid. Below is an example of one grid section and ...

Using jQuery to compare input with the current select optgroup choices

I'm struggling with looping through options within a form select. The options are grouped into optgroups, and I believe this is where my issue lies as I can't seem to find the correct syntax. My objective is to compare the value entered using .v ...

Utilize npm to set dynamic variables in SCSS

My knowledge of npm and SCSS is limited, so I have a question that follows: I have a SCSS file named app.scss where I frequently use one default color for the background. I would like to define a variable $background_color to easily change that color. Cou ...

Utilizing jQuery, a captivating slideshow showcasing a series of images elegantly emerges from the center-right of

I've been working on creating a slideshow of images and I came across this tutorial: After downloading the zip file and getting started, everything was working fine. The only issue I encountered was trying to position those four dots at the bottom ri ...

Unusual scrolling behavior in iOS 7 when using PhoneGap and jQuery Mobile

I am currently working on a simple app that includes a list of elements within a page. I am utilizing PhoneGap in conjunction with jQueryMobile for this project. After testing both the jQuery Mobile Listview and a custom list implementation, I have encount ...

What is the best way to create an empty grid-template-row that can expand to occupy the remaining space?

Is there a way to design a grid layout with 3 rows that will take up the entire screen, even if some of the rows are empty? Let's say the header and footer are 100px and 50px respectively. The main area should occupy the rest of the screen. .grid ...

Steps to insert a new row for a grid item using material-ui

When it comes to breaking a line of grid item like this, the image shown below illustrates how the rest space of the grid should be empty. https://i.stack.imgur.com/pvSwo.png <Grid container spacing={3} <Grid item xs={12}> "Grid Item xs ...