I have to implement a design looking like this:
This will be the index.php page for WordPress and therefore it will be using the WordPress loop to output the blog articles as individual "resources".
Normally I would implement this as a flexbox, because the number of items is variable and I need it to be responsive, however this time our designer has added borders in between the items.
This would be fine but the borders are not included before or after the end of the rows. I cant solve this any of the pseudo selectors that I know of.
Currently my HTML and CSS look something like this:
section {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
section div {
display: flex;
flex-direction: column;
border-right: 1px solid red;
padding-right: 20px;
margin-right: 20px;
margin-bottom: 10px;
}
<section>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
</section>
Is this something I can solve with CSS grid and is there a way to achieve this using flexbox or another layout that isn't grid?
This really depends on the background under the items, but if it is made of one color, you could use the solution of simply overlapping the left border with the pseudo element, like this:
section {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
position: relative;
margin-left: -20px;
}
section::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 20px;
width: 1px;
background: #fff;
}
section div {
display: flex;
flex-grow: 1;
flex-direction: column;
border-left: 1px solid red;
padding-left: 20px;
margin-left: 20px;
text-align: center;
}
<section>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
</section>
I kind of have what you need, I'm displaying the right border with pseudo element and pushing it off screen if its the last element of a row, and some empty divs to push a new row to keep the layout.
section {
display: flex;
align-items: center;
flex-wrap: wrap;
overflow: hidden;
}
section div {
display: flex;
flex-grow: 1;
flex-direction: column;
padding: 0 20px;
position: relative;
}
section div:nth-last-child(9) ~ div{
height: 0;
width: 140px;
}
section div::after {
content: '';
position: absolute;
border-right: 1px solid red;
left: 100%;
display: block;
height: 100%;
}
section div:nth-last-child(9)::after {
border: none;
}
section div p { width: 140px; }
<section>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div>
<img class="http://lorempixel.com/output/fashion-q-c-200-200-6.jpg">
<p>
This is some text
</p>
</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
Related
I'd like to put images with centered text underneath each image in columns, I'd also like to size the images to a specific width and height each and I also want to make it so that when the text gets too long the text gets split into 2 lines, but I dont want it to move the image up and down at the same time. What code should I use? I used some code I found on the internet which is linked below but that didn't work as I expected it to (the code can be found below). Thanks!
So far, I've used this code but it doesn't center the text and also skips spots which I don't want to happen. Please check the image I've attached to see the skipped spotsskipped spot
HTML
<div class="column">
<img src="extra/road96.jpg" alt="Road 96"style="width:180px;height:180px;"/\>
<a class='neon-button' href='https://www.mediafire.com/file/4a05b4tkaal5e50/Road_96.zip/file'\>Road 96
<a/>
<div/>
CSS
.column {
float: left;
width: 13.33%;
padding: 5px;
}
.row::after {
content: "";
clear: both;
}
.sjamg {
text-align: justify;
width: [width of img];
}
.sjamg img {
display: block;
margin: 0 auto;
}
try using flex, and by using flex-direction change the direction of the container's items to Column
.main-container {
display: flex;
width: 100%;
height: 100vh;
flex-direction: Column;
justify-content: space-evenly;
}
.main-container p {
margin-left: 64px;
height: 5%;
width: auto;
}
.small-container {
height: 180px;
width: 180px;
background-color: blue;
}
<!DOCTYPE html>
<html>
<body>
<div class="main-container">
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
</div>
</body>
</html>
Try this:
.main-container {
display: flex;
width: 100%;
justify-content: space-evenly;
}
.mycolumn {
display: flex;
flex-direction: column;
width: 100%;
justify-content: space-evenly;
}
.main-container p {
margin: auto;
height: 5%;
width: auto;
}
.small-container {
height: 180px;
width: 50%;
background-color: red;
margin: auto;
}
<!DOCTYPE html>
<html>
<body>
<div class="main-container">
<div class="mycolumn">
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
</div>
<div class="mycolumn">
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
</div>
<div class="mycolumn">
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
</div>
<div class="mycolumn">
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
<div class="small-container"></div>
<p> your text </p>
</div>
</div>
</body>
</html>
I am trying to move an image underneath the text. I tried (position: absolute) and moving the image with -px but I want the image to stay in the same place on the HTML but have it moved using only CSS. I am doing a mobile-first approach and because of the desktop design, the IMG has to stay at the same position in HTML. I also tried to moving the text but it just goes over the image or the padding and just doesn't work. It might be easier to explain by screenshots:
The results I want to achieve:
And my current results are:
And my current code is:
img {
display: block;
width: 50%;
}
<header id="titleHeader">
<h1><span id="boot">Example</span>Text</h1>
<a class="navbutton" href="#navbar">Menu</a>
</header>
<section id="intromessage">
<img alt="shoes" src="https://cdn11.bigcommerce.com/s-h8hjw/products/927/images/735/WBHQ19-Retail-Product-OnWhite-Boots-M-1500x1425__00368.1599677007.475.500.png?c=2">
<div>
<h2>Example Text</h2>
<p>
example text, example text, example text<span> example text </span>example text
<span>example text</span>
</p>
</div>
</section>
Try this out
<style>
#intromessage{
display: flex;
flex-direction: column-reverse;
}
</style>
A few options are avalaible.
transform:scale(x) can be used
#intromessage,
#intromessage>* {
transform: scale(1, -1); /* value to add/remove via mediaquerie */
}
img {
display: block;
width: 50%;
margin:auto;
}
<header id="titleHeader">
<h1><span id="boot">Example</span>Text</h1>
<a class="navbutton" href="#navbar">Menu</a>
</header>
<section id="intromessage">
<img alt="shoes" src="https://cdn11.bigcommerce.com/s-h8hjw/products/927/images/735/WBHQ19-Retail-Product-OnWhite-Boots-M-1500x1425__00368.1599677007.475.500.png?c=2">
<div>
<h2>Example Text</h2>
<p>
example text, example text, example text<span> example text </span>example text
<span>example text</span>
</p>
</div>
</section>
for older browser, display:table could be involved :
#intromessage {
display: table;
width: 100%
}
#intromessage>div {
display: table-header-group;/* value to add/remove via mediaquerie */
}
img {
display: block;
width: 50%;
margin: auto;
}
<header id="titleHeader">
<h1><span id="boot">Example</span>Text</h1>
<a class="navbutton" href="#navbar">Menu</a>
</header>
<section id="intromessage">
<img alt="shoes" src="https://cdn11.bigcommerce.com/s-h8hjw/products/927/images/735/WBHQ19-Retail-Product-OnWhite-Boots-M-1500x1425__00368.1599677007.475.500.png?c=2">
<div>
<h2>Example Text</h2>
<p>
example text, example text, example text<span> example text </span>example text
<span>example text</span>
</p>
</div>
</section>
flex is already answered but order can be used too :
#intromessage {
display: flex;
flex-wrap:wrap;/* to be different from other answer */
}
#intromessage>div {
width: 100%;
}
img {
display: block;
width: 50%;
margin: auto;
order: 1;/* value to add/remove via mediaquerie */
}
<header id="titleHeader">
<h1><span id="boot">Example</span>Text</h1>
<a class="navbutton" href="#navbar">Menu</a>
</header>
<section id="intromessage">
<img alt="shoes" src="https://cdn11.bigcommerce.com/s-h8hjw/products/927/images/735/WBHQ19-Retail-Product-OnWhite-Boots-M-1500x1425__00368.1599677007.475.500.png?c=2">
<div>
<h2>Example Text</h2>
<p>
example text, example text, example text<span> example text </span>example text
<span>example text</span>
</p>
</div>
</section>
and the latest : grid can use order too
#intromessage {
display: grid;
grid-template-rows: auto;
}
img {
display: block;
width: 50%;
margin: auto;
order: 1;/* value to add/remove via mediaquerie */
}
<header id="titleHeader">
<h1><span id="boot">Example</span>Text</h1>
<a class="navbutton" href="#navbar">Menu</a>
</header>
<section id="intromessage">
<img alt="shoes" src="https://cdn11.bigcommerce.com/s-h8hjw/products/927/images/735/WBHQ19-Retail-Product-OnWhite-Boots-M-1500x1425__00368.1599677007.475.500.png?c=2">
<div>
<h2>Example Text</h2>
<p>
example text, example text, example text<span> example text </span>example text
<span>example text</span>
</p>
</div>
</section>
I would use flexbox for a responsive layout.
#intromessage {
display: flex;
flex-direction: column-reverse;
}
Here is a good resource: CSS Tricks - Guide to Flexbox
I already centered the images inside the "main-content" class, but I don't know if I do it right. And the second thing that I want to do is to center also the text, but if I use margin it also affects the images. Sorry for my confusing code, I'm just only a beginner.
Here is the HTML5 markup:
<main class="main-content">
<div class="item">
<a href="Products/groceries-pets/Fruits-Package.html">
<img
src="./Images/Products/groceries-pets/fruits-package.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="Products/groceries-pets/Fruits-Package.html" class="desc">
<strong>Fruit Package</strong> <em> P1200 </em>
</p>
</div>
</div>
<div class="item">
<a href="products/home-living/Indoor-Plant.html">
<img
src="./Images/products/home-living/indoor-plant.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="products/home-living/Indoor-Plant.html" class="desc">
<strong> Indoor Plant </strong> <em> P300 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/babies-toys/Baby-Cars.html">
<img
src="./Images/Products/babies-toys/baby-cars.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="Products/babies-toys/Baby-Cars.html" class="desc">
<strong> Baby Cars</strong> <em> P150 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/fashion/Black-Shirt.html">
<img
src="./Images/Products/fashion/black-shirt.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="Products/fashion/Black-Shirt.html" class="desc">
<strong> Black Shirt</strong> <em> P400 </em>
</p>
</div>
</div>
</main>
Here is my CSS:
.main-content {
background-color: rgb(219, 95, 95);
height: 340px;
width: 95%;
margin: 140px auto 0;
text-align: center;
}
.item {
float: left;
}
.img-content {
width: 200px;
height: 200px;
padding: 10px;
margin: 30px -200px 0px 200px;
object-fit: cover;
}
.desc {
text-align: center;
}
Try removing the margin from .img-content so that it aligns to the text, then add a margin to .item instead:
.main-content {
background-color: rgb(219, 95, 95);
height: 340px;
width: 95%;
margin: 140px auto 0;
text-align: center;
}
.item {
float: left;
margin: 20px;
}
.img-content {
width: 200px;
height: 200px;
padding: 10px;
object-fit: cover;
background-color: green;
}
.desc {
text-align: center;
}
<main class="main-content">
<div class="item">
<a href="Products/groceries-pets/Fruits-Package.html">
<img src="./Images/Products/groceries-pets/fruits-package.jpg" class="img-content" />
</a>
<br />
<div>
<p href="Products/groceries-pets/Fruits-Package.html" class="desc">
<strong>Fruit Package</strong> <em> P1200 </em>
</p>
</div>
</div>
<div class="item">
<a href="products/home-living/Indoor-Plant.html">
<img src="./Images/products/home-living/indoor-plant.jpg" class="img-content" />
</a>
<br />
<div>
<p href="products/home-living/Indoor-Plant.html" class="desc">
<strong> Indoor Plant </strong> <em> P300 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/babies-toys/Baby-Cars.html">
<img src="./Images/Products/babies-toys/baby-cars.jpg" class="img-content" />
</a>
<br />
<div>
<p href="Products/babies-toys/Baby-Cars.html" class="desc">
<strong> Baby Cars</strong> <em> P150 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/fashion/Black-Shirt.html">
<img src="./Images/Products/fashion/black-shirt.jpg" class="img-content" />
</a>
<br />
<div>
<p href="Products/fashion/Black-Shirt.html" class="desc">
<strong> Black Shirt</strong> <em> P400 </em>
</p>
</div>
</div>
</main>
One of the Css properties is the display property.
This property will put the items you have horizontally.
If you want to place it vertically,
Add this property after display: flex; and flex-direction: column;.
Then use the properties justify-content: center; and align-items: center;
This property will center the item.
I have a row with six columns of image and text here: https://i.imgur.com/UKGGCrX.png
I want to align the two-line text (e.g. Resources and Reserves) with "Exploration" and "Geological Data". https://i.imgur.com/Tplzivb.png
Adding a margin below the image doesn't do the trick, nor does adding a margin above the text... also tried setting a min-height for the icon, but that doesn't do it either.
How can I accomplish this?
Using flexbox you can easily align your text on "top".
This is default flexbox behaviour and does align your text on "baseline":
.container{
display: flex;
flex-flow: row nowarp;
}
.text{
max-width: 30px;
}
<html>
<div class="container">
<div class="imgt">
<img src="https://dummyimage.com/20x20">
<div>Some Text</div>
</div>
<div class="imgt">
<img src="https://dummyimage.com/20x20">
<div class="text">Some Long long Text</div>
</div>
</div>
</html>
I assume you have a margin-top: auto; and margin-bottom: auto;, or some flexbox properties like align-items: center; somewhere, that your text gets centered inside the div.
You can use flex styles to obtain your desired results. I created an example showing how the align-items: stretch; property will help with keeping icons/images/and content aligned with siblings.
Live example: https://codepen.io/SROwl/pen/xNrRmw
HTML:
<h3> align content top </h3>
<div class="bucket-row">
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Triplen Extra Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
</div>
<h3> align content bottom </h3>
<div class="bucket-row content-end">
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Single Line</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Triplen Extra Line Title</span>
</div>
<div class="bucket">
<img src="https://dummyimage.com/85x85">
<span>Double Line Title</span>
</div>
</div>
SCSS:
body {
font-family: sans-serif;
}
.bucket-row {
display: flex;
flex-direction: row;
align-items: stretch;
justify-content: space-between;
width: 600px;
margin-bottom: 40px;
.bucket {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
max-width: 85px;
background-color: #eee;
padding-bottom: 20px;
img {
margin-bottom: 15px;
}
}
&.content-end {
.bucket {
span {
margin-top: auto;
}
}
}
}
I am working on a project and want to have infinite horizontal scrolling for cells that each contain a thumbnail and some text. I have everything mostly working. However, I can not get text to wrap. The css and html are below. The first two items in the scroll box have text that should wrap, but overflows.
This is the style (cell 2 is only for demo to alternate background colors):
.container {
display: flex;
flex-direction: row;
padding: 10px;
height: 10%;
overflow-x: auto;
white-space: nowrap;
}
.cell {
background: #818181;
flex: fit-content;
padding: 10px;
width: 220px;
height: 8%;
display: inline-block;
}
.cell2 {
background: #f44336;
flex: fit-content;
padding: 10px;
width: 220px;
height: 8%;
display: inline-block;
}
.data {
justify-content: center;
word-wrap: break-word;
}
<div>
<h2>Scroll Test</h2>
<div class="container">
<div class="cell">
<a href= "#">
<span class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"></span>
<span class="data"> <h3>Some text here</h3></span>
</a>
</div>
<div class="cell2">
<a href= "#">
<span class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"></span>
<span class="data"> <h3>Some text here that will over flow</h3></span>
</a>
</div>
<div class="cell">
<a href= "#">
<span class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"></span>
<span class="data"> <h3>Some text here</h3></span>
</a>
</div>
<div class="cell2">
<a href= "#">
<div class="data"> <img src="http://roncabeanz.com/Roncabeanz/images/CoffeeIcon.jpg" width="200px"> </div>
<div class="data"> <h3>Some text here</h3></div>
</a>
</div>
<?php } ?>
</div>
</div>
Link to fiddle: https://jsfiddle.net/jonathannah/3rxcktpm/29/
Just add
h3 { white-space: normal }. The children of .containerinherit white-space: nowrap, which causes the problem.
Another issue in your html is the h3 inside a span, which is invalid. You shouldn't use block elements inside inline elements. You can change it to a div give it the style display: inline