The accepted solution wouldn’t work for me as I need a child element with display: inline-block
to be both horizontally and vertically centered within a 100% width parent.
I used Flexbox’s justify-content
and align-items
properties, which respectively allow you to center elements horizontally and vertically. By setting both to center
on the parent, the child element (or even multiple elements!) will be perfectly in the middle.
This solution does not require fixed width, which would have been unsuitable for me as my button’s text will change.
Here is a CodePen demo and a snippet of the relevant code below:
.parent { display: flex; justify-content: center; align-items: center; }
<div class="parent"> <a class="child" href="#0">Button</a> </div>
Related Posts:
- React.js inline style best practices [closed]
- What is a user agent stylesheet?
- multiple classes on single element html
- Making an iframe responsive
- Applying Comic Sans Ms font style
- Chrome give error **Failed to load resource: net::ERR_NAME_NOT_RESOLVED** while other browser load images
- How to change text transparency in HTML/CSS?
- How do I rotate text in css?
- Responsive Bootstrap Jumbotron Background Image
- CSS float left not working right
- What does “*” mean in CSS?
- Text not wrapping inside a div element
- How to make padding:auto work in CSS?
- How to vertically center inside the parent element with CSS?
- How can I horizontally align my divs?
- background-size: cover not working?
- CSS not working in Chrome
- Pure CSS to make font-size responsive based on dynamic amount of characters
- CSS – display: none; not working
- Is there an opposite to display:none?
- font-weight is not working properly?
- What would be the best method to code heading/title for ul or ol, Like we have caption in table?
- How to enqueue style before style.css
- wp_enqueue_scripts, wp_register_scripts, wp_print_scripts: i’m confused
- What is the correct way to enqueue multiple CSS files?
- Which Details Are Required For Style.css File Header?
- How to implement different color schemes in your themes
- @font-face doesn’t work
- wp_enqueue_script + how to load JS or CSS in one URL merge call?
- How to target first img in every wordpress post with CSS [closed]
- How to change Parent Themes in Child Theme CSS
- hSite has no css on mobile [closed]
- Set Image width=’x%’ (percentage width not pixel width) when inserted via the media manager?
- How to set different font family based on text language?
- Relative path in wp_register_style not working
- Enable specific CSS Code for Visitors and specific Roles
- WordPress on localhost working, when sharing IP to connect receive err_connection_refused on .js and .css files
- How to decrease picture size
- How to Add Custom CSS Buttons to WordPress as a Shortcode?
- Can’t remove WordPress post image height/width attributes?
- How do you customize the admin bar for buddypress?
- Load in wp admin assets from child theme
- Enqueue styles in new site editor in WordPress 5.9
- style.css doesn’t load on the home page when front-page.php activated (using stock theme)
- Two column Dropdown for WordPress Toolbar
- Code working just in specific time period
- Images loaded as background-image are shown but s aren’t
- Style WP_Debug?
- Is there a way to add additional CSS styles to the styles css file dynamically?
- Is it safe to use WordPress generated classes?
- CSS styling of native (inbuilt) wordpress thumbnails [closed]
- Search Bar in Thesis WordPress Site
- Add core style in theme
- How to sync the style in the visual editor with the style of a child theme?
- Update wordpress causes white space on admin
- Add Custom Alignment Option to WordPress Blocks/Gutenberg
- Responsive issue with secondary logo – not sticking in position [closed]
- How would one set or override a Fallback font for characters in second language?
- why has this element moved despite css being the same as other pages?
- Custom css author role
- Test wrapping cutting words in half on my WP site [closed]
- WordPress aggressively caching CSS
- Embed widget from external source (Zendesk)
- Custom CSS in Widget editor (Gutenberg) gets stripped out
- Custom Gutenberg color classes naming convention
- Save custom option in CSS file
- broken internal JavaScript and CSS files
- avada woocommerce product details in grid
- child-theme style not changing on localhost
- Modifications on css file are not visible
- How to display image on WordPress homepage
- My sites mobile view design is different when not logged in – how do I ensure it’s all the same?
- Trying to hide the contact form on a particular page
- Default Gutenberg CSS on frontend
- Two CSS files loading extremely slow
- How to adjust text’s width and put it at center of post (Visual) editor?
- different theme output for desktop and mobile
- Font size decreases when child theme is created from parent theme?
- Enqueue CSS and Script only if needeed
- Bootstrap News: Image Modifciation
- How to remove the Welcome watermark on this page? [closed]
- resize chat bar on mobile [closed]
- add css to only body text
- Improving the readibility of bright text on a bright background [closed]
- Trying to remove white space between nav bar and content?
- Colouring the text on the video
- Padding added in sidebar, not coming from my style sheet [closed]
- Bootsrtap.css overlap style.css in custom wordpress theme
- image background is cut [closed]
- How to change wordpress registration form submit button value?
- Move main menu to top of screen
- Changing from primary to a custom menu changes my CSS navigation layout
- Woocommerce align prices and texts [closed]
- Rollover button doesn’t appear in sidebar [closed]
- how to include other jquery file and css file in wordpress plugins
- How do I make my block-editor styles match my front-end styles?
- Dequeue classic-themes.min.css
- How can I add a section with a different background color?
- plugin not loading my css and js
- Best practice for custom primary color of pages