Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image to the right on desktop, centered on mobile

Tags:

html

css

I am making a website with bootstrap. In my jumbotron, I have some text to the left, and an image to the right. On mobile, the image appears on the top instead.

How it looks on desktop:
enter image description here

How it looks on mobile:
enter image description here
The problem is, on mobile, it would be nicer if the image was centered, instead of floated to the right. Is that possible?

HTML:

<div class="jumbotron">
  <div class="container" id="main">

    <div class="img"
        <p id="pic"><img id="pic" src="http://cube.crider.co.uk/visualcube.php?fmt=png&amp;size=800&amp;bg=t&amp;alg=F2%20D2%20L2%20F2%20L2%20F%20U2%20R2%20L%20U%27%20B%20D%27%20F%27%20U2%20L%20D2%20L2%20F2" width="300px" height="300px"></p> 
    </div>

    <h1>Example </h1>
    <p>Example Example Example Example Example Example Example</p>
    <p>Example Example Example Example Example Example Example</p>
    <p>Example Example Example Example Example Example Example</p>

  </div>
</div>

CSS:

.img{
  float: right;
  margin-right: 40px;
  margin-bottom: 20px;
  display: inline;
} 

Example website

like image 680
Mikkel Avatar asked May 16 '15 11:05

Mikkel


People also ask

How do I center a picture in mobile view?

The first is to set your image to 'display: inline-block' and then wrap it with an outer div where you set the 'text-align' property to center. The other solution is to make sure your img is a block element (display: block) and then set the margin-right and margin-left to auto.

How do I position one image on top of another?

As the simplest solution. That is: Create a relative div that is placed in the flow of the page; place the base image first as relative so that the div knows how big it should be; place the overlays as absolutes relative to the upper left of the first image. The trick is to get the relatives and absolutes correct.


2 Answers

Try this below code in to your css:

.img{
  float: right;
  margin-right: 40px;
  margin-bottom: 20px;
  display: inline;
} 

@media (min-width: 320px) and (max-width: 767px) {
    .img{
        float:none;
        display:table;
        margin: 0 auto;
    }
    }

or

@media (max-width: 767px) and (min-width: 320px)
.img {
  /* float: none; */
  /* display: table; */
  margin: 0;
  width: 100%;
  TEXT-ALIGN: CENTER;
}
like image 75
Nitekurs Avatar answered Sep 24 '22 18:09

Nitekurs


first enclose your text in <div>:

<div id="textwithimg">
    <h1>Example </h1>
    <p>Example Example Example Example Example Example Example</p>
    <p>Example Example Example Example Example Example Example</p>
    <p>Example Example Example Example Example Example Example</p>
</div>

then use media queries :

//for mobile 
       @media (min-width: 400px) and (max-width: 500px) {
        .img{
            margin-left: auto;
            margin-right: auto;
            width: 70%;
            background-color: #b0e0e6;
            }
        }
    //normal css for any size 
          .img{
              float: right;
              margin-right: 40px;
              margin-bottom: 20px;
              display: inline;
           } 
           #textwithimg{
              desplay:inline;
              float:left;
            }
like image 37
Abdul Manan Avatar answered Sep 21 '22 18:09

Abdul Manan