Removing padding on last-child results in different sized element - css

I have an image gallery with horizontal thumbnails below the featured image. I am adding padding-right:10px; to all but the last-child in an effort to equally space these elements. This works as intended on all but the last thumbnail, where it's slightly larger than the rest.
It makes sense why this is happening, but I am not sure how to fix it. This was the solution recommended on numerous other Stack Overflow posts. Any workaround would be appreciated.
<style>
.gallery-grid {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 1fr;
column-gap: 10px;
row-gap: 30px;
}
.image-thumb {
float: left;
width: 20%;
padding-right: 10px;
box-sizing: border-box;
}
.image-thumb:last-child {
padding-right: 0;
}
</style>
<div class="gallery-grid">
<img id="featured-image" src="featured.jpg" style="width:100%">
<div class="image-thumb">
<img src="featured_thumb.jpg" alt="Mountains" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Nature" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Snow" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Lights" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Lights" style="width:100%" onclick="myFunction(this);">
</div>
</div>

If you want to stick using float insteed of flex you could use margin insteed of padding. Then you can calculate the total space taken by the margin, divide it between the number of elements and rest it to the width using calc
(margin-right:10px * 4) / 5 = 8
Example:
* {box-sizing:border-box;}
.box {
height:100px;
background-color:blue;
float:left;
width:calc(20% - 8px);
margin-right:10px;
}
.box:nth-child(5) {margin-right:0;}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

Related

Resizing images in a flex container

Im trying to have 3 images side by side in a flex container but the images are much too large and its stretching the page and creating a scroll bar.Tried a tip to use flex wrap but that didn't work.Should I just resize in photoshop?.
<section class="main-content">
<div class="image">
<img src="img/devil-ivy-can.jpg">
</div>
<div class="image">
<img src="img/krimson-princess-can.jpg">
</div>
<div class="image">
<img src="img/spiderwort-can.jpg">
</div>
</section>
.main-content{
display: flex;
}
div{
width:100%;
padding:10px;
margin:10px;
}
Try this
.container {
display: flex;
flex-wrap: wrap;
background-color: grey;
}
img {
width: 100%;
}
div {
flex: 1;
padding: 10px;
margin: 10px;
}
<section class="container">
<div class="image">
<img src="https://atlas-content1-cdn.pixelsquid.com/assets_v2/127/1273408777629996108/jpeg-600/G13.jpg">
</div>
<div class="image">
<img src="https://atlas-content1-cdn.pixelsquid.com/assets_v2/127/1273408777629996108/jpeg-600/G13.jpg">
</div>
<div class="image">
<img src="https://atlas-content1-cdn.pixelsquid.com/assets_v2/127/1273408777629996108/jpeg-600/G13.jpg">
</div>
</section>
hello i have try to solve your problem you can try this in your CSS code.
.main-content{
display: flex;
gap:10px
}
div{
width:100%;
gap:10px;
}div.image img{
width:100%;
object-fit:cover;
}
<section class="main-content">
<div class="image">
<img src="https://www.imgonline.com.ua/examples/random-pixels-wallpaper.jpg">
</div>
<div class="image">
<img src="https://www.imgonline.com.ua/examples/random-pixels-wallpaper.jpg">
</div>
<div class="image">
<img src="https://www.imgonline.com.ua/examples/random-pixels-wallpaper.jpg">
</div>
</section>
in your css code at div selector i prefer using gap to give space between items than using padding and a margin
if you give the img width 100% it will fill the div.image and set object-fit with value cover. The CSS object-fit property is used to specify how an or should be resized to fit its container.

How align several texts and images without flex

There are several subjects on StackOverFlow as here, but the answers are not intuitive for a beginner.
How to align several text and images in CSS
How to align several text and images in CSS
Actually, I would like to center 2 images on the same line, in bottom of each image there are a title and a subtitle. I would like to make it without display: flex.
I don't understand why the seconde image is not aligned horizontally correctly?
.row{
padding-top: 15px;
text-align: center;
}
.imgP{
background-color:red;
margin: auto 5px;
}
<body>
<div class="row">
<img class="imgP" src="https://zupimages.net/up/21/18/te8n.png" alt="image">
<div class="title">My title</div>
<div class="subtitle">Subtitle</div>
<img class="imgP" src="https://zupimages.net/up/21/18/te8n.png" alt="image">
<div class="title">My title</div>
<div class="subtitle">Subtitle</div>
</div>
</body>
Flex is the easy and modern way to do it. If you don't want to use flex, use display:inline-block. For that, you need to create 2 column divs and wrap the content inside it.
.row {
padding-top: 15px;
text-align: center;
}
.col {
display: inline-block;
}
.imgP {
background-color: red;
margin: auto 5px;
}
<body>
<div class="row">
<div class="col">
<img class="imgP" src="https://zupimages.net/up/21/18/te8n.png" alt="image">
<div class="title">My title</div>
<div class="subtitle">Subtitle</div>
</div>
<div class="col">
<img class="imgP" src="https://zupimages.net/up/21/18/te8n.png" alt="image">
<div class="title">My title</div>
<div class="subtitle">Subtitle</div>
</div>
</div>
</body>

CSS question: Flexible width to make it reflow ready

Could anyone please help me with this, How can I make the parent container flexible and make it reflow ready?
.container{
width: 350px;
border: 1px solid red;
}
.item{
margin-top:2px;
display: flex;
}
.line{
flex-grow:1;
}
<div class="container">
<div class="item">
<div class="line"><span>xyx</span></div>
<span>10 USD</span>
</div>
<div class="item">
<div class="line"><span>q</span></div>
<span>* 2</span>
</div>
<div class="item">
<div class="line"><span>total</span></div>
<span>20 USD</span>
</div>
</div>
Since you are referring reflow as browser width change and your default width for the container is 350px
just your change width: 350px to max-width: 350px. Your container is now responsive for smaller browser width
You can play around around here and change the browser here:
.container{
max-width: 350px;
border: 1px solid red;
}
.item{
margin-top:2px;
display: flex;
}
.line{
flex-grow:1;
}
<div class="container">
<div class="item">
<div class="line"><span>xyx</span></div>
<span>10 USD</span>
</div>
<div class="item">
<div class="line"><span>q</span></div>
<span>* 2</span>
</div>
<div class="item">
<div class="line"><span>total</span></div>
<span>20 USD</span>
</div>
</div>

How to close gap between image and text?

I dont understand why I have a large gap between the second picture and the text to its right. I've attached a fiddle for the code. How do I close this gap?
http://jsfiddle.net/7Qchr/
.main {
-webkit-column-gap: 1em;
-webkit-column-rule: 2px;
-webkit-columns: 2;
}
#image {
max-width: 100%;
}
<div class="main">
<p id="text_l">
“ The best selection of cheese I've ever seen! Cannot wait for our next order!”
<p>
<img src="img/cheese1.jpg" alt="Picture of cheese" id="image">
</div>
<div class="main">
<img src="img/cheese2.jpg" alt="Picture of cheese" id="image">
<p id="text_r">
“ Wow,amazing cheese selection and fast delivery! I highly recommed you try!”
<p>
</div>
You'll have to rewrite your code a bit... Try something like this:
HTML
<div class="main">
<div>
<p id="text_l">blah</p>
</div>
<div>
<img src="cheese1.jpg" class="image">
</div>
</div>
<div class="main">
<div>
<img src="cheese2.jpg" class="image odd">
</div>
<div>
<p id="text_r">blah</p>
</div>
</div>
CSS
.main div{
width: 48%;
display: inline-block;
vertical-align: top;
}
.image {
max-width: 100%;
padding: 0 10px;
}
.image.odd {float: right;}
http://jsfiddle.net/7Qchr/6/
Updated Demo
The following CSS was added (none of the existing HTML or CSS was changed).
.main + .main {
text-align: right;
}
p {
text-align: left;
}

confused about percentages in dimensions of web-app

this is the first time, i'm building a totally scalable application with a css layout, when the user resizes the window the fields should shrink and grow accordingly. first i thought, easy, right? but now i'm really scratching my head over the dimensions, cause it seems like the margins are not quite right... i want to have a border-like separator thingy in between all the individual fields...
my code is this:
<div style='background-color:#000000;width:100%;height:100%;'>
<div style='width:100%;height:66%;margin-bottom:1%;'>
<div style='float:left; width:19%;height:100%;margin-right:1%;' class='app_14_concept_block'>keypartners</div>
<div style='float:left; width:19%;height:100%;margin-right:1%;'>
<div style='height:49%;margin-bottom:6%' class='app_14_concept_block'>key activities</div>
<div style='height:49%;' class='app_14_concept_block'>key resources</div>
</div>
<div style='float:left; width:19%; height:100%;margin-right:1%;' class='app_14_concept_block'>value propositions</div>
<div style='float:left; width:19%; height:100%;margin-right:1%;'>
<div style='height:49%;margin-bottom:6%' class='app_14_concept_block'>customer relationship</div>
<div style='height:49%;' class='app_14_concept_block'>channels</div>
</div>
<div style='float:left; width:19%; height:100%;padding-right:1%' class='app_14_concept_block'>customer segments</div>
</div>
<div style='width:100%;height:33%;'>
<div style='float:left; width:49%; height:100%;margin-right:1%;' class='app_14_concept_block'>cost</div>
<div style='float:left; width:50%; height:100%;' class='app_14_concept_block'>revenue</div>
</div>
</div>
the css is this:
.app_14_concept_block{
background-color:#ffffff;
}
.app_14_concept_block:hover{
background-color:#eeeeee;
}
and this is what it looks like at the moment (the blue thingy there is part of my app viewer layout that would open comments) - my main concern is the added empty(black) space on the right, at the end of the rows:
jsfiddle here: http://jsfiddle.net/gbMZy/51/
i also tried setting the "customer segments" width to 20% - sadly to no avail:
screenshot:
jsfiddle: http://jsfiddle.net/gbMZy/52/
Maybe this solution Footer Items Expanding with Viewport Evenly could also work in your case.
Percentage width won't play well together with borders or margins.
A possible workaround is to have wrapper containers with full 20% / 50% width and then elements with border etc. inside them. This should help to avoid flickering and random spacings.
So, in your case this could look something like: http://jsfiddle.net/qC4JV/1/
HTML:
<div class="top">
<div class="cell">
<div class="border right"></div>
<div class="border bottom"></div>
<p>value</p>
</div>
<div class="cell">
<div class="border right"></div>
<div class="border bottom"></div>
<p>value</p>
</div>
<div class="cell">
<div class="border right"></div>
<div class="border bottom"></div>
<p>value</p>
</div>
<div class="cell">
<div class="border right"></div>
<div class="border bottom"></div>
<p>value</p>
</div>
<div class="cell">
<div class="border bottom"></div>
<p>value</p>
</div>
</div>
<div class="bottom">
<div class="cell">
<div class="border right"></div>
<p>value</p>
</div>
<div class="cell">
<p>value</p>
</div>
</div>​
CSS:
body, html{
height: 100%;
background: #000;
}
.top{
height: 60%;
}
.bottom{
height: 40%;
}
.cell{
float: left;
height: 100%;
background: #fff;
position: relative;
}
.cell .border.right{
position: absolute;
right: 0;
top: 0;
width: 10px;
height: 100%;
background: #000;
}
.cell .border.bottom{
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 10px;
background: #000;
}
.top .cell{
width: 20%;
}
.bottom .cell{
width: 50%;
}
​
Set the font-size explicitly in the first div and then use 'em' to adjust the row margins.
But I'm unsure whether there will be scalability issues or not ..
When using percentages in this manner, the way browsers round values to obtain discrete pixel values will often result in remainders. if 1% of the total width of the viewport is not an exact number of pixels, the browser will round up or down, resulting in a few pixels too many or too few, hence the jittery divs and unequal margins.
Even Twitter's Bootstrap framework, a very well developed system, suffers from the same issues when using its fluid grid system.
I hate to say it, but if you absolutely have to create a layout like this, and the unreliable element dimensions are not acceptable, using a table may be the way to go.
On the other hand, if you go with white 'borders' instead of black, the margin jitteriness will be less noticeable.
Your HTML is painful to read. You should really separate styles out into CSS instead of writing everything inline like that, it's harder to read/debug and makes it very easy to make a mistake.
Here is a little bit better of a solution: http://jsfiddle.net/gbMZy/51/
Percentage always will be calculated in round values manner on browser and will be justified according to total width of body or parent element wrap area/width. These dimensions will be assign after calculating inner and outer pixels width given to sub elements or padding styles as well as border given on element with specified size of pixels too.
HTML:
<div class="top">
<div class="cell">
<div class="border right">
</div>
<div class="border bottom">
</div>
<p>
Top Block-1
</p>
</div>
<div class="cell">
<div class="border right">
</div>
<div class="border bottom">
</div>
<p>
Top Block-2
</p>
</div>
<div class="cell">
<div class="border right">
</div>
<div class="border bottom">
</div>
<p>
Top Block-1
</p>
</div>
<div class="cell">
<div class="border right">
</div>
<div class="border bottom">
</div>
<p>
Top Block-3
</p>
</div>
<div class="cell">
<div class="border bottom">
</div>
<p>
Top Block-4
</p>
</div>
</div>
<div class="bottom">
<div class="cell">
<div class="border right">
</div>
<p>
Bottom Block-1
</p>
</div>
<div class="cell">
<p>
Bottom Block-2
</p>
</div>
</div>
CSS Styles:
body, html{
height: 100%;
background: #3355A9;
}
.top{
height: 60%;
}
.bottom{
height: 40%;
}
.cell{
float: left;
height: 100%;
background: #ffca77;
position: relative;
text-align:center;
font-weight:bold;
}
.cell p{
padding:10px;
}
.cell .border.right{
position: absolute;
right: 0;
top: 0;
width: 10px;
height: 100%;
background: #3355A9;
}
.cell .border.bottom{
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 10px;
background: #3355A9;
}
.top .cell{
width: 20%;
}
.bottom .cell{
width: 50%;
}
Try this solution on codebins: http://codebins.com/codes/home/4ldqpbt
So, you might be found a better solution because it has better user interface and clear vision to display exact result as we want.

Resources