Position the component at the center of another component

Looking to center a component inside another component. I have a div with two components - the main one and a banner that I want to display in the center of the main component. Check out my issue on this jsfiddle:

https://jsfiddle.net/CanadianDevGuy/vjb4k9un/38/

<div id="app">
  <first></first>
  <second></second>
</div>

Picture showing current layout

The main div with white border needs to align exactly with the first component (black one) without any space at the bottom.

I also added 'display: relative' because I need the red component to follow the black one while remaining centered.

Answer №1

SOLUTION

If you want the child element to match the dimensions of its parent, you can achieve this by first setting the width and height of the parent element, then making the child element occupy the full width and height inside the parent.

#container {
    border: 3px solid black;
    width: 300px;
    height: 300px;
}
#child {
    background-color: blue;
    color: white;
    width: 100%;
    height: 100%;
}
#inner-child {
    position: absolute;
    top: 50px;
    left: 50px;
    background-color: green;
    color: white;
    width: 50px;
    height: 50px;
}

Answer №2

I attempted to fulfill the requirements by adjusting the height of the parent element.

You can view the Vue implementation of this solution here https://jsfiddle.net/9d0vhs3y/

Below is the code snippet demonstrating the pure HTML/CSS version:

html {
  height: 100%;
}

#app {
  border: 4px solid white;
  position: absolute;
  height: 300px;
}

body {
  background-color: grey;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#element {
  background-color: black;
  color: white;
  width: 250px;
  height: 100%;
  position: relative;
  top: 0;
}

#element2 {
  position: relative;
  bottom: calc(50% + 25px);
  background-color: red;
  border-radius: 10px;
  color: white;
  height: 50px;
}
<div id="app">
  <div id="element">First</div>
  <div id="element2">Second</div>
</div>

Answer №3

Here is some CSS for you to test out:

html {
  height: 100%;
}

#container {
  border: 2px solid black;
  height: 200px;
  width: 150px;
}

body {
  background-color: lightblue;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#box1 {
  background-color: yellow;
  color: black;
  width: inherit;
  height: inherit;
}

#box2 {
    position: relative;
  top: 50%;
  width: inherit;
  background-color: green;
  border-radius:5px;
  color: white;
  height: 30px;
}

Answer №4

Adjust the height of #app to 250px so that extra space at the bottom is eliminated and the second component can be centered properly.

[insert image description here][1]

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

What is the best way to capture a screenshot using selenium in conjunction with synchronous JavaScript?

Currently, I am developing an automated test using javaScript and leveraging a node library called webdriver-sync. This library simplifies writing selenium tests by eliminating the need for callbacks and promises, and it utilizes the java Webdriver API. Su ...

Juggling numerous pages in a React application

Our application is primarily flask/python based, with flask handling everything from user sessions to URLs. We are currently in the process of developing a UI component for our application using reactjs. We have successfully built the react bundle (bundle ...

Incorporating S3 images into vanilla JavaScript within a Django storages environment

I am facing a simple issue that I cannot seem to resolve. I have configured Django storages to serve static files from S3. In my template, I define the image source like this: "{% static 'fun_share/img/logo/logo.svg' %}" with STATIC_UR ...

The error "navigator.permissions.query is not a defined object" is encountered in the evaluation

Whenever I try to access my website on an iPhone 7, I encounter a frustrating error. The main screen loads without any issues, but as soon as I click on something, a white bank screen appears. I believe this piece of code might be the cause: useEffect( ...

Observing changes in the dimensions of flex children using MutationObserver

Considering the following structure of the Document Object Model... <div id="outer-container"> <div class="side-panel"><div width="200px"/></div> <div id="content"><!-- some content --></div> <div class=" ...

What is the method for implementing tooltips using JQuery?

AJAX call returns data for dropdown lists 'list1' and 'list2'. If there is no data available, I want to hide the dropdowns with `pointer-events: none` and display a tooltip that says "Data not available". else if (monthly_list.lengt ...

Numerous buttons activating a single modal component

I have an array called videos, which contains objects of the type Video (defined below). My goal is to create a functionality where clicking on a specific video button opens a modal containing information about that particular video. interface VideosInfo ...

creating a distinct angular service for every controller

Lately, I've been utilizing Angular services to store my commonly used codes that are required across different parts of my project. However, I have encountered an issue where variables in the service are shared between controllers. This means that if ...

Loading page with asynchronous JavaScript requests

As I send my AJAX request just before the page loads and then call it on the same page afterwards, here is what it looks like: <input type="text" class="hidden" id="storeID" value="<?php echo $_GET['store']; ?>"> $(document).ready(fu ...

The most effective way to initiate an action following a popup

Here's a button that triggers the opening of a popup: <button type="button" id="btnBuscarCuenta" onClick="javascript:AbrirPopUpBusqueda('id_AyudaCuentas', 'pop_cuentas_contables.cfm','', '900px&a ...

Updating a particular element within nested arrays: best practices

I have developed a data table containing student records using material UI. Each row represents a different student array with a fee verification button. My goal is to change the fee status when the button is clicked for a specific student, but currently t ...

A tutorial on how to customize the hover effect for TableHead Column Identifiers in MaterialUI by adjusting

I'm struggling to customize the appearance of child th elements using the TableHead component from MaterialUI. While I've been successful in modifying various properties, I'm facing difficulty in changing the hover color. Below is the snipp ...

Dynamic, adaptable, and expandable tiles that effortlessly span the full width of the screen

Trying to design fluid and flexible "tiles" for a website, which should expand across 100% of the browser viewport. The goal is for them to adjust in size if necessary to fill any white space gaps left by unfitted tiles. Using a standard div tag with a mi ...

Tips for aligning pagination in the MUI v5 table component and creating a fixed column

I am currently experimenting with MUI table components and have created an example below with pagination. const MuiTable = () => { const [page, setPage] = useState(0); const [rowsPerPage, setRowsPerPage] = useState(5); const [data, setData] ...

React Material UI issue: You cannot render objects as a React child. If you intended to display a group of children, make sure to use an array instead

I am encountering an issue with the code provided below and despite trying various fixes, I am unable to resolve it. componentDidMount() { axios.get('http://localhost:8080/home') .then((response) => { this.setState({ ...

Tips for passing a parameter to getters in vue

I've implemented a getter in my application to verify the stock availability and amount of products in the cart. Below is the code for my getters.js: export const checkStock = (state, productID) => { let stockAvailable = true; state.cart.fo ...

Material UI fails to display any content

I attempted to integrate a navbar into my website, but React is not displaying anything. Even creating a new project did not resolve the issue. Additionally, Stack Overflow restricts posts that contain mostly code and I am unsure why. Here is my App.js im ...

The width of a div element seems to mimic the width of the entire page inexplic

There is a page containing various div elements. HTML: <div class="utilitiesHeader"> <div id="utilitiesLogo"></div> <div id="utilitiesFIO">Name of user</div> </div> <div class="utilitiesContent"> <d ...

Conceal a Component within an Embedded Frame

Hey there! I've been attempting to hide a header within an iframe, but it seems like my code isn't doing the trick. Could someone take a look and help me diagnose why the header is still visible? Thanks in advance! <iframe id="booking_iframe" ...

Guide on converting arrays and sending this information in Node.js

I managed to retrieve quiz data and now I want to enhance it by mapping each answer to its respective quiz. I have a feeling that I should use something like forEach.push, but I haven't quite figured out the exact method... const API_KEY="https: ...