Can I have a responsive image with a set height? - css

I working in a page builder
For a shop, I am creating. I can change the CSS which is great.
I’m struggling to get a responsive resize of the images in this 4 column row. Since the images are different heights I have to have to set a height and have responsive width. Is there any way to get it to scale correctly?
The width is auto and the height is a set height based on the size of the screen.
You can see that when I resize it separates from the box and then sometimes get squished.

object-fit property
I did your design by using display : flex; and object-fit : cover; properties. I think that this object-fit property directly on the image is the only lacking property to make your images still looking good despite the screen resolution.
Notice the use of object-position : center; which makes the resizing always axed on the center of the image.
index.html
<div class="foo">
<div class="bar">
<img src="https://picsum.photos/200/300" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/200" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/200/700" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/400" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
</div>
style.css
body {
background-color: #f7f7f7;
font-family: Arial, Helvetica, sans-serif;
}
.foo {
width: 100%;
display: flex;
gap: 20px;
}
.bar {
background-color: #ffffff;
display: flex;
flex-direction: column;
width: 100%;
border-radius: 15px;
overflow: hidden;
box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}
.bar > img {
width: 100%;
height: 200px;
object-fit: cover;
object-position: center;
}
h4 {
color:#9ccf74;
}
.bar > div {
padding: 10px;
height: 100px;
}

Related

lines with text around the Circle

How to make lines around the circle which holds text for each line. I don't want text to be in the pseudo class. Below is the required output Image
.
Below code is which I have tried from Paulie_D previous solutions
.outCircle {
width: 20px;
height: 20px;
background-color: lightblue;
position: relative;
border-radius: 50%;
margin: 100px auto;
}
.marker {
width: 50px;
height: 2px;
position: absolute;
top: 50%;
left: 50%;
background: linear-gradient(to right, black, black 25%, transparent 25%, transparent 75%, black 75%);
transform: translate(-50%, -50%);
}
.vert {
width: 2px;
height: 50px;
background: linear-gradient(to bottom, black, black 25%, transparent 25%, transparent 75%, red 75%);
transform: translate(-50%, -50%);
}
.angle-1 {
transform: translate(-50%, -50%) rotate(45deg);
}
.angle-2 {
transform: translate(-50%, -50%) rotate(-45deg);
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="outCircle">
<div class="inner">
<div class="marker horiz"></div>
<div class="marker vert"></div>
<div class="marker angle-1"></div>
<div class="marker angle-2"></div>
</div>
</div>
Unfortunately your question is not entirely clear, but my guess is that you want to create some info-graphic about seeds with text around a nice graphic.
Instead of focusing on showing you how to create bordered circles with CSS and place text around them, I removed the text from your original image and cropped it. Because, as I suggested in my comment to visit my answer on SO62815794, it made more sense to me to take that code and the cropped image and show you how to create a responsive info-graphic with Flexbox Layout and a media query.
If this is not what you meant, please modify your answer and be more specific about what you need. However, while it is fun to create a graphic with CSS, in terms of maintenance you would be much better off with a cropped image or SVG file (more graphical elements for creating custom shapes).
The Snippet
has two demos showing how to use Flexbox Layout combined with Custom Attributes to create responsively sizing 2D XxY grids with the cropped image in the center.
demo 1 is a fairly straightforward 3x3 flexbox grid with equally sized cells
demo 2 is more complicated and has a main 3x1 grid with nested 1x2, 1x3 and 1x2 child grids giving it a more 'circular feel' than demo 1
The snippet is heavily commented and should be self explanatory, but additionally:
I left your original .outCircle in the HTML, just disabled it.
the cropped image is 138x138px PNG file (11.4Kb), with a transparent background (alpha channel) so you can play with the info-graphic background-color (: White for now)
The #media considers anything smaller than 720px (W/H) to be a 'smartphone' and will neither show the image (or .outCircle) nor any grid layout.
All you need to do is some fine-tuning with spacing, colors and font-sizes.
Make sure to go 'Full page' on SO and resize the browser window.
/*************************************/
/* main page structure, MOBILE first */
/*************************************/
body { height: 100vh; background-color: White }
.main-structure {
display: flex; flex-flow: column wrap;
/* Center everything inside the element (vertical and horizontal) */
justify-content: center; align-content: center; align-items: center
/* add your specific mobile settings */
}
.main-structure .outCircle { display: none } /* don't show on mobile */
.main-structure img { display: none } /* ditto */
.main-structure ul { padding-right: 40px } /* [OPTIONAL] to make L/R padding equal */
/*************/
/* EYE-CANDY */
/*************/
[band*="header"] {
background-color: rgba(254,190,0,1); /* egg yolk-ish */
text-align: center
}
.main-structure .headline {
width: 100%; /* stretch to fill parent */
padding: .25em 1em; /* some inner spacing */
color: CornflowerBlue;
font-size: 1.25em; font-weight: bold;
}
.outCircle {
position: relative; /* creates a new 'stacking context' for child elements */
/* Center everything inside the element (vertical and horizontal) */
justify-content: center; align-content: center; align-items: center;
}
.inner {
position: absolute;
width : 1.25rem;
height: 1.25rem;
border-radius: 50%;
background-color: lightblue;
}
.marker {
position: absolute; /* direct positioning inside parent 'stacking context' */
width: 3rem; height: 0.125rem;
/* center element in parent */
top: 50%; left: 50%;
transform: translate(-50%, -50%);
background: linear-gradient(to right, black, black 25%, transparent 25%, transparent 75%, black 75%);
}
.vert {
width: 0.125rem; height: 3rem;
transform: translate(-50%, -50%);
background: linear-gradient(to bottom, black, black 25%, transparent 25%, transparent 75%, red 75%);
}
.angle-1 { transform: translate(-50%, -50%) rotate(45deg) }
.angle-2 { transform: translate(-50%, -50%) rotate(-45deg) }
/*****************************/
/* DESKTOP specific settings */
/*****************************/
#media (min-width: 720px) and (min-height: 720px) { /* anything larger than average mobile */
/* base flexbox patch grid structure */
[patch],[patch]>* { display: flex; flex-wrap: wrap } /* patch and cell containers */
[patch] { align-content: flex-start; position: relative }
[patch]>* { flex-grow: 1; /* [MANDATORY] */
overflow: hidden; /* [OPTIONAL] */
/* Center everything inside the element (vertical and horizontal) */
justify-content: center; align-content: center; align-items: center }
/* the "nine-patch", inspired by Android 9-patch image; essentially a 3x3 matrix */
[patch^= "3x"]>* { height : 33.33333%; max-height: 33.33333% } /* ^...starts with */
[patch*= "x3"]>* { flex-basis: 33.33333%; max-width : 33.33333% } /* *...contains */
[patch^= "1x"]>* { height: 100%; max-height: 100% } [patch*= "x1"]>* { flex-basis: 100%; max-width: 100% }
[patch^= "2x"]>* { height: 50%; max-height: 50% } [patch*= "x2"]>* { flex-basis: 50%; max-width: 50% }
/* add your own specific patch cell sizes like below */
/*
7x1 days in a week, 18x8 Periodic Table, 24x1 hours in a day, 22x6 keyboard+numpad, etc.
just divide 100% by the required XxY values
Usage:
<parent-tag patch="XxY">
requires X * Y number of child-tags to work properly
<child-tag 1>
....
<child-tag N>
</parent-tag>
*/
body { background-color: rgba(0,0,0,.1) } /* light grey */
.main-structure {
flex-flow: row wrap; /* we have 3 rows of several columns */
height: 75vmin; width: 75vmin; /* Modify to your needs */
background-color: White;
}
[band*="info2"] .main-structure>[patch="1x2"] {
max-width: 75%;
/* instead of 100%, this will move the cells closer for a 'circular feel' */
}
.main-structure img { display: block; width: 80% } /* Modify to your needs */
.main-structure .outCircle {
/* flex for easy centering of '.inner' */
display: flex; /* make visible */
}
}
/**************************/
/* preferred global rules */
/**************************/
html,body { width: 100%; max-width: 100% }
html { -webkit-box-sizing: border-box; box-sizing: border-box }
*, *:before, *:after { -webkit-box-sizing: inherit; box-sizing: inherit }
body { margin: 0 } /* remove default <body> spacing */
/*
Above CSS defines ALL elements to use 'border-box' (most common practice nowadays)
make sure to own the boxmodel knowledge!
MDN 'The box model': https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/The_box_model
w3schools 'CSS box-sizing Property': https://www.w3schools.com/cssref/css3_pr_box-sizing.asp
*/
/*
All math reference: https://www.mathsisfun.com/equation_of_line.html
*/
/* responsive base font size using y = mx + b */
html { font-size: calc(0.625vmin + 0.625rem) } /* (320,12)(1280,18) */
/* Simple banding attribute for use in 'Landing Page' layout */
[band] { display: flex; flex-flow: column wrap; align-content: center }
body[padded="1"],
body[padded="0"] [band*="padded"] {
/*
responsive page padding
and responsive band padding (same as responsive page padding, but at band level)
Top/Bottom padding: p1(320,16) p2(1920, 72) => 0.035x + 4.8 => vary from 16 to 72px
Left/Right padding: p3(320, 8) p4(1920,320) => 0.195x - 54.4 => vary from 8 to 320px
'Band padding' is only active when 'page padding' is off (0)
*/
padding: calc(3.5vh + 4.8px) calc(19.5vw - 54.4px);
}
/* for debugging (put in <body>) */
[outlines="1"] * { outline: 1px dotted }
<body outlines="0" padded="0">
<div band="padded.header">
<h1>3x3 easy grid with two dummies</h1>
<h3>easy, but straight columns</h3>
</div>
<div band="padded.info1">
<div class="main-structure" patch="3x3">
<div>
<div class="headline">Meet the farmer</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<div class="headline dummy"></div>
</div>
<div>
<div class="headline">Provenance</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<div class="headline">Certification</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<img src="https://i.postimg.cc/zvBCdxp4/circles.png">
</div>
<!--div class="outCircle">
<div class="inner">
<div class="marker horiz"></div>
<div class="marker vert"></div>
<div class="marker angle-1"></div>
<div class="marker angle-2"></div>
</div>
</div -->
<div>
<div class="headline">Life of the seed</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<div class="headline">History of seed</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<div class="headline dummy"></div>
</div>
<div>
<div class="headline">IoT data this seed season</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
</div>
</div>
<div band="padded.header">
<h1>3x1 nested grid with 1x2, 1x3 and 1x2 child grids</h1>
<h3>more complicated, but has rows with a 'circular feel'</h3>
</div>
<div band="padded.info2">
<div class="main-structure" patch="3x1">
<div patch="1x2">
<div>
<div class="headline">Meet the farmer</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<div class="headline">Provenance</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
</div>
<div patch="1x3">
<div>
<div class="headline">Certification</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<img src="https://i.postimg.cc/zvBCdxp4/circles.png">
</div>
<!--div class="outCircle">
<div class="inner">
<div class="marker horiz"></div>
<div class="marker vert"></div>
<div class="marker angle-1"></div>
<div class="marker angle-2"></div>
</div>
</div -->
<div>
<div class="headline">Life of the seed</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
</div>
<div patch="1x2">
<div>
<div class="headline">History of seed</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
<div>
<div class="headline">IoT data this seed season</div>
<ul>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
<li>Lorem ipsum dolor sit amet, exerci dolorem est ad.</li>
</ul>
</div>
</div>
</div>
</div>
</body>
You could wrap it in a div and give the <div> the following styling:
#circle {
border: solid black 2px;
border-radius: 50%;
width: 10em; //Depends on the circles size..
height: 10em;
}

How to distributed unequal width items to fulfill the container with flexbox

I am having a requirement that displaying the dynamic unequal width items in a container but it needs to fulfill the parent container. The item is dynamic so we can not know how long it is. If there is no room in the row, we need to display that item in the next row but the existing rows must be distributed equally to fulfill the container's width. Something like this:
My expectation:
What I did is create a container with display: flex and put child items inside with their's width fit the content inside. The items can be displayed in the next row if there is no room for it but I have no clue how to distributed existing items to fulfill the whole row?
Code:
.container {
display: flex;
gap: 10px;
flex-wrap: wrap;
width: 350px;
border-style: dotted;
}
.item {
background-color: coral;
padding: 10px 1em;
margin: 5px;
}
<div class="container">
<div class="item">Lorem ipsum</div>
<div class="item">Lorem ipsum dolor</div>
<div class="item">adipiscing</div>
<div class="item">consectetur adipiscing elit</div>
<div class="item">Lorem ipsum dolor sit amet</div>
<div class="item">Lorem ipsum dolor</div>
</div>
I just wonder is there any flexbox's CSS property to solve this problem?
some extra CSS for the behavior of the content and the children could be used :
flex:1 0 auto;/* make it stretch as much as possible*/
max-width:calc(100% - 10px);/* do not let it be bigger than container width minus margin */
white-space: pre-wrap;/* keep on a single line as much as possible */
Possible demo
* {
box-sizing: border-box;
}
.container {
display: flex;
gap: 10px;/*NOTICE: understood by Firefox at the moment */
flex-wrap: wrap;
width: 350px;
border-style: dotted;
}
.item {
background-color: coral;
padding: 10px 1em;
margin: 5px;
/* update a few behaviors */
flex:1 0 auto;
max-width:calc(100% - 10px);/* 10px is right + left margin */
white-space: pre-wrap;
}
<div class="container">
<div class="item">Lorem ipsum</div>
<div class="item">Lorem ipsum dolor</div>
<div class="item">adipiscing</div>
<div class="item">consectetur adipiscing elit</div>
<div class="item">Lorem ipsum dolor sit amet</div>
<div class="item">Lorem ipsum dolor</div>
<div class="item">Lorem ipsum dolor</div>
<p class="item">Pellentesque habitant morbi tristique senectus et netus et netus et malesuada ac turpis egestas.</p>
</div>

How can I display 2 items in a flex container and keep set width and height of a flex item?

*I have added padding inside the divs so you're able to see the boxes
I need to display these 2 items next to each other on desktop screen sizes.
I the purple bordered box which contains the 2 elements is set to display flex.
The circle div sqaushes up.
I have set is to a height: 200px width: 200px - this is fine before I set the parent to display flex.
How can I make sure that the circle stays at the set width and height and the rest of the content in the red box resizes - rather than the other way around?
If you could also please explain like I'm 5 why this is happening that would be really appreciated.
.card__inner {
display: flex;
}
.news__feature-image {
border-radius: 100%;
width: 200px;
height: 200px;
background: red;
}
<article class="card">
<div class="card__inner">
<div class="news__feature-image"></div>
<div class="card__content">
<header class="news__header">
<span class="new__post-date">
18 Sep
</span>
<a class="card__link" href="/">
Read More
</a>
</header>
<h2 class="news__title">Lorem ipsum title this is a title etc Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed turpis est, eleifend.</h2>
</div>
</div>
</article>
I have tried using flex grow and flex shrink on the feature image and the content.
I have added settings for the flex items that contains the circle. By default the setting for flex-shrink is 1, which allows it to shrink if necessary. Setting it to zero ensures the circle is displayed as desired.
* {
box-sizing: border-box;
}
.container {
border: thin solid green;
padding: 1rem;
}
.inner {
border: thin solid purple;
display: flex;
padding: 1rem;
}
.circle {
width: 200px;
height: 200px;
background-color: red;
border-radius: 100%;
flex-shrink: 0;
}
<div class="container">
<div class="inner">
<div class="circle"></div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
</div>

Right div on float:right appears a line below the left one

Here's my problem.
I have a wrapper div(width: 800px and height: 250px) which contains two divs occupying all the space in height and dividing their width in half.
I set up my css, float the right div to float: right and this one appears where it should but "below" the other one, exceeding the wrapper div space(which shouldn't even be possibile).
I'm posting both the jdfiddle and the code.
JS Fiddle http://jsfiddle.net/FV9yC/
HTML
<div id="wrapper">
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
CSS
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
#leftDiv {
background-color: purple;
height: 200px;
width: 250px; }
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }
Just shift the div with ID rightDiv above the div with ID leftDiv. That's it.
Here is the WORKING SOLUTION
The Code:
<div id="wrapper">
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
http://jsfiddle.net/FV9yC/1/
You should add float: left to your left div.
Add float: left to the other div. Also you can use float: left for both; unless you have other reasons than just positioning there to use it.
Used to this code
#leftDiv{float:left;}
#wrapper:after{
content:"";
clear:both;
display:table;
}
#wrapper {
height:200px; // remove this line
}
Demo
try this
http://jsfiddle.net/FV9yC/5/
#wrapper {
background-color: grey;
height: 200px;
width: 500px;
}
#leftDiv {
background-color: purple;
height: 200px;
width: 250px;
float:left;
}
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }
You don't need to float your div to the right — you just need to align each block beside the other, and you can do this using float: left;.
I made a consistent solution for you. See below:
Using a class to remove the DRY of your code, I grouped your blocks into a common class with common behaviors.
See your new CSS:
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
.block {
float: left;
width: 250px;
}
#leftDiv {
background-color: purple;
height: 200px; }
#rightDiv {
background-color: green;
height: 250px; }
And your new HTML:
<div id="wrapper">
<!-- left div -->
<div class="block" id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div class="block" id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
Using classes to execute common behaviors it is a good practice to avoid redundancy and future problems.
To see your code working on jsFiddle, just click here.

Easy way to center variable width divs in CSS

I'm trying to center a Div that will have varying width's (based on content of a website).
I read about a relative positioning technique here:
http://www.tightcss.com/centering/center_variable_width.htm
But I thought there has to be an easier way to do it?
That's a pretty solid method that should work well in most browsers. It's not really that complex when you break it down. Here's a basic example:
<style type="text/css">
#hideoverflow { overflow: hidden; }
#outer { position: relative; left: 50%; float: left; }
#inner { position: relative; left: -50%; float: left; }
</style>
<div id="hideoverflow">
<div id="outer">
<div id="inner">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id velit vel augue fringilla rhoncus at et odio. Suspendisse potenti. Aliquam justo libero, commodo ut iaculis in, placerat vel purus.
</div>
</div>
</div>
#Talon; you can do it like this http://jsfiddle.net/sandeep/7PXQF/
CSS:
.container{
background-color:red;
text-align:center;
}
.center{
background-color:yellow;
display:inline-block;
text-align:left;}
HTML:
<div class="container">
<div class="center">
<p>This is a div with an much wider width, to make the yellow div go off the page to the right. We'll type a bit more to be sure.</p>
<p>Most people will see a horizontal scroll bar on the bottom, unless their screen is very wide.</p>
</div>
</div>
Well, it can't get any simpler than this and has full support on all browsers; doesn't even need a container:
.centered {
display:table;
margin:0 auto;
}
<div class="centered">
content
</div>
Here is a working fiddle: https://jsfiddle.net/1tnprnoz/
Now with flex-box you can easily achieve this with justify-content: center;.
#container{
background: gray;
display: flex;
justify-content: center;
}
<div id="container">
<div id="content" style="width: 200px; padding: 5px; background: #ffa637;">
This is a centered div This is a centered div This is a centered div This is a centered div
</div>
</div>
This can also be achieved by applying margin: auto to the containers child selector #container>*.
#container{
background: #c7c7c7;
}
#container>*{
margin: auto;
}
<div id="container">
<div id="content" style="width: 200px; padding: 5px; background: #ffa637;">
This is a centered div This is a centered div This is a centered div This is a centered div
</div>
</div>
Note: content div is styled inline as these styles are generated styles and are out of the scope of this question.

Resources