Make sibling div follow width of its sibling img - html

I want to show an image and two buttons below it, image dimensions could vary a lot, so I set max-width to 60% and max-height to 80%, but I'm not sure how to make these two buttons below to be aligned correctly under the image, left button floating on the left, right on the right of .buttons div, and .buttons div should be exact width of image.
Here's my HTML:
<div class='content'>
<img class='nft'>
<div class='buttons'>
<button class='buy'>Buy</button>
<button class='share'>Share</button>
</div>
</div>
CSS looks like this:
.content {
display: table-cell;
vertical-align: middle;
text-align: center;
width: 100%;
}
.nft {
max-width: 60%;
max-height: 80%;
display: block;
margin: auto;
margin-bottom: 32px;
}
.buy {
float: left;
}
.share {
float: right;
}

It's unclear if you want the buttons to expand to each fill half of the width of the image above them. That was my take, though, so here is an example showing the buttons filling space at different image widths:
.content {
display: inline-flex;
flex-direction: column;
}
.buttons {
display: flex;
}
.buttons>button {
flex: 1;
}
<div class='content'>
<img src='http://placekitten.com/400/200' class='nft'>
<div class='buttons'>
<button class='buy'>Buy</button>
<button class='share'>Share</button>
</div>
</div>
<div class='content'>
<img src='http://placekitten.com/200/200' class='nft'>
<div class='buttons'>
<button class='buy'>Buy</button>
<button class='share'>Share</button>
</div>
</div>

Related

How to align a picture and text vertically in css

Hi I am creating a website and I am trying to align a picture and some text vertically, but I am not being able to do this and the picture is only taking 100% space of the website, this is the code:
body {
width: 100%;
max-width: 960px;
margin: 0;
}
div.content {
width: 100vw;
display: flex;
}
div.column1 {
width: 15%;
background-color: #F7F7F7;
overflow: hidden;
height: 100vh;
}
div.column2 {
width: 70%;
overflow: hidden;
}
.banner {
width: 100vw;
height: 10vh;
}
.container2 {
display: flex;
}
<div class="content">
<div class="column1">
</div>
<div class="column2">
<div class="container2">
<div class="lobby">
<img src="img/lobby.jpg" alt="" /> </div>
<div class="content">
<p>lorem50gsdgsdsgdgsgdfgdfgdfgdfgfdggsd</p>
</div>
</div>
</div>
<div class="column1">
</div>
</div>
The website is divided into 3 columns and I am putting the content on the middle one.
Shouldn't the display flex align them vertically? Why is it not working? Thank you in advance!
You need to set align-items:center on flex parent in order to vertically center its children. Check this for more details about flex-container, and this for more general info about flexbox
You can add justify-content:center for horizontal alignment.
Since you are using display: flex to the content div, add just the property align-items:center and your text will be centred vertically:
body {
width: 100%;
max-width: 960px;
margin: 0;
}
div.content {
width: 100vw;
display: flex;
align-items:center;
}
div.column1 {
width: 15%;
background-color: #F7F7F7;
overflow: hidden;
height: 100vh;
}
div.column2 {
width: 70%;
overflow: hidden;
}
.banner {
width: 100vw;
height: 10vh;
}
.container2 {
display: flex;
}
<div class="content">
<div class="column1">
</div>
<div class="column2">
<div class="container2">
<div class="lobby">
<img src="img/lobby.jpg" alt="" /> </div>
<div class="content">
<p>lorem50gsdgsdsgdgsgdfgdfgdfgdfgfdggsd</p>
</div>
</div>
</div>
<div class="column1">
</div>
</div>
In order to make it work, try to think with me ok? In order to you understand what is happening here:
First of all if you have a parent div its children should be one bellow one another
Your first step is to set the div to have flex: 1, flex check it out this website to learn more.
now set the items to be side by side with display: flex
set the same container with justify-content: center and align-items:center
and if you wish to align the div to the middle of your page, try this: margin: 0 auto
Here is where the magic happens: flex-direction: column, check the documentation flex-direction

how to dispay 3 divs inline

i have 3 divs, i want for a navbar, i need the navbar to be responsive
They cant stay inline on a width less than 800px, what im i doing wrong
.logo {
width: 18%;
min-width: 120px;
display: inline-block;
}
.middle {
width: 60%;
display: inline-block;
}
.user {
width: 18%;
min-width: 100px;
display: inline-block;
}
<div class="navbar">
<div class="logo"> LOGO </div>
<div class="middle"> About, contact </div>
<div class="user"> signin </div>
</div>
Use display: flex; in .navbar
The default flex direction is row so that should align your 3 divs horizontally .
And remove display properties from the child divs.
Give them flex-basis for respective widths

CSS divs into columns and image stretch to window

I have three columns inside a div called row and each column is in a div called third-col. I want the three columns side by side (inline) and then the next div contact to be below them. Currently all of the divs are in block one after another.
Another problem I'm having is with my home image. When the browser window is not maximized I want the image to still stretch to the bottom of the page.
img {
padding: 0;
display: block;
margin: 0 auto;
max-height: 100%;
max-width: 100%;
text-align: center;
}
.row {
padding: 0 20px;
display: inline;
}
.third-col {
width: 30.3%;
font-size: 16px;
display: inline;
}
.col {
float: left;
box-sizing: border-box;
}
<img class="center" src="homepage.jpg" alt="">
<section id="skills">
<p class="header">My Skills</p>
<div class="skillsContainer">
<div id="row">
<div class="third-col">
<ul>
<li>items</li>
</ul>
</div>
</div>
</div>
</section>
For you background-image, you will want it to set it using CSS. This will allow it to stretch from side to side, top to bottom. Here is an example:
.body { margin: 0; padding: 0;}
.full-page-image {
background-image: url(https://images.unsplash.com/photo-1489914099268-1dad649f76bf?auto=format&fit=crop&w=2850&q=80);
background-size: cover; /* THIS MAKES THE IMAGE STRETCH TO ALWAYS COVER THE PAGE */
background-position: center center;
position: relative;
width: 100%;
height: 100vh; /* This is 100% of the height */
}
<div class="full-page-image"></div>
<h1>Page content goes here</h1>
For the rows, I suggest using flexbox. Here is a complete guide: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
The idea is that each row is 100% of the witdh of the page. The content inside the row will be divided by the width you want. Here is an example:
.row{
padding: 0 20px;
display: flex; /* Makes the sub-elements flex */
flex-wrap: wrap; /* Forces flexbox to respect children width */
align-items: stretch; /* Makes childs the same height */
}
.third-col{
width: 33%;
font-size: 16px;
min-height: 40px;
}
<div class="row">
<div class="third-col">Col 1</div>
<div class="third-col">Col 2</div>
<div class="third-col">Col 3</div>
</div>
<div class="row">
<div>Other page content</div>
</div>

responsive 3-columns layout with third column having fixed size

I am trying to create a simple element consisting of 3 columns, the 3. column being an icon-image.
I want the element to be responsive, but the icon-image should always have the same size (e.g. 40x40 px).
So, the 1. and 2. column should have % values, the 3. column should have width 40px. I don't want the icon-image to resize on smaller screens. The icon should always have width 40px.
.container {
width: 80%;
}
.text1 {
float: left;
width: 30%;
}
.text2 {
float: left;
width: 50%;
}
.image {
float: right;
width: 120px;
}
<div class="container">
<p class="text1">Some Titel</p>
<h3 class="text2">Some Text and some more text...</h3>
<img src="image1.jpeg" alt="" class="image" />
</div>
https://jsfiddle.net/mkrizanek/usfmk6r7
Kind regards,
Milan
I have tweaked a little bit with HTML and CSS. But I hope you will get the logic. I have changed the display-type of container to flex. You can change the with of elements inside container as per your requirements.
.container {
background-color: #DDDDDD;
width: 80%;
display: flex;
}
.text {
background-color: #BBBBBB;
width: 50%;
}
.heading {
background-color: #999999;
width: 50%;
}
img {
max-width: 120px;
max-height: 120px;
}
<div class="container">
<p class="text">Some Text</p>
<h3 class="heading">Heading</h3>
<img src="https://www.w3.org/html/logo/downloads/HTML5_Logo_512.png" class="image" />
</div>
Your best option is probably to use flex.
Set "display: flex; justify-content: space-between" on the container div, and "flex: 1;" on each section that you want responsive.
If you want the text in the second div to push all the way to the image, you can also add "text-align: right;" to it.
.container {
display: flex;
justify-content: space-between;
}
.text1 {
flex: 1;
}
.text2 {
flex: 1;
}
.image {
width: 40px;
height: 40px;
background: #088;
}
<div class="container">
<p class="text1">Some Title</p>
<h3 class="text2">Some Text and some more text...</h3>
<img alt="" class="image" />
</div>
Here is a good resource for flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/.

HTML / CSS - Center multiple divs inline

I'm try to accomplish something rather basic but I failed to succeed, so far.
I have an image and a title which should be displayed in the center of the parent selector (div). Unfortunately I don't know how to properly align them when I center both the logo and the text.
Html:
<div class="content">
<div class="header">
<img src="path/to/image">
<span>Hey this is my title</span>
</div>
</div>
CSS:
.header{
text-align: center;
width: 100%;
height: 40px;
}
.header img{
height: 40px;
width: auto;
display: inline-block;
}
.header span{
display: inline-block;
}
As you can see the logo has a static width/height while the text can have a variable height.
The code above makes the styling like the example below:
Can anyone tell me how to do this? I basically want several divs next to eachother, but all aligned in the center.
Are you looking for vertical-align: middle to center vertically?
.header {
text-align: center;
width: 100%;
height: 40px;
}
.header img {
height: 40px;
width: auto;
display: inline-block;
vertical-align: middle;
}
.header span {
display: inline-block;
vertical-align: middle;
background: #eef;
}
<div class="content">
<div class="header">
<img src="//placehold.it/40?text=Logo" />
<span>Hey this is my title</span>
</div>
</div>
Note: I have added background for better visibility of the borders.
A better explanation:
Use flexbox. I don't know if I correctly got that you want your items to be centered both horizontally and vertically inside header. If not just delete the justify-content part.
.header {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
More info about flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/