代码之家  ›  专栏  ›  技术社区  ›  Rohit Azad Malik

如何在css中创建斜体框?

  •  1
  • Rohit Azad Malik  · 技术社区  · 6 年前

    如何像这样在css中创建斜体框

    enter image description here

    .ml {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    
    .ml li {
      display: inline-block;
      border: solid 1px #000;
      font-style: italic;
      padding: 5px 10px;
    }
    
    .ml li.active,
    .ml li:hover {
      background: #000;
      color: #ffffff
    }
    <ul class="ml">
      <li class="active">day</li>
      <li>week</li>
      <li>month</li>
      <li>year</li>
    </ul>
    5 回复  |  直到 5 年前
        1
  •  5
  •   Rohit RC    6 年前

    只需要在css中添加skew属性

    .ml{
        list-style-type:none;margin:0;padding:0;}
        .ml li{display:inline-block;border:solid 1px #000;
    font-style:italic;padding:5px 10px;transform: skewX(-20deg);}
    
        .ml li.active, .ml li:hover{
        background:#000; color:#ffffff}
    <ul class="ml">
        <li class="active">day</li>
        <li>week</li>
        <li>month</li>
        <li>year</li>
        </ul>
        2
  •  4
  •   Andy Hoffman    4 年前

    到目前为止,我看到的一些答案的问题是,文本变得过度倾斜。这个 <li>

    enter image description here

    我们希望盒子歪了,但文本不受影响。

    enter image description here

    为此,我添加了一个 span 对每个 li 将文本反方向展开。

    /* Keep things organized and store skew value in CSS variable */
    :root {
      --skew-value: -20deg;
    }
    
    .ml {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    
    .ml li {
      display: inline-block;
      border: solid 1px #000;
      font-style: italic;
      padding: 5px 10px;
     
      /* Skew the box */
      transform: skew(var(--skew-value));
    }
    
    .ml li > span {
      /* Unskew the text */
      transform: skew(calc(var(--skew-value) * -1));
      display: inline-block;
    }
    
    .ml li.active,
    .ml li:hover {
      background: #000;
      color: #ffffff
    }
    <ul class="ml">
      <li class="active"><span>day</span></li>
      <li><span>week</span></li>
      <li><span>month</span></li>
      <li><span>year</span></li>
    </ul>

    http://jsfiddle.net/xftywz1h/

        3
  •  1
  •   Biswajit Nath    6 年前

    div {
      width: 300px;
      height: 100px;
      background-color: yellow;
      border: 1px solid black;
      margin:20px;
    }
    
    div#myDiv {
      -ms-transform: skewX(-20deg); /* IE 9 */
      -webkit-transform: skewX(-20deg); /* Safari */
      transform: skewX(-20deg); /* Standard syntax */
    }
    <!DOCTYPE html>
    <html>
    <head>
    
    </head>
    <body>
    
    <div id="myDiv">
    This div element is skewed 20 degrees along the X-axis.
    </div>
    
    </body>
    </html>
        4
  •  1
  •   Md. Abu Sayed    6 年前

    你可以试试这个:

    不能将方框字体样式斜体显示为所需的变换和值skewX,因为scewX会正常旋转方框X轴,而方框会旋转此内部元素或子元素自动旋转。

    .ml{
    list-style-type:none;margin:0;padding:0;}
    .ml li {
      display:inline-block;
      border:solid 1px #000;
      font-style:normal;
      padding:5px 10px;
      transform:skewX(-15deg);
      text-transform: uppercase;
      margin-right: 5px;
    }
    
    .ml li.active, 
    .ml li:hover {
       background:#000; color:#ffffff
    }
    <ul class="ml">
      <li class="active">day</li>
      <li>week</li>
      <li>month</li>
      <li>year</li>
    </ul>
        5
  •  1
  •   Anto    6 年前

    这里您需要的是,您可能需要再使用一个包装器来保留border属性

    html格式

    <ul class="ml">
    <li class="active"><span>day</span></li>
    <li><span>week</span></li>
    <li><span>month</span></li>
    <li><span>year</span></li>
    </ul>
    

    CSS格式

    .ml li:after, .ml li span:after {
      content: '';
      position: absolute;
      bottom: -1px;
      right: -1.5px;
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 0 0 31px 5px;
      border-color: transparent transparent #f8f8f8 transparent;
    }
    
    .ml li:before, .ml li span:before {
      content: '';
      position: absolute;
      top: -1px;
      left: 0px;
      width: 0;
      height: 0;
      border-style: solid;
     border-width: 30px 5px 0 0;
        border-color: #000 transparent transparent transparent;
    }
    
    .ml li span:before{
      left: -1px;
      border-color: #F8F8F8 transparent transparent transparent;
    }
    
    .ml li span:after{
      right: -0.5px;
      border-color: transparent transparent #000 transparent;
    }
    

    jsfiddle 请参阅此处的工作副本