arrange a div inside another div

I'm struggling to grasp the concept of how divs function in HTML. Below is a snippet of my HTML code:

<div id="user_p">
    <img src="img/pp/djbaptou.jpg">
    <div class="followings"> djbaptou </br> Baptiste Arnaud </br> Description: Salut j'aime les carottes lol
        <a href="#">Modifier profil</a>
    </div>
    <div class="followings"> Suivi de: </br> 200 </br> personnes</div>
    <div class="followings"> Suivi par: </br> 300 </br> personnes</div>
</div>

Additionally, here is the corresponding CSS stylesheet:

#user_p{
    background-color: red;
    width: 60%;
    height: 200px;
    margin: auto;
}

#user_p img{
    display: inline-block;
    width: 200px;
    height: 200px;
}

.followings{
    font-family: "Roboto-Light", Helvetica, Arial, sans-serif;
    width: 200px;
    display: inline-block;
    background-color: green;
    margin-left: 60px;
    text-align: center;
}

How can I achieve a display like this?

https://i.stack.imgur.com/vQO3L.jpg

I want to center my green divs.

When resizing the window, it looks like this:

https://i.stack.imgur.com/oYadT.jpg

I just need the green divs to remain within the red one...

Any ideas?

Answer №1

To achieve vertical centering, consider using flexbox by setting #user_p as a flex container with display: flex; and using align-items: center. Remove the inline-block display values of child elements for better alignment. For responsiveness, utilize media queries to adjust the flex-direction value to column on smaller screens and row on larger screens.

#user_p{
    background-color: red;
    width: 60%;
    height: 200px;
    margin: auto;
    display: flex;
    align-items: center;

}

#user_p img{
    width: 200px;
    height: 200px;
}

.followings{
    font-family: "Roboto-Light", Helvetica, Arial, sans-serif;
    width: 200px;
    background-color: green;
    margin-left: 60px;
    text-align: center;
}

Answer №2

Here is my very first response, and I truly hope it proves to be helpful for you. This is a breakdown of how I modified the style sheet to align with your specific needs:

        #user_p {
        background-color: red;
        display:inline-block;
        padding-right:100px;
        margin: auto;
    }

        #user_p img {
            display: inline-block;
            width: 200px;
            height: 200px;
        }

    .followings {
        font-family: "Roboto-Light", Helvetica, Arial, sans-serif;
        width: 200px;
        display: inline-block;
        background-color: green;
        margin-left: 60px;
        text-align: center;
    }

First and foremost, ensure that your main div is set to display:inline-block;. This will allow its width to adjust automatically based on the elements within it. Since the green divs are also set to display:inline-block;, the red div will adjust in width accordingly. Make sure to disable the width property for the red div and use padding-right to offset the rightmost green div.

Consider using media queries? Based on my HTML experience, if the website is designed well from the start, even without media queries, it should remain functional when scaled down to the bare minimum. While it may not look as polished as with media queries, everything should still be visible.

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

Retrieve the data file using input text with jQuery

Is there a way to extract data like this? data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9UeXBlIC9DYXR…AKdHJhaWxlcgo8PC9TaXplIDE4Ci9Sb290IDEgMCBSPj4Kc3RhcnR4cmVmCjg4MzEzCiUlRU9G from an input file? $('#file').change(functio ...

Tips for dynamically retrieving a CSS file for an HTML page

With multiple html pages containing the same navbar, I made the decision to place the navbar in its own file called navbar.html and use ajax with jquery.get() to dynamically load it onto each page. This prevents redundant code across all pages. Currently, ...

Modifying the Color of Individual Items within the Pagination Component in MUI

I am attempting to modify the background color of every item in my Pagination component by utilizing makeStyles. import { Pagination } from "@material-ui/lab"; import { makeStyles } from "@material-ui/core"; const pagination = makeStyl ...

Struggling to make the fgetcsv file function seamlessly with my MySQL database

I have been attempting to use the fgetcsv function to import all rows of a CSV file into my database. This functionality should be straightforward, but for some reason it is not working as expected. Below is the code I am using: <?php $file = $_FILES[ ...

Close the Bootstrap modal upon clicking the "Ok" button in SweetAlert2

I need help with closing my Modal Bootstrap after clicking the Ok button in SweetAlert. I've attempted various methods, but none of them have worked so far. I'm not sure if I'm implementing it correctly, but I've utilized JQuery for thi ...

Unexpected behavior with first-child selector in a simple CSS code

It seemed so clear to me, but it turns out I was mistaken. Check out this link $('p:first-child').css({color:'red'}); The color of all text is not being altered. <div class="post text"> <a href="#">this is a url</a> ...

When a table's row is clicked, it will expand to reveal a secondary table

I am facing an issue with rendering a table using v-for in Vue. I want to display a subtable or more information section below a specific row when it is clicked on. My attempts to add a new tr within the existing tr have resulted in errors or the new row ...

Chrome causing the vanishing act of floated iframes

I encountered an unexpected issue on my website recently. For the past three years, everything was working smoothly until I updated Chrome to version 60. Now, whenever I try to load iframes from Archive.org, they momentarily appear and then vanish instantl ...

What is the best way to disable the click function for <a> tags that have a specific class?

I am dealing with parent navigation items that have children, and I want to prevent the parent items from being clickable. Here is an example of how they currently look: <a href="parent">Parent Item</a> Is there a way to select the <a> ...

The function send_filer either returns None or finishes execution without encountering a return statement

I am currently developing an application where users can download a plot.png and a file.csv, but the send_files function isn't working as expected. Below is my Python code: app = Flask(__name__) app.USE_X_SENDFILE = True @app.route('/', met ...

Alter the typography of the text that has been enhanced by Google

I am attempting to modify the default font of certain text that is processed through google-code-prettify within an angular directive. The structure of the template served by my directive appears as follows: <pre class= "prettyprint" style= "border:1p ...

I've encountered a peculiar error that is new to me while using bootstrap and javascript. Can anyone shed light on what this error signifies?

Hey there! I've come across this error in the console while attempting to utilize Bootstrap. It seems that a style is being refused from 'http://127.0.0.1:5500/node_modules/font-awesome/css/font-awesome.min.css' due to its MIME type ('t ...

What is the most effective way to transfer a value from the server to a React user interface and incorporate it into the styling of the user interface?

Currently, I am utilizing Python as the backend for my React frontend. The backend is passing in values such as 17 or 87, and I aim to use these values as a percentage on the scale that I am constructing. Currently, the scale starts in the center: https:/ ...

Can you modify a specific column in a table using mat-table in Angular material?

For my project, I am utilizing Angular Material's table to present data in a tabular format. However, due to a new requirement, I now need to enable in-line editing for the last 2 columns alongside highlighting another column when the user clicks on t ...

What is the best way to use jQuery to attach functions to every input element?

Imagine you have a set of HTML markup and CSS like this: #CSS .inputhelp_text { background: #000; color: #fff; } .nodisplay { display: none; } <input class="inputhelp" id="firstname" /><span class="inputhelp_text nodisplay" id="help_firstname"& ...

Tips for incorporating unique images into each individual flex box

I am new to the world of coding, having just embarked on this journey less than a week ago. Following a tutorial by a developer on YouTube, I tried to add my own twist to the project in order to differentiate it from the original. However, I've hit a ...

Exemplary CSS Text Alignment When Vertical Align is Exceptional

I'm currently working on a menu where some text needs to be aligned vertically with the property vertical-align:super. However, I am facing an issue where this particular item with "super" text is not aligning properly with the other items. Below is ...

Transformation of looks post a refresh

Initially, the CSS and appearance of the page look fine when I first open it (after clearing the cache). However, upon refreshing the page, a part of it changes (specifically, the padding direction of a div). This change occurs consistently with each refre ...

Tips for ensuring an animation is triggered only after Angular has fully initialized

Within this demonstration, the use of the dashOffset property initiates the animation for the dash-offset. For instance, upon entering a new percentage in the input field, the animation is activated. The code responsible for updating the dashOffset state ...

What is the best way to align an InputAdornment IconButton with an OutlinedInput in Material-UI?

Struggling to replicate a text input from a mockup using Material-UI components. Initially tried rendering a button next to the input, but it didn't match. Moving on to InputAdornments, getting closer but can't get the button flush with the input ...