Looking for some CSS guidance. I want to ensure that all the elements remain proportional to each other when the screen size changes

Trying to create CSS art for an eye, but struggling with keeping all parts of the eye together when the screen size changes. The pupil seems to move around a lot. Any suggestions?

Having trouble submitting due to code/comment ratio, hopefully it works now.

body {
  background-color: grey;
}

p {
  top: 50%;
  position: relative;
}

.eye-outline {
  width: 600px;
  height: 350px;
  background-color: black;
  border-radius: 50%;
  position: relative;
}

.eye-white {
  width: 580px;
  height: 330px;
  background-color: white;
  border-radius: 50%;
  position: relative;
  top: 3%;
  left: 1.8%;
}

.iris-outline {
  width: 325px;
  height: 325px;
  border-radius: 50%;
  background-color: black;
  position: relative;
  left: 23%;
  bottom: 97%;
}

.iris {
  width: 300px;
  height: 300px;
  background-color: blue;
  bottom: 186.5%;
  left: 25%;
  border-radius: 50%;
  position: relative;
}

.pupil {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background-color: black;
  left: 20%;
  top: -20%;
  position: relative;
}
Back

Eye

Answer №1

Your code has been refactored to eliminate repeated code and remove hard-coded left and top values, resulting in automatic centering.

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

For a responsive example, refer to the following snippet rather than the one below.

body {
  background-color: grey;
}

.eye-outline,
.eye-white,
.iris,
.pupil,
.iris-outline {
  border-radius: 50%;
}

.eye-outline {
  position: relative;
}


/* The key aspect is centered alignment */

.eye-white,
.iris,
.pupil,
.iris-outline {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.eye-outline,
.iris-outline,
.pupil {
  background-color: black;
}

.eye-outline {
  width: 600px;
  height: 350px;
}

.eye-white {
  width: 580px;
  height: 330px;
  background-color: white;
}

.iris-outline {
  width: 325px;
  height: 325px;
}

.iris {
  width: 300px;
  height: 300px;
  background-color: blue;
}

.pupil {
  width: 150px;
  height: 150px;
}


/* Debugging styles only */

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  margin: 0;
}
<div class="eye-outline">
  <div class="eye-white"></div>
  <div class="iris-outline"></div>
  <div class="iris"></div>
  <div class="pupil"></div>
</div>


Responsive Example

I have converted the dimensions from px to vmin, ensuring full responsiveness!

body {
  background-color: grey;
}

.eye-outline,
.eye-white,
.iris,
.pupil,
.iris-outline {
  border-radius: 50%;
}

.eye-outline {
  position: relative;
}


/* The key aspect is centered alignment */

.eye-white,
.iris,
.pupil,
.iris-outline {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.eye-outline,
.iris-outline,
.pupil {
  background-color: black;
}

.eye-outline {
  width: 80vmin;
  height: 55vmin;
}

.eye-white {
  width: 78vmin;
  height: 53vmin;
  background-color: white;
}

.iris-outline {
  width: 52.5vmin;
  height: 52.5vmin;
}

.iris {
  width: 50vmin;
  height: 50vmin;
  background-color: blue;
}

.pupil {
  width: 35vmin;
  height: 35vmin;
}


/* Debugging styles only */

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  margin: 0;
}
<div class="eye-outline">
  <div class="eye-white"></div>
  <div class="iris-outline"></div>
  <div class="iris"></div>
  <div class="pupil"></div>
</div>

Mobile View:

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

Small Height Device View: https://i.sstatic.net/ESK2Y.png

Desktop View:

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

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

Having a fixed footer in JQuery mobile does not adjust its position downwards as intended

I attempted to move the footer down using the provided code below, but it doesn't seem to be working. I even went ahead and removed all standard text from the CSS file to eliminate any potential issues. It should normally shift downward with <div ...

What is the best way to access a local variable within a POST method in Node.js?

Below are some of the modules that I have utilized: const mysql = require('mysql'); const express = require('express'); const session = require('express-session'); const path = require('path'); const bodyParser = req ...

Ways to position a dropdown submenu on the left-hand side in Bootstrap 4

I'm currently utilizing Bootstrap 4 with a multilevel dropdown feature as shown in the image below: https://i.sstatic.net/xgVLp.png My goal is to shift the submenus to the left side, in the opposite direction from how they are currently displayed. De ...

Exploring Molecular Structures with ThreeJS - Understanding the Mechanics of Double Bonds

Currently working with threejs and experimenting with the example found in this link: The challenge I'm facing is related to creating double bonds between grey and red spheres in the example. While I came across some resources suggesting ways to achi ...

Not all the divs nested within the main div are having the class applied to them

Here is the HTML code I have: <?php require_once("../RMS/php/component.php") ?> <!DOCTYPE html> <html lang="en"> <head> <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-prot ...

The JavaScript program for the shopping list is experiencing issues with the formatting of list items

I have been working on developing a shopping list program using JavaScript. The program includes an input box and an "add item" button, which adds the text entered in the input field to an unordered list as a list item. Each list item also contains an imag ...

Personalized design created using v-for

I have an array being passed through v-for and I want to use the values within the "style" attribute. Essentially, I need to append the value from v-for to style:"left"+EachValue+"px", but I'm having trouble with the syntax. I'm unsure if this ap ...

IE6/7 dilemma with CSS floating

Encountering a strange CSS float issue in IE6 and IE7. The HTML code looks like this: <fieldset style="float:left"> <legend>Summary</legend> <div class="display-label">Recruitment type</div> <div class="displa ...

What is the most effective method for creating a central file containing multiple subdomain pages?

I am working with a file named master.php, containing a code snippet that retrieves the city and state from the subdomain and inserts it into the body. $url = "//$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $parsedUrl = parse_url($url); $host = explode(&apo ...

Tips for creating a function to assign a CSS width using a JavaScript variable?

Here is the code snippet I have been working on: ...<script> function adjustElementSize() { var newSize = window.innerWidth - 600; document.getElementById("spin").style.width = newSize + "px"; document.getElementById("spin").style.height = newSize + ...

Transforming jQuery Object into a String after making an AJAX request

If I were to submit a form with some text in the value of user_input, let's say "I am free," through AJAX, and it comes back to me as a string. Once it becomes an Object, how could I convert it back into a string format? Thanks, <!DOCTYPE HTML> ...

Quickly retrieve the chosen option from a dropdown menu (XHTML Select element)

Scenario In a drop-down menu built with XHTML Select element, there exists an extensive list of options to choose from. I am seeking a way to extract the chosen option using JavaScript. Dilemma My current method involves utilizing jQuery's :select ...

I'm having trouble getting the CSS to print properly, even after adding the media="print" attribute

Currently, I am working on creating a marksheet format with some CSS styles that need to be included in the print version as well. So far, I have been successful in achieving this goal. However, I have encountered an issue where the background color in t ...

Could you show me how the easyrtcid is generated in the demonstration of audio-only chat? I would appreciate a step-by

Currently, I am utilizing the easyrtc webpage to test out the audio only chat demo and everything seems to be running smoothly. However, when connecting, the easyrtcid variable is automatically assigned a random string. I was wondering if there is a way t ...

Rearrange the first division to the second division in a responsive manner

.box { display: block; width: 200px; height: 200px; padding: 20px; background: black; } .class1 { position: relative; color: red; } .class2 { position: relative; color: red; } <div class="box"> <div class="class1"> Th ...

The Bootstrap table spills over the edges of the container

I'm currently working on a project where I need to create a table with a grey background, but I am facing an issue where the lines from my table are extending past the div container. I even tried placing it inside a form, but that did not resolve the ...

Showing array within an object in HTML using Angular 7

I am trying to showcase an array within an object in HTML. The JSON data I have looks like this: I want to display the names of categories that are nested under Product > ProductCategoryRelations in a single string separated by commas, for example: "Ca ...

Backdrop in Bootstrap Modal Does Not Fully Cover Buttons and Input Fields

Currently, I am working with mysql and php on a user.php page that caters to user interactions. The main content in the center <div> is dynamically updated through php whenever a link on the same page is clicked. To elaborate, when a user clicks on a ...

What is the best way to utilize jQuery to send JSON data and then parse it on a subsequent HTML page via the

My goal is to send JSON data through the URL to the next HTML page. While testing it on an emulator for a mobile app, I encountered an issue where the URL crashed instead of redirecting to the next page. Can someone help me understand why this might be hap ...

JavaScript code not functioning properly to submit form

My journey with my first web application has hit a roadblock. I am attempting to use javascript to submit a form by clicking on text, but nothing happens when I click. All I want is for it to open a simple webpage at this stage. Although I have experience ...