Is there a straightforward method to make a <div>
with a relative width (like width: 50%) have the same height as its width without using JavaScript?
Is there a straightforward method to make a <div>
with a relative width (like width: 50%) have the same height as its width without using JavaScript?
Discover a clever trick I stumbled upon that makes it possible to maintain aspect ratio for an HTML element in a responsive design on this particular blog
#square {
width: 100%;
height: 0;
padding-bottom: 100%;
}
While the simple answer is no, it's not entirely feasible. The longer explanation is that under certain conditions and compromises (like additional html markup and restricted functionalities), it can be achieved to some extent.
If you follow this CSS styling:
.square {
position: relative;
margin: 20px;
display: inline-block; /* could be float */
overflow: auto; /* UPDATE: if content may overflow square */
}
.sq-setter-w {
width: 100%;
height: auto;
visibility: hidden;
}
.sq-setter-h {
width: auto;
height: 100%;
visibility: hidden;
}
.sq-content {
position: absolute;
z-index: 1;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
And use this HTML structure:
<div class="square" style="width: 200px">
<img src="http://dummyimage.com/50x50/000/fff.gif&text=50x50" class="sq-setter-w"/>
<div class="sq-content">Here is content</div>
</div>
<div class="square" style="height: 100px">
<img src="http://dummyimage.com/50x50/000/fff.gif&text=50x50" class="sq-setter-h"/>
<div class="sq-content">Here is content</div>
</div>
<div class="extrawrapper">
<div class="square" style="width: 200px">
<img src="http://dummyimage.com/50x50/000/fff.gif&text=50x50" class="sq-setter-w"/>
<div class="sq-content">Here is content</div>
</div>
</div>
<div class="extrawrapper">
<div class="square" style="height: 100px">
<img src="http://dummyimage.com/50x50/000/fff.gif&text=50x50" class="sq-setter-h"/>
<div class="sq-content">Here is content</div>
</div>
</div>
The main components to make this work are:
img
element can proportionally size based on the image itself.img
eliminates the need for a class with a percentage value. In my example, assume the size is set on the wrapper div (.square
).display: inline-block
or float to the div (as in the provided css).Note that this solution involves considerable extra mark-up. While using JavaScript may be simpler in many cases, I wanted to demonstrate that, to some extent, achieving proportional resizing using only HTML and CSS is possible.
For an example where a percentage determines size, refer to this fiddle. Here is the corresponding html code with width set to 30%:
<div class="square" style="width: 30%">
<img src="http://dummyimage.com/50x50/000/fff.gif&text=50x50" class="sq-setter-w"/>
<div class="sq-content">Here is content</div>
</div>
Today, I was looking to achieve a similar effect and thought of using an image. However, I decided to explore other options and ended up finding this solution through Google.
Interestingly, you can create a square without the need for an image src
by using a hash
, thus saving a http request.
For different aspect ratios, it's recommended to use a src
. For example, for a ratio of 16:9, the image should be 16px
wide and 9px
high (as small as possible).
img
vs. padding-bottom
You can see a comparison between the two techniques in this fiddle. The img
version is more versatile and can handle pixel values smoothly.
In cases where percentage values are used, both techniques yield similar results. However, the padding method eliminates the need for extra markup (<img src="#"/>
).
Each technique has its own advantages and disadvantages depending on the implementation requirements.
<div class="floater">
<div class="square square_noImg">
<div class="inner">Hey blue you look totally squared</div>
</div>
<div class="square square_img">
<img src="#"/>
<div class="inner">Hey red you seem off</div>
</div>
</div>
.floater {
font-size: 0;
}
.square {
position: relative;
display: inline-block;
width: 100px;
margin: 0 5px;
}
.square_noImg {
padding-bottom: 100px;
background: red;
}
.square_img {
background: blue;
}
img{
width: 100%;
height: auto;
border: 0;
visibility: hidden;
}
.inner {
position: absolute;
top: 10%;
right: 10%;
bottom: 10%;
left: 10%;
background: white;
font-size: 14px;
text-align: center;
overflow: hidden;
padding: 10px 5px;
}
For those looking for a pure JavaScript solution following @Dave's suggestion on @praveen-kumar's response:
function makeSquare(element) {
element.style.height = element.offsetWidth + 'px';
}
You can easily implement this function wherever needed:
makeSquare(document.querySelector('your-selector'));
When using relative units like %
, the size of an element will be calculated in relation to its container. However, you can create a square element by setting both dimensions using units such as px
or em
.
It's not possible to achieve this using just CSS. With the help of jQuery, you can do the following:
var childWidth = $('.child').width();
$('.child').css({'height': childWidth + 'px'});
View a demonstration at http://jsfiddle.net/4Jnfq/
An alternative solution using only CSS can be found here in the "Resize with content" section.
Even though it was originally intended for circles, you can eliminate the border-radius: 50%
to adapt it for squares as well.
I have incorporated the Bootstrap 5 carousel with a total of 9 items The first item is a YouTube video, while the other 7 items are empty Divs with background-images The issue arises where only the first three items seem to be working, and only three ind ...
Is there a way to configure a web component in vue.config.js and separate the iconfont.css section when building? I am using the vue-cli-service build --target wc-async --name chat-ui src/views/Chat/Launcher.vue --report command to package the web compon ...
While working on a template with nested rows and columns in Bootstrap, the layout ended up looking like this: <div class="col-5"> <div class="row"> <div class="col-3 border border-secondary">Truck Number</div> ...
Recently, I've delved into React and have been experimenting with creating a Modal component using styled components. My goal is to achieve a similar effect to Framer Motion's motion.div, but utilizing styled components instead. Currently, I hav ...
Several months ago, I successfully implemented a cool effect on a Shopify website that I had designed. However, recently the client decided to switch to a new theme without my knowledge, and now one of my CSS tricks is no longer working. If you look at t ...
I need assistance with a straightforward task - creating a button that remains centered at all times. What would be the most effective method to achieve this, and is it possible to do so easily? Currently, I have managed to create a blue button; however, ...
In the div, there are elements like h1, p, and a. The goal is to have the h1 at the top, the a at the bottom, and the p in the middle regardless of its height. Check out the jsfiddle for a live demo. Here is the source code: HTML <div class="box"> ...
While developing a website, I noticed that the virtual keyboard fails to appear when an input field gains focus during the page load process. As per Apple's policy restrictions, this functionality is not allowed. However, I am interested in finding a ...
I am facing an issue with resizing window size. The image on the page is not scaling along with it and I don't want the image to distort when I resize the window. My goal is to keep the center of the image always in the middle of the window. On smalle ...
I have a React form that I need help with. The issue is that I want to reduce the space between the list icon and the label. Here is the CSS I am using: .form__container { display: flex; flex-wrap: wrap; } .form__container input { color: rgb(115, 0, ...
I need help with a specific pseudo-class selector issue. Even with !important, the text in the li:first-child element is not appearing in blue. The selector specificity for p is: (0, 0, 1) The selector specificity for li:first-child is: (0, 1, 1) I want ...
The text I added to a website appears perfectly aligned on Chrome and Explorer, but on Safari, it's positioned below the logo. How can I resolve this issue without affecting the view in other browsers? The desired layout should show as "LOGO | TEXT," ...
I am facing an issue with the FAB button and 3 Icons. The functionality is working fine on click for both show and hide actions, but the transition is too delayed. I want the icons to appear step by step, even though I have adjusted the transition delay se ...
I have a plethora of colors stored in JSON format. I am utilizing rootbeer with gulp to convert them into Sass maps for processing by SCSS: { "blue": "33A2FF" } into $colors: ( "blue": "33A2FF" ); I am able to use ...
In my Bootstrap 5 layout, I have a fixed-width left sidebar and a responsive table on the right. The sidebar is set to be hidden at smaller screen sizes using d-xl-block. .body { width: auto; } .side { background:gr ...
Hey there, I've been building a website that I'm confident is correct, but there seems to be an error preventing the layout from displaying correctly. Can anyone offer assistance in resolving this issue? ...
I need each this.state.title to be aligned based on a different classname. I attempted using css flex boxes/nth-of-type/nth-child, but it didn't work well with React. I'm utilizing this.state to access my objects. My failed strategy render: f ...
I'm struggling to ensure that long lines of text break and wrap correctly within a chat feature I am developing. The crucial elements in question are .chat__messages__item and .chat__messages__body (these are all contained within a parent element set ...
I encountered an issue while converting a component to mui 5. Here is the original code snippet: const useStyles = makeStyles({ imageContainer: { display: "flex", width: "65%", float: "left", marginRight ...
Use this URL, you can unparse it online and beautify. It is working on dev but not after a build. If you have any ideas, please let me know. Thank you. Background image: URL("data:image/svg+xml, etc."); ...