Why I can't center the components horizontally in the div? - html

I have block and inside the blocks, there are three boxes. The CSS is
.blocks {
display: block;
margin: 0 auto;
width: 60%;
height: 350px;
}
.box1, .box2, .box3 {
width: 33.333%;
height: 300px;
vertical-align: top;
display: inline-block;
zoom: 1;
}
In the box, there is an image and a text. I want them centered horizontally.
So I did as
.box1, .box2, .box3 {
width: 33.333%;
height: 300px;
vertical-align: top;
display: inline-block;
zoom: 1;
margin-left: auto;
margin-right: auto;
}
But they are not centered. As shown in the image.
My HTML is
<div class="blocks">
<div class="col-sm-4 col-xs-4 box1" style="background-color:lavender;">
<!-- One image and text here -->
</div>
<div class="col-sm-4 col-xs-4 box2" style="background-color:lavenderblush;">
<!-- One image and text here -->
</div>
<div class="col-sm-4 col-xs-4 box3" style="background-color:lavender;">
<!-- One image and text here -->
</div>
</div>
How to center the components horizontally in the div?

Just give text-align: center; to all the three box.
.box1, .box2, .box3 {
width: 33.333%;
height: 300px;
vertical-align: top;
display: inline-block;
zoom: 1;
text-align: center;
}
Working Fiddle

To center the text, you could use
text-align: center;
To center the images, you need to put a margin on them
.blocks img {
margin: 0 auto;
}

give the imgs a width: 100% and center the text
.blocks {
display: block;
margin: 0 auto;
width: 60%;
height: 350px;
}
.box1,
.box2,
.box3 {
width: 33.333%;
height: 300px;
vertical-align: top;
display: inline-block;
}
.box1,
.box2,
.box3 {
width: 33.333%;
height: 300px;
vertical-align: top;
display: inline-block;
margin-left: auto;
margin-right: auto;
}
img{
width: 100%;
}
h5{
text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="blocks">
<div class="col-sm-4 col-xs-4 box1" style="background-color:lavender;">
<!-- One image and text here -->
<img src="http://i.imgur.com/IMiabf0.jpg" alt="" />
<h5>This is Cute Cat</h5>
</div>
<div class="col-sm-4 col-xs-4 box2" style="background-color:lavenderblush;">
<!-- One image and text here -->
<img src="http://i.imgur.com/IMiabf0.jpg" alt="" />
<h5>This is Cute Cat</h5>
</div>
<div class="col-sm-4 col-xs-4 box3" style="background-color:lavender;">
<!-- One image and text here -->
<img src="http://i.imgur.com/IMiabf0.jpg" alt="" />
<h5>This is Cute Cat</h5>
</div>
</div>

The logic to centre align the container is fine, but there are some spacing between those components. hence, all components doesn't fit in blocks.
Adding float:left; to each box will fix the issue.
.blocks {
display: block;
margin: 0 auto;
width: 60%;
height: 350px;
}
.box1, .box2, .box3 {
width: 33.333%;
height: 300px;
vertical-align: top;
display: inline-block;
zoom: 1;
float: left;
}
<div class="blocks">
<div class="col-sm-4 col-xs-4 box1" style="background-color:lavender;">
<img src="pic_mountain.jpg" alt="Mountain View" style="width:100px;height:100px;">
</div>
<div class="col-sm-4 col-xs-4 box2" style="background-color:lavenderblush;">
<img src="pic_mountain.jpg" alt="Mountain View" style="width:100px;height:100px;">
</div>
<div class="col-sm-4 col-xs-4 box3" style="background-color:lavender;">
<img src="pic_mountain.jpg" alt="Mountain View" style="width:100px;height:100px;">
</div>
</div>

Related

Align Title/Header with Text HTML, CSS

I am using a centred div to contain an image and some text but want my title to have the same margin/alignment as the text. Right now the title is placed on the extreme left of the page and I want it to have a responsive margin on the left.
.row {
margin: auto;
max-width: 1150px;
display: flex;
align-items: center;
img {
width: auto;
}
}
<div class="container-fluid">
<h1>Choosing a Console</h1>
<div class="row">
<div class="col-sm" id="textbox">
<p>
Some Text
</p>
</div>
<div class="col-sm" id="img">
<img style="height: 350px" src="which.png" alt="Which One">
</div>
</div>
</div>
there are two ways you could do this;
Add margin-left to the div h2 tag or add a container
.row {
margin: auto;
max-width: 1150px;
display: flex;
align-items: center;
img {
width: auto;
}
}
.container-fluid h1 {
margin-left: 150px;
}
or you can over complicate it like I do and contain containers, I have added border outlines so you understand what that container is containing, if you wish to move the h1 and p text, you can margin-left them both.
.row {
max-width: 1150px;
height: auto;
border: 1px solid red;
}
.rowTextContainer {
float: left;
width: auto;
margin-left: 50px;
}
.rowText {
text-align: center;
border: 1px solid blue;
}
.row img {
width: auto;
padding-left: 20px;
}
<div class="row">
<div class="rowTextContainer">
<div class="rowText">
<h1>Title Text</h1>
<p>Text here.</p>
</div>
</div>
<img src="https://via.placeholder.com/350.png">
</div>
.row {
margin: auto;
max-width: 1150px;
display: flex;
flex-direction: column;
align-items: center;
}
.col-sm {
display: flex;
flex-direction: row;
margin-left: 10%;
}
img {
width: auto;
margin-left: 10px;
}
<div class="container-fluid">
<h1>Choosing a Console</h1>
<div class="row">
<div class="col-sm" id="textbox">
<p>
Some Text go ahead . . . . .
</p>
<img style="height: 350px" src="https://picsum.photos/200" alt="Which One">
</div>
</div>
</div>
I hope it will solve your problem.

Having an issue with alignment of image/text

Trying to align the image vertically, and the text centered according to image. The whole card is about 300px * 200px. I want the image to be on the left hand side with text on the right.
<div class="card center-block">
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="photo">
<img class="rounded" src="../im/3.jpg" width="130" height="130" style="min-height:50px;" />
}
</div>
</div>
<div class="col-sm-6" style="height: 250px; align-items:center">
<div class="content">
<h5 class="left-align"><strong>John Doe</strong></h5>
<p class="left-align">Software Developer</p>
</div>
</div>
</div>
</div>
CSS:
.card {
min-width: 350px;
max-width: 350px;
min-height: 200px;
max-height: 200px;
}
.photo {
padding-top: 35px;
margin-right: 10px;
display:inline-block;
height: 100%;
float: left;
}
.content{
height: 100%;
}
You can add a class to the row and use flexbox positioning. align-items: center on the row that holds the 2 columns, then also use flex on the .content element and create a flex column with justify-content: center
.card {
min-width: 350px;
max-width: 350px;
min-height: 200px;
max-height: 200px;
}
.photo {
margin-right: 10px;
display:inline-block;
height: 100%;
float: left;
}
.special-row {
display: flex;
align-items: center;
}
.special-row .content {
display: flex;
flex-direction: column;
justify-content: center;
height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card center-block">
<div class="container"><div class="row special-row">
<div class="col-sm-6">
<div class="photo">
<img class="rounded" src="http://androidandme.com/wp-content/uploads/2015/12/Google-Now-on-Tap.jpg"width="130" height="130" style="min-height:50px;" />
</div>
</div>
<div class="col-sm-6 details" style="height: 250px; align-items:center">
<div class="content">
<h5 class="left-align"><strong>John Doe</strong></h5>
<p class="left-align">Software Developer</p>
</div>
</div>
</div>
</div>

Center vertically when the height of element is unknown

I want to center vertically text, when the elements height is unknown?
html
<div class="table">
<div class="table-resp">
<div class="second-row">
<div class="col-md-5">
<div class="left-col-text">
Center vertically
</div>
</div>
<div class="col-md-7">
<div class="right-col-text">
<div class="example">Ex1</div>
<div class="example">Ex2</div>
<div class="example">Ex3</div>
</div>
</div>
</div>
</div>
</div>
css
/* CSS used here will be applied after bootstrap.css */
.table{
text-align: center;
padding-top: 70px;
padding-left: 0px;
padding-right: 35px;
}
.table-resp{
border: 1px solid green;
overflow-x: hidden;
}
.text1{
float: left;
display: inline-block;
}
.second-row{
line-height: 30px;
clear: left;
min-height: 30px;
overflow: auto;
}
.left-col-text{
height: 100%;
}
Elements "Ex1, Ex2" count is unknown, so, if there are more of those, obviously, the table row will get bigger in height. I need some solution, that would be responsive to this also...
https://www.codeply.com/go/bp/4ZEUS7Q7lm
Just add row-ht-eq class to row <div class="second-row">
CSS:
.row-ht-eq {
display: flex;
align-items: center;
}
position: absolute;
top: 50%;
transform: translateY(-50%);
Also you can play with:
display: table-cell;
vertical-align: middle;
Note: Use span Element as helper.
Html:
<div class="col-md-5">
<span class="helper"></span>
<div class="left-col-text">
Center vertically
</div>
</div>
Css:
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
Full Code:
.table{
text-align: center;
padding-top: 70px;
padding-left: 0px;
padding-right: 35px;
}
.table-resp{
border: 1px solid green;
overflow-x: hidden;
}
.text1{
float: left;
display: inline-block;
}
.second-row{
line-height: 30px;
clear: left;
min-height: 30px;
overflow: auto;
}
.left-col-text{
height: 100%;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="table">
<div class="table-resp">
<div class="second-row">
<div class="col-md-5">
<span class="helper"></span>
<div class="left-col-text">
Center vertically
</div>
</div>
<div class="col-md-7">
<div class="right-col-text">
<div class="example">Ex1</div>
<div class="example">Ex2</div>
<div class="example">Ex3</div>
</div>
</div>
</div>
</div>
</div>
Change your text class to:
.left-col-text {
margin:0 auto;
}
This will automatically decide equal distance from top to bottom.

Trouble centering divs within a row in brackets

I'm trying to center 4 columns inside a a row. Trying in code pen or similar works like I want, but in adobe-brackets is not working, I don't know if there is a bug or something...
Does anyone has any idea?
PD.: in my brackets output, everything is on the left.
body {
position: relative;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.container-fluid {
width: inherit;
height: inherit;
margin: 0;
padding: 0;
}
.content {
padding: 100px 0;
}
.content-2 {
color: violet;
background-color: blueviolet;
}
div.img {
margin: 5px;
float: center;
width: 180px;
}
div.img img {
width: 100%;
height: auto;
}
div.desc {
padding: 15px;
text-align: center;
}
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
margin-right: 0 auto;
width: 90%;
}
<section class="content content-2">
<div class="container">
<div class="row row-centered">
<div class="img col-md-2 col-centered">
<img src="forest.jpg" alt="Forest">
<div class="desc">
<h3>CREATIVIDAD</h3>
<p>hhhhhhhhhhhhhhhh</p>
</div>
</div>
<div class="img col-md-2 col-centered">
<img src="forest.jpg" alt="Forest">
<div class="desc">
<h3>DISEÑO</h3>
<p>hhhhhhhhhhh</p>
</div>
</div>
<div class="img col-md-2 col-centered">
<img src="forest.jpg" alt="Forest">
<div class="desc">
<h3>MULTIMEDIA</h3>
<p>hhhhhhhhhhhhh</p>
</div>
</div>
<div class="img col-md-2 col-centered">
<img src="forest.jpg" alt="Forest">
<div class="desc">
<h3>WEB</h3>
<p>hhhhhhhhhhh</p>
</div>
</div>
</div>
</div>
</section>
Use CSS Flexbox. Apply flexbox properties to .row-centered as well as .col-centered:
.row-centered {
display: flex; /* Flex Container */
flex-wrap: wrap; /* Wrap the content to next line if required */
justify-content: center; /* Horizontally content to center */
align-items: center;
}
.col-centered {
display: flex; /* Flex Container */
flex-direction: column; /* Flex Direction - Column */
justify-content: center; /* Vertically Center Alignment */
float: none;
margin-right: 0 auto;
width: 90%;
}
Have a look at this snippet below (use full screen):
body {
position: relative;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.container-fluid {
width: inherit;
height: inherit;
margin: 0;
padding: 0;
}
.content {
padding: 100px 0;
}
.content-2 {
color: violet;
background-color: blueviolet;
}
div.img {
margin: 5px;
float: center;
width: 180px;
}
div.img img {
width: 100%;
height: auto;
}
div.desc {
padding: 15px;
text-align: center;
}
.row-centered {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.col-centered {
display: flex;
flex-direction: column;
justify-content: center;
float: none;
margin-right: 0 auto;
width: 90%;
}
<section class="content content-2">
<div class="container">
<div class="row row-centered">
<div class="img col-md-2 col-centered">
<img src="http://placehold.it/30x30" alt="Forest">
<div class="desc">
<h3>CREATIVIDAD</h3>
<p>hhhhhhhhhhhhhhhh</p>
</div>
</div>
<div class="img col-md-2 col-centered">
<img src="http://placehold.it/30x30" alt="Forest">
<div class="desc">
<h3>DISEÑO</h3>
<p>hhhhhhhhhhh</p>
</div>
</div>
<div class="img col-md-2 col-centered">
<img src="http://placehold.it/30x30" alt="Forest">
<div class="desc">
<h3>MULTIMEDIA</h3>
<p>hhhhhhhhhhhhh</p>
</div>
</div>
<div class="img col-md-2 col-centered">
<img src="http://placehold.it/30x30" alt="Forest">
<div class="desc">
<h3>WEB</h3>
<p>hhhhhhhhhhh</p>
</div>
</div>
</div>
</div>
</section>
Hope this helps!

CSS How to have a text caption in the bottom of an image?

I'm very new to css and I am trying to have a text caption in the bottom of my images, like so:
I have 3 images in 3 different bootstrap columns.
.img-preview {
display: block;
margin: 0 auto;
object-fit: cover;
min-height: 300px;
max-width: 350px;
}
.img-container {
width: 100%;
height: auto;
padding: 0;
background-position: center top;
}
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="col-md-4 col-sm-6">
<img class="img-container img-preview" src="../img/Referenzen_Stimmungsbild.jpg">
<h3><span>Maler<br />und<br />Tapezierarbeiten</span></h3>
</div>
<div class="col-md-4 col-sm-6">
<img class="img-container img-preview" src="../img/Referenzen_Stimmungsbild.jpg">
</div>
<div class="col-md-4 col-sm-6">
<img class="img-container img-preview" src="../img/Referenzen_Stimmungsbild.jpg">
</div>
</div>
I tried a lot of possible solutions but nothing really worked.
Tried this for example https://css-tricks.com/text-blocks-over-image/ but when I resized my window, the text caption didn't fit anymore.
What is the best way to achieve this effect?
you can simply do with Position:absolute , try with this answer
.relative_box {
position:relative;
width:400px;
float:left;
}
.relative_box img {
width:100%;
}
.relative_box h3 {
position:absolute;
bottom:0;
left:0;
background:#000;
color:#fff;
width:100%;
margin:0;
padding:10px
}
<div class="relative_box">
<img src="https://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg" alt="" />
<h3><span>Maler<br />und<br />Tapezierarbeiten</span></h3>
</div>
You can do it using CSS Positioning. But you need to change the structure a little bit. Have a look at the snippet below:
.img-holder {
min-height: 300px;
max-width: 350px;
margin: 0 auto;
position: relative;
}
.img-holder h3 {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
background: rgba(65, 55, 91, 0.8);
color: white;
bottom: 0;
left: 0;
width: 80%;
height: 30%;
margin: 0;
padding: 10px;
text-align: center;
}
.img-preview {
display: block;
margin: 0 auto;
object-fit: cover;
min-height: 300px;
max-width: 350px;
}
.img-container{
width: 100%;
height: auto;
padding: 0;
background-position: center top;
}
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="col-md-4 col-sm-6">
<div class="img-holder">
<img class="img-container img-preview" src="http://placehold.it/200x200">
<h3><span>Maler<br />und<br />Tapezierarbeiten</span></h3>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="img-holder">
<img class="img-container img-preview" src="http://placehold.it/200x200">
<h3><span>Maler<br />und</span></h3>
</div>
</div>
</div>
Hope this helps!