/    Sign up×
Community /Pin to ProfileBookmark

square with a width 100%

How can I make a square with a width of 100% of its parent?
What should I set the height to?

to post a comment
CSS

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 31.2019 — "of it's parent" - this makes it difficult. Otherwise an easy procedure would be to set width and height both to the same vw value.

Some days ago I found a different procedure which enables for creating a square inside a parent. However it's not straight forward at all:

https://jsfiddle.net/Sempervivum/ryj0c5u3/12/

The trick is using a container that has a padding-top of 100%. As the padding is always a percentage of the width, this makes the container a square.

There is one more disadvantage: The container consists of the padding only and no content can be written inside. To do the latter one needs to create another container.

All things considered this is not a neat procedure. One might consider to set the heigt by javascript.
Copy linkTweet thisAlerts:
@codyhillauthorAug 01.2019 — @Sempervivum#1607011 I also show this, but this doesn't allow for more squares in the same div. One else thing I can do is to set the width to 100% and height on auto and add an img with the height and object-fit cover. But I dont think this would work.
Copy linkTweet thisAlerts:
@SempervivumAug 01.2019 — More divs in a container would be possible but would be more confusing:

https://jsfiddle.net/Sempervivum/3mdvLtka/14/

I recommend using javascript instead to set the height.
Copy linkTweet thisAlerts:
@SempervivumAug 01.2019 — The solution described here is more simple by applying padding-bottom:

https://stackoverflow.com/questions/19068070/how-to-style-a-div-to-be-a-responsive-square

http://jsfiddle.net/Sempervivum/oxps9atd/13/
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...