PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label center. Show all posts
Showing posts with label center. Show all posts

Friday, November 18, 2022

[FIXED] Why are images centered vertically with `line-height` positioned 2 pixels below where they should be?

 November 18, 2022     browser-bugs, center, css, image, vertical-alignment     No comments   

Issue

Short story:

jsfiddle here. The behavior is consistently wrong in Chrome 21, Firefox 15 and IE9, which makes me think I'm misunderstanding something about the CSS spec.

Longer story:

I want to center an image vertically using line-height. I've set the height of the image container equal to the line-height, I've reset margins, paddings and borders for all elements, yet the image is 2 pixels below where it should be. This happens whether the image is smaller than the container, or larger than it (in which case I used max-width: 100; max-height: 100% to size it down).

The image has an even number of pixels in both cases. I wouldn't care that much for images smaller than the container, but for images that are larger, a 2-pixel line from the container background will bleed through at the top of the image.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Images centered vertically with line-height have center miscalculated by 2 pixels</title>

    <style type="text/css">
        * { margin: 0; padding: 0; border: 0px solid cyan; }  /* reset all margins, paddings and borders */
        html {
            font-size: 16px;
            line-height: normal;
        }
        body {
            line-height: 100%;
            background: gray;
        }
        .img-wrapper-center {
            width: 400px;
            height: 200px;  /* necessary, besides line-height, if images are taller than it */
            line-height: 200px;  /* must be equal to line-height */
            text-align: center;
            background: white;
        }
        .img-wrapper-center img {
            vertical-align: middle;
            /* Comment the two rules below. The first image will still be mis-aligned by two pixels, but the second will not. */
            max-width: 100%; max-height: 100%;  /* force scaling down large images */
        }

    </style>
</head>

<body>

    <div class="img-wrapper-center" id="div1">
        <img src="http://gravatar.com/avatar" id="img1"/>  <!-- 80x80 -->
    </div>

    <div class="img-wrapper-center" id="div2" style="background: green">
        <img src="http://www.w3.org/html/logo/img/class-header-semantics.jpg" id="img2" />  <!-- 495x370 -->
    </div>

    <p>
    Note how the second image is misaligned down by two pixels.
    The first one is mis-aligned the same way. Sizes and coordinates are below.
    </p>

    <script>
        document.writeln('div1 top: ' + document.getElementById('div1').offsetTop + '<br/>');
        document.writeln('image 1 height: ' + document.getElementById('img1').offsetHeight + '<br/>');
        document.writeln('div1 height: ' + (document.getElementById('div1').offsetHeight) + '<br/>');
        document.writeln('image 1 top should be at: ' + (document.getElementById('div1').offsetHeight - document.getElementById('img1').height) / 2 + '<br/>');
        document.writeln('image 1 top actually is at: ' + document.getElementById('img1').offsetTop + '<br/>');
        document.writeln('div2 top: ' + document.getElementById('div2').offsetTop + '<br/>');
        document.writeln('img2 top: ' + document.getElementById('img2').offsetTop + '<br/>');
    </script>

</body>
</html>

Solution

You have to set zero font-size for container div elements. Because images are displayed as inline elements they are affected by text in container div. Zero font-size fixes this:

.img-wrapper-center
{
    font-size:0;
}

Here is fiddle: http://jsfiddle.net/bZBsR/



Answered By - keaukraine
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to center image vertically in div

 November 18, 2022     center, css, html, image, vertical-alignment     No comments   

Issue

I have a div which has a height of 100vh so that it's always the height of the browser screen. Inside of this div I want to place an image and center it vertical to its parent.

The height is variable so I can't use fixed margins. My current Markup is as follows:

HTML

   <div class="textportfolio">

      <h1>This is a title</h1>

      <p class="textbio-small">
      The Roosevelt dime is the current ten-cent piece of the United States.
      </p>

      <img class="portfolio-slides-img" src="https://i.imgur.com/iheO43X.png">

   </div>

CSS:

.textportfolio {
    font-family: "Lora", serif;
    margin: 5%;
    background: #e9cca3;
    height: 100vh;
}

img.portfolio-slides-img {
    max-width: 40%;
    max-height: 40%;
    margin: 0 auto;
    display: block;
    }

Does anybody know how to center the image vertically according to the browser height?

Here is the code snippet

.textportfolio {
    font-family: "Lora", serif;
    margin: 5%;
    background: #e9cca3;
    height: 100vh
}

img.portfolio-slides-img {
    margin-top: 15%;
    max-width: 40%;
    max-height: 40%;
    margin: 0 auto;
    display: block;
    }
<div class="textportfolio">
  <h1>This is a title</h1>
  
  <p class="textbio-small">
  The Roosevelt dime is the current ten-cent piece of the United States.
  </p>
  
  <img class="portfolio-slides-img" src="https://i.imgur.com/iheO43X.png">
  
  </div>


Solution

I use this css snippet:

.selector {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}

Applied to your sample: https://jsfiddle.net/nhdh8ycr/4/



Answered By - Emaro
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do I vertically align my element in the entire DIV as opposed to only the element it is next to?

 November 18, 2022     center, css, html, vertical-alignment     No comments   

Issue

I want to vertically center a button in my DIV. There are several horizontal elements in the DIV -- an image ,a slider, another image, and the button ...

<div id="votingForm">
        <div id="sliderScaleDiv">
            <div id="halo">
                <img width="75" src="http://www.clker.com/cliparts/B/O/X/F/G/V/angel-halo-with-wings-hi.png" alt="Angel halo">
                <span class="caption">Angel</span>
            </div>
            <div class="fluid">
                <div class="slider ui-slider ui-corner-all ui-slider-horizontal ui-widget ui-widget-content ui-slider-pips"><span tabindex="0" class="ui-slider-handle ui-corner-all ui-state-default" style="left: 22.2222%;"></span><span class="ui-slider-pip ui-slider-pip-first ui-slider-pip-label ui-slider-pip-1" style="left: 0%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="1">1</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-2" style="left: 11.1111%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="2">2</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-3 ui-slider-pip-initial ui-slider-pip-selected" style="left: 22.2222%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="3">3</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-4" style="left: 33.3333%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="4">4</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-5" style="left: 44.4444%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="5">5</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-6" style="left: 55.5556%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="6">6</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-7" style="left: 66.6667%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="7">7</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-8" style="left: 77.7778%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="8">8</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-9" style="left: 88.8889%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="9">9</span></span><span class="ui-slider-pip ui-slider-pip-last ui-slider-pip-label ui-slider-pip-10" style="left: 100%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="10">10</span></span></div> 
            </div>
            <div id="skull">
                <img width="75" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Skull_and_crossbones.svg/2000px-Skull_and_crossbones.svg.png" alt="Skull">
                <span class="caption">Devil</span>
            </div>

            <form class="voteForm" id="edit_vote_16" action="/votes/16" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch"><input type="hidden" name="authenticity_token" value="QEDnhWcF8KbI+D0WGuPB8b6a7Et8RO43PMAVI4sonn6JVR8P0nCVeVoE8YAc4bVqEXeYWsSPagfA8V8m+ONNsw==">
                <input type="hidden" value="16" name="vote[id]" id="vote_id">
                <input value="43" type="hidden" name="vote[person_id]" id="vote_person_id">
                <input type="hidden" value="3" name="vote[score]" id="vote_score">
                <button name="next" type="button" id="nextButton" class="btn-feedback">Next</button>
</form>
        </div>

    </div>

I thought I could pull this off by using this CSS

#votingForm {
        width: 100%;
        display: flex;
        align-items: center;
        background-color: red;
}

#sliderScaleDiv {
        display: table;
        width: 95%;
}

#halo {
  position: relative;
  z-index: 20;
  vertical-align: top;
  display: inline-block;
  width: 75px;
  display: table-cell;
  font-family: 'kingthings_petrockregular';
}

@media only screen and (max-width: 500px) {
        #halo {
                display:none;
        }
}

.fluid {
        display: table-cell;
        width: 100%;
}

.slider {
        margin: 25px 10px;
        cursor: pointer;
}

#skull {
  position: relative;
  z-index: 20;
  vertical-align: top;
  display: inline-block;
  width: 75px;
  display: table-cell;
  color: orange;
  font-family: 'char_bbregular';
}


#nextButton {
    display: inline-block;
}

.caption {
        display: block;
}

.voteForm {
        width: 5%;
        display: inline-block;
        padding: 10px;
        background-color: orange;
}

but my button seems to be vertically aligning with the image to its left as opposed to the entire DIV --https://jsfiddle.net/aL47cLpq/ . How do I get the button to vertically align in the DIV as opposed to just the element it is next to?


Solution

I think the easiest way of doing this is with flexbox. Chris Coyier has done a really useful guide to using this here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Make sure all items have equal height:

#sliderScaleDiv {
    display: flex;
    align-items: stretch;
}

Matches height of form to parent and aligns the button using "align-items":

.voteForm {
    display: flex;
    align-items: center;
}

Updated jsfiddle: https://jsfiddle.net/uL7j2ah4/3/



Answered By - Antfish
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do I vertically align my element in the entire DIV as opposed to only the element it is next to?

 November 18, 2022     center, css, html, vertical-alignment     No comments   

Issue

I want to vertically center a button in my DIV. There are several horizontal elements in the DIV -- an image ,a slider, another image, and the button ...

<div id="votingForm">
        <div id="sliderScaleDiv">
            <div id="halo">
                <img width="75" src="http://www.clker.com/cliparts/B/O/X/F/G/V/angel-halo-with-wings-hi.png" alt="Angel halo">
                <span class="caption">Angel</span>
            </div>
            <div class="fluid">
                <div class="slider ui-slider ui-corner-all ui-slider-horizontal ui-widget ui-widget-content ui-slider-pips"><span tabindex="0" class="ui-slider-handle ui-corner-all ui-state-default" style="left: 22.2222%;"></span><span class="ui-slider-pip ui-slider-pip-first ui-slider-pip-label ui-slider-pip-1" style="left: 0%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="1">1</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-2" style="left: 11.1111%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="2">2</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-3 ui-slider-pip-initial ui-slider-pip-selected" style="left: 22.2222%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="3">3</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-4" style="left: 33.3333%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="4">4</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-5" style="left: 44.4444%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="5">5</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-6" style="left: 55.5556%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="6">6</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-7" style="left: 66.6667%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="7">7</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-8" style="left: 77.7778%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="8">8</span></span><span class="ui-slider-pip ui-slider-pip-label ui-slider-pip-9" style="left: 88.8889%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="9">9</span></span><span class="ui-slider-pip ui-slider-pip-last ui-slider-pip-label ui-slider-pip-10" style="left: 100%"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="10">10</span></span></div> 
            </div>
            <div id="skull">
                <img width="75" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Skull_and_crossbones.svg/2000px-Skull_and_crossbones.svg.png" alt="Skull">
                <span class="caption">Devil</span>
            </div>

            <form class="voteForm" id="edit_vote_16" action="/votes/16" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch"><input type="hidden" name="authenticity_token" value="QEDnhWcF8KbI+D0WGuPB8b6a7Et8RO43PMAVI4sonn6JVR8P0nCVeVoE8YAc4bVqEXeYWsSPagfA8V8m+ONNsw==">
                <input type="hidden" value="16" name="vote[id]" id="vote_id">
                <input value="43" type="hidden" name="vote[person_id]" id="vote_person_id">
                <input type="hidden" value="3" name="vote[score]" id="vote_score">
                <button name="next" type="button" id="nextButton" class="btn-feedback">Next</button>
</form>
        </div>

    </div>

I thought I could pull this off by using this CSS

#votingForm {
        width: 100%;
        display: flex;
        align-items: center;
        background-color: red;
}

#sliderScaleDiv {
        display: table;
        width: 95%;
}

#halo {
  position: relative;
  z-index: 20;
  vertical-align: top;
  display: inline-block;
  width: 75px;
  display: table-cell;
  font-family: 'kingthings_petrockregular';
}

@media only screen and (max-width: 500px) {
        #halo {
                display:none;
        }
}

.fluid {
        display: table-cell;
        width: 100%;
}

.slider {
        margin: 25px 10px;
        cursor: pointer;
}

#skull {
  position: relative;
  z-index: 20;
  vertical-align: top;
  display: inline-block;
  width: 75px;
  display: table-cell;
  color: orange;
  font-family: 'char_bbregular';
}


#nextButton {
    display: inline-block;
}

.caption {
        display: block;
}

.voteForm {
        width: 5%;
        display: inline-block;
        padding: 10px;
        background-color: orange;
}

but my button seems to be vertically aligning with the image to its left as opposed to the entire DIV --https://jsfiddle.net/aL47cLpq/ . How do I get the button to vertically align in the DIV as opposed to just the element it is next to?


Solution

I think the easiest way of doing this is with flexbox. Chris Coyier has done a really useful guide to using this here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Make sure all items have equal height:

#sliderScaleDiv {
    display: flex;
    align-items: stretch;
}

Matches height of form to parent and aligns the button using "align-items":

.voteForm {
    display: flex;
    align-items: center;
}

Updated jsfiddle: https://jsfiddle.net/uL7j2ah4/3/



Answered By - Antfish
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to vertically center text inside flex element

 November 18, 2022     center, css, flexbox, vertical-alignment     No comments   

Issue

I have an element, which can contain 1-3 another elements. Let's say it's like this:

<div id="elem1">
   <div class="inner_element">aaaa</div>
   <div class="inner_element">bbbb</div>
</div>

The inner elements have to be positioned in column direction. I need their text to be centered vertically and horizontally.

I have my CSS like this:

#elem1 {
    display: flex;
    flex-direction: column;
}

.inner_element {
    text-align: center;
    flex: 1 100%;
}

The text is aligned horizontally, but I cannot find any combination of flex attributes, which will align the text vertically as well. Please, can anybody advice, how to center the text inside the inner elements vertically?


Solution

To center the flex items along the main axis (in your case column/vertically), add justify-content: center; to the container (and erase the flex settings from the children):

#elem1 {
  height: 180px;
  border: 1px solid #ddd;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.inner_element {
  text-align: center;
}
<div id="elem1">
  <div class="inner_element">aaaa</div>
  <div class="inner_element">bbbb</div>
</div>



Answered By - Johannes
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to make "vertical-align:middle" work when parent container specifies "display: table"?

 November 18, 2022     center, css, html, vertical-alignment     No comments   

Issue

I was reading here -- CSS vertical-align: middle not working that if the "vertical-align:middle;" property isn't working in CSS, its because the parent container does not have a "display: table;" specified. So I gave this a try in my parent DIV

.btn
{
    position: relative;
    margin-bottom:20px;
    padding: 0 10px;
    text-align: left;
    font-size: 16px;
    color: #333;
    background:gray;
    display: table;
    vertical-align: middle;
}

but the child element

<span>Start</span>

still aligns at the bottom -- https://jsfiddle.net/xwdnvcy5/35/ . How do I make my span element align vertically in the middle?


Solution

You missed to add display: table-cell in the .btn span style.

    .btn
    {
        position: relative;
        margin-bottom:20px;
        padding: 0 10px;
        text-align: left;
        font-size: 16px;
        color: #333;
        background:gray;
        display: table;
        
    }

    #btn_container
    {
        border-radius:5px;
        background:red;
        position: relative;
        display: inline-block;
    }

    .btn img
    {
        left: 50%;
        margin-left: 0px;
        top: 50%;
        margin-top: 0px;
    }


    .btn span
    {
        vertical-align: middle;
        display: table-cell;
    }
<div id="control" class="btn">

<div id="btn_container"><img width="100" height="100" src="https://cdn3.iconfinder.com/data/icons/minecraft-icons/512/Stone_Pickaxe.png" alt="Mining pick" /></div>

<span>Start</span> 

</div> 



Answered By - Nehemiah
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, November 17, 2022

[FIXED] How to VERTICALLY centered the logo on LEFT and navigation on RIGHT of the header?

 November 17, 2022     center, css, html, navigation, vertical-alignment     No comments   

Issue

I've been trying to solve this problem. What I want to learn is to know different ways to center the elements on navigation vertically, using semantic HTML. I want my logo on left and navigation on right.

I tried to use float on my nav element but the logo will break and will not be vertically centered. I used clearfix for that but I still can't find ways to vertically center both the logo and nav.

Will you please help me? And explain your answer please? Then if possible, can you please show me other ways of vertically centering the logo (left) and nav (right) using the exact format of my html?

Here's my code: https://codepen.io/yortz/pen/pQdKWd

HTML

  <!-- HEADER -->
  <header>
    <!-- LOGO -->
    <a href="#"><img id="site-logo" src="http://lorempixel.com/190/25/" alt="Bookworm"></a>
    <nav>
      <ul class="clearfix">
        <li><a href="#">About</a></li>
        <li><a href="#">Articles</a></li>
        <li><a href="#">News</a></li>
        <li><a href="#">Get in Touch</a></li>  
      </ul>
    </nav>
  </header>

CSS

* {
  box-sizing: border-box;
}

/* HIGHLIGHTING NAVIGATION ELEMENTS */
header {
  background-color: #ccc;
}
nav {
  background-color: aqua;
}

/* CENTERING NAVIGATION */
header {
  width: 100%;
}
#site-logo,
  nav {
  display: inline-block;
  vertical-align: middle;
}
nav ul {
  list-style-type: none;
  padding-left: 0;
}
nav ul li {
  display: inline-block;
}
nav ul li a {
  border: 1px solid red;
  padding: 10px 15px;
  display: block;
  text-decoration: none;
}
nav ul li a:hover {
  background-color: red;
  color: #fff;
}

/* CLEAR FLOATS */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

Please Help. Thank you!


Solution

I would use flexbox for the positioning in the nav

header {
   display: flex;
   justify-content: space-between; // pushes the logo to the left and the nav to the right
   align-items: center; // center aligns children of nav vertically
}

If you want to achieve something similar without using flexbox, you can position the logo absolutely:

header {
  position: relative; // with this all children can be positioned absolutely, relative to the header
  text-align: right; // this aligns the nav to the right of the header
}

header > a {
  position: absolute; // positions the logo absolute, relative to header
  top: 50%; // aligns the logo in the middle of the relative parent
  left: 0; // aligns the logo to the left edge of the relative parent
  transform: translateY(-50%); // changes the coordinates of the logo, to center it vertically (y-axis)
}

nav {
  text-align: left; // just used to reset the text-alignment in the nav elements
}

I would consider using a class instead of selecting the a-tag, for example <a class="logo" href="...">...</a> and then header .logo {...} in the CSS, instead of header > a {}. That is more future proof if you add more elements to the header.

Quick tip: If the logo is higher than the nav if will overflow the parent container, so you would need to modify the height of the parent to fix that. If you can guarantee, that the nav is always higher than the logo, this is not a problem for you and you can leave the height of the header untouched.



Answered By - Michael
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to make Bootstrap columns take the height of main?

 November 17, 2022     center, css, html, twitter-bootstrap, vertical-alignment     No comments   

Issue

I want to center vertically the divs inside the container but the columns take the height of the document and not of main (whom height equals the one of its content) Hereby my code:

<main class="col-lg-12">

 <div class="col-lg-1 col-md-0"></div>
 <div class="col-lg-4 col-md-6 col-xs-12">
  <img src={{image}}>
 </div>

<div class="col-lg-1 col-md-0" ></div>

<div class="col-lg-4 col-md-6 col-xs-12">
 <div class="title">{{title}}</div>
 <div  class="text">{{text1}}</div>  
</div>

<div class="col-lg-1 col-md-0"></div>

</main>

main div{
 height: 100%;
}

I can see in Developer Tools that the div takes 100% of the doc not of main... How could I fix this in order to vertically align the image?

Thank you!


Solution

first bootstrap .col should be in .row container you might need one in your main col to nest columns inside

then, don’t write css to set height:100% on columns, you dont need that.

If I’m right .col have display: flex, so you can use align-items-stretch class to make you column taking the height of their wrapper, being the missing .row

I suppose you will need a height:100% on the row. to do that add it a class h-100



Answered By - Faz
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, November 8, 2022

[FIXED] How to 'center' <div > for mobile

 November 08, 2022     alignment, center, css, menu, navigation     No comments   

Issue

So it works for desktop, even I'm sure I'm doing it the wrong way, but it aligns itself to left on mobile.

I tried to adding some code to @media but couldn't figure out.

I was messing mainly with right: float: padding:

any align commands don't work

@media not all and (min-width: 780px) {
  #centeredmenu { max-width: 220px; }
}



#centeredmenu {
   clear:both;
   min-width: 220px;
}


#centeredmenu ul {
   margin:0;
   padding:0;
   list-style:none;
   float:right;
   position:relative;
   right:50%;
 
}
#centeredmenu ul li {
   margin:1px 0 0 1px;
   padding:0;
   float: left;
   position: relative;
   left:50%;
   top:11px;

}
#centeredmenu ul li a {
    z-index: 999;
   display:block;
   margin:0;
   padding:.4em .2em .4em;
   line-height:1em;
   background:#ddd;
   text-decoration:none;
   color:#444;
    width: 70px;
   font-size: 13px;
    font-weight: 100;
  text-align: center;
   font-family: Proxima Nova;
}
#centeredmenu ul li.active a {
   color:#fff;
   background:#D2383C;

  
}
#centeredmenu ul li a:hover {
   background:#36f; /* Top menu items background colour */
   color:#fff;
}
#centeredmenu ul li:hover a,
#centeredmenu ul li.hover a { /* This line is required for IE 6 and below */
   background:#777777; /* Top menu items background colour */
   color:#fff;
}
<div id="centeredmenu">
<ul>
<li class=""><a href="#">2014</a></li>
<li class="active"><a href="#">2013</a></li>
<li class=""><a href="#">2012</a></li>
<li class=""><a href="#">2011</a></li>
<li class=""><a href="#">2010</a></li>
<li class=""><a href="#">2009</a></li>
<li class=""><a href="#">2008</a></li>
<li class=""><a href="#">2007</a></li>
<li class=""><a href="#">2006</a></li>
<li class=""><a href="#">2005</a></li>
  
</ul>
</div>


Solution

You could use

margin: auto;

Then your css definition would be as follows

#centeredmenu {
   clear:both;
   min-width: 220px;
   margin: auto;
}


Answered By - rasso
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to make menu button in middle and center

 November 08, 2022     alignment, center, css, css-position, menu     No comments   

Issue

I'm trying to make this text align with the border. However, as of now what I have found are just mismatches.

Defect menu-button img

.subscribe-now { 
position: absolute;
display: table-cell;
text-align: center;
background-color: #eb5e28;
line-height: 1em;
border-radius: 25px;
padding: 0.5rem 1rem;
vertical-align: center !important;
width: 200px !important;
height: 40px !important; // If I change this to auto then the size is so big and I need minus padding :(
padding-top: 0px;
padding-bottom: 0px;
border-image: 
margin-left: 20px !important;
margin-top: 20px !important;
margin-bottom: 20px !important;
}

Solution

You can try this approach. See if this works for you. Let me know

.subscribe-now { 

text-align: center;
background-color: #eb5e28;
border-radius: 25px;
vertical-align: middle;
width: 200px;
height: 40px ;
color: white;
font-weight: bold;
border: 0px solid #eb5e28;
}
<button class="subscribe-now">Langganan Sekarang</button>



Answered By - Crystal
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, July 29, 2022

[FIXED] How to center image which is bigger than screen?

 July 29, 2022     center, css, html, image     No comments   

Issue

I've following code:

HTML

<div class="banner">
    <img src="banner.png">
</div>

CSS

.banner > img {
    margin: auto;
}

The Image have a width of 3000px and a height of 200px I want the image horizontal centered and no horizontal scroll bar. (Browser resizing should no problem then)

I've tried to give the .banner div the banner image as background-image but then the div won't find it's size automatically ...

How is this possible?


Solution

Your best option is using the images as background, but if you want/need to use the image, do it like this

.banner{overflow:hidden; height:200px; width:100%; text-align:center;}
.banner img{width:100%; height:100%; margin:0 auto;}


Answered By - Devin
Answer Checked By - Cary Denson (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, July 26, 2022

[FIXED] How to center crop an image in SwiftUI

 July 26, 2022     center, crop, image, ios, swiftui     No comments   

Issue

I'm new to SwiftUI. I guess everyone is at this point. I've been an app developer for about 6 years now and I feel stupid asking this question on StackOverflow. But I looked everywhere. How do I center crop an image in an ImageView in SwiftUI?

I know there's an option to change the aspect ratio but I only see fit and fill. I just want the imageView to centerCrop(android term) the image. Does anybody know?


Solution

Android's ImageView.ScaleType documentation describes CENTER_CROP as:

CENTER_CROP

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view.

This is essentially what Aspect Fill Scaling (aka .scaledToFill()) does, except (surprisingly) Aspect Fill doesn't clip the parts that fall outside of the frame.

By making the image .resizable, and applying .scaledToFill(). the image will be scaled proportionally to fill its available frame leaving off the top and bottom or sides as necessary. .clipped() then removes the parts of the image outside of the frame.

Image("myImage")
    .resizable()
    .scaledToFill()
    .frame(width: 200, height: 200, alignment: .center)
    .clipped()

To make this more convenient, I created this extension of Image:

extension Image {
    func centerCropped() -> some View {
        GeometryReader { geo in
            self
            .resizable()
            .scaledToFill()
            .frame(width: geo.size.width, height: geo.size.height)
            .clipped()
        }
    }
}

To use the Image extension, just put it in a file in your project (a name like image-centercropped.swift will work nicely). Then just add .centerCropped() to any image you want to be center cropped.

Image("apolloimage").centerCropped()

It uses GeometryReader to figure out its frame so that it can crop the image correctly, which means you don't have to specify the frame to get proper clipping. You are free to size the image however you like using an explicit frame, or by just adding padding() and Spacer() to keep it nicely placed relative to other user interface items.

For example: If you want an image to fill the screen of the phone:

struct ContentView: View { 
    var body: some View {
        Image("apolloimage")
            .centerCropped()
            .edgesIgnoringSafeArea(.all)
    }
}

will nicely show the center of the image by scaling the image to show either the full height or the full width of the image and cropping the parts the hang over on the other dimension.


Demonstration:

Here's a demo that shows how the image is centered and cropped as the image grows. In this demo, the frame width is a constant 360 while the frame height varies from 50 to 700 as the slider advances to the right. At the beginning when the frame is short, the tops and bottoms of the image are cropped. As the frame exceeds the aspectRatio of the original image, the resulting image is centered but cropped on the left and right sides.

struct ContentView: View {
    
    @State private var frameheight: CGFloat = 50
    
    var body: some View {
        VStack(spacing: 20) {
            Spacer()
            Image("apolloimage")
                .resizable()
                .scaledToFill()
                .frame(width: 360, height: self.frameheight)
                .clipped()
            Spacer()
            Slider(value: self.$frameheight, in: 50...700)
                .padding(.horizontal, 20)
        }
    }
}

or an equivalent test using .centerCropped():

struct ContentView: View {
    
    @State private var frameheight: CGFloat = 50
    
    var body: some View {
        VStack(spacing: 20) {
            Spacer()
            Image("apolloimage")
                .centerCropped()
                .frame(width: 360, height: self.frameheight)
            Spacer()
            Slider(value: self.$frameheight, in: 50...700)
                .padding(.horizontal, 20)
        }
    }
}

Demo running on the simulator of .centerCropped scaling


Alternate Solution

Another way to make a center cropped image is to make the image an .overlay() of Color.clear. This allows Color.clear to establish the clipping bounds.

Color.clear
.overlay(
    Image("apolloimage")
    .resizable()
    .scaledToFill()
)
.clipped()

and the corresponding extension to Image looks like this:

extension Image {
    func centerCropped() -> some View {
        Color.clear
        .overlay(
            self
            .resizable()
            .scaledToFill()
        )
        .clipped()
    }
}


Answered By - vacawama
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, May 9, 2022

[FIXED] How Do I Center Product Title And Pricing Info On My Shopify Website?

 May 09, 2022     center, css, product, shopify, title     No comments   

Issue

I'm trying to figure out how to center the title of the product I'm selling on my website (Safe Guard Lenz) as well as center the pricing details that's under the product title as well. I need them centered for both mobile and desktop.

It's located at the bottom of the home page and on the product page itself. Ideally would want it centered for both.

Any help to achieve this would be greatly appreciated as I've been looking for a solution for this online and can't seem to have any success.

My website is safeguardlenz.com and the password is: Bebe

Thank you!


Solution

You can use style sth like this.

.product-single__title{
  text-align: center;
}

.product__price, #automizely_reviews_product_detail_title_star_widget{
  width: fit-content;
  margin: auto;
}

enter image description here

And this is the style for homepage value.

.price__pricing-group {
  margin: auto;
}


Answered By - Oscar Cardoso Garcia
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, May 6, 2022

[FIXED] how to center an image inside form?

 May 06, 2022     center, css, html, image     No comments   

Issue

Hello, I want to center an image inside the form
I used position: absolute to make it in center but didn't work

 position: absolute;
  left: 50%;
  top: 50%;

like this: enter image description here

I've tried this code so far:

body {
  top: 0;
  padding: 0;
  left: 0;
  margin: 0;
  height: 100vh;
  width: 100%;
  font-family: Arial;
  position: relative;
  background: linear-gradient(48deg, #1f3347 50%, #2c4050 50%);
}
.from_body {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  height: 390px;
  width: 400px;
  border-radius: 10px;
  border-top-left-radius: 50%;
  border-bottom-right-radius: 50%;
  border: 1px solid #2980b9;
}
.from_body img {
  height: 120px;
  width: 120px;
  position: absolute;
  left: 33%;
  margin-top: -67px;
}
.from_body .text {
  font-size: 30px;
  font-weight: 600;
  color: white;
  margin-left: 120px;
  margin-top: -5px;
  border-bottom: 2px solid;
  border-radius: 3px;
  width: 160px;
}
<body>
    <div class="form_body">
        <img
            src="https://pngset.com/images/google-contacts-icon-google-contacts-icon-disk-dvd-symbol-number-transparent-png-2734641.png">
        <p class="text">User login</p>
    </div>
</body>

I'm a beginner and I would appreciate any help!


Solution

The PNG in OP didn't look like it was transparent so I got one that does. The body has flexbox properties, <div>s are replaced by a <figure> and <figcaption> for semantics. The <img> will conform to the dimensions of the <figure> due to object-fit: contain.

body {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
  min-height: 100%;
  width: 100%;
  font: 3ch/1 'Segoe UI';
  background: linear-gradient(48deg, #1f3347 50%, #2c4050 50%);
}

figure {
  width: max-content;
  margin: 0 auto;
}

figcaption {
  text-align: center;
  color: #fff
}

img {
  object-fit: contain;
}
<body>
  <figure>
    <img src="https://9to5google.com/wp-content/uploads/sites/4/2017/05/google-contacts.png">
    <figcaption>User Login</figcaption>
  </figure>
</body>



Answered By - zer00ne
Answer Checked By - David Marino (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing