Is there a way to align the top offset of a div with an outer div to match the side offset of the same outer div using just CSS?

Let me show you a visual representation of what I'm trying to achieve for better understanding. There's an outer div that occupies 33% of the page and inside it, there's an inner div with a width of 50% in relation to the outer div. The outer div is styled with "text-align: center;" so that there is a 25% space on each side of the inner div. My goal is to align the inner div from the top of the page so that it has the same distance from the top as it does from the sides. Many suggestions I found online recommend using jquery for this, but I'd prefer a CSS-only solution if possible. How can I accomplish this?

I apologize if this seems like a basic question. I am relatively new to frontend development and haven't come across a way to set CSS attributes equal to other attribute values.

Any assistance would be greatly appreciated!

The CSS:

    .side{
        background-color: #ECEFF1;
        height: 100%;
        width: 33%;
        text-align: center;

    }

    .profpic{
        width: 50%;
        border-radius: 50%;
        position: relative;

    }

The Html:

        <div class="side mdl-shadow--4dp" >
            <image class="profpic mdl-shadow--4dp" src="/profpic.jpg"></image>

        </div>
        <div class="content">

        </div>

Answer №1

To easily center an image, you can use the following two CSS rules: padding: 25% and width: 50%. This will ensure that the image is centered equally on all sides. Check out the code snippet below for reference:

HTML

<div id="side">
    <image src="http://placehold.it/120x120&text=image1" id="box">
</div>

CSS

#side {
    width: 33.33%;
    height: 1000px;
    background-color: #474747;
}

#box {
    width: 50%;
    padding: 25%;
}

You can also view a demo on JSFIDDLE.


EDIT

In relation to the original question, here is how the OP's CSS would look like:

.side{
    background-color: #ECEFF1;
    height: 100%;
    width: 33%;
}

.profpic{
    width: 50%;
    border-radius: 50%;
    padding: 25%;
}

Answer №2

By utilizing CSS3, you have the option to utilize the units vw and vh to accomplish this design.

vh corresponds to the height of the viewport, while vw pertains to its width.

In your scenario, the outer div occupies 33% of the width, with the inner box taking up 50% of that amount. Consequently, the margins on the left and right will be approximately 25% of the outer div's width.

To determine the top margin, you need to calculate 33% * 25% = 8.25% of the vw unit:

<div id="outer">
    <div id="inner">
        lorem ipsum
    </div>
</div>

#outer{
  background-color:red;    
  width:33vw;  
  margin:0;
  padding-top:1px;
  height:100vh;
}

#inner{
    background-color:blue;
    width:50%;
    margin: 0 auto;
    margin-top:8.25vw; // equivalent to 25% of 33% width
    padding-top:1px;
}

http://jsfiddle.net/xaLc4zd2/

Adjust the window size to observe how the inner div remains proportionally positioned.

Answer №3

Here's a helpful tip for your project. Make sure to set the box-sizing property on the 33% container and add a 25% margin to the element inside the container.

<div class="box">
  <div class="innerbox"></div>
</div>
<div class="box">
  <div class="innerbox"></div>
</div>
<div class="box">
  <div class="innerbox"></div>
</div>

.box{
  width: 33%;
  min-height:400px;
  background-color: #777;
  display: inline-block;
  box-sizing: border-box;
}

.innerbox{
  width:50%;
  margin:25%;
  min-height:200px;
  background-color: #444;
}

Take a look at this codepen example.

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

Can you affix a tag to the picture?

Currently, I have a static page located at . However, I am planning to dynamically generate this page by retrieving data from a database. My challenge lies in assigning a position ID obtained from the database to each of the tyres on the page. Any sugges ...

When using the keyboard to navigate, the aria-label attribute is not being properly recognized

<span class="phone" aria-label="phone number 9 4 9 . 5 5 5 . 1 2 3 4">949.555.1234</span> This code snippet displays a phone number on the webpage. When clicking on the phone number, it reads the aria-label as intended. However, if you navigat ...

How to effectively manage an overflowing navigation bar

I am currently facing a challenge with the main navigation bar on my website, specifically with how it handles an overflow of links on smaller screen resolutions. I have attempted to adjust the height of the navigation bar at smaller media queries, but I&a ...

From jQuery integration to Wordpress - Frustrating uncaught errors are making me lose

I recently completed a small application on Codepen and decided to transfer it to my Wordpress website. After updating the necessary elements, such as replacing $ with jQuery and adjusting the JSON file location, I encountered some issues in the console. ...

Javascript: Harnessing Textbox Arrays for Improved Functionality

Check out the form displayed below. <form id="upload_form" enctype="multipart/form-data" method="post"> <input type="text" name="name[]" id="name"><br> <input type="text" name="name[]" id="name"><br> <input type="fil ...

Having trouble with CSS Locator identifying an element in your Protractor Test?

In the Protractor test snippet below, I am attempting to calculate the quantity of div elements using the CSS locator: let list = element.all(by.css('div[class="ag-header-cell ag-header-cell-sortable"]')); expect(list.count()).toBe(11); ...

Troubleshooting Problems with jQuery, Ajax, and iOS

Has anyone encountered any issues specifically with using jQuery, Ajax events, and iOS? I work as a web analyst and we are facing problems with iOS during the checkout process on our website. We rely on jQuery and Ajax events to manage the shopping basket ...

Fill in a URL using the information provided in the boxes and navigate to the website

My colleagues and I access the following link multiple times a day, clicking through several pages to reach this specific URL: http://eharita.mamak.bel.tr/imararsiv/test.aspx?f_ada=36391&f_parsel=4 I am looking to create an HTML page with two input b ...

Tips for aligning a horizontal list in the center when the viewport size is reduced?

My centered list looks great on larger screens, but when the screen size decreases, the extra items shift to the left instead of staying centered. I've tried a couple of different methods to fix this issue, but nothing has worked as desired. https:// ...

Tips on maintaining and hiding the vertical scrollbar when a popup is open, alongside the navigation bar positioned at the top of the page

After reviewing this pen, my goal is to create a popup that maintains access to the navigation bar (hence avoiding Bootstrap's Modal). However, I am facing the challenge of keeping the scrollbar visible while preventing scrolling in the background whe ...

From plain to vibrant: Using CSS gradients for background design

I have customized a button by adding a gradient to it. Below is the code snippet: .mybutton { background: #258dc8; /* Old browsers */ background: -moz-linear-gradient(top, #258dc8 0%, #258dc8 100%); /* FF3.6+ */ background: -webkit-gradient( ...

Interactive button with jQuery that toggles between clicked and unclicked states, automatically reverting to unclicked when clicked elsewhere on the page

I am trying to create a button that changes its appearance when clicked, and returns to its normal state if clicked again or if the user clicks outside of it. Here is the JavaScript code I have written: if($('#taskbar-start').hasClass('ta ...

What is the best way to restrict ngFor loop in Angular?

<tr *ngFor="let post of posts.slice(currentPage*10, (currentPage+1)*10)"> <td>{{post.kind}}</td> <td>{{post.title}}</td> <td>{{post.author}}</td> <td>{{post.publisher}}</td> <td& ...

Adjust the size of the font to suit your needs

Instead of changing the font-size, I'm interested in altering two distinct properties based on the text element's width and height. For example, if I were to use font-width: 50% on this particular element: https://i.sstatic.net/K9u5I.png The t ...

Concealing a div element in JSP loop depending on a certain condition

Currently, I am working on jsp for loops with a specific requirement. The task involves looping through a list and displaying data for each iteration using an html div tag with a designated class name. However, there is a condition that if met during the l ...

Converting an HTML div to a string for export

In the process of developing my application, I am utilizing an angular widget to construct a dynamic dashboard. Here is the snippet of HTML code along with the associated JavaScript that enables this functionality: <div class="page" gridster="gridsterO ...

Bootstrap 4 - The Mystery of 'col-sm-auto' Taking Precedence Over 'col-md-4'

I am currently learning Bootstrap and have encountered a challenge that I can't seem to figure out. Let's take a look at a snippet of the code: <button className=" col-md-4 col-sm-auto btn btn-info ...

Chrome bug where clicking a hover effect does not work

When utilizing the :hover-attributes and selecting (left mouse button held) a cell while moving the cursor across other rows, only the initially clicked row will be highlighted on hover: Check out the code here table { border-collapse: collapse; } ...

Switching Present Tab in Rails

Within my application, I have a set of tabs displayed at the top thanks to a general layout in application.html.erb. Here's how they are structured: <li class="current"><%= link_to "Home", provider_path(current_user.id), :method=> "GET"%& ...

Attempting to get a webGL game downloaded from Unity to automatically enter fullscreen mode

Can someone help me with my Unity webGL game issue? I downloaded it from the internet, but I'm not sure what version of Unity was used to create it. When the game starts playing, it displays in a small canvas along with other elements like the Unity ...